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