comparison gendss.c @ 4:fe6bca95afa7

Makefile.in contains updated files required
author Matt Johnston <matt@ucc.asn.au>
date Tue, 01 Jun 2004 02:46:09 +0000
parents
children aecba0e16e9c b0316ce64e4b
comparison
equal deleted inserted replaced
-1:000000000000 4:fe6bca95afa7
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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. */
24
25 #include "includes.h"
26 #include "dbutil.h"
27 #include "signkey.h"
28 #include "bignum.h"
29 #include "random.h"
30 #include "buffer.h"
31 #include "gendss.h"
32 #include "dss.h"
33
34 #define PSIZE 128 /* 1024 bit*/
35 #define QSIZE 20 /* 160 bit */
36
37 #ifdef DROPBEAR_DSS
38
39 static void getq(dss_key *key);
40 static void getp(dss_key *key, unsigned int size);
41 static void getg(dss_key *key);
42 static void getx(dss_key *key);
43 static void gety(dss_key *key);
44
45 dss_key * gen_dss_priv_key(unsigned int size) {
46
47 dss_key *key;
48
49 key = (dss_key*)m_malloc(sizeof(dss_key));
50
51 key->p = (mp_int*)m_malloc(sizeof(mp_int));
52 key->q = (mp_int*)m_malloc(sizeof(mp_int));
53 key->g = (mp_int*)m_malloc(sizeof(mp_int));
54 key->y = (mp_int*)m_malloc(sizeof(mp_int));
55 key->x = (mp_int*)m_malloc(sizeof(mp_int));
56 m_mp_init_multi(key->p, key->q, key->g, key->y, key->x, NULL);
57
58 seedrandom();
59
60 getq(key);
61 getp(key, size);
62 getg(key);
63 getx(key);
64 gety(key);
65
66 return key;
67
68 }
69
70 static void getq(dss_key *key) {
71
72 char buf[QSIZE];
73
74 /* 160 bit prime */
75 genrandom(buf, QSIZE);
76 buf[0] |= 0x80; /* top bit high */
77 buf[QSIZE-1] |= 0x01; /* bottom bit high */
78
79 if (mp_read_unsigned_bin(key->q, buf, QSIZE) != MP_OKAY) {
80 fprintf(stderr, "dss key generation failed\n");
81 exit(1);
82 }
83
84 /* 18 rounds are required according to HAC */
85 if (mp_prime_next_prime(key->q, 18, 0) != MP_OKAY) {
86 fprintf(stderr, "dss key generation failed\n");
87 exit(1);
88 }
89 }
90
91 static void getp(dss_key *key, unsigned int size) {
92
93 mp_int tempX, tempC, tempP, temp2q;
94 int result;
95 unsigned char *buf;
96
97 m_mp_init_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
98
99
100 /* 2*q */
101 if (mp_mul_d(key->q, 2, &temp2q) != MP_OKAY) {
102 fprintf(stderr, "dss key generation failed\n");
103 exit(1);
104 }
105
106 buf = (unsigned char*)m_malloc(size);
107
108 result = 0;
109 do {
110
111 genrandom(buf, size);
112 buf[0] |= 0x80; /* set the top bit high */
113
114 /* X is a random mp_int */
115 if (mp_read_unsigned_bin(&tempX, buf, size) != MP_OKAY) {
116 fprintf(stderr, "dss key generation failed\n");
117 exit(1);
118 }
119
120 /* C = X mod 2q */
121 if (mp_mod(&tempX, &temp2q, &tempC) != MP_OKAY) {
122 fprintf(stderr, "dss key generation failed\n");
123 exit(1);
124 }
125
126 /* P = X - (C - 1) = X - C + 1*/
127 if (mp_sub(&tempX, &tempC, &tempP) != MP_OKAY) {
128 fprintf(stderr, "dss key generation failed\n");
129 exit(1);
130 }
131
132 if (mp_add_d(&tempP, 1, key->p) != MP_OKAY) {
133 fprintf(stderr, "dss key generation failed\n");
134 exit(1);
135 }
136
137 /* now check for prime, 5 rounds is enough according to HAC */
138 /* result == 1 => p is prime */
139 if (mp_prime_is_prime(key->p, 5, &result) != MP_OKAY) {
140 fprintf(stderr, "dss key generation failed\n");
141 exit(1);
142 }
143 } while (!result);
144
145 mp_clear_multi(&tempX, &tempC, &tempP, &temp2q, NULL);
146 m_free(buf);
147 }
148
149 static void getg(dss_key * key) {
150
151 char printbuf[1000];
152 mp_int div, h, val;
153
154 m_mp_init_multi(&div, &h, &val, NULL);
155
156 /* get div=(p-1)/q */
157 if (mp_sub_d(key->p, 1, &val) != MP_OKAY) {
158 fprintf(stderr, "dss key generation failed\n");
159 exit(1);
160 }
161 if (mp_div(&val, key->q, &div, NULL) != MP_OKAY) {
162 fprintf(stderr, "dss key generation failed\n");
163 exit(1);
164 }
165
166 /* initialise h=1 */
167 mp_set(&h, 1);
168 do {
169 /* now keep going with g=h^div mod p, until g > 1 */
170 if (mp_exptmod(&h, &div, key->p, key->g) != MP_OKAY) {
171 fprintf(stderr, "dss key generation failed\n");
172 exit(1);
173 }
174
175 if (mp_add_d(&h, 1, &h) != MP_OKAY) {
176 fprintf(stderr, "dss key generation failed\n");
177 exit(1);
178 }
179
180 } while (mp_cmp_d(key->g, 1) != MP_GT);
181
182 mp_toradix(key->g, printbuf, 10);
183
184 mp_clear_multi(&div, &h, &val, NULL);
185 }
186
187 static void getx(dss_key *key) {
188
189 mp_int val;
190 char buf[QSIZE];
191
192 m_mp_init(&val);
193
194 do {
195 genrandom(buf, QSIZE);
196
197 if (mp_read_unsigned_bin(&val, buf, QSIZE) != MP_OKAY) {
198 fprintf(stderr, "dss key generation failed\n");
199 }
200 } while ((mp_cmp_d(&val, 1) == MP_GT) && (mp_cmp(&val, key->q) == MP_LT));
201
202 mp_copy(&val, key->x);
203 mp_clear(&val);
204
205 }
206
207 static void gety(dss_key *key) {
208
209 if (mp_exptmod(key->g, key->x, key->p, key->y) != MP_OKAY) {
210 fprintf(stderr, "dss key generation failed\n");
211 exit(1);
212 }
213 }
214
215 #endif /* DROPBEAR_DSS */