8 #define GET_32BIT(cp) \
9 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
10 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
11 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
12 ((unsigned long)(unsigned char)(cp)[3]))
14 #define PUT_32BIT(cp, value) { \
15 (cp)[0] = (unsigned char)((value) >> 24); \
16 (cp)[1] = (unsigned char)((value) >> 16); \
17 (cp)[2] = (unsigned char)((value) >> 8); \
18 (cp)[3] = (unsigned char)(value); }
20 static void sha_mpint(SHA_State * s, Bignum b)
23 unsigned char lenbuf[4];
25 len = (bignum_bitcount(b) + 8) / 8;
26 PUT_32BIT(lenbuf, len);
27 SHA_Bytes(s, lenbuf, 4);
29 lenbuf[0] = bignum_byte(b, len);
30 SHA_Bytes(s, lenbuf, 1);
32 memset(lenbuf, 0, sizeof(lenbuf));
35 static void sha512_mpint(SHA512_State * s, Bignum b)
38 unsigned char lenbuf[4];
40 len = (bignum_bitcount(b) + 8) / 8;
41 PUT_32BIT(lenbuf, len);
42 SHA512_Bytes(s, lenbuf, 4);
44 lenbuf[0] = bignum_byte(b, len);
45 SHA512_Bytes(s, lenbuf, 1);
47 memset(lenbuf, 0, sizeof(lenbuf));
50 static void getstring(char **data, int *datalen, char **p, int *length)
55 *length = GET_32BIT(*data);
58 if (*datalen < *length)
64 static Bignum getmp(char **data, int *datalen)
70 getstring(data, datalen, &p, &length);
74 return NULL; /* negative mp */
75 b = bignum_from_bytes(p, length);
79 static Bignum get160(char **data, int *datalen)
83 b = bignum_from_bytes(*data, 20);
90 static void *dss_newkey(char *data, int len)
96 dss = smalloc(sizeof(struct dss_key));
99 getstring(&data, &len, &p, &slen);
105 for (i = 0; i < len; i++)
106 printf(" %02x", (unsigned char) (data[i]));
111 if (!p || memcmp(p, "ssh-dss", 7)) {
115 dss->p = getmp(&data, &len);
116 dss->q = getmp(&data, &len);
117 dss->g = getmp(&data, &len);
118 dss->y = getmp(&data, &len);
123 static void dss_freekey(void *key)
125 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;
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, "\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 = smalloc(strlen(buffer) + 1);
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 the IETF drafts:
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 IETF-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, 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);
310 static unsigned char *dss_public_blob(void *key, int *len)
312 struct dss_key *dss = (struct dss_key *) key;
313 int plen, qlen, glen, ylen, bloblen;
315 unsigned char *blob, *p;
317 plen = (bignum_bitcount(dss->p) + 8) / 8;
318 qlen = (bignum_bitcount(dss->q) + 8) / 8;
319 glen = (bignum_bitcount(dss->g) + 8) / 8;
320 ylen = (bignum_bitcount(dss->y) + 8) / 8;
323 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
324 * 27 + sum of lengths. (five length fields, 20+7=27).
326 bloblen = 27 + plen + qlen + glen + ylen;
327 blob = smalloc(bloblen);
331 memcpy(p, "ssh-dss", 7);
336 *p++ = bignum_byte(dss->p, i);
340 *p++ = bignum_byte(dss->q, i);
344 *p++ = bignum_byte(dss->g, i);
348 *p++ = bignum_byte(dss->y, i);
349 assert(p == blob + bloblen);
354 static unsigned char *dss_private_blob(void *key, int *len)
356 struct dss_key *dss = (struct dss_key *) key;
359 unsigned char *blob, *p;
361 unsigned char digest[20];
363 xlen = (bignum_bitcount(dss->x) + 8) / 8;
366 * mpint x, string[20] the SHA of p||q||g. Total 28 + xlen.
367 * (two length fields and twenty bytes, 20+8=28).
370 blob = smalloc(bloblen);
375 *p++ = bignum_byte(dss->x, i);
378 sha_mpint(&s, dss->p);
379 sha_mpint(&s, dss->q);
380 sha_mpint(&s, dss->g);
381 SHA_Final(&s, digest);
383 for (i = 0; i < 20; i++)
385 assert(p == blob + bloblen);
390 static void *dss_createkey(unsigned char *pub_blob, int pub_len,
391 unsigned char *priv_blob, int priv_len)
394 char *pb = (char *) priv_blob;
398 unsigned char digest[20];
401 dss = dss_newkey((char *) pub_blob, pub_len);
402 dss->x = getmp(&pb, &priv_len);
403 getstring(&pb, &priv_len, &hash, &hashlen);
406 * Verify details of the key. First check that the hash is
407 * indeed a hash of p||q||g.
414 sha_mpint(&s, dss->p);
415 sha_mpint(&s, dss->q);
416 sha_mpint(&s, dss->g);
417 SHA_Final(&s, digest);
418 if (0 != memcmp(hash, digest, 20)) {
424 * Now ensure g^x mod p really is y.
426 ytest = modpow(dss->g, dss->x, dss->p);
427 if (0 != bignum_cmp(ytest, dss->y)) {
436 static void *dss_openssh_createkey(unsigned char **blob, int *len)
438 char **b = (char **) blob;
441 dss = smalloc(sizeof(struct dss_key));
445 dss->p = getmp(b, len);
446 dss->q = getmp(b, len);
447 dss->g = getmp(b, len);
448 dss->y = getmp(b, len);
449 dss->x = getmp(b, len);
451 if (!dss->p || !dss->q || !dss->g || !dss->y || !dss->x) {
464 static int dss_openssh_fmtkey(void *key, unsigned char *blob, int len)
466 struct dss_key *dss = (struct dss_key *) key;
470 ssh2_bignum_length(dss->p) +
471 ssh2_bignum_length(dss->q) +
472 ssh2_bignum_length(dss->g) +
473 ssh2_bignum_length(dss->y) +
474 ssh2_bignum_length(dss->x);
481 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
482 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
492 unsigned char *dss_sign(void *key, char *data, int datalen, int *siglen)
495 * The basic DSS signing algorithm is:
497 * - invent a random k between 1 and q-1 (exclusive).
498 * - Compute r = (g^k mod p) mod q.
499 * - Compute s = k^-1 * (hash + x*r) mod q.
501 * This has the dangerous properties that:
503 * - if an attacker in possession of the public key _and_ the
504 * signature (for example, the host you just authenticated
505 * to) can guess your k, he can reverse the computation of s
506 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
507 * can deduce the private half of your key, and masquerade
508 * as you for as long as the key is still valid.
510 * - since r is a function purely of k and the public key, if
511 * the attacker only has a _range of possibilities_ for k
512 * it's easy for him to work through them all and check each
513 * one against r; he'll never be unsure of whether he's got
516 * - if you ever sign two different hashes with the same k, it
517 * will be immediately obvious because the two signatures
518 * will have the same r, and moreover an attacker in
519 * possession of both signatures (and the public key of
520 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
521 * and from there deduce x as before.
523 * - the Bleichenbacher attack on DSA makes use of methods of
524 * generating k which are significantly non-uniformly
525 * distributed; in particular, generating a 160-bit random
526 * number and reducing it mod q is right out.
528 * For this reason we must be pretty careful about how we
529 * generate our k. Since this code runs on Windows, with no
530 * particularly good system entropy sources, we can't trust our
531 * RNG itself to produce properly unpredictable data. Hence, we
532 * use a totally different scheme instead.
534 * What we do is to take a SHA-512 (_big_) hash of the private
535 * key x, and then feed this into another SHA-512 hash that
536 * also includes the message hash being signed. That is:
538 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
540 * This number is 512 bits long, so reducing it mod q won't be
541 * noticeably non-uniform. So
545 * This has the interesting property that it's _deterministic_:
546 * signing the same hash twice with the same key yields the
549 * Despite this determinism, it's still not predictable to an
550 * attacker, because in order to repeat the SHA-512
551 * construction that created it, the attacker would have to
552 * know the private key value x - and by assumption he doesn't,
553 * because if he knew that he wouldn't be attacking k!
555 * (This trick doesn't, _per se_, protect against reuse of k.
556 * Reuse of k is left to chance; all it does is prevent
557 * _excessively high_ chances of reuse of k due to entropy
560 * Thanks to Colin Plumb for the general idea of using x to
561 * ensure k is hard to guess, and to the Cambridge University
562 * Computer Security Group for helping to argue out all the
565 struct dss_key *dss = (struct dss_key *) key;
567 unsigned char digest[20], digest512[64];
568 Bignum proto_k, k, gkp, hash, kinv, hxr, r, s;
569 unsigned char *bytes;
572 SHA_Simple(data, datalen, digest);
575 * Hash some identifying text plus x.
578 SHA512_Bytes(&ss, "DSA deterministic k generator", 30);
579 sha512_mpint(&ss, dss->x);
580 SHA512_Final(&ss, digest512);
583 * Now hash that digest plus the message hash.
586 SHA512_Bytes(&ss, digest512, sizeof(digest512));
587 SHA512_Bytes(&ss, digest, sizeof(digest));
588 SHA512_Final(&ss, digest512);
590 memset(&ss, 0, sizeof(ss));
593 * Now convert the result into a bignum, and reduce it mod q.
595 proto_k = bignum_from_bytes(digest512, 64);
596 k = bigmod(proto_k, dss->q);
599 memset(digest512, 0, sizeof(digest512));
602 * Now we have k, so just go ahead and compute the signature.
604 gkp = modpow(dss->g, k, dss->p); /* g^k mod p */
605 r = bigmod(gkp, dss->q); /* r = (g^k mod p) mod q */
608 hash = bignum_from_bytes(digest, 20);
609 kinv = modinv(k, dss->q); /* k^-1 mod q */
610 hxr = bigmuladd(dss->x, r, hash); /* hash + x*r */
611 s = modmul(kinv, hxr, dss->q); /* s = k^-1 * (hash + x*r) mod q */
620 * string two 20-byte numbers r and s, end to end
622 * i.e. 4+7 + 4+40 bytes.
624 nbytes = 4 + 7 + 4 + 40;
625 bytes = smalloc(nbytes);
627 memcpy(bytes + 4, "ssh-dss", 7);
628 PUT_32BIT(bytes + 4 + 7, 40);
629 for (i = 0; i < 20; i++) {
630 bytes[4 + 7 + 4 + i] = bignum_byte(r, 19 - i);
631 bytes[4 + 7 + 4 + 20 + i] = bignum_byte(s, 19 - i);
640 const struct ssh_signkey ssh_dss = {
647 dss_openssh_createkey,