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