dropbear: update to 2015.67
[tomato.git] / release / src / router / dropbear / gendss.c
blob21d13a04883055cb6ea0dbbd1cfdc2d8f302ace6
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "dbutil.h"
27 #include "signkey.h"
28 #include "bignum.h"
29 #include "dbrandom.h"
30 #include "buffer.h"
31 #include "gendss.h"
32 #include "dss.h"
34 #define QSIZE 20 /* 160 bit */
36 /* This is just a test */
38 #ifdef DROPBEAR_DSS
40 static void getq(dropbear_dss_key *key);
41 static void getp(dropbear_dss_key *key, unsigned int size);
42 static void getg(dropbear_dss_key *key);
43 static void getx(dropbear_dss_key *key);
44 static void gety(dropbear_dss_key *key);
46 dropbear_dss_key * gen_dss_priv_key(unsigned int size) {
48 dropbear_dss_key *key;
50 if (size != 1024) {
51 dropbear_exit("DSS keys have a fixed size of 1024 bits");
54 key = m_malloc(sizeof(*key));
56 m_mp_alloc_init_multi(&key->p, &key->q, &key->g, &key->y, &key->x, NULL);
58 getq(key);
59 getp(key, size/8);
60 getg(key);
61 getx(key);
62 gety(key);
64 return key;
68 static void getq(dropbear_dss_key *key) {
70 char buf[QSIZE];
72 /* 160 bit prime */
73 genrandom(buf, QSIZE);
74 buf[0] |= 0x80; /* top bit high */
75 buf[QSIZE-1] |= 0x01; /* bottom bit high */
77 bytes_to_mp(key->q, buf, QSIZE);
79 /* 18 rounds are required according to HAC */
80 if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) {
81 fprintf(stderr, "DSS key generation failed\n");
82 exit(1);
86 static void getp(dropbear_dss_key *key, unsigned int size) {
88 DEF_MP_INT(tempX);
89 DEF_MP_INT(tempC);
90 DEF_MP_INT(tempP);
91 DEF_MP_INT(temp2q);
92 int result;
93 unsigned char *buf;
95 m_mp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
98 /* 2*q */
99 if (mp_mul_d(key->q, 2, &temp2q) != MP_OKAY) {
100 fprintf(stderr, "DSS key generation failed\n");
101 exit(1);
104 buf = (unsigned char*)m_malloc(size);
106 result = 0;
107 do {
109 genrandom(buf, size);
110 buf[0] |= 0x80; /* set the top bit high */
112 /* X is a random mp_int */
113 bytes_to_mp(&tempX, buf, size);
115 /* C = X mod 2q */
116 if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) {
117 fprintf(stderr, "DSS key generation failed\n");
118 exit(1);
121 /* P = X - (C - 1) = X - C + 1*/
122 if (mp_sub(&tempX, &tempC, &tempP) != MP_OKAY) {
123 fprintf(stderr, "DSS key generation failed\n");
124 exit(1);
127 if (mp_add_d(&tempP, 1, key->p) != MP_OKAY) {
128 fprintf(stderr, "DSS key generation failed\n");
129 exit(1);
132 /* now check for prime, 5 rounds is enough according to HAC */
133 /* result == 1 => p is prime */
134 if (mp_prime_is_prime(key->p, 5, &result) != MP_OKAY) {
135 fprintf(stderr, "DSS key generation failed\n");
136 exit(1);
138 } while (!result);
140 mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
141 m_burn(buf, size);
142 m_free(buf);
145 static void getg(dropbear_dss_key * key) {
147 DEF_MP_INT(div);
148 DEF_MP_INT(h);
149 DEF_MP_INT(val);
151 m_mp_init_multi(&div, &h, &val, NULL);
153 /* get div=(p-1)/q */
154 if (mp_sub_d(key->p, 1, &val) != MP_OKAY) {
155 fprintf(stderr, "DSS key generation failed\n");
156 exit(1);
158 if (mp_div(&val, key->q, &div, NULL) != MP_OKAY) {
159 fprintf(stderr, "DSS key generation failed\n");
160 exit(1);
163 /* initialise h=1 */
164 mp_set(&h, 1);
165 do {
166 /* now keep going with g=h^div mod p, until g > 1 */
167 if (mp_exptmod(&h, &div, key->p, key->g) != MP_OKAY) {
168 fprintf(stderr, "DSS key generation failed\n");
169 exit(1);
172 if (mp_add_d(&h, 1, &h) != MP_OKAY) {
173 fprintf(stderr, "DSS key generation failed\n");
174 exit(1);
177 } while (mp_cmp_d(key->g, 1) != MP_GT);
179 mp_clear_multi(&div, &h, &val, NULL);
182 static void getx(dropbear_dss_key *key) {
184 gen_random_mpint(key->q, key->x);
187 static void gety(dropbear_dss_key *key) {
189 if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) {
190 fprintf(stderr, "DSS key generation failed\n");
191 exit(1);
195 #endif /* DROPBEAR_DSS */