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