eaf1e20a |
1 | /* |
2 | * Digital Signature Standard implementation for PuTTY. |
3 | */ |
4 | |
7cca0d81 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
65a22376 |
7 | #include <assert.h> |
7cca0d81 |
8 | |
e5574168 |
9 | #include "ssh.h" |
5c72ca61 |
10 | #include "misc.h" |
e5574168 |
11 | |
5c72ca61 |
12 | static void sha_mpint(SHA_State * s, Bignum b) |
13 | { |
5c72ca61 |
14 | unsigned char lenbuf[4]; |
15 | int len; |
16 | len = (bignum_bitcount(b) + 8) / 8; |
17 | PUT_32BIT(lenbuf, len); |
18 | SHA_Bytes(s, lenbuf, 4); |
19 | while (len-- > 0) { |
20 | lenbuf[0] = bignum_byte(b, len); |
21 | SHA_Bytes(s, lenbuf, 1); |
22 | } |
dfb88efd |
23 | smemclr(lenbuf, sizeof(lenbuf)); |
5c72ca61 |
24 | } |
25 | |
26 | static void sha512_mpint(SHA512_State * s, Bignum b) |
27 | { |
5c72ca61 |
28 | unsigned char lenbuf[4]; |
29 | int len; |
30 | len = (bignum_bitcount(b) + 8) / 8; |
31 | PUT_32BIT(lenbuf, len); |
32 | SHA512_Bytes(s, lenbuf, 4); |
33 | while (len-- > 0) { |
34 | lenbuf[0] = bignum_byte(b, len); |
35 | SHA512_Bytes(s, lenbuf, 1); |
36 | } |
dfb88efd |
37 | smemclr(lenbuf, sizeof(lenbuf)); |
5c72ca61 |
38 | } |
9c621433 |
39 | |
32874aea |
40 | static void getstring(char **data, int *datalen, char **p, int *length) |
41 | { |
7cca0d81 |
42 | *p = NULL; |
43 | if (*datalen < 4) |
32874aea |
44 | return; |
b1650067 |
45 | *length = toint(GET_32BIT(*data)); |
af1da246 |
46 | if (*length < 0) |
47 | return; |
32874aea |
48 | *datalen -= 4; |
49 | *data += 4; |
7cca0d81 |
50 | if (*datalen < *length) |
32874aea |
51 | return; |
7cca0d81 |
52 | *p = *data; |
32874aea |
53 | *data += *length; |
54 | *datalen -= *length; |
7cca0d81 |
55 | } |
32874aea |
56 | static Bignum getmp(char **data, int *datalen) |
57 | { |
7cca0d81 |
58 | char *p; |
3709bfe9 |
59 | int length; |
7cca0d81 |
60 | Bignum b; |
61 | |
62 | getstring(data, datalen, &p, &length); |
63 | if (!p) |
32874aea |
64 | return NULL; |
7cca0d81 |
65 | if (p[0] & 0x80) |
32874aea |
66 | return NULL; /* negative mp */ |
9202b1b2 |
67 | b = bignum_from_bytes((unsigned char *)p, length); |
7cca0d81 |
68 | return b; |
69 | } |
70 | |
32874aea |
71 | static Bignum get160(char **data, int *datalen) |
72 | { |
7cca0d81 |
73 | Bignum b; |
74 | |
9febf7ed |
75 | if (*datalen < 20) |
76 | return NULL; |
77 | |
9202b1b2 |
78 | b = bignum_from_bytes((unsigned char *)*data, 20); |
32874aea |
79 | *data += 20; |
80 | *datalen -= 20; |
7cca0d81 |
81 | |
7cca0d81 |
82 | return b; |
83 | } |
84 | |
32874aea |
85 | static void *dss_newkey(char *data, int len) |
86 | { |
7cca0d81 |
87 | char *p; |
88 | int slen; |
e055a386 |
89 | struct dss_key *dss; |
90 | |
3d88e64d |
91 | dss = snew(struct dss_key); |
7cca0d81 |
92 | getstring(&data, &len, &p, &slen); |
9c621433 |
93 | |
94 | #ifdef DEBUG_DSS |
95 | { |
32874aea |
96 | int i; |
97 | printf("key:"); |
98 | for (i = 0; i < len; i++) |
99 | printf(" %02x", (unsigned char) (data[i])); |
100 | printf("\n"); |
9c621433 |
101 | } |
102 | #endif |
103 | |
af1da246 |
104 | if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) { |
dcbde236 |
105 | sfree(dss); |
e055a386 |
106 | return NULL; |
7cca0d81 |
107 | } |
e055a386 |
108 | dss->p = getmp(&data, &len); |
109 | dss->q = getmp(&data, &len); |
110 | dss->g = getmp(&data, &len); |
111 | dss->y = getmp(&data, &len); |
e6d62ec3 |
112 | dss->x = NULL; |
e055a386 |
113 | |
114 | return dss; |
7cca0d81 |
115 | } |
116 | |
32874aea |
117 | static void dss_freekey(void *key) |
118 | { |
119 | struct dss_key *dss = (struct dss_key *) key; |
e6d62ec3 |
120 | if (dss->p) |
121 | freebn(dss->p); |
122 | if (dss->q) |
123 | freebn(dss->q); |
124 | if (dss->g) |
125 | freebn(dss->g); |
126 | if (dss->y) |
127 | freebn(dss->y); |
128 | if (dss->x) |
129 | freebn(dss->x); |
dcbde236 |
130 | sfree(dss); |
e055a386 |
131 | } |
132 | |
32874aea |
133 | static char *dss_fmtkey(void *key) |
134 | { |
135 | struct dss_key *dss = (struct dss_key *) key; |
7cca0d81 |
136 | char *p; |
d5859615 |
137 | int len, i, pos, nibbles; |
138 | static const char hex[] = "0123456789abcdef"; |
e055a386 |
139 | if (!dss->p) |
32874aea |
140 | return NULL; |
141 | len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */ |
142 | len += 4 * (bignum_bitcount(dss->p) + 15) / 16; |
143 | len += 4 * (bignum_bitcount(dss->q) + 15) / 16; |
144 | len += 4 * (bignum_bitcount(dss->g) + 15) / 16; |
145 | len += 4 * (bignum_bitcount(dss->y) + 15) / 16; |
3d88e64d |
146 | p = snewn(len, char); |
32874aea |
147 | if (!p) |
148 | return NULL; |
d5859615 |
149 | |
150 | pos = 0; |
32874aea |
151 | pos += sprintf(p + pos, "0x"); |
152 | nibbles = (3 + bignum_bitcount(dss->p)) / 4; |
153 | if (nibbles < 1) |
154 | nibbles = 1; |
155 | for (i = nibbles; i--;) |
156 | p[pos++] = |
157 | hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF]; |
158 | pos += sprintf(p + pos, ",0x"); |
159 | nibbles = (3 + bignum_bitcount(dss->q)) / 4; |
160 | if (nibbles < 1) |
161 | nibbles = 1; |
162 | for (i = nibbles; i--;) |
163 | p[pos++] = |
164 | hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF]; |
165 | pos += sprintf(p + pos, ",0x"); |
166 | nibbles = (3 + bignum_bitcount(dss->g)) / 4; |
167 | if (nibbles < 1) |
168 | nibbles = 1; |
169 | for (i = nibbles; i--;) |
170 | p[pos++] = |
171 | hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF]; |
172 | pos += sprintf(p + pos, ",0x"); |
173 | nibbles = (3 + bignum_bitcount(dss->y)) / 4; |
174 | if (nibbles < 1) |
175 | nibbles = 1; |
176 | for (i = nibbles; i--;) |
177 | p[pos++] = |
178 | hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF]; |
d5859615 |
179 | p[pos] = '\0'; |
7cca0d81 |
180 | return p; |
181 | } |
182 | |
32874aea |
183 | static char *dss_fingerprint(void *key) |
184 | { |
185 | struct dss_key *dss = (struct dss_key *) key; |
d5859615 |
186 | struct MD5Context md5c; |
187 | unsigned char digest[16], lenbuf[4]; |
32874aea |
188 | char buffer[16 * 3 + 40]; |
d5859615 |
189 | char *ret; |
190 | int numlen, i; |
191 | |
192 | MD5Init(&md5c); |
9202b1b2 |
193 | MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11); |
d5859615 |
194 | |
195 | #define ADD_BIGNUM(bignum) \ |
ddecd643 |
196 | numlen = (bignum_bitcount(bignum)+8)/8; \ |
d5859615 |
197 | PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \ |
198 | for (i = numlen; i-- ;) { \ |
199 | unsigned char c = bignum_byte(bignum, i); \ |
200 | MD5Update(&md5c, &c, 1); \ |
201 | } |
e055a386 |
202 | ADD_BIGNUM(dss->p); |
203 | ADD_BIGNUM(dss->q); |
204 | ADD_BIGNUM(dss->g); |
205 | ADD_BIGNUM(dss->y); |
d5859615 |
206 | #undef ADD_BIGNUM |
207 | |
208 | MD5Final(digest, &md5c); |
209 | |
ddecd643 |
210 | sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p)); |
d5859615 |
211 | for (i = 0; i < 16; i++) |
32874aea |
212 | sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "", |
213 | digest[i]); |
3d88e64d |
214 | ret = snewn(strlen(buffer) + 1, char); |
d5859615 |
215 | if (ret) |
32874aea |
216 | strcpy(ret, buffer); |
d5859615 |
217 | return ret; |
218 | } |
219 | |
e055a386 |
220 | static int dss_verifysig(void *key, char *sig, int siglen, |
32874aea |
221 | char *data, int datalen) |
222 | { |
223 | struct dss_key *dss = (struct dss_key *) key; |
7cca0d81 |
224 | char *p; |
c5fbd713 |
225 | int slen; |
7cca0d81 |
226 | char hash[20]; |
59600f67 |
227 | Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v; |
7cca0d81 |
228 | int ret; |
229 | |
e055a386 |
230 | if (!dss->p) |
32874aea |
231 | return 0; |
7cca0d81 |
232 | |
9c621433 |
233 | #ifdef DEBUG_DSS |
234 | { |
32874aea |
235 | int i; |
236 | printf("sig:"); |
237 | for (i = 0; i < siglen; i++) |
238 | printf(" %02x", (unsigned char) (sig[i])); |
239 | printf("\n"); |
9c621433 |
240 | } |
241 | #endif |
7f7837c8 |
242 | /* |
243 | * Commercial SSH (2.0.13) and OpenSSH disagree over the format |
bccbeb71 |
244 | * of a DSA signature. OpenSSH is in line with RFC 4253: |
7f7837c8 |
245 | * it uses a string "ssh-dss", followed by a 40-byte string |
246 | * containing two 160-bit integers end-to-end. Commercial SSH |
247 | * can't be bothered with the header bit, and considers a DSA |
248 | * signature blob to be _just_ the 40-byte string containing |
249 | * the two 160-bit integers. We tell them apart by measuring |
250 | * the length: length 40 means the commercial-SSH bug, anything |
bccbeb71 |
251 | * else is assumed to be RFC-compliant. |
7f7837c8 |
252 | */ |
32874aea |
253 | if (siglen != 40) { /* bug not present; read admin fields */ |
254 | getstring(&sig, &siglen, &p, &slen); |
255 | if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) { |
256 | return 0; |
257 | } |
258 | sig += 4, siglen -= 4; /* skip yet another length field */ |
7cca0d81 |
259 | } |
7cca0d81 |
260 | r = get160(&sig, &siglen); |
261 | s = get160(&sig, &siglen); |
262 | if (!r || !s) |
32874aea |
263 | return 0; |
7cca0d81 |
264 | |
265 | /* |
266 | * Step 1. w <- s^-1 mod q. |
267 | */ |
e055a386 |
268 | w = modinv(s, dss->q); |
7cca0d81 |
269 | |
270 | /* |
271 | * Step 2. u1 <- SHA(message) * w mod q. |
272 | */ |
9202b1b2 |
273 | SHA_Simple(data, datalen, (unsigned char *)hash); |
32874aea |
274 | p = hash; |
275 | slen = 20; |
276 | sha = get160(&p, &slen); |
e055a386 |
277 | u1 = modmul(sha, w, dss->q); |
7cca0d81 |
278 | |
279 | /* |
280 | * Step 3. u2 <- r * w mod q. |
281 | */ |
e055a386 |
282 | u2 = modmul(r, w, dss->q); |
7cca0d81 |
283 | |
284 | /* |
285 | * Step 4. v <- (g^u1 * y^u2 mod p) mod q. |
286 | */ |
e055a386 |
287 | gu1p = modpow(dss->g, u1, dss->p); |
e055a386 |
288 | yu2p = modpow(dss->y, u2, dss->p); |
e055a386 |
289 | gu1yu2p = modmul(gu1p, yu2p, dss->p); |
e055a386 |
290 | v = modmul(gu1yu2p, One, dss->q); |
7cca0d81 |
291 | |
292 | /* |
293 | * Step 5. v should now be equal to r. |
294 | */ |
295 | |
c5fbd713 |
296 | ret = !bignum_cmp(v, r); |
7cca0d81 |
297 | |
298 | freebn(w); |
7cca0d81 |
299 | freebn(sha); |
038ec85e |
300 | freebn(u1); |
301 | freebn(u2); |
59600f67 |
302 | freebn(gu1p); |
303 | freebn(yu2p); |
304 | freebn(gu1yu2p); |
7cca0d81 |
305 | freebn(v); |
306 | freebn(r); |
307 | freebn(s); |
308 | |
309 | return ret; |
310 | } |
311 | |
32874aea |
312 | static unsigned char *dss_public_blob(void *key, int *len) |
313 | { |
314 | struct dss_key *dss = (struct dss_key *) key; |
65a22376 |
315 | int plen, qlen, glen, ylen, bloblen; |
316 | int i; |
317 | unsigned char *blob, *p; |
318 | |
32874aea |
319 | plen = (bignum_bitcount(dss->p) + 8) / 8; |
320 | qlen = (bignum_bitcount(dss->q) + 8) / 8; |
321 | glen = (bignum_bitcount(dss->g) + 8) / 8; |
322 | ylen = (bignum_bitcount(dss->y) + 8) / 8; |
65a22376 |
323 | |
324 | /* |
325 | * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total |
326 | * 27 + sum of lengths. (five length fields, 20+7=27). |
327 | */ |
32874aea |
328 | bloblen = 27 + plen + qlen + glen + ylen; |
3d88e64d |
329 | blob = snewn(bloblen, unsigned char); |
65a22376 |
330 | p = blob; |
32874aea |
331 | PUT_32BIT(p, 7); |
332 | p += 4; |
333 | memcpy(p, "ssh-dss", 7); |
334 | p += 7; |
335 | PUT_32BIT(p, plen); |
336 | p += 4; |
337 | for (i = plen; i--;) |
338 | *p++ = bignum_byte(dss->p, i); |
339 | PUT_32BIT(p, qlen); |
340 | p += 4; |
341 | for (i = qlen; i--;) |
342 | *p++ = bignum_byte(dss->q, i); |
343 | PUT_32BIT(p, glen); |
344 | p += 4; |
345 | for (i = glen; i--;) |
346 | *p++ = bignum_byte(dss->g, i); |
347 | PUT_32BIT(p, ylen); |
348 | p += 4; |
349 | for (i = ylen; i--;) |
350 | *p++ = bignum_byte(dss->y, i); |
65a22376 |
351 | assert(p == blob + bloblen); |
352 | *len = bloblen; |
353 | return blob; |
354 | } |
355 | |
32874aea |
356 | static unsigned char *dss_private_blob(void *key, int *len) |
357 | { |
5c72ca61 |
358 | struct dss_key *dss = (struct dss_key *) key; |
359 | int xlen, bloblen; |
360 | int i; |
361 | unsigned char *blob, *p; |
5c72ca61 |
362 | |
363 | xlen = (bignum_bitcount(dss->x) + 8) / 8; |
364 | |
365 | /* |
7bedb13c |
366 | * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen. |
5c72ca61 |
367 | */ |
7bedb13c |
368 | bloblen = 4 + xlen; |
3d88e64d |
369 | blob = snewn(bloblen, unsigned char); |
5c72ca61 |
370 | p = blob; |
371 | PUT_32BIT(p, xlen); |
372 | p += 4; |
373 | for (i = xlen; i--;) |
374 | *p++ = bignum_byte(dss->x, i); |
5c72ca61 |
375 | assert(p == blob + bloblen); |
376 | *len = bloblen; |
377 | return blob; |
65a22376 |
378 | } |
379 | |
380 | static void *dss_createkey(unsigned char *pub_blob, int pub_len, |
32874aea |
381 | unsigned char *priv_blob, int priv_len) |
382 | { |
5c72ca61 |
383 | struct dss_key *dss; |
384 | char *pb = (char *) priv_blob; |
385 | char *hash; |
386 | int hashlen; |
387 | SHA_State s; |
388 | unsigned char digest[20]; |
389 | Bignum ytest; |
390 | |
391 | dss = dss_newkey((char *) pub_blob, pub_len); |
392 | dss->x = getmp(&pb, &priv_len); |
5c72ca61 |
393 | |
394 | /* |
7bedb13c |
395 | * Check the obsolete hash in the old DSS key format. |
5c72ca61 |
396 | */ |
7bedb13c |
397 | hashlen = -1; |
398 | getstring(&pb, &priv_len, &hash, &hashlen); |
399 | if (hashlen == 20) { |
400 | SHA_Init(&s); |
401 | sha_mpint(&s, dss->p); |
402 | sha_mpint(&s, dss->q); |
403 | sha_mpint(&s, dss->g); |
404 | SHA_Final(&s, digest); |
405 | if (0 != memcmp(hash, digest, 20)) { |
406 | dss_freekey(dss); |
407 | return NULL; |
408 | } |
5c72ca61 |
409 | } |
410 | |
411 | /* |
412 | * Now ensure g^x mod p really is y. |
413 | */ |
414 | ytest = modpow(dss->g, dss->x, dss->p); |
415 | if (0 != bignum_cmp(ytest, dss->y)) { |
416 | dss_freekey(dss); |
038ec85e |
417 | freebn(ytest); |
5c72ca61 |
418 | return NULL; |
419 | } |
420 | freebn(ytest); |
421 | |
422 | return dss; |
65a22376 |
423 | } |
424 | |
32874aea |
425 | static void *dss_openssh_createkey(unsigned char **blob, int *len) |
426 | { |
5c72ca61 |
427 | char **b = (char **) blob; |
428 | struct dss_key *dss; |
429 | |
3d88e64d |
430 | dss = snew(struct dss_key); |
5c72ca61 |
431 | |
432 | dss->p = getmp(b, len); |
433 | dss->q = getmp(b, len); |
434 | dss->g = getmp(b, len); |
435 | dss->y = getmp(b, len); |
436 | dss->x = getmp(b, len); |
437 | |
438 | if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x) { |
378c6504 |
439 | freebn(dss->p); |
440 | freebn(dss->q); |
441 | freebn(dss->g); |
442 | freebn(dss->y); |
443 | freebn(dss->x); |
5c72ca61 |
444 | sfree(dss); |
445 | return NULL; |
446 | } |
447 | |
448 | return dss; |
45cebe79 |
449 | } |
450 | |
32874aea |
451 | static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len) |
452 | { |
5c72ca61 |
453 | struct dss_key *dss = (struct dss_key *) key; |
454 | int bloblen, i; |
455 | |
456 | bloblen = |
457 | ssh2_bignum_length(dss->p) + |
458 | ssh2_bignum_length(dss->q) + |
459 | ssh2_bignum_length(dss->g) + |
460 | ssh2_bignum_length(dss->y) + |
461 | ssh2_bignum_length(dss->x); |
462 | |
463 | if (bloblen > len) |
464 | return bloblen; |
465 | |
466 | bloblen = 0; |
467 | #define ENC(x) \ |
468 | PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \ |
469 | for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i); |
470 | ENC(dss->p); |
471 | ENC(dss->q); |
472 | ENC(dss->g); |
473 | ENC(dss->y); |
474 | ENC(dss->x); |
475 | |
476 | return bloblen; |
ddecd643 |
477 | } |
478 | |
47a6b94c |
479 | static int dss_pubkey_bits(void *blob, int len) |
480 | { |
481 | struct dss_key *dss; |
482 | int ret; |
483 | |
484 | dss = dss_newkey((char *) blob, len); |
485 | ret = bignum_bitcount(dss->p); |
486 | dss_freekey(dss); |
487 | |
488 | return ret; |
489 | } |
490 | |
900852ab |
491 | static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen) |
32874aea |
492 | { |
5c72ca61 |
493 | /* |
494 | * The basic DSS signing algorithm is: |
495 | * |
496 | * - invent a random k between 1 and q-1 (exclusive). |
497 | * - Compute r = (g^k mod p) mod q. |
498 | * - Compute s = k^-1 * (hash + x*r) mod q. |
499 | * |
500 | * This has the dangerous properties that: |
501 | * |
502 | * - if an attacker in possession of the public key _and_ the |
503 | * signature (for example, the host you just authenticated |
504 | * to) can guess your k, he can reverse the computation of s |
505 | * and work out x = r^-1 * (s*k - hash) mod q. That is, he |
506 | * can deduce the private half of your key, and masquerade |
507 | * as you for as long as the key is still valid. |
508 | * |
509 | * - since r is a function purely of k and the public key, if |
510 | * the attacker only has a _range of possibilities_ for k |
511 | * it's easy for him to work through them all and check each |
512 | * one against r; he'll never be unsure of whether he's got |
513 | * the right one. |
514 | * |
515 | * - if you ever sign two different hashes with the same k, it |
516 | * will be immediately obvious because the two signatures |
517 | * will have the same r, and moreover an attacker in |
518 | * possession of both signatures (and the public key of |
519 | * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q, |
520 | * and from there deduce x as before. |
521 | * |
522 | * - the Bleichenbacher attack on DSA makes use of methods of |
523 | * generating k which are significantly non-uniformly |
524 | * distributed; in particular, generating a 160-bit random |
525 | * number and reducing it mod q is right out. |
526 | * |
527 | * For this reason we must be pretty careful about how we |
528 | * generate our k. Since this code runs on Windows, with no |
529 | * particularly good system entropy sources, we can't trust our |
530 | * RNG itself to produce properly unpredictable data. Hence, we |
531 | * use a totally different scheme instead. |
532 | * |
533 | * What we do is to take a SHA-512 (_big_) hash of the private |
534 | * key x, and then feed this into another SHA-512 hash that |
535 | * also includes the message hash being signed. That is: |
536 | * |
537 | * proto_k = SHA512 ( SHA512(x) || SHA160(message) ) |
538 | * |
539 | * This number is 512 bits long, so reducing it mod q won't be |
540 | * noticeably non-uniform. So |
541 | * |
542 | * k = proto_k mod q |
543 | * |
544 | * This has the interesting property that it's _deterministic_: |
545 | * signing the same hash twice with the same key yields the |
546 | * same signature. |
547 | * |
5ced2a02 |
548 | * Despite this determinism, it's still not predictable to an |
549 | * attacker, because in order to repeat the SHA-512 |
550 | * construction that created it, the attacker would have to |
551 | * know the private key value x - and by assumption he doesn't, |
552 | * because if he knew that he wouldn't be attacking k! |
553 | * |
554 | * (This trick doesn't, _per se_, protect against reuse of k. |
555 | * Reuse of k is left to chance; all it does is prevent |
556 | * _excessively high_ chances of reuse of k due to entropy |
557 | * problems.) |
5c72ca61 |
558 | * |
559 | * Thanks to Colin Plumb for the general idea of using x to |
560 | * ensure k is hard to guess, and to the Cambridge University |
561 | * Computer Security Group for helping to argue out all the |
562 | * fine details. |
563 | */ |
564 | struct dss_key *dss = (struct dss_key *) key; |
565 | SHA512_State ss; |
566 | unsigned char digest[20], digest512[64]; |
567 | Bignum proto_k, k, gkp, hash, kinv, hxr, r, s; |
568 | unsigned char *bytes; |
569 | int nbytes, i; |
570 | |
571 | SHA_Simple(data, datalen, digest); |
572 | |
573 | /* |
574 | * Hash some identifying text plus x. |
575 | */ |
576 | SHA512_Init(&ss); |
577 | SHA512_Bytes(&ss, "DSA deterministic k generator", 30); |
578 | sha512_mpint(&ss, dss->x); |
579 | SHA512_Final(&ss, digest512); |
580 | |
581 | /* |
582 | * Now hash that digest plus the message hash. |
583 | */ |
584 | SHA512_Init(&ss); |
585 | SHA512_Bytes(&ss, digest512, sizeof(digest512)); |
586 | SHA512_Bytes(&ss, digest, sizeof(digest)); |
587 | SHA512_Final(&ss, digest512); |
588 | |
dfb88efd |
589 | smemclr(&ss, sizeof(ss)); |
5c72ca61 |
590 | |
591 | /* |
592 | * Now convert the result into a bignum, and reduce it mod q. |
593 | */ |
594 | proto_k = bignum_from_bytes(digest512, 64); |
595 | k = bigmod(proto_k, dss->q); |
596 | freebn(proto_k); |
597 | |
dfb88efd |
598 | smemclr(digest512, sizeof(digest512)); |
5c72ca61 |
599 | |
600 | /* |
601 | * Now we have k, so just go ahead and compute the signature. |
602 | */ |
603 | gkp = modpow(dss->g, k, dss->p); /* g^k mod p */ |
604 | r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */ |
605 | freebn(gkp); |
606 | |
607 | hash = bignum_from_bytes(digest, 20); |
608 | kinv = modinv(k, dss->q); /* k^-1 mod q */ |
609 | hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */ |
610 | s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */ |
611 | freebn(hxr); |
612 | freebn(kinv); |
613 | freebn(hash); |
614 | |
615 | /* |
616 | * Signature blob is |
617 | * |
618 | * string "ssh-dss" |
619 | * string two 20-byte numbers r and s, end to end |
620 | * |
621 | * i.e. 4+7 + 4+40 bytes. |
622 | */ |
623 | nbytes = 4 + 7 + 4 + 40; |
3d88e64d |
624 | bytes = snewn(nbytes, unsigned char); |
5c72ca61 |
625 | PUT_32BIT(bytes, 7); |
626 | memcpy(bytes + 4, "ssh-dss", 7); |
627 | PUT_32BIT(bytes + 4 + 7, 40); |
628 | for (i = 0; i < 20; i++) { |
629 | bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i); |
630 | bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i); |
631 | } |
632 | freebn(r); |
633 | freebn(s); |
634 | |
635 | *siglen = nbytes; |
636 | return bytes; |
e055a386 |
637 | } |
638 | |
65a22376 |
639 | const struct ssh_signkey ssh_dss = { |
e055a386 |
640 | dss_newkey, |
641 | dss_freekey, |
7cca0d81 |
642 | dss_fmtkey, |
65a22376 |
643 | dss_public_blob, |
644 | dss_private_blob, |
645 | dss_createkey, |
45cebe79 |
646 | dss_openssh_createkey, |
ddecd643 |
647 | dss_openssh_fmtkey, |
47a6b94c |
648 | dss_pubkey_bits, |
d5859615 |
649 | dss_fingerprint, |
7cca0d81 |
650 | dss_verifysig, |
e055a386 |
651 | dss_sign, |
d5859615 |
652 | "ssh-dss", |
653 | "dss" |
e5574168 |
654 | }; |