2 * Digital Signature Standard implementation for PuTTY.
12 static void sha_mpint(SHA_State * s, Bignum b)
14 unsigned char lenbuf[4];
16 len = (bignum_bitcount(b) + 8) / 8;
17 PUT_32BIT(lenbuf, len);
18 SHA_Bytes(s, lenbuf, 4);
20 lenbuf[0] = bignum_byte(b, len);
21 SHA_Bytes(s, lenbuf, 1);
23 smemclr(lenbuf, sizeof(lenbuf));
26 static void sha512_mpint(SHA512_State * s, Bignum b)
28 unsigned char lenbuf[4];
30 len = (bignum_bitcount(b) + 8) / 8;
31 PUT_32BIT(lenbuf, len);
32 SHA512_Bytes(s, lenbuf, 4);
34 lenbuf[0] = bignum_byte(b, len);
35 SHA512_Bytes(s, lenbuf, 1);
37 smemclr(lenbuf, sizeof(lenbuf));
40 static void getstring(char **data, int *datalen, char **p, int *length)
45 *length = toint(GET_32BIT(*data));
50 if (*datalen < *length)
56 static Bignum getmp(char **data, int *datalen)
62 getstring(data, datalen, &p, &length);
66 return NULL; /* negative mp */
67 b = bignum_from_bytes((unsigned char *)p, length);
71 static Bignum get160(char **data, int *datalen)
78 b = bignum_from_bytes((unsigned char *)*data, 20);
85 static void *dss_newkey(char *data, int len)
91 dss = snew(struct dss_key);
94 getstring(&data, &len, &p, &slen);
100 for (i = 0; i < len; i++)
101 printf(" %02x", (unsigned char) (data[i]));
106 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
110 dss->p = getmp(&data, &len);
111 dss->q = getmp(&data, &len);
112 dss->g = getmp(&data, &len);
113 dss->y = getmp(&data, &len);
119 static void dss_freekey(void *key)
121 struct dss_key *dss = (struct dss_key *) key;
135 static char *dss_fmtkey(void *key)
137 struct dss_key *dss = (struct dss_key *) key;
139 int len, i, pos, nibbles;
140 static const char hex[] = "0123456789abcdef";
143 len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
144 len += 4 * (bignum_bitcount(dss->p) + 15) / 16;
145 len += 4 * (bignum_bitcount(dss->q) + 15) / 16;
146 len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
147 len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
148 p = snewn(len, char);
153 pos += sprintf(p + pos, "0x");
154 nibbles = (3 + bignum_bitcount(dss->p)) / 4;
157 for (i = nibbles; i--;)
159 hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF];
160 pos += sprintf(p + pos, ",0x");
161 nibbles = (3 + bignum_bitcount(dss->q)) / 4;
164 for (i = nibbles; i--;)
166 hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF];
167 pos += sprintf(p + pos, ",0x");
168 nibbles = (3 + bignum_bitcount(dss->g)) / 4;
171 for (i = nibbles; i--;)
173 hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF];
174 pos += sprintf(p + pos, ",0x");
175 nibbles = (3 + bignum_bitcount(dss->y)) / 4;
178 for (i = nibbles; i--;)
180 hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF];
185 static char *dss_fingerprint(void *key)
187 struct dss_key *dss = (struct dss_key *) key;
188 struct MD5Context md5c;
189 unsigned char digest[16], lenbuf[4];
190 char buffer[16 * 3 + 40];
195 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11);
197 #define ADD_BIGNUM(bignum) \
198 numlen = (bignum_bitcount(bignum)+8)/8; \
199 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
200 for (i = numlen; i-- ;) { \
201 unsigned char c = bignum_byte(bignum, i); \
202 MD5Update(&md5c, &c, 1); \
210 MD5Final(digest, &md5c);
212 sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p));
213 for (i = 0; i < 16; i++)
214 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
216 ret = snewn(strlen(buffer) + 1, char);
222 static int dss_verifysig(void *key, char *sig, int siglen,
223 char *data, int datalen)
225 struct dss_key *dss = (struct dss_key *) key;
229 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
239 for (i = 0; i < siglen; i++)
240 printf(" %02x", (unsigned char) (sig[i]));
245 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
246 * of a DSA signature. OpenSSH is in line with RFC 4253:
247 * it uses a string "ssh-dss", followed by a 40-byte string
248 * containing two 160-bit integers end-to-end. Commercial SSH
249 * can't be bothered with the header bit, and considers a DSA
250 * signature blob to be _just_ the 40-byte string containing
251 * the two 160-bit integers. We tell them apart by measuring
252 * the length: length 40 means the commercial-SSH bug, anything
253 * else is assumed to be RFC-compliant.
255 if (siglen != 40) { /* bug not present; read admin fields */
256 getstring(&sig, &siglen, &p, &slen);
257 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
260 sig += 4, siglen -= 4; /* skip yet another length field */
262 r = get160(&sig, &siglen);
263 s = get160(&sig, &siglen);
268 * Step 1. w <- s^-1 mod q.
270 w = modinv(s, dss->q);
273 * Step 2. u1 <- SHA(message) * w mod q.
275 SHA_Simple(data, datalen, (unsigned char *)hash);
278 sha = get160(&p, &slen);
279 u1 = modmul(sha, w, dss->q);
282 * Step 3. u2 <- r * w mod q.
284 u2 = modmul(r, w, dss->q);
287 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
289 gu1p = modpow(dss->g, u1, dss->p);
290 yu2p = modpow(dss->y, u2, dss->p);
291 gu1yu2p = modmul(gu1p, yu2p, dss->p);
292 v = modmul(gu1yu2p, One, dss->q);
295 * Step 5. v should now be equal to r.
298 ret = !bignum_cmp(v, r);
314 static unsigned char *dss_public_blob(void *key, int *len)
316 struct dss_key *dss = (struct dss_key *) key;
317 int plen, qlen, glen, ylen, bloblen;
319 unsigned char *blob, *p;
321 plen = (bignum_bitcount(dss->p) + 8) / 8;
322 qlen = (bignum_bitcount(dss->q) + 8) / 8;
323 glen = (bignum_bitcount(dss->g) + 8) / 8;
324 ylen = (bignum_bitcount(dss->y) + 8) / 8;
327 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
328 * 27 + sum of lengths. (five length fields, 20+7=27).
330 bloblen = 27 + plen + qlen + glen + ylen;
331 blob = snewn(bloblen, unsigned char);
335 memcpy(p, "ssh-dss", 7);
340 *p++ = bignum_byte(dss->p, i);
344 *p++ = bignum_byte(dss->q, i);
348 *p++ = bignum_byte(dss->g, i);
352 *p++ = bignum_byte(dss->y, i);
353 assert(p == blob + bloblen);
358 static unsigned char *dss_private_blob(void *key, int *len)
360 struct dss_key *dss = (struct dss_key *) key;
363 unsigned char *blob, *p;
365 xlen = (bignum_bitcount(dss->x) + 8) / 8;
368 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
371 blob = snewn(bloblen, unsigned char);
376 *p++ = bignum_byte(dss->x, i);
377 assert(p == blob + bloblen);
382 static void *dss_createkey(unsigned char *pub_blob, int pub_len,
383 unsigned char *priv_blob, int priv_len)
386 char *pb = (char *) priv_blob;
390 unsigned char digest[20];
393 dss = dss_newkey((char *) pub_blob, pub_len);
394 dss->x = getmp(&pb, &priv_len);
397 * Check the obsolete hash in the old DSS key format.
400 getstring(&pb, &priv_len, &hash, &hashlen);
403 sha_mpint(&s, dss->p);
404 sha_mpint(&s, dss->q);
405 sha_mpint(&s, dss->g);
406 SHA_Final(&s, digest);
407 if (0 != memcmp(hash, digest, 20)) {
414 * Now ensure g^x mod p really is y.
416 ytest = modpow(dss->g, dss->x, dss->p);
417 if (0 != bignum_cmp(ytest, dss->y)) {
427 static void *dss_openssh_createkey(unsigned char **blob, int *len)
429 char **b = (char **) blob;
432 dss = snew(struct dss_key);
436 dss->p = getmp(b, len);
437 dss->q = getmp(b, len);
438 dss->g = getmp(b, len);
439 dss->y = getmp(b, len);
440 dss->x = getmp(b, len);
442 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x) {
455 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
457 struct dss_key *dss = (struct dss_key *) key;
461 ssh2_bignum_length(dss->p) +
462 ssh2_bignum_length(dss->q) +
463 ssh2_bignum_length(dss->g) +
464 ssh2_bignum_length(dss->y) +
465 ssh2_bignum_length(dss->x);
472 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
473 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
483 static int dss_pubkey_bits(void *blob, int len)
488 dss = dss_newkey((char *) blob, len);
489 ret = bignum_bitcount(dss->p);
495 static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
498 * The basic DSS signing algorithm is:
500 * - invent a random k between 1 and q-1 (exclusive).
501 * - Compute r = (g^k mod p) mod q.
502 * - Compute s = k^-1 * (hash + x*r) mod q.
504 * This has the dangerous properties that:
506 * - if an attacker in possession of the public key _and_ the
507 * signature (for example, the host you just authenticated
508 * to) can guess your k, he can reverse the computation of s
509 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
510 * can deduce the private half of your key, and masquerade
511 * as you for as long as the key is still valid.
513 * - since r is a function purely of k and the public key, if
514 * the attacker only has a _range of possibilities_ for k
515 * it's easy for him to work through them all and check each
516 * one against r; he'll never be unsure of whether he's got
519 * - if you ever sign two different hashes with the same k, it
520 * will be immediately obvious because the two signatures
521 * will have the same r, and moreover an attacker in
522 * possession of both signatures (and the public key of
523 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
524 * and from there deduce x as before.
526 * - the Bleichenbacher attack on DSA makes use of methods of
527 * generating k which are significantly non-uniformly
528 * distributed; in particular, generating a 160-bit random
529 * number and reducing it mod q is right out.
531 * For this reason we must be pretty careful about how we
532 * generate our k. Since this code runs on Windows, with no
533 * particularly good system entropy sources, we can't trust our
534 * RNG itself to produce properly unpredictable data. Hence, we
535 * use a totally different scheme instead.
537 * What we do is to take a SHA-512 (_big_) hash of the private
538 * key x, and then feed this into another SHA-512 hash that
539 * also includes the message hash being signed. That is:
541 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
543 * This number is 512 bits long, so reducing it mod q won't be
544 * noticeably non-uniform. So
548 * This has the interesting property that it's _deterministic_:
549 * signing the same hash twice with the same key yields the
552 * Despite this determinism, it's still not predictable to an
553 * attacker, because in order to repeat the SHA-512
554 * construction that created it, the attacker would have to
555 * know the private key value x - and by assumption he doesn't,
556 * because if he knew that he wouldn't be attacking k!
558 * (This trick doesn't, _per se_, protect against reuse of k.
559 * Reuse of k is left to chance; all it does is prevent
560 * _excessively high_ chances of reuse of k due to entropy
563 * Thanks to Colin Plumb for the general idea of using x to
564 * ensure k is hard to guess, and to the Cambridge University
565 * Computer Security Group for helping to argue out all the
568 struct dss_key *dss = (struct dss_key *) key;
570 unsigned char digest[20], digest512[64];
571 Bignum proto_k, k, gkp, hash, kinv, hxr, r, s;
572 unsigned char *bytes;
575 SHA_Simple(data, datalen, digest);
578 * Hash some identifying text plus x.
581 SHA512_Bytes(&ss, "DSA deterministic k generator", 30);
582 sha512_mpint(&ss, dss->x);
583 SHA512_Final(&ss, digest512);
586 * Now hash that digest plus the message hash.
589 SHA512_Bytes(&ss, digest512, sizeof(digest512));
590 SHA512_Bytes(&ss, digest, sizeof(digest));
591 SHA512_Final(&ss, digest512);
593 smemclr(&ss, sizeof(ss));
596 * Now convert the result into a bignum, and reduce it mod q.
598 proto_k = bignum_from_bytes(digest512, 64);
599 k = bigmod(proto_k, dss->q);
602 smemclr(digest512, sizeof(digest512));
605 * Now we have k, so just go ahead and compute the signature.
607 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
608 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
611 hash = bignum_from_bytes(digest, 20);
612 kinv = modinv(k, dss->q); /* k^-1 mod q */
613 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
614 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
623 * string two 20-byte numbers r and s, end to end
625 * i.e. 4+7 + 4+40 bytes.
627 nbytes = 4 + 7 + 4 + 40;
628 bytes = snewn(nbytes, unsigned char);
630 memcpy(bytes + 4, "ssh-dss", 7);
631 PUT_32BIT(bytes + 4 + 7, 40);
632 for (i = 0; i < 20; i++) {
633 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
634 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
643 const struct ssh_signkey ssh_dss = {
650 dss_openssh_createkey,