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