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 memset(lenbuf
, 0, 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 memset(lenbuf
, 0, sizeof(lenbuf
));
40 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
45 *length
= GET_32BIT(*data
);
48 if (*datalen
< *length
)
54 static Bignum
getmp(char **data
, int *datalen
)
60 getstring(data
, datalen
, &p
, &length
);
64 return NULL
; /* negative mp */
65 b
= bignum_from_bytes((unsigned char *)p
, length
);
69 static Bignum
get160(char **data
, int *datalen
)
73 b
= bignum_from_bytes((unsigned char *)*data
, 20);
80 static void *dss_newkey(char *data
, int len
)
86 dss
= snew(struct dss_key
);
89 getstring(&data
, &len
, &p
, &slen
);
95 for (i
= 0; i
< len
; i
++)
96 printf(" %02x", (unsigned char) (data
[i
]));
101 if (!p
|| memcmp(p
, "ssh-dss", 7)) {
105 dss
->p
= getmp(&data
, &len
);
106 dss
->q
= getmp(&data
, &len
);
107 dss
->g
= getmp(&data
, &len
);
108 dss
->y
= getmp(&data
, &len
);
113 static void dss_freekey(void *key
)
115 struct dss_key
*dss
= (struct dss_key
*) key
;
123 static char *dss_fmtkey(void *key
)
125 struct dss_key
*dss
= (struct dss_key
*) key
;
127 int len
, i
, pos
, nibbles
;
128 static const char hex
[] = "0123456789abcdef";
131 len
= 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
132 len
+= 4 * (bignum_bitcount(dss
->p
) + 15) / 16;
133 len
+= 4 * (bignum_bitcount(dss
->q
) + 15) / 16;
134 len
+= 4 * (bignum_bitcount(dss
->g
) + 15) / 16;
135 len
+= 4 * (bignum_bitcount(dss
->y
) + 15) / 16;
136 p
= snewn(len
, char);
141 pos
+= sprintf(p
+ pos
, "0x");
142 nibbles
= (3 + bignum_bitcount(dss
->p
)) / 4;
145 for (i
= nibbles
; i
--;)
147 hex
[(bignum_byte(dss
->p
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
148 pos
+= sprintf(p
+ pos
, ",0x");
149 nibbles
= (3 + bignum_bitcount(dss
->q
)) / 4;
152 for (i
= nibbles
; i
--;)
154 hex
[(bignum_byte(dss
->q
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
155 pos
+= sprintf(p
+ pos
, ",0x");
156 nibbles
= (3 + bignum_bitcount(dss
->g
)) / 4;
159 for (i
= nibbles
; i
--;)
161 hex
[(bignum_byte(dss
->g
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
162 pos
+= sprintf(p
+ pos
, ",0x");
163 nibbles
= (3 + bignum_bitcount(dss
->y
)) / 4;
166 for (i
= nibbles
; i
--;)
168 hex
[(bignum_byte(dss
->y
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
173 static char *dss_fingerprint(void *key
)
175 struct dss_key
*dss
= (struct dss_key
*) key
;
176 struct MD5Context md5c
;
177 unsigned char digest
[16], lenbuf
[4];
178 char buffer
[16 * 3 + 40];
183 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-dss", 11);
185 #define ADD_BIGNUM(bignum) \
186 numlen = (bignum_bitcount(bignum)+8)/8; \
187 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
188 for (i = numlen; i-- ;) { \
189 unsigned char c = bignum_byte(bignum, i); \
190 MD5Update(&md5c, &c, 1); \
198 MD5Final(digest
, &md5c
);
200 sprintf(buffer
, "ssh-dss %d ", bignum_bitcount(dss
->p
));
201 for (i
= 0; i
< 16; i
++)
202 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
204 ret
= snewn(strlen(buffer
) + 1, char);
210 static int dss_verifysig(void *key
, char *sig
, int siglen
,
211 char *data
, int datalen
)
213 struct dss_key
*dss
= (struct dss_key
*) key
;
217 Bignum r
, s
, w
, gu1p
, yu2p
, gu1yu2p
, u1
, u2
, sha
, v
;
227 for (i
= 0; i
< siglen
; i
++)
228 printf(" %02x", (unsigned char) (sig
[i
]));
233 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
234 * of a DSA signature. OpenSSH is in line with the IETF drafts:
235 * it uses a string "ssh-dss", followed by a 40-byte string
236 * containing two 160-bit integers end-to-end. Commercial SSH
237 * can't be bothered with the header bit, and considers a DSA
238 * signature blob to be _just_ the 40-byte string containing
239 * the two 160-bit integers. We tell them apart by measuring
240 * the length: length 40 means the commercial-SSH bug, anything
241 * else is assumed to be IETF-compliant.
243 if (siglen
!= 40) { /* bug not present; read admin fields */
244 getstring(&sig
, &siglen
, &p
, &slen
);
245 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-dss", 7)) {
248 sig
+= 4, siglen
-= 4; /* skip yet another length field */
250 r
= get160(&sig
, &siglen
);
251 s
= get160(&sig
, &siglen
);
256 * Step 1. w <- s^-1 mod q.
258 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
);
300 static unsigned char *dss_public_blob(void *key
, int *len
)
302 struct dss_key
*dss
= (struct dss_key
*) key
;
303 int plen
, qlen
, glen
, ylen
, bloblen
;
305 unsigned char *blob
, *p
;
307 plen
= (bignum_bitcount(dss
->p
) + 8) / 8;
308 qlen
= (bignum_bitcount(dss
->q
) + 8) / 8;
309 glen
= (bignum_bitcount(dss
->g
) + 8) / 8;
310 ylen
= (bignum_bitcount(dss
->y
) + 8) / 8;
313 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
314 * 27 + sum of lengths. (five length fields, 20+7=27).
316 bloblen
= 27 + plen
+ qlen
+ glen
+ ylen
;
317 blob
= snewn(bloblen
, unsigned char);
321 memcpy(p
, "ssh-dss", 7);
326 *p
++ = bignum_byte(dss
->p
, i
);
330 *p
++ = bignum_byte(dss
->q
, i
);
334 *p
++ = bignum_byte(dss
->g
, i
);
338 *p
++ = bignum_byte(dss
->y
, i
);
339 assert(p
== blob
+ bloblen
);
344 static unsigned char *dss_private_blob(void *key
, int *len
)
346 struct dss_key
*dss
= (struct dss_key
*) key
;
349 unsigned char *blob
, *p
;
351 xlen
= (bignum_bitcount(dss
->x
) + 8) / 8;
354 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
357 blob
= snewn(bloblen
, unsigned char);
362 *p
++ = bignum_byte(dss
->x
, i
);
363 assert(p
== blob
+ bloblen
);
368 static void *dss_createkey(unsigned char *pub_blob
, int pub_len
,
369 unsigned char *priv_blob
, int priv_len
)
372 char *pb
= (char *) priv_blob
;
376 unsigned char digest
[20];
379 dss
= dss_newkey((char *) pub_blob
, pub_len
);
380 dss
->x
= getmp(&pb
, &priv_len
);
383 * Check the obsolete hash in the old DSS key format.
386 getstring(&pb
, &priv_len
, &hash
, &hashlen
);
389 sha_mpint(&s
, dss
->p
);
390 sha_mpint(&s
, dss
->q
);
391 sha_mpint(&s
, dss
->g
);
392 SHA_Final(&s
, digest
);
393 if (0 != memcmp(hash
, digest
, 20)) {
400 * Now ensure g^x mod p really is y.
402 ytest
= modpow(dss
->g
, dss
->x
, dss
->p
);
403 if (0 != bignum_cmp(ytest
, dss
->y
)) {
412 static void *dss_openssh_createkey(unsigned char **blob
, int *len
)
414 char **b
= (char **) blob
;
417 dss
= snew(struct dss_key
);
421 dss
->p
= getmp(b
, len
);
422 dss
->q
= getmp(b
, len
);
423 dss
->g
= getmp(b
, len
);
424 dss
->y
= getmp(b
, len
);
425 dss
->x
= getmp(b
, len
);
427 if (!dss
->p
|| !dss
->q
|| !dss
->g
|| !dss
->y
|| !dss
->x
) {
440 static int dss_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
442 struct dss_key
*dss
= (struct dss_key
*) key
;
446 ssh2_bignum_length(dss
->p
) +
447 ssh2_bignum_length(dss
->q
) +
448 ssh2_bignum_length(dss
->g
) +
449 ssh2_bignum_length(dss
->y
) +
450 ssh2_bignum_length(dss
->x
);
457 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
458 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
468 static int dss_pubkey_bits(void *blob
, int len
)
473 dss
= dss_newkey((char *) blob
, len
);
474 ret
= bignum_bitcount(dss
->p
);
480 static unsigned char *dss_sign(void *key
, char *data
, int datalen
, int *siglen
)
483 * The basic DSS signing algorithm is:
485 * - invent a random k between 1 and q-1 (exclusive).
486 * - Compute r = (g^k mod p) mod q.
487 * - Compute s = k^-1 * (hash + x*r) mod q.
489 * This has the dangerous properties that:
491 * - if an attacker in possession of the public key _and_ the
492 * signature (for example, the host you just authenticated
493 * to) can guess your k, he can reverse the computation of s
494 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
495 * can deduce the private half of your key, and masquerade
496 * as you for as long as the key is still valid.
498 * - since r is a function purely of k and the public key, if
499 * the attacker only has a _range of possibilities_ for k
500 * it's easy for him to work through them all and check each
501 * one against r; he'll never be unsure of whether he's got
504 * - if you ever sign two different hashes with the same k, it
505 * will be immediately obvious because the two signatures
506 * will have the same r, and moreover an attacker in
507 * possession of both signatures (and the public key of
508 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
509 * and from there deduce x as before.
511 * - the Bleichenbacher attack on DSA makes use of methods of
512 * generating k which are significantly non-uniformly
513 * distributed; in particular, generating a 160-bit random
514 * number and reducing it mod q is right out.
516 * For this reason we must be pretty careful about how we
517 * generate our k. Since this code runs on Windows, with no
518 * particularly good system entropy sources, we can't trust our
519 * RNG itself to produce properly unpredictable data. Hence, we
520 * use a totally different scheme instead.
522 * What we do is to take a SHA-512 (_big_) hash of the private
523 * key x, and then feed this into another SHA-512 hash that
524 * also includes the message hash being signed. That is:
526 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
528 * This number is 512 bits long, so reducing it mod q won't be
529 * noticeably non-uniform. So
533 * This has the interesting property that it's _deterministic_:
534 * signing the same hash twice with the same key yields the
537 * Despite this determinism, it's still not predictable to an
538 * attacker, because in order to repeat the SHA-512
539 * construction that created it, the attacker would have to
540 * know the private key value x - and by assumption he doesn't,
541 * because if he knew that he wouldn't be attacking k!
543 * (This trick doesn't, _per se_, protect against reuse of k.
544 * Reuse of k is left to chance; all it does is prevent
545 * _excessively high_ chances of reuse of k due to entropy
548 * Thanks to Colin Plumb for the general idea of using x to
549 * ensure k is hard to guess, and to the Cambridge University
550 * Computer Security Group for helping to argue out all the
553 struct dss_key
*dss
= (struct dss_key
*) key
;
555 unsigned char digest
[20], digest512
[64];
556 Bignum proto_k
, k
, gkp
, hash
, kinv
, hxr
, r
, s
;
557 unsigned char *bytes
;
560 SHA_Simple(data
, datalen
, digest
);
563 * Hash some identifying text plus x.
566 SHA512_Bytes(&ss
, "DSA deterministic k generator", 30);
567 sha512_mpint(&ss
, dss
->x
);
568 SHA512_Final(&ss
, digest512
);
571 * Now hash that digest plus the message hash.
574 SHA512_Bytes(&ss
, digest512
, sizeof(digest512
));
575 SHA512_Bytes(&ss
, digest
, sizeof(digest
));
576 SHA512_Final(&ss
, digest512
);
578 memset(&ss
, 0, sizeof(ss
));
581 * Now convert the result into a bignum, and reduce it mod q.
583 proto_k
= bignum_from_bytes(digest512
, 64);
584 k
= bigmod(proto_k
, dss
->q
);
587 memset(digest512
, 0, sizeof(digest512
));
590 * Now we have k, so just go ahead and compute the signature.
592 gkp
= modpow(dss
->g
, k
, dss
->p
); /* g^k mod p */
593 r
= bigmod(gkp
, dss
->q
); /* r = (g^k mod p) mod q */
596 hash
= bignum_from_bytes(digest
, 20);
597 kinv
= modinv(k
, dss
->q
); /* k^-1 mod q */
598 hxr
= bigmuladd(dss
->x
, r
, hash
); /* hash + x*r */
599 s
= modmul(kinv
, hxr
, dss
->q
); /* s = k^-1 * (hash + x*r) mod q */
608 * string two 20-byte numbers r and s, end to end
610 * i.e. 4+7 + 4+40 bytes.
612 nbytes
= 4 + 7 + 4 + 40;
613 bytes
= snewn(nbytes
, unsigned char);
615 memcpy(bytes
+ 4, "ssh-dss", 7);
616 PUT_32BIT(bytes
+ 4 + 7, 40);
617 for (i
= 0; i
< 20; i
++) {
618 bytes
[4 + 7 + 4 + i
] = bignum_byte(r
, 19 - i
);
619 bytes
[4 + 7 + 4 + 20 + i
] = bignum_byte(s
, 19 - i
);
628 const struct ssh_signkey ssh_dss
= {
635 dss_openssh_createkey
,