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 int dss_verifysig(void *key, const char *sig, int siglen,
194 const char *data, int datalen)
196 struct dss_key *dss = (struct dss_key *) key;
200 Bignum r, s, w, gu1p, yu2p, gu1yu2p, u1, u2, sha, v;
210 for (i = 0; i < siglen; i++)
211 printf(" %02x", (unsigned char) (sig[i]));
216 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
217 * of a DSA signature. OpenSSH is in line with RFC 4253:
218 * it uses a string "ssh-dss", followed by a 40-byte string
219 * containing two 160-bit integers end-to-end. Commercial SSH
220 * can't be bothered with the header bit, and considers a DSA
221 * signature blob to be _just_ the 40-byte string containing
222 * the two 160-bit integers. We tell them apart by measuring
223 * the length: length 40 means the commercial-SSH bug, anything
224 * else is assumed to be RFC-compliant.
226 if (siglen != 40) { /* bug not present; read admin fields */
227 getstring(&sig, &siglen, &p, &slen);
228 if (!p || slen != 7 || memcmp(p, "ssh-dss", 7)) {
231 sig += 4, siglen -= 4; /* skip yet another length field */
233 r = get160(&sig, &siglen);
234 s = get160(&sig, &siglen);
243 if (!bignum_cmp(s, Zero)) {
250 * Step 1. w <- s^-1 mod q.
252 w = modinv(s, dss->q);
260 * Step 2. u1 <- SHA(message) * w mod q.
262 SHA_Simple(data, datalen, (unsigned char *)hash);
265 sha = get160(&p, &slen);
266 u1 = modmul(sha, w, dss->q);
269 * Step 3. u2 <- r * w mod q.
271 u2 = modmul(r, w, dss->q);
274 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
276 gu1p = modpow(dss->g, u1, dss->p);
277 yu2p = modpow(dss->y, u2, dss->p);
278 gu1yu2p = modmul(gu1p, yu2p, dss->p);
279 v = modmul(gu1yu2p, One, dss->q);
282 * Step 5. v should now be equal to r.
285 ret = !bignum_cmp(v, r);
301 static unsigned char *dss_public_blob(void *key, int *len)
303 struct dss_key *dss = (struct dss_key *) key;
304 int plen, qlen, glen, ylen, bloblen;
306 unsigned char *blob, *p;
308 plen = (bignum_bitcount(dss->p) + 8) / 8;
309 qlen = (bignum_bitcount(dss->q) + 8) / 8;
310 glen = (bignum_bitcount(dss->g) + 8) / 8;
311 ylen = (bignum_bitcount(dss->y) + 8) / 8;
314 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
315 * 27 + sum of lengths. (five length fields, 20+7=27).
317 bloblen = 27 + plen + qlen + glen + ylen;
318 blob = snewn(bloblen, unsigned char);
322 memcpy(p, "ssh-dss", 7);
327 *p++ = bignum_byte(dss->p, i);
331 *p++ = bignum_byte(dss->q, i);
335 *p++ = bignum_byte(dss->g, i);
339 *p++ = bignum_byte(dss->y, i);
340 assert(p == blob + bloblen);
345 static unsigned char *dss_private_blob(void *key, int *len)
347 struct dss_key *dss = (struct dss_key *) key;
350 unsigned char *blob, *p;
352 xlen = (bignum_bitcount(dss->x) + 8) / 8;
355 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
358 blob = snewn(bloblen, unsigned char);
363 *p++ = bignum_byte(dss->x, i);
364 assert(p == blob + bloblen);
369 static void *dss_createkey(const unsigned char *pub_blob, int pub_len,
370 const unsigned char *priv_blob, int priv_len)
373 const char *pb = (const char *) priv_blob;
377 unsigned char digest[20];
380 dss = dss_newkey((char *) pub_blob, pub_len);
383 dss->x = getmp(&pb, &priv_len);
390 * Check the obsolete hash in the old DSS key format.
393 getstring(&pb, &priv_len, &hash, &hashlen);
396 sha_mpint(&s, dss->p);
397 sha_mpint(&s, dss->q);
398 sha_mpint(&s, dss->g);
399 SHA_Final(&s, digest);
400 if (0 != memcmp(hash, digest, 20)) {
407 * Now ensure g^x mod p really is y.
409 ytest = modpow(dss->g, dss->x, dss->p);
410 if (0 != bignum_cmp(ytest, dss->y)) {
420 static void *dss_openssh_createkey(const unsigned char **blob, int *len)
422 const char **b = (const char **) blob;
425 dss = snew(struct dss_key);
427 dss->p = getmp(b, len);
428 dss->q = getmp(b, len);
429 dss->g = getmp(b, len);
430 dss->y = getmp(b, len);
431 dss->x = getmp(b, len);
433 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x ||
434 !bignum_cmp(dss->q, Zero) || !bignum_cmp(dss->p, Zero)) {
443 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
445 struct dss_key *dss = (struct dss_key *) key;
449 ssh2_bignum_length(dss->p) +
450 ssh2_bignum_length(dss->q) +
451 ssh2_bignum_length(dss->g) +
452 ssh2_bignum_length(dss->y) +
453 ssh2_bignum_length(dss->x);
460 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
461 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
471 static int dss_pubkey_bits(const void *blob, int len)
476 dss = dss_newkey((const char *) blob, len);
479 ret = bignum_bitcount(dss->p);
485 Bignum *dss_gen_k(const char *id_string, Bignum modulus, Bignum private_key,
486 unsigned char *digest, int digest_len)
489 * The basic DSS signing algorithm is:
491 * - invent a random k between 1 and q-1 (exclusive).
492 * - Compute r = (g^k mod p) mod q.
493 * - Compute s = k^-1 * (hash + x*r) mod q.
495 * This has the dangerous properties that:
497 * - if an attacker in possession of the public key _and_ the
498 * signature (for example, the host you just authenticated
499 * to) can guess your k, he can reverse the computation of s
500 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
501 * can deduce the private half of your key, and masquerade
502 * as you for as long as the key is still valid.
504 * - since r is a function purely of k and the public key, if
505 * the attacker only has a _range of possibilities_ for k
506 * it's easy for him to work through them all and check each
507 * one against r; he'll never be unsure of whether he's got
510 * - if you ever sign two different hashes with the same k, it
511 * will be immediately obvious because the two signatures
512 * will have the same r, and moreover an attacker in
513 * possession of both signatures (and the public key of
514 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
515 * and from there deduce x as before.
517 * - the Bleichenbacher attack on DSA makes use of methods of
518 * generating k which are significantly non-uniformly
519 * distributed; in particular, generating a 160-bit random
520 * number and reducing it mod q is right out.
522 * For this reason we must be pretty careful about how we
523 * generate our k. Since this code runs on Windows, with no
524 * particularly good system entropy sources, we can't trust our
525 * RNG itself to produce properly unpredictable data. Hence, we
526 * use a totally different scheme instead.
528 * What we do is to take a SHA-512 (_big_) hash of the private
529 * key x, and then feed this into another SHA-512 hash that
530 * also includes the message hash being signed. That is:
532 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
534 * This number is 512 bits long, so reducing it mod q won't be
535 * noticeably non-uniform. So
539 * This has the interesting property that it's _deterministic_:
540 * signing the same hash twice with the same key yields the
543 * Despite this determinism, it's still not predictable to an
544 * attacker, because in order to repeat the SHA-512
545 * construction that created it, the attacker would have to
546 * know the private key value x - and by assumption he doesn't,
547 * because if he knew that he wouldn't be attacking k!
549 * (This trick doesn't, _per se_, protect against reuse of k.
550 * Reuse of k is left to chance; all it does is prevent
551 * _excessively high_ chances of reuse of k due to entropy
554 * Thanks to Colin Plumb for the general idea of using x to
555 * ensure k is hard to guess, and to the Cambridge University
556 * Computer Security Group for helping to argue out all the
560 unsigned char digest512[64];
564 * Hash some identifying text plus x.
567 SHA512_Bytes(&ss, id_string, strlen(id_string) + 1);
568 sha512_mpint(&ss, private_key);
569 SHA512_Final(&ss, digest512);
572 * Now hash that digest plus the message hash.
575 SHA512_Bytes(&ss, digest512, sizeof(digest512));
576 SHA512_Bytes(&ss, digest, digest_len);
579 SHA512_State ss2 = ss; /* structure copy */
580 SHA512_Final(&ss2, digest512);
582 smemclr(&ss2, sizeof(ss2));
585 * Now convert the result into a bignum, and reduce it mod q.
587 proto_k = bignum_from_bytes(digest512, 64);
588 k = bigmod(proto_k, modulus);
591 if (bignum_cmp(k, One) != 0 && bignum_cmp(k, Zero) != 0) {
592 smemclr(&ss, sizeof(ss));
593 smemclr(digest512, sizeof(digest512));
597 /* Very unlikely we get here, but if so, k was unsuitable. */
599 /* Perturb the hash to think of a different k. */
600 SHA512_Bytes(&ss, "x", 1);
601 /* Go round and try again. */
605 static unsigned char *dss_sign(void *key, const char *data, int datalen,
608 struct dss_key *dss = (struct dss_key *) key;
609 Bignum k, gkp, hash, kinv, hxr, r, s;
610 unsigned char digest[20];
611 unsigned char *bytes;
614 SHA_Simple(data, datalen, digest);
616 k = dss_gen_k("DSA deterministic k generator", dss->q, dss->x,
617 digest, sizeof(digest));
618 kinv = modinv(k, dss->q); /* k^-1 mod q */
622 * Now we have k, so just go ahead and compute the signature.
624 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
625 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
628 hash = bignum_from_bytes(digest, 20);
629 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
630 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
640 * string two 20-byte numbers r and s, end to end
642 * i.e. 4+7 + 4+40 bytes.
644 nbytes = 4 + 7 + 4 + 40;
645 bytes = snewn(nbytes, unsigned char);
647 memcpy(bytes + 4, "ssh-dss", 7);
648 PUT_32BIT(bytes + 4 + 7, 40);
649 for (i = 0; i < 20; i++) {
650 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
651 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
660 const struct ssh_signkey ssh_dss = {
667 dss_openssh_createkey,