Sanitise freeing of DSA keys.
[u/mdw/putty] / sshdss.c
1 /*
2 * Digital Signature Standard implementation for PuTTY.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8
9 #include "ssh.h"
10 #include "misc.h"
11
12 static void sha_mpint(SHA_State * s, Bignum b)
13 {
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 }
23 smemclr(lenbuf, sizeof(lenbuf));
24 }
25
26 static void sha512_mpint(SHA512_State * s, Bignum b)
27 {
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 }
37 smemclr(lenbuf, sizeof(lenbuf));
38 }
39
40 static void getstring(char **data, int *datalen, char **p, int *length)
41 {
42 *p = NULL;
43 if (*datalen < 4)
44 return;
45 *length = toint(GET_32BIT(*data));
46 if (*length < 0)
47 return;
48 *datalen -= 4;
49 *data += 4;
50 if (*datalen < *length)
51 return;
52 *p = *data;
53 *data += *length;
54 *datalen -= *length;
55 }
56 static Bignum getmp(char **data, int *datalen)
57 {
58 char *p;
59 int length;
60 Bignum b;
61
62 getstring(data, datalen, &p, &length);
63 if (!p)
64 return NULL;
65 if (p[0] & 0x80)
66 return NULL; /* negative mp */
67 b = bignum_from_bytes((unsigned char *)p, length);
68 return b;
69 }
70
71 static Bignum get160(char **data, int *datalen)
72 {
73 Bignum b;
74
75 if (*datalen < 20)
76 return NULL;
77
78 b = bignum_from_bytes((unsigned char *)*data, 20);
79 *data += 20;
80 *datalen -= 20;
81
82 return b;
83 }
84
85 static void *dss_newkey(char *data, int len)
86 {
87 char *p;
88 int slen;
89 struct dss_key *dss;
90
91 dss = snew(struct dss_key);
92 if (!dss)
93 return NULL;
94 getstring(&data, &len, &p, &slen);
95
96 #ifdef DEBUG_DSS
97 {
98 int i;
99 printf("key:");
100 for (i = 0; i < len; i++)
101 printf(" %02x", (unsigned char) (data[i]));
102 printf("\n");
103 }
104 #endif
105
106 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
107 sfree(dss);
108 return NULL;
109 }
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 dss->x = NULL;
115
116 return dss;
117 }
118
119 static void dss_freekey(void *key)
120 {
121 struct dss_key *dss = (struct dss_key *) key;
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);
132 sfree(dss);
133 }
134
135 static char *dss_fmtkey(void *key)
136 {
137 struct dss_key *dss = (struct dss_key *) key;
138 char *p;
139 int len, i, pos, nibbles;
140 static const char hex[] = "0123456789abcdef";
141 if (!dss->p)
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;
148 p = snewn(len, char);
149 if (!p)
150 return NULL;
151
152 pos = 0;
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];
181 p[pos] = '\0';
182 return p;
183 }
184
185 static char *dss_fingerprint(void *key)
186 {
187 struct dss_key *dss = (struct dss_key *) key;
188 struct MD5Context md5c;
189 unsigned char digest[16], lenbuf[4];
190 char buffer[16 * 3 + 40];
191 char *ret;
192 int numlen, i;
193
194 MD5Init(&md5c);
195 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11);
196
197 #define ADD_BIGNUM(bignum) \
198 numlen = (bignum_bitcount(bignum)+8)/8; \
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 }
204 ADD_BIGNUM(dss->p);
205 ADD_BIGNUM(dss->q);
206 ADD_BIGNUM(dss->g);
207 ADD_BIGNUM(dss->y);
208 #undef ADD_BIGNUM
209
210 MD5Final(digest, &md5c);
211
212 sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p));
213 for (i = 0; i < 16; i++)
214 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
215 digest[i]);
216 ret = snewn(strlen(buffer) + 1, char);
217 if (ret)
218 strcpy(ret, buffer);
219 return ret;
220 }
221
222 static int dss_verifysig(void *key, char *sig, int siglen,
223 char *data, int datalen)
224 {
225 struct dss_key *dss = (struct dss_key *) key;
226 char *p;
227 int slen;
228 char hash[20];
229 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
230 int ret;
231
232 if (!dss->p)
233 return 0;
234
235 #ifdef DEBUG_DSS
236 {
237 int i;
238 printf("sig:");
239 for (i = 0; i < siglen; i++)
240 printf(" %02x", (unsigned char) (sig[i]));
241 printf("\n");
242 }
243 #endif
244 /*
245 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
246 * of a DSA signature. OpenSSH is in line with RFC 4253:
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
253 * else is assumed to be RFC-compliant.
254 */
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 */
261 }
262 r = get160(&sig, &siglen);
263 s = get160(&sig, &siglen);
264 if (!r || !s)
265 return 0;
266
267 /*
268 * Step 1. w <- s^-1 mod q.
269 */
270 w = modinv(s, dss->q);
271
272 /*
273 * Step 2. u1 <- SHA(message) * w mod q.
274 */
275 SHA_Simple(data, datalen, (unsigned char *)hash);
276 p = hash;
277 slen = 20;
278 sha = get160(&p, &slen);
279 u1 = modmul(sha, w, dss->q);
280
281 /*
282 * Step 3. u2 <- r * w mod q.
283 */
284 u2 = modmul(r, w, dss->q);
285
286 /*
287 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
288 */
289 gu1p = modpow(dss->g, u1, dss->p);
290 yu2p = modpow(dss->y, u2, dss->p);
291 gu1yu2p = modmul(gu1p, yu2p, dss->p);
292 v = modmul(gu1yu2p, One, dss->q);
293
294 /*
295 * Step 5. v should now be equal to r.
296 */
297
298 ret = !bignum_cmp(v, r);
299
300 freebn(w);
301 freebn(sha);
302 freebn(u1);
303 freebn(u2);
304 freebn(gu1p);
305 freebn(yu2p);
306 freebn(gu1yu2p);
307 freebn(v);
308 freebn(r);
309 freebn(s);
310
311 return ret;
312 }
313
314 static unsigned char *dss_public_blob(void *key, int *len)
315 {
316 struct dss_key *dss = (struct dss_key *) key;
317 int plen, qlen, glen, ylen, bloblen;
318 int i;
319 unsigned char *blob, *p;
320
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;
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 */
330 bloblen = 27 + plen + qlen + glen + ylen;
331 blob = snewn(bloblen, unsigned char);
332 p = blob;
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);
353 assert(p == blob + bloblen);
354 *len = bloblen;
355 return blob;
356 }
357
358 static unsigned char *dss_private_blob(void *key, int *len)
359 {
360 struct dss_key *dss = (struct dss_key *) key;
361 int xlen, bloblen;
362 int i;
363 unsigned char *blob, *p;
364
365 xlen = (bignum_bitcount(dss->x) + 8) / 8;
366
367 /*
368 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
369 */
370 bloblen = 4 + xlen;
371 blob = snewn(bloblen, unsigned char);
372 p = blob;
373 PUT_32BIT(p, xlen);
374 p += 4;
375 for (i = xlen; i--;)
376 *p++ = bignum_byte(dss->x, i);
377 assert(p == blob + bloblen);
378 *len = bloblen;
379 return blob;
380 }
381
382 static void *dss_createkey(unsigned char *pub_blob, int pub_len,
383 unsigned char *priv_blob, int priv_len)
384 {
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);
395
396 /*
397 * Check the obsolete hash in the old DSS key format.
398 */
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 }
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);
419 freebn(ytest);
420 return NULL;
421 }
422 freebn(ytest);
423
424 return dss;
425 }
426
427 static void *dss_openssh_createkey(unsigned char **blob, int *len)
428 {
429 char **b = (char **) blob;
430 struct dss_key *dss;
431
432 dss = snew(struct dss_key);
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) {
443 freebn(dss->p);
444 freebn(dss->q);
445 freebn(dss->g);
446 freebn(dss->y);
447 freebn(dss->x);
448 sfree(dss);
449 return NULL;
450 }
451
452 return dss;
453 }
454
455 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
456 {
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;
481 }
482
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
495 static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
496 {
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 *
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.)
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
593 smemclr(&ss, sizeof(ss));
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
602 smemclr(digest512, sizeof(digest512));
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;
628 bytes = snewn(nbytes, unsigned char);
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;
641 }
642
643 const struct ssh_signkey ssh_dss = {
644 dss_newkey,
645 dss_freekey,
646 dss_fmtkey,
647 dss_public_blob,
648 dss_private_blob,
649 dss_createkey,
650 dss_openssh_createkey,
651 dss_openssh_fmtkey,
652 dss_pubkey_bits,
653 dss_fingerprint,
654 dss_verifysig,
655 dss_sign,
656 "ssh-dss",
657 "dss"
658 };