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(const char **data, int *datalen,
41 const char **p, int *length)
46 *length = toint(GET_32BIT(*data));
51 if (*datalen < *length)
57 static Bignum getmp(const char **data, int *datalen)
63 getstring(data, datalen, &p, &length);
67 return NULL; /* negative mp */
68 b = bignum_from_bytes((const unsigned char *)p, length);
72 static Bignum get160(const char **data, int *datalen)
79 b = bignum_from_bytes((const unsigned char *)*data, 20);
86 static void dss_freekey(void *key); /* forward reference */
88 static void *dss_newkey(const char *data, int len)
94 dss = snew(struct dss_key);
95 getstring(&data, &len, &p, &slen);
101 for (i = 0; i < len; i++)
102 printf(" %02x", (unsigned char) (data[i]));
107 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
111 dss->p = getmp(&data, &len);
112 dss->q = getmp(&data, &len);
113 dss->g = getmp(&data, &len);
114 dss->y = getmp(&data, &len);
117 if (!dss->p || !dss->q || !dss->g || !dss->y ||
118 !bignum_cmp(dss->q, Zero) || !bignum_cmp(dss->p, Zero)) {
127 static void dss_freekey(void *key)
129 struct dss_key *dss = (struct dss_key *) key;
143 static char *dss_fmtkey(void *key)
145 struct dss_key *dss = (struct dss_key *) key;
147 int len, i, pos, nibbles;
148 static const char hex[] = "0123456789abcdef";
151 len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
152 len += 4 * (bignum_bitcount(dss->p) + 15) / 16;
153 len += 4 * (bignum_bitcount(dss->q) + 15) / 16;
154 len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
155 len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
156 p = snewn(len, char);
161 pos += sprintf(p + pos, "0x");
162 nibbles = (3 + bignum_bitcount(dss->p)) / 4;
165 for (i = nibbles; i--;)
167 hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF];
168 pos += sprintf(p + pos, ",0x");
169 nibbles = (3 + bignum_bitcount(dss->q)) / 4;
172 for (i = nibbles; i--;)
174 hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF];
175 pos += sprintf(p + pos, ",0x");
176 nibbles = (3 + bignum_bitcount(dss->g)) / 4;
179 for (i = nibbles; i--;)
181 hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF];
182 pos += sprintf(p + pos, ",0x");
183 nibbles = (3 + bignum_bitcount(dss->y)) / 4;
186 for (i = nibbles; i--;)
188 hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF];
193 static char *dss_fingerprint(void *key)
195 struct dss_key *dss = (struct dss_key *) key;
196 struct MD5Context md5c;
197 unsigned char digest[16], lenbuf[4];
198 char buffer[16 * 3 + 40];
203 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-dss", 11);
205 #define ADD_BIGNUM(bignum) \
206 numlen = (bignum_bitcount(bignum)+8)/8; \
207 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
208 for (i = numlen; i-- ;) { \
209 unsigned char c = bignum_byte(bignum, i); \
210 MD5Update(&md5c, &c, 1); \
218 MD5Final(digest, &md5c);
220 sprintf(buffer, "ssh-dss %d ", bignum_bitcount(dss->p));
221 for (i = 0; i < 16; i++)
222 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
224 ret = snewn(strlen(buffer) + 1, char);
230 static int dss_verifysig(void *key, const char *sig, int siglen,
231 const char *data, int datalen)
233 struct dss_key *dss = (struct dss_key *) key;
237 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
247 for (i = 0; i < siglen; i++)
248 printf(" %02x", (unsigned char) (sig[i]));
253 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
254 * of a DSA signature. OpenSSH is in line with RFC 4253:
255 * it uses a string "ssh-dss", followed by a 40-byte string
256 * containing two 160-bit integers end-to-end. Commercial SSH
257 * can't be bothered with the header bit, and considers a DSA
258 * signature blob to be _just_ the 40-byte string containing
259 * the two 160-bit integers. We tell them apart by measuring
260 * the length: length 40 means the commercial-SSH bug, anything
261 * else is assumed to be RFC-compliant.
263 if (siglen != 40) { /* bug not present; read admin fields */
264 getstring(&sig, &siglen, &p, &slen);
265 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
268 sig += 4, siglen -= 4; /* skip yet another length field */
270 r = get160(&sig, &siglen);
271 s = get160(&sig, &siglen);
280 if (!bignum_cmp(s, Zero)) {
287 * Step 1. w <- s^-1 mod q.
289 w = modinv(s, dss->q);
297 * Step 2. u1 <- SHA(message) * w mod q.
299 SHA_Simple(data, datalen, (unsigned char *)hash);
302 sha = get160(&p, &slen);
303 u1 = modmul(sha, w, dss->q);
306 * Step 3. u2 <- r * w mod q.
308 u2 = modmul(r, w, dss->q);
311 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
313 gu1p = modpow(dss->g, u1, dss->p);
314 yu2p = modpow(dss->y, u2, dss->p);
315 gu1yu2p = modmul(gu1p, yu2p, dss->p);
316 v = modmul(gu1yu2p, One, dss->q);
319 * Step 5. v should now be equal to r.
322 ret = !bignum_cmp(v, r);
338 static unsigned char *dss_public_blob(void *key, int *len)
340 struct dss_key *dss = (struct dss_key *) key;
341 int plen, qlen, glen, ylen, bloblen;
343 unsigned char *blob, *p;
345 plen = (bignum_bitcount(dss->p) + 8) / 8;
346 qlen = (bignum_bitcount(dss->q) + 8) / 8;
347 glen = (bignum_bitcount(dss->g) + 8) / 8;
348 ylen = (bignum_bitcount(dss->y) + 8) / 8;
351 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
352 * 27 + sum of lengths. (five length fields, 20+7=27).
354 bloblen = 27 + plen + qlen + glen + ylen;
355 blob = snewn(bloblen, unsigned char);
359 memcpy(p, "ssh-dss", 7);
364 *p++ = bignum_byte(dss->p, i);
368 *p++ = bignum_byte(dss->q, i);
372 *p++ = bignum_byte(dss->g, i);
376 *p++ = bignum_byte(dss->y, i);
377 assert(p == blob + bloblen);
382 static unsigned char *dss_private_blob(void *key, int *len)
384 struct dss_key *dss = (struct dss_key *) key;
387 unsigned char *blob, *p;
389 xlen = (bignum_bitcount(dss->x) + 8) / 8;
392 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
395 blob = snewn(bloblen, unsigned char);
400 *p++ = bignum_byte(dss->x, i);
401 assert(p == blob + bloblen);
406 static void *dss_createkey(const unsigned char *pub_blob, int pub_len,
407 const unsigned char *priv_blob, int priv_len)
410 const char *pb = (const char *) priv_blob;
414 unsigned char digest[20];
417 dss = dss_newkey((char *) pub_blob, pub_len);
420 dss->x = getmp(&pb, &priv_len);
427 * Check the obsolete hash in the old DSS key format.
430 getstring(&pb, &priv_len, &hash, &hashlen);
433 sha_mpint(&s, dss->p);
434 sha_mpint(&s, dss->q);
435 sha_mpint(&s, dss->g);
436 SHA_Final(&s, digest);
437 if (0 != memcmp(hash, digest, 20)) {
444 * Now ensure g^x mod p really is y.
446 ytest = modpow(dss->g, dss->x, dss->p);
447 if (0 != bignum_cmp(ytest, dss->y)) {
457 static void *dss_openssh_createkey(const unsigned char **blob, int *len)
459 const char **b = (const char **) blob;
462 dss = snew(struct dss_key);
464 dss->p = getmp(b, len);
465 dss->q = getmp(b, len);
466 dss->g = getmp(b, len);
467 dss->y = getmp(b, len);
468 dss->x = getmp(b, len);
470 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x ||
471 !bignum_cmp(dss->q, Zero) || !bignum_cmp(dss->p, Zero)) {
480 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
482 struct dss_key *dss = (struct dss_key *) key;
486 ssh2_bignum_length(dss->p) +
487 ssh2_bignum_length(dss->q) +
488 ssh2_bignum_length(dss->g) +
489 ssh2_bignum_length(dss->y) +
490 ssh2_bignum_length(dss->x);
497 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
498 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
508 static int dss_pubkey_bits(const void *blob, int len)
513 dss = dss_newkey((const char *) blob, len);
516 ret = bignum_bitcount(dss->p);
522 Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key,
523 unsigned char *digest, int digest_len)
526 * The basic DSS signing algorithm is:
528 * - invent a random k between 1 and q-1 (exclusive).
529 * - Compute r = (g^k mod p) mod q.
530 * - Compute s = k^-1 * (hash + x*r) mod q.
532 * This has the dangerous properties that:
534 * - if an attacker in possession of the public key _and_ the
535 * signature (for example, the host you just authenticated
536 * to) can guess your k, he can reverse the computation of s
537 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
538 * can deduce the private half of your key, and masquerade
539 * as you for as long as the key is still valid.
541 * - since r is a function purely of k and the public key, if
542 * the attacker only has a _range of possibilities_ for k
543 * it's easy for him to work through them all and check each
544 * one against r; he'll never be unsure of whether he's got
547 * - if you ever sign two different hashes with the same k, it
548 * will be immediately obvious because the two signatures
549 * will have the same r, and moreover an attacker in
550 * possession of both signatures (and the public key of
551 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
552 * and from there deduce x as before.
554 * - the Bleichenbacher attack on DSA makes use of methods of
555 * generating k which are significantly non-uniformly
556 * distributed; in particular, generating a 160-bit random
557 * number and reducing it mod q is right out.
559 * For this reason we must be pretty careful about how we
560 * generate our k. Since this code runs on Windows, with no
561 * particularly good system entropy sources, we can't trust our
562 * RNG itself to produce properly unpredictable data. Hence, we
563 * use a totally different scheme instead.
565 * What we do is to take a SHA-512 (_big_) hash of the private
566 * key x, and then feed this into another SHA-512 hash that
567 * also includes the message hash being signed. That is:
569 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
571 * This number is 512 bits long, so reducing it mod q won't be
572 * noticeably non-uniform. So
576 * This has the interesting property that it's _deterministic_:
577 * signing the same hash twice with the same key yields the
580 * Despite this determinism, it's still not predictable to an
581 * attacker, because in order to repeat the SHA-512
582 * construction that created it, the attacker would have to
583 * know the private key value x - and by assumption he doesn't,
584 * because if he knew that he wouldn't be attacking k!
586 * (This trick doesn't, _per se_, protect against reuse of k.
587 * Reuse of k is left to chance; all it does is prevent
588 * _excessively high_ chances of reuse of k due to entropy
591 * Thanks to Colin Plumb for the general idea of using x to
592 * ensure k is hard to guess, and to the Cambridge University
593 * Computer Security Group for helping to argue out all the
597 unsigned char digest512[64];
601 * Hash some identifying text plus x.
604 SHA512_Bytes(&ss, id_string, strlen(id_string) + 1);
605 sha512_mpint(&ss, private_key);
606 SHA512_Final(&ss, digest512);
609 * Now hash that digest plus the message hash.
612 SHA512_Bytes(&ss, digest512, sizeof(digest512));
613 SHA512_Bytes(&ss, digest, digest_len);
616 SHA512_State ss2 = ss; /* structure copy */
617 SHA512_Final(&ss2, digest512);
619 smemclr(&ss2, sizeof(ss2));
622 * Now convert the result into a bignum, and reduce it mod q.
624 proto_k = bignum_from_bytes(digest512, 64);
625 k = bigmod(proto_k, modulus);
628 if (bignum_cmp(k, One) != 0 && bignum_cmp(k, Zero) != 0) {
629 smemclr(&ss, sizeof(ss));
630 smemclr(digest512, sizeof(digest512));
634 /* Very unlikely we get here, but if so, k was unsuitable. */
636 /* Perturb the hash to think of a different k. */
637 SHA512_Bytes(&ss, "x", 1);
638 /* Go round and try again. */
642 static unsigned char *dss_sign(void *key, const char *data, int datalen,
645 struct dss_key *dss = (struct dss_key *) key;
646 Bignum k, gkp, hash, kinv, hxr, r, s;
647 unsigned char digest[20];
648 unsigned char *bytes;
651 SHA_Simple(data, datalen, digest);
653 k = dss_gen_k("DSA deterministic k generator", dss->q, dss->x,
654 digest, sizeof(digest));
655 kinv = modinv(k, dss->q); /* k^-1 mod q */
659 * Now we have k, so just go ahead and compute the signature.
661 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
662 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
665 hash = bignum_from_bytes(digest, 20);
666 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
667 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
677 * string two 20-byte numbers r and s, end to end
679 * i.e. 4+7 + 4+40 bytes.
681 nbytes = 4 + 7 + 4 + 40;
682 bytes = snewn(nbytes, unsigned char);
684 memcpy(bytes + 4, "ssh-dss", 7);
685 PUT_32BIT(bytes + 4 + 7, 40);
686 for (i = 0; i < 20; i++) {
687 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
688 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
697 const struct ssh_signkey ssh_dss = {
704 dss_openssh_createkey,