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