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