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);
92 getstring(&data, &len, &p, &slen);
98 for (i = 0; i < len; i++)
99 printf(" %02x", (unsigned char) (data[i]));
104 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
108 dss->p = getmp(&data, &len);
109 dss->q = getmp(&data, &len);
110 dss->g = getmp(&data, &len);
111 dss->y = getmp(&data, &len);
117 static void dss_freekey(void *key)
119 struct dss_key *dss = (struct dss_key *) key;
133 static char *dss_fmtkey(void *key)
135 struct dss_key *dss = (struct dss_key *) key;
137 int len, i, pos, nibbles;
138 static const char hex[] = "0123456789abcdef";
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 = snewn(len, char);
151 pos += sprintf(p + pos, "0x");
152 nibbles = (3 + bignum_bitcount(dss->p)) / 4;
155 for (i = nibbles; i--;)
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;
162 for (i = nibbles; i--;)
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;
169 for (i = nibbles; i--;)
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;
176 for (i = nibbles; i--;)
178 hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF];
183 static char *dss_fingerprint(void *key)
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];
193 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11);
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); \
208 MD5Final(digest, &md5c);
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 ? ":" : "",
214 ret = snewn(strlen(buffer) + 1, char);
220 static int dss_verifysig(void *key, char *sig, int siglen,
221 char *data, int datalen)
223 struct dss_key *dss = (struct dss_key *) key;
227 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
237 for (i = 0; i < siglen; i++)
238 printf(" %02x", (unsigned char) (sig[i]));
243 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
244 * of a DSA signature. OpenSSH is in line with RFC 4253:
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 RFC-compliant.
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)) {
258 sig += 4, siglen -= 4; /* skip yet another length field */
260 r = get160(&sig, &siglen);
261 s = get160(&sig, &siglen);
266 * Step 1. w <- s^-1 mod q.
268 w = modinv(s, dss->q);
271 * Step 2. u1 <- SHA(message) * w mod q.
273 SHA_Simple(data, datalen, (unsigned char *)hash);
276 sha = get160(&p, &slen);
277 u1 = modmul(sha, w, dss->q);
280 * Step 3. u2 <- r * w mod q.
282 u2 = modmul(r, w, dss->q);
285 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
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);
293 * Step 5. v should now be equal to r.
296 ret = !bignum_cmp(v, r);
312 static unsigned char *dss_public_blob(void *key, int *len)
314 struct dss_key *dss = (struct dss_key *) key;
315 int plen, qlen, glen, ylen, bloblen;
317 unsigned char *blob, *p;
319 plen = (bignum_bitcount(dss->p) + 8) / 8;
320 qlen = (bignum_bitcount(dss->q) + 8) / 8;
321 glen = (bignum_bitcount(dss->g) + 8) / 8;
322 ylen = (bignum_bitcount(dss->y) + 8) / 8;
325 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
326 * 27 + sum of lengths. (five length fields, 20+7=27).
328 bloblen = 27 + plen + qlen + glen + ylen;
329 blob = snewn(bloblen, unsigned char);
333 memcpy(p, "ssh-dss", 7);
338 *p++ = bignum_byte(dss->p, i);
342 *p++ = bignum_byte(dss->q, i);
346 *p++ = bignum_byte(dss->g, i);
350 *p++ = bignum_byte(dss->y, i);
351 assert(p == blob + bloblen);
356 static unsigned char *dss_private_blob(void *key, int *len)
358 struct dss_key *dss = (struct dss_key *) key;
361 unsigned char *blob, *p;
363 xlen = (bignum_bitcount(dss->x) + 8) / 8;
366 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
369 blob = snewn(bloblen, unsigned char);
374 *p++ = bignum_byte(dss->x, i);
375 assert(p == blob + bloblen);
380 static void *dss_createkey(unsigned char *pub_blob, int pub_len,
381 unsigned char *priv_blob, int priv_len)
384 char *pb = (char *) priv_blob;
388 unsigned char digest[20];
391 dss = dss_newkey((char *) pub_blob, pub_len);
392 dss->x = getmp(&pb, &priv_len);
395 * Check the obsolete hash in the old DSS key format.
398 getstring(&pb, &priv_len, &hash, &hashlen);
401 sha_mpint(&s, dss->p);
402 sha_mpint(&s, dss->q);
403 sha_mpint(&s, dss->g);
404 SHA_Final(&s, digest);
405 if (0 != memcmp(hash, digest, 20)) {
412 * Now ensure g^x mod p really is y.
414 ytest = modpow(dss->g, dss->x, dss->p);
415 if (0 != bignum_cmp(ytest, dss->y)) {
425 static void *dss_openssh_createkey(unsigned char **blob, int *len)
427 char **b = (char **) blob;
430 dss = snew(struct dss_key);
432 dss->p = getmp(b, len);
433 dss->q = getmp(b, len);
434 dss->g = getmp(b, len);
435 dss->y = getmp(b, len);
436 dss->x = getmp(b, len);
438 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x) {
451 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
453 struct dss_key *dss = (struct dss_key *) key;
457 ssh2_bignum_length(dss->p) +
458 ssh2_bignum_length(dss->q) +
459 ssh2_bignum_length(dss->g) +
460 ssh2_bignum_length(dss->y) +
461 ssh2_bignum_length(dss->x);
468 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
469 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
479 static int dss_pubkey_bits(void *blob, int len)
484 dss = dss_newkey((char *) blob, len);
485 ret = bignum_bitcount(dss->p);
491 static unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
494 * The basic DSS signing algorithm is:
496 * - invent a random k between 1 and q-1 (exclusive).
497 * - Compute r = (g^k mod p) mod q.
498 * - Compute s = k^-1 * (hash + x*r) mod q.
500 * This has the dangerous properties that:
502 * - if an attacker in possession of the public key _and_ the
503 * signature (for example, the host you just authenticated
504 * to) can guess your k, he can reverse the computation of s
505 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
506 * can deduce the private half of your key, and masquerade
507 * as you for as long as the key is still valid.
509 * - since r is a function purely of k and the public key, if
510 * the attacker only has a _range of possibilities_ for k
511 * it's easy for him to work through them all and check each
512 * one against r; he'll never be unsure of whether he's got
515 * - if you ever sign two different hashes with the same k, it
516 * will be immediately obvious because the two signatures
517 * will have the same r, and moreover an attacker in
518 * possession of both signatures (and the public key of
519 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
520 * and from there deduce x as before.
522 * - the Bleichenbacher attack on DSA makes use of methods of
523 * generating k which are significantly non-uniformly
524 * distributed; in particular, generating a 160-bit random
525 * number and reducing it mod q is right out.
527 * For this reason we must be pretty careful about how we
528 * generate our k. Since this code runs on Windows, with no
529 * particularly good system entropy sources, we can't trust our
530 * RNG itself to produce properly unpredictable data. Hence, we
531 * use a totally different scheme instead.
533 * What we do is to take a SHA-512 (_big_) hash of the private
534 * key x, and then feed this into another SHA-512 hash that
535 * also includes the message hash being signed. That is:
537 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
539 * This number is 512 bits long, so reducing it mod q won't be
540 * noticeably non-uniform. So
544 * This has the interesting property that it's _deterministic_:
545 * signing the same hash twice with the same key yields the
548 * Despite this determinism, it's still not predictable to an
549 * attacker, because in order to repeat the SHA-512
550 * construction that created it, the attacker would have to
551 * know the private key value x - and by assumption he doesn't,
552 * because if he knew that he wouldn't be attacking k!
554 * (This trick doesn't, _per se_, protect against reuse of k.
555 * Reuse of k is left to chance; all it does is prevent
556 * _excessively high_ chances of reuse of k due to entropy
559 * Thanks to Colin Plumb for the general idea of using x to
560 * ensure k is hard to guess, and to the Cambridge University
561 * Computer Security Group for helping to argue out all the
564 struct dss_key *dss = (struct dss_key *) key;
566 unsigned char digest[20], digest512[64];
567 Bignum proto_k, k, gkp, hash, kinv, hxr, r, s;
568 unsigned char *bytes;
571 SHA_Simple(data, datalen, digest);
574 * Hash some identifying text plus x.
577 SHA512_Bytes(&ss, "DSA deterministic k generator", 30);
578 sha512_mpint(&ss, dss->x);
579 SHA512_Final(&ss, digest512);
582 * Now hash that digest plus the message hash.
585 SHA512_Bytes(&ss, digest512, sizeof(digest512));
586 SHA512_Bytes(&ss, digest, sizeof(digest));
587 SHA512_Final(&ss, digest512);
589 smemclr(&ss, sizeof(ss));
592 * Now convert the result into a bignum, and reduce it mod q.
594 proto_k = bignum_from_bytes(digest512, 64);
595 k = bigmod(proto_k, dss->q);
598 smemclr(digest512, sizeof(digest512));
601 * Now we have k, so just go ahead and compute the signature.
603 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
604 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
607 hash = bignum_from_bytes(digest, 20);
608 kinv = modinv(k, dss->q); /* k^-1 mod q */
609 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
610 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
619 * string two 20-byte numbers r and s, end to end
621 * i.e. 4+7 + 4+40 bytes.
623 nbytes = 4 + 7 + 4 + 40;
624 bytes = snewn(nbytes, unsigned char);
626 memcpy(bytes + 4, "ssh-dss", 7);
627 PUT_32BIT(bytes + 4 + 7, 40);
628 for (i = 0; i < 20; i++) {
629 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
630 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
639 const struct ssh_signkey ssh_dss = {
646 dss_openssh_createkey,