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