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