8 static void sha_mpint(SHA_State
* s
, Bignum b
)
10 unsigned char lenbuf
[4];
12 len
= (bignum_bitcount(b
) + 8) / 8;
13 PUT_32BIT(lenbuf
, len
);
14 SHA_Bytes(s
, lenbuf
, 4);
16 lenbuf
[0] = bignum_byte(b
, len
);
17 SHA_Bytes(s
, lenbuf
, 1);
19 memset(lenbuf
, 0, sizeof(lenbuf
));
22 static void sha512_mpint(SHA512_State
* s
, Bignum b
)
24 unsigned char lenbuf
[4];
26 len
= (bignum_bitcount(b
) + 8) / 8;
27 PUT_32BIT(lenbuf
, len
);
28 SHA512_Bytes(s
, lenbuf
, 4);
30 lenbuf
[0] = bignum_byte(b
, len
);
31 SHA512_Bytes(s
, lenbuf
, 1);
33 memset(lenbuf
, 0, sizeof(lenbuf
));
36 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
41 *length
= GET_32BIT(*data
);
44 if (*datalen
< *length
)
50 static Bignum
getmp(char **data
, int *datalen
)
56 getstring(data
, datalen
, &p
, &length
);
60 return NULL
; /* negative mp */
61 b
= bignum_from_bytes((unsigned char *)p
, length
);
65 static Bignum
get160(char **data
, int *datalen
)
69 b
= bignum_from_bytes((unsigned char *)*data
, 20);
76 static void *dss_newkey(char *data
, int len
)
82 dss
= snew(struct dss_key
);
85 getstring(&data
, &len
, &p
, &slen
);
91 for (i
= 0; i
< len
; i
++)
92 printf(" %02x", (unsigned char) (data
[i
]));
97 if (!p
|| memcmp(p
, "ssh-dss", 7)) {
101 dss
->p
= getmp(&data
, &len
);
102 dss
->q
= getmp(&data
, &len
);
103 dss
->g
= getmp(&data
, &len
);
104 dss
->y
= getmp(&data
, &len
);
109 static void dss_freekey(void *key
)
111 struct dss_key
*dss
= (struct dss_key
*) key
;
119 static char *dss_fmtkey(void *key
)
121 struct dss_key
*dss
= (struct dss_key
*) key
;
123 int len
, i
, pos
, nibbles
;
124 static const char hex
[] = "0123456789abcdef";
127 len
= 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
128 len
+= 4 * (bignum_bitcount(dss
->p
) + 15) / 16;
129 len
+= 4 * (bignum_bitcount(dss
->q
) + 15) / 16;
130 len
+= 4 * (bignum_bitcount(dss
->g
) + 15) / 16;
131 len
+= 4 * (bignum_bitcount(dss
->y
) + 15) / 16;
132 p
= snewn(len
, char);
137 pos
+= sprintf(p
+ pos
, "0x");
138 nibbles
= (3 + bignum_bitcount(dss
->p
)) / 4;
141 for (i
= nibbles
; i
--;)
143 hex
[(bignum_byte(dss
->p
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
144 pos
+= sprintf(p
+ pos
, ",0x");
145 nibbles
= (3 + bignum_bitcount(dss
->q
)) / 4;
148 for (i
= nibbles
; i
--;)
150 hex
[(bignum_byte(dss
->q
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
151 pos
+= sprintf(p
+ pos
, ",0x");
152 nibbles
= (3 + bignum_bitcount(dss
->g
)) / 4;
155 for (i
= nibbles
; i
--;)
157 hex
[(bignum_byte(dss
->g
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
158 pos
+= sprintf(p
+ pos
, ",0x");
159 nibbles
= (3 + bignum_bitcount(dss
->y
)) / 4;
162 for (i
= nibbles
; i
--;)
164 hex
[(bignum_byte(dss
->y
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
169 static char *dss_fingerprint(void *key
)
171 struct dss_key
*dss
= (struct dss_key
*) key
;
172 struct MD5Context md5c
;
173 unsigned char digest
[16], lenbuf
[4];
174 char buffer
[16 * 3 + 40];
179 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-dss", 11);
181 #define ADD_BIGNUM(bignum) \
182 numlen = (bignum_bitcount(bignum)+8)/8; \
183 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
184 for (i = numlen; i-- ;) { \
185 unsigned char c = bignum_byte(bignum, i); \
186 MD5Update(&md5c, &c, 1); \
194 MD5Final(digest
, &md5c
);
196 sprintf(buffer
, "ssh-dss %d ", bignum_bitcount(dss
->p
));
197 for (i
= 0; i
< 16; i
++)
198 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
200 ret
= snewn(strlen(buffer
) + 1, char);
206 static int dss_verifysig(void *key
, char *sig
, int siglen
,
207 char *data
, int datalen
)
209 struct dss_key
*dss
= (struct dss_key
*) key
;
213 Bignum r
, s
, w
, gu1p
, yu2p
, gu1yu2p
, u1
, u2
, sha
, v
;
223 for (i
= 0; i
< siglen
; i
++)
224 printf(" %02x", (unsigned char) (sig
[i
]));
229 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
230 * of a DSA signature. OpenSSH is in line with the IETF drafts:
231 * it uses a string "ssh-dss", followed by a 40-byte string
232 * containing two 160-bit integers end-to-end. Commercial SSH
233 * can't be bothered with the header bit, and considers a DSA
234 * signature blob to be _just_ the 40-byte string containing
235 * the two 160-bit integers. We tell them apart by measuring
236 * the length: length 40 means the commercial-SSH bug, anything
237 * else is assumed to be IETF-compliant.
239 if (siglen
!= 40) { /* bug not present; read admin fields */
240 getstring(&sig
, &siglen
, &p
, &slen
);
241 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-dss", 7)) {
244 sig
+= 4, siglen
-= 4; /* skip yet another length field */
246 r
= get160(&sig
, &siglen
);
247 s
= get160(&sig
, &siglen
);
252 * Step 1. w <- s^-1 mod q.
254 w
= modinv(s
, dss
->q
);
257 * Step 2. u1 <- SHA(message) * w mod q.
259 SHA_Simple(data
, datalen
, (unsigned char *)hash
);
262 sha
= get160(&p
, &slen
);
263 u1
= modmul(sha
, w
, dss
->q
);
266 * Step 3. u2 <- r * w mod q.
268 u2
= modmul(r
, w
, dss
->q
);
271 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
273 gu1p
= modpow(dss
->g
, u1
, dss
->p
);
274 yu2p
= modpow(dss
->y
, u2
, dss
->p
);
275 gu1yu2p
= modmul(gu1p
, yu2p
, dss
->p
);
276 v
= modmul(gu1yu2p
, One
, dss
->q
);
279 * Step 5. v should now be equal to r.
282 ret
= !bignum_cmp(v
, r
);
296 static unsigned char *dss_public_blob(void *key
, int *len
)
298 struct dss_key
*dss
= (struct dss_key
*) key
;
299 int plen
, qlen
, glen
, ylen
, bloblen
;
301 unsigned char *blob
, *p
;
303 plen
= (bignum_bitcount(dss
->p
) + 8) / 8;
304 qlen
= (bignum_bitcount(dss
->q
) + 8) / 8;
305 glen
= (bignum_bitcount(dss
->g
) + 8) / 8;
306 ylen
= (bignum_bitcount(dss
->y
) + 8) / 8;
309 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
310 * 27 + sum of lengths. (five length fields, 20+7=27).
312 bloblen
= 27 + plen
+ qlen
+ glen
+ ylen
;
313 blob
= snewn(bloblen
, unsigned char);
317 memcpy(p
, "ssh-dss", 7);
322 *p
++ = bignum_byte(dss
->p
, i
);
326 *p
++ = bignum_byte(dss
->q
, i
);
330 *p
++ = bignum_byte(dss
->g
, i
);
334 *p
++ = bignum_byte(dss
->y
, i
);
335 assert(p
== blob
+ bloblen
);
340 static unsigned char *dss_private_blob(void *key
, int *len
)
342 struct dss_key
*dss
= (struct dss_key
*) key
;
345 unsigned char *blob
, *p
;
347 xlen
= (bignum_bitcount(dss
->x
) + 8) / 8;
350 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
353 blob
= snewn(bloblen
, unsigned char);
358 *p
++ = bignum_byte(dss
->x
, i
);
359 assert(p
== blob
+ bloblen
);
364 static void *dss_createkey(unsigned char *pub_blob
, int pub_len
,
365 unsigned char *priv_blob
, int priv_len
)
368 char *pb
= (char *) priv_blob
;
372 unsigned char digest
[20];
375 dss
= dss_newkey((char *) pub_blob
, pub_len
);
376 dss
->x
= getmp(&pb
, &priv_len
);
379 * Check the obsolete hash in the old DSS key format.
382 getstring(&pb
, &priv_len
, &hash
, &hashlen
);
385 sha_mpint(&s
, dss
->p
);
386 sha_mpint(&s
, dss
->q
);
387 sha_mpint(&s
, dss
->g
);
388 SHA_Final(&s
, digest
);
389 if (0 != memcmp(hash
, digest
, 20)) {
396 * Now ensure g^x mod p really is y.
398 ytest
= modpow(dss
->g
, dss
->x
, dss
->p
);
399 if (0 != bignum_cmp(ytest
, dss
->y
)) {
408 static void *dss_openssh_createkey(unsigned char **blob
, int *len
)
410 char **b
= (char **) blob
;
413 dss
= snew(struct dss_key
);
417 dss
->p
= getmp(b
, len
);
418 dss
->q
= getmp(b
, len
);
419 dss
->g
= getmp(b
, len
);
420 dss
->y
= getmp(b
, len
);
421 dss
->x
= getmp(b
, len
);
423 if (!dss
->p
|| !dss
->q
|| !dss
->g
|| !dss
->y
|| !dss
->x
) {
436 static int dss_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
438 struct dss_key
*dss
= (struct dss_key
*) key
;
442 ssh2_bignum_length(dss
->p
) +
443 ssh2_bignum_length(dss
->q
) +
444 ssh2_bignum_length(dss
->g
) +
445 ssh2_bignum_length(dss
->y
) +
446 ssh2_bignum_length(dss
->x
);
453 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
454 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
464 static int dss_pubkey_bits(void *blob
, int len
)
469 dss
= dss_newkey((char *) blob
, len
);
470 ret
= bignum_bitcount(dss
->p
);
476 static unsigned char *dss_sign(void *key
, char *data
, int datalen
, int *siglen
)
479 * The basic DSS signing algorithm is:
481 * - invent a random k between 1 and q-1 (exclusive).
482 * - Compute r = (g^k mod p) mod q.
483 * - Compute s = k^-1 * (hash + x*r) mod q.
485 * This has the dangerous properties that:
487 * - if an attacker in possession of the public key _and_ the
488 * signature (for example, the host you just authenticated
489 * to) can guess your k, he can reverse the computation of s
490 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
491 * can deduce the private half of your key, and masquerade
492 * as you for as long as the key is still valid.
494 * - since r is a function purely of k and the public key, if
495 * the attacker only has a _range of possibilities_ for k
496 * it's easy for him to work through them all and check each
497 * one against r; he'll never be unsure of whether he's got
500 * - if you ever sign two different hashes with the same k, it
501 * will be immediately obvious because the two signatures
502 * will have the same r, and moreover an attacker in
503 * possession of both signatures (and the public key of
504 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
505 * and from there deduce x as before.
507 * - the Bleichenbacher attack on DSA makes use of methods of
508 * generating k which are significantly non-uniformly
509 * distributed; in particular, generating a 160-bit random
510 * number and reducing it mod q is right out.
512 * For this reason we must be pretty careful about how we
513 * generate our k. Since this code runs on Windows, with no
514 * particularly good system entropy sources, we can't trust our
515 * RNG itself to produce properly unpredictable data. Hence, we
516 * use a totally different scheme instead.
518 * What we do is to take a SHA-512 (_big_) hash of the private
519 * key x, and then feed this into another SHA-512 hash that
520 * also includes the message hash being signed. That is:
522 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
524 * This number is 512 bits long, so reducing it mod q won't be
525 * noticeably non-uniform. So
529 * This has the interesting property that it's _deterministic_:
530 * signing the same hash twice with the same key yields the
533 * Despite this determinism, it's still not predictable to an
534 * attacker, because in order to repeat the SHA-512
535 * construction that created it, the attacker would have to
536 * know the private key value x - and by assumption he doesn't,
537 * because if he knew that he wouldn't be attacking k!
539 * (This trick doesn't, _per se_, protect against reuse of k.
540 * Reuse of k is left to chance; all it does is prevent
541 * _excessively high_ chances of reuse of k due to entropy
544 * Thanks to Colin Plumb for the general idea of using x to
545 * ensure k is hard to guess, and to the Cambridge University
546 * Computer Security Group for helping to argue out all the
549 struct dss_key
*dss
= (struct dss_key
*) key
;
551 unsigned char digest
[20], digest512
[64];
552 Bignum proto_k
, k
, gkp
, hash
, kinv
, hxr
, r
, s
;
553 unsigned char *bytes
;
556 SHA_Simple(data
, datalen
, digest
);
559 * Hash some identifying text plus x.
562 SHA512_Bytes(&ss
, "DSA deterministic k generator", 30);
563 sha512_mpint(&ss
, dss
->x
);
564 SHA512_Final(&ss
, digest512
);
567 * Now hash that digest plus the message hash.
570 SHA512_Bytes(&ss
, digest512
, sizeof(digest512
));
571 SHA512_Bytes(&ss
, digest
, sizeof(digest
));
572 SHA512_Final(&ss
, digest512
);
574 memset(&ss
, 0, sizeof(ss
));
577 * Now convert the result into a bignum, and reduce it mod q.
579 proto_k
= bignum_from_bytes(digest512
, 64);
580 k
= bigmod(proto_k
, dss
->q
);
583 memset(digest512
, 0, sizeof(digest512
));
586 * Now we have k, so just go ahead and compute the signature.
588 gkp
= modpow(dss
->g
, k
, dss
->p
); /* g^k mod p */
589 r
= bigmod(gkp
, dss
->q
); /* r = (g^k mod p) mod q */
592 hash
= bignum_from_bytes(digest
, 20);
593 kinv
= modinv(k
, dss
->q
); /* k^-1 mod q */
594 hxr
= bigmuladd(dss
->x
, r
, hash
); /* hash + x*r */
595 s
= modmul(kinv
, hxr
, dss
->q
); /* s = k^-1 * (hash + x*r) mod q */
604 * string two 20-byte numbers r and s, end to end
606 * i.e. 4+7 + 4+40 bytes.
608 nbytes
= 4 + 7 + 4 + 40;
609 bytes
= snewn(nbytes
, unsigned char);
611 memcpy(bytes
+ 4, "ssh-dss", 7);
612 PUT_32BIT(bytes
+ 4 + 7, 40);
613 for (i
= 0; i
< 20; i
++) {
614 bytes
[4 + 7 + 4 + i
] = bignum_byte(r
, 19 - i
);
615 bytes
[4 + 7 + 4 + 20 + i
] = bignum_byte(s
, 19 - i
);
624 const struct ssh_signkey ssh_dss
= {
631 dss_openssh_createkey
,