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