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 struct ssh_signkey *self,
89 const char *data, int len)
95 dss = snew(struct dss_key);
96 getstring(&data, &len, &p, &slen);
102 for (i = 0; i < len; i++)
103 printf(" %02x", (unsigned char) (data[i]));
108 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
112 dss->p = getmp(&data, &len);
113 dss->q = getmp(&data, &len);
114 dss->g = getmp(&data, &len);
115 dss->y = getmp(&data, &len);
118 if (!dss->p || !dss->q || !dss->g || !dss->y ||
119 !bignum_cmp(dss->q, Zero) || !bignum_cmp(dss->p, Zero)) {
128 static void dss_freekey(void *key)
130 struct dss_key *dss = (struct dss_key *) key;
144 static char *dss_fmtkey(void *key)
146 struct dss_key *dss = (struct dss_key *) key;
148 int len, i, pos, nibbles;
149 static const char hex[] = "0123456789abcdef";
152 len = 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
153 len += 4 * (bignum_bitcount(dss->p) + 15) / 16;
154 len += 4 * (bignum_bitcount(dss->q) + 15) / 16;
155 len += 4 * (bignum_bitcount(dss->g) + 15) / 16;
156 len += 4 * (bignum_bitcount(dss->y) + 15) / 16;
157 p = snewn(len, char);
162 pos += sprintf(p + pos, "0x");
163 nibbles = (3 + bignum_bitcount(dss->p)) / 4;
166 for (i = nibbles; i--;)
168 hex[(bignum_byte(dss->p, i / 2) >> (4 * (i % 2))) & 0xF];
169 pos += sprintf(p + pos, ",0x");
170 nibbles = (3 + bignum_bitcount(dss->q)) / 4;
173 for (i = nibbles; i--;)
175 hex[(bignum_byte(dss->q, i / 2) >> (4 * (i % 2))) & 0xF];
176 pos += sprintf(p + pos, ",0x");
177 nibbles = (3 + bignum_bitcount(dss->g)) / 4;
180 for (i = nibbles; i--;)
182 hex[(bignum_byte(dss->g, i / 2) >> (4 * (i % 2))) & 0xF];
183 pos += sprintf(p + pos, ",0x");
184 nibbles = (3 + bignum_bitcount(dss->y)) / 4;
187 for (i = nibbles; i--;)
189 hex[(bignum_byte(dss->y, i / 2) >> (4 * (i % 2))) & 0xF];
194 static int dss_verifysig(void *key, const char *sig, int siglen,
195 const char *data, int datalen)
197 struct dss_key *dss = (struct dss_key *) key;
201 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
211 for (i = 0; i < siglen; i++)
212 printf(" %02x", (unsigned char) (sig[i]));
217 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
218 * of a DSA signature. OpenSSH is in line with RFC 4253:
219 * it uses a string "ssh-dss", followed by a 40-byte string
220 * containing two 160-bit integers end-to-end. Commercial SSH
221 * can't be bothered with the header bit, and considers a DSA
222 * signature blob to be _just_ the 40-byte string containing
223 * the two 160-bit integers. We tell them apart by measuring
224 * the length: length 40 means the commercial-SSH bug, anything
225 * else is assumed to be RFC-compliant.
227 if (siglen != 40) { /* bug not present; read admin fields */
228 getstring(&sig, &siglen, &p, &slen);
229 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
232 sig += 4, siglen -= 4; /* skip yet another length field */
234 r = get160(&sig, &siglen);
235 s = get160(&sig, &siglen);
244 if (!bignum_cmp(s, Zero)) {
251 * Step 1. w <- s^-1 mod q.
253 w = modinv(s, dss->q);
261 * Step 2. u1 <- SHA(message) * w mod q.
263 SHA_Simple(data, datalen, (unsigned char *)hash);
266 sha = get160(&p, &slen);
267 u1 = modmul(sha, w, dss->q);
270 * Step 3. u2 <- r * w mod q.
272 u2 = modmul(r, w, dss->q);
275 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
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);
283 * Step 5. v should now be equal to r.
286 ret = !bignum_cmp(v, r);
302 static unsigned char *dss_public_blob(void *key, int *len)
304 struct dss_key *dss = (struct dss_key *) key;
305 int plen, qlen, glen, ylen, bloblen;
307 unsigned char *blob, *p;
309 plen = (bignum_bitcount(dss->p) + 8) / 8;
310 qlen = (bignum_bitcount(dss->q) + 8) / 8;
311 glen = (bignum_bitcount(dss->g) + 8) / 8;
312 ylen = (bignum_bitcount(dss->y) + 8) / 8;
315 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
316 * 27 + sum of lengths. (five length fields, 20+7=27).
318 bloblen = 27 + plen + qlen + glen + ylen;
319 blob = snewn(bloblen, unsigned char);
323 memcpy(p, "ssh-dss", 7);
328 *p++ = bignum_byte(dss->p, i);
332 *p++ = bignum_byte(dss->q, i);
336 *p++ = bignum_byte(dss->g, i);
340 *p++ = bignum_byte(dss->y, i);
341 assert(p == blob + bloblen);
346 static unsigned char *dss_private_blob(void *key, int *len)
348 struct dss_key *dss = (struct dss_key *) key;
351 unsigned char *blob, *p;
353 xlen = (bignum_bitcount(dss->x) + 8) / 8;
356 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
359 blob = snewn(bloblen, unsigned char);
364 *p++ = bignum_byte(dss->x, i);
365 assert(p == blob + bloblen);
370 static void *dss_createkey(const struct ssh_signkey *self,
371 const unsigned char *pub_blob, int pub_len,
372 const unsigned char *priv_blob, int priv_len)
375 const char *pb = (const char *) priv_blob;
379 unsigned char digest[20];
382 dss = dss_newkey(self, (char *) pub_blob, pub_len);
385 dss->x = getmp(&pb, &priv_len);
392 * Check the obsolete hash in the old DSS key format.
395 getstring(&pb, &priv_len, &hash, &hashlen);
398 sha_mpint(&s, dss->p);
399 sha_mpint(&s, dss->q);
400 sha_mpint(&s, dss->g);
401 SHA_Final(&s, digest);
402 if (0 != memcmp(hash, digest, 20)) {
409 * Now ensure g^x mod p really is y.
411 ytest = modpow(dss->g, dss->x, dss->p);
412 if (0 != bignum_cmp(ytest, dss->y)) {
422 static void *dss_openssh_createkey(const struct ssh_signkey *self,
423 const unsigned char **blob, int *len)
425 const char **b = (const char **) blob;
428 dss = snew(struct dss_key);
430 dss->p = getmp(b, len);
431 dss->q = getmp(b, len);
432 dss->g = getmp(b, len);
433 dss->y = getmp(b, len);
434 dss->x = getmp(b, len);
436 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x ||
437 !bignum_cmp(dss->q, Zero) || !bignum_cmp(dss->p, Zero)) {
446 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
448 struct dss_key *dss = (struct dss_key *) key;
452 ssh2_bignum_length(dss->p) +
453 ssh2_bignum_length(dss->q) +
454 ssh2_bignum_length(dss->g) +
455 ssh2_bignum_length(dss->y) +
456 ssh2_bignum_length(dss->x);
463 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
464 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
474 static int dss_pubkey_bits(const struct ssh_signkey *self,
475 const void *blob, int len)
480 dss = dss_newkey(self, (const char *) blob, len);
483 ret = bignum_bitcount(dss->p);
489 Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key,
490 unsigned char *digest, int digest_len)
493 * The basic DSS signing algorithm is:
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.
499 * This has the dangerous properties that:
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.
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
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.
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.
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.
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:
536 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
538 * This number is 512 bits long, so reducing it mod q won't be
539 * noticeably non-uniform. So
543 * This has the interesting property that it's _deterministic_:
544 * signing the same hash twice with the same key yields the
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!
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
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
564 unsigned char digest512[64];
568 * Hash some identifying text plus x.
571 SHA512_Bytes(&ss, id_string, strlen(id_string) + 1);
572 sha512_mpint(&ss, private_key);
573 SHA512_Final(&ss, digest512);
576 * Now hash that digest plus the message hash.
579 SHA512_Bytes(&ss, digest512, sizeof(digest512));
580 SHA512_Bytes(&ss, digest, digest_len);
583 SHA512_State ss2 = ss; /* structure copy */
584 SHA512_Final(&ss2, digest512);
586 smemclr(&ss2, sizeof(ss2));
589 * Now convert the result into a bignum, and reduce it mod q.
591 proto_k = bignum_from_bytes(digest512, 64);
592 k = bigmod(proto_k, modulus);
595 if (bignum_cmp(k, One) != 0 && bignum_cmp(k, Zero) != 0) {
596 smemclr(&ss, sizeof(ss));
597 smemclr(digest512, sizeof(digest512));
601 /* Very unlikely we get here, but if so, k was unsuitable. */
603 /* Perturb the hash to think of a different k. */
604 SHA512_Bytes(&ss, "x", 1);
605 /* Go round and try again. */
609 static unsigned char *dss_sign(void *key, const char *data, int datalen,
612 struct dss_key *dss = (struct dss_key *) key;
613 Bignum k, gkp, hash, kinv, hxr, r, s;
614 unsigned char digest[20];
615 unsigned char *bytes;
618 SHA_Simple(data, datalen, digest);
620 k = dss_gen_k("DSA deterministic k generator", dss->q, dss->x,
621 digest, sizeof(digest));
622 kinv = modinv(k, dss->q); /* k^-1 mod q */
626 * Now we have k, so just go ahead and compute the signature.
628 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
629 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
632 hash = bignum_from_bytes(digest, 20);
633 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
634 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
644 * string two 20-byte numbers r and s, end to end
646 * i.e. 4+7 + 4+40 bytes.
648 nbytes = 4 + 7 + 4 + 40;
649 bytes = snewn(nbytes, unsigned char);
651 memcpy(bytes + 4, "ssh-dss", 7);
652 PUT_32BIT(bytes + 4 + 7, 40);
653 for (i = 0; i < 20; i++) {
654 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
655 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
664 const struct ssh_signkey ssh_dss = {
671 dss_openssh_createkey,