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(char **data
, int *datalen
, char **p
, int *length
)
45 *length
= toint(GET_32BIT(*data
));
50 if (*datalen
< *length
)
56 static Bignum
getmp(char **data
, int *datalen
)
62 getstring(data
, datalen
, &p
, &length
);
66 return NULL
; /* negative mp */
67 b
= bignum_from_bytes((unsigned char *)p
, length
);
71 static Bignum
get160(char **data
, int *datalen
)
78 b
= bignum_from_bytes((unsigned char *)*data
, 20);
85 static void dss_freekey(void *key
); /* forward reference */
87 static void *dss_newkey(char *data
, int len
)
93 dss
= snew(struct dss_key
);
94 getstring(&data
, &len
, &p
, &slen
);
100 for (i
= 0; i
< len
; i
++)
101 printf(" %02x", (unsigned char) (data
[i
]));
106 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-dss", 7)) {
110 dss
->p
= getmp(&data
, &len
);
111 dss
->q
= getmp(&data
, &len
);
112 dss
->g
= getmp(&data
, &len
);
113 dss
->y
= getmp(&data
, &len
);
116 if (!dss
->p
|| !dss
->q
|| !dss
->g
|| !dss
->y
||
117 !bignum_cmp(dss
->q
, Zero
) || !bignum_cmp(dss
->p
, Zero
)) {
126 static void dss_freekey(void *key
)
128 struct dss_key
*dss
= (struct dss_key
*) key
;
142 static char *dss_fmtkey(void *key
)
144 struct dss_key
*dss
= (struct dss_key
*) key
;
146 int len
, i
, pos
, nibbles
;
147 static const char hex
[] = "0123456789abcdef";
150 len
= 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
151 len
+= 4 * (bignum_bitcount(dss
->p
) + 15) / 16;
152 len
+= 4 * (bignum_bitcount(dss
->q
) + 15) / 16;
153 len
+= 4 * (bignum_bitcount(dss
->g
) + 15) / 16;
154 len
+= 4 * (bignum_bitcount(dss
->y
) + 15) / 16;
155 p
= snewn(len
, char);
160 pos
+= sprintf(p
+ pos
, "0x");
161 nibbles
= (3 + bignum_bitcount(dss
->p
)) / 4;
164 for (i
= nibbles
; i
--;)
166 hex
[(bignum_byte(dss
->p
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
167 pos
+= sprintf(p
+ pos
, ",0x");
168 nibbles
= (3 + bignum_bitcount(dss
->q
)) / 4;
171 for (i
= nibbles
; i
--;)
173 hex
[(bignum_byte(dss
->q
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
174 pos
+= sprintf(p
+ pos
, ",0x");
175 nibbles
= (3 + bignum_bitcount(dss
->g
)) / 4;
178 for (i
= nibbles
; i
--;)
180 hex
[(bignum_byte(dss
->g
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
181 pos
+= sprintf(p
+ pos
, ",0x");
182 nibbles
= (3 + bignum_bitcount(dss
->y
)) / 4;
185 for (i
= nibbles
; i
--;)
187 hex
[(bignum_byte(dss
->y
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
192 static char *dss_fingerprint(void *key
)
194 struct dss_key
*dss
= (struct dss_key
*) key
;
195 struct MD5Context md5c
;
196 unsigned char digest
[16], lenbuf
[4];
197 char buffer
[16 * 3 + 40];
202 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-dss", 11);
204 #define ADD_BIGNUM(bignum) \
205 numlen = (bignum_bitcount(bignum)+8)/8; \
206 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
207 for (i = numlen; i-- ;) { \
208 unsigned char c = bignum_byte(bignum, i); \
209 MD5Update(&md5c, &c, 1); \
217 MD5Final(digest
, &md5c
);
219 sprintf(buffer
, "ssh-dss %d ", bignum_bitcount(dss
->p
));
220 for (i
= 0; i
< 16; i
++)
221 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
223 ret
= snewn(strlen(buffer
) + 1, char);
229 static int dss_verifysig(void *key
, char *sig
, int siglen
,
230 char *data
, int datalen
)
232 struct dss_key
*dss
= (struct dss_key
*) key
;
236 Bignum r
, s
, w
, gu1p
, yu2p
, gu1yu2p
, u1
, u2
, sha
, v
;
246 for (i
= 0; i
< siglen
; i
++)
247 printf(" %02x", (unsigned char) (sig
[i
]));
252 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
253 * of a DSA signature. OpenSSH is in line with RFC 4253:
254 * it uses a string "ssh-dss", followed by a 40-byte string
255 * containing two 160-bit integers end-to-end. Commercial SSH
256 * can't be bothered with the header bit, and considers a DSA
257 * signature blob to be _just_ the 40-byte string containing
258 * the two 160-bit integers. We tell them apart by measuring
259 * the length: length 40 means the commercial-SSH bug, anything
260 * else is assumed to be RFC-compliant.
262 if (siglen
!= 40) { /* bug not present; read admin fields */
263 getstring(&sig
, &siglen
, &p
, &slen
);
264 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-dss", 7)) {
267 sig
+= 4, siglen
-= 4; /* skip yet another length field */
269 r
= get160(&sig
, &siglen
);
270 s
= get160(&sig
, &siglen
);
279 if (!bignum_cmp(s
, Zero
)) {
286 * Step 1. w <- s^-1 mod q.
288 w
= modinv(s
, dss
->q
);
296 * Step 2. u1 <- SHA(message) * w mod q.
298 SHA_Simple(data
, datalen
, (unsigned char *)hash
);
301 sha
= get160(&p
, &slen
);
302 u1
= modmul(sha
, w
, dss
->q
);
305 * Step 3. u2 <- r * w mod q.
307 u2
= modmul(r
, w
, dss
->q
);
310 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
312 gu1p
= modpow(dss
->g
, u1
, dss
->p
);
313 yu2p
= modpow(dss
->y
, u2
, dss
->p
);
314 gu1yu2p
= modmul(gu1p
, yu2p
, dss
->p
);
315 v
= modmul(gu1yu2p
, One
, dss
->q
);
318 * Step 5. v should now be equal to r.
321 ret
= !bignum_cmp(v
, r
);
337 static unsigned char *dss_public_blob(void *key
, int *len
)
339 struct dss_key
*dss
= (struct dss_key
*) key
;
340 int plen
, qlen
, glen
, ylen
, bloblen
;
342 unsigned char *blob
, *p
;
344 plen
= (bignum_bitcount(dss
->p
) + 8) / 8;
345 qlen
= (bignum_bitcount(dss
->q
) + 8) / 8;
346 glen
= (bignum_bitcount(dss
->g
) + 8) / 8;
347 ylen
= (bignum_bitcount(dss
->y
) + 8) / 8;
350 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
351 * 27 + sum of lengths. (five length fields, 20+7=27).
353 bloblen
= 27 + plen
+ qlen
+ glen
+ ylen
;
354 blob
= snewn(bloblen
, unsigned char);
358 memcpy(p
, "ssh-dss", 7);
363 *p
++ = bignum_byte(dss
->p
, i
);
367 *p
++ = bignum_byte(dss
->q
, i
);
371 *p
++ = bignum_byte(dss
->g
, i
);
375 *p
++ = bignum_byte(dss
->y
, i
);
376 assert(p
== blob
+ bloblen
);
381 static unsigned char *dss_private_blob(void *key
, int *len
)
383 struct dss_key
*dss
= (struct dss_key
*) key
;
386 unsigned char *blob
, *p
;
388 xlen
= (bignum_bitcount(dss
->x
) + 8) / 8;
391 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
394 blob
= snewn(bloblen
, unsigned char);
399 *p
++ = bignum_byte(dss
->x
, i
);
400 assert(p
== blob
+ bloblen
);
405 static void *dss_createkey(unsigned char *pub_blob
, int pub_len
,
406 unsigned char *priv_blob
, int priv_len
)
409 char *pb
= (char *) priv_blob
;
413 unsigned char digest
[20];
416 dss
= dss_newkey((char *) pub_blob
, pub_len
);
419 dss
->x
= getmp(&pb
, &priv_len
);
426 * Check the obsolete hash in the old DSS key format.
429 getstring(&pb
, &priv_len
, &hash
, &hashlen
);
432 sha_mpint(&s
, dss
->p
);
433 sha_mpint(&s
, dss
->q
);
434 sha_mpint(&s
, dss
->g
);
435 SHA_Final(&s
, digest
);
436 if (0 != memcmp(hash
, digest
, 20)) {
443 * Now ensure g^x mod p really is y.
445 ytest
= modpow(dss
->g
, dss
->x
, dss
->p
);
446 if (0 != bignum_cmp(ytest
, dss
->y
)) {
456 static void *dss_openssh_createkey(unsigned char **blob
, int *len
)
458 char **b
= (char **) blob
;
461 dss
= snew(struct dss_key
);
463 dss
->p
= getmp(b
, len
);
464 dss
->q
= getmp(b
, len
);
465 dss
->g
= getmp(b
, len
);
466 dss
->y
= getmp(b
, len
);
467 dss
->x
= getmp(b
, len
);
469 if (!dss
->p
|| !dss
->q
|| !dss
->g
|| !dss
->y
|| !dss
->x
||
470 !bignum_cmp(dss
->q
, Zero
) || !bignum_cmp(dss
->p
, Zero
)) {
479 static int dss_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
481 struct dss_key
*dss
= (struct dss_key
*) key
;
485 ssh2_bignum_length(dss
->p
) +
486 ssh2_bignum_length(dss
->q
) +
487 ssh2_bignum_length(dss
->g
) +
488 ssh2_bignum_length(dss
->y
) +
489 ssh2_bignum_length(dss
->x
);
496 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
497 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
507 static int dss_pubkey_bits(void *blob
, int len
)
512 dss
= dss_newkey((char *) blob
, len
);
515 ret
= bignum_bitcount(dss
->p
);
521 static unsigned char *dss_sign(void *key
, char *data
, int datalen
, int *siglen
)
524 * The basic DSS signing algorithm is:
526 * - invent a random k between 1 and q-1 (exclusive).
527 * - Compute r = (g^k mod p) mod q.
528 * - Compute s = k^-1 * (hash + x*r) mod q.
530 * This has the dangerous properties that:
532 * - if an attacker in possession of the public key _and_ the
533 * signature (for example, the host you just authenticated
534 * to) can guess your k, he can reverse the computation of s
535 * and work out x = r^-1 * (s*k - hash) mod q. That is, he
536 * can deduce the private half of your key, and masquerade
537 * as you for as long as the key is still valid.
539 * - since r is a function purely of k and the public key, if
540 * the attacker only has a _range of possibilities_ for k
541 * it's easy for him to work through them all and check each
542 * one against r; he'll never be unsure of whether he's got
545 * - if you ever sign two different hashes with the same k, it
546 * will be immediately obvious because the two signatures
547 * will have the same r, and moreover an attacker in
548 * possession of both signatures (and the public key of
549 * course) can compute k = (hash1-hash2) * (s1-s2)^-1 mod q,
550 * and from there deduce x as before.
552 * - the Bleichenbacher attack on DSA makes use of methods of
553 * generating k which are significantly non-uniformly
554 * distributed; in particular, generating a 160-bit random
555 * number and reducing it mod q is right out.
557 * For this reason we must be pretty careful about how we
558 * generate our k. Since this code runs on Windows, with no
559 * particularly good system entropy sources, we can't trust our
560 * RNG itself to produce properly unpredictable data. Hence, we
561 * use a totally different scheme instead.
563 * What we do is to take a SHA-512 (_big_) hash of the private
564 * key x, and then feed this into another SHA-512 hash that
565 * also includes the message hash being signed. That is:
567 * proto_k = SHA512 ( SHA512(x) || SHA160(message) )
569 * This number is 512 bits long, so reducing it mod q won't be
570 * noticeably non-uniform. So
574 * This has the interesting property that it's _deterministic_:
575 * signing the same hash twice with the same key yields the
578 * Despite this determinism, it's still not predictable to an
579 * attacker, because in order to repeat the SHA-512
580 * construction that created it, the attacker would have to
581 * know the private key value x - and by assumption he doesn't,
582 * because if he knew that he wouldn't be attacking k!
584 * (This trick doesn't, _per se_, protect against reuse of k.
585 * Reuse of k is left to chance; all it does is prevent
586 * _excessively high_ chances of reuse of k due to entropy
589 * Thanks to Colin Plumb for the general idea of using x to
590 * ensure k is hard to guess, and to the Cambridge University
591 * Computer Security Group for helping to argue out all the
594 struct dss_key
*dss
= (struct dss_key
*) key
;
596 unsigned char digest
[20], digest512
[64];
597 Bignum proto_k
, k
, gkp
, hash
, kinv
, hxr
, r
, s
;
598 unsigned char *bytes
;
601 SHA_Simple(data
, datalen
, digest
);
604 * Hash some identifying text plus x.
607 SHA512_Bytes(&ss
, "DSA deterministic k generator", 30);
608 sha512_mpint(&ss
, dss
->x
);
609 SHA512_Final(&ss
, digest512
);
612 * Now hash that digest plus the message hash.
615 SHA512_Bytes(&ss
, digest512
, sizeof(digest512
));
616 SHA512_Bytes(&ss
, digest
, sizeof(digest
));
619 SHA512_State ss2
= ss
; /* structure copy */
620 SHA512_Final(&ss2
, digest512
);
622 smemclr(&ss2
, sizeof(ss2
));
625 * Now convert the result into a bignum, and reduce it mod q.
627 proto_k
= bignum_from_bytes(digest512
, 64);
628 k
= bigmod(proto_k
, dss
->q
);
630 kinv
= modinv(k
, dss
->q
); /* k^-1 mod q */
631 if (!kinv
) { /* very unlikely */
633 /* Perturb the hash to think of a different k. */
634 SHA512_Bytes(&ss
, "x", 1);
635 /* Go round and try again. */
642 smemclr(&ss
, sizeof(ss
));
644 smemclr(digest512
, sizeof(digest512
));
647 * Now we have k, so just go ahead and compute the signature.
649 gkp
= modpow(dss
->g
, k
, dss
->p
); /* g^k mod p */
650 r
= bigmod(gkp
, dss
->q
); /* r = (g^k mod p) mod q */
653 hash
= bignum_from_bytes(digest
, 20);
654 hxr
= bigmuladd(dss
->x
, r
, hash
); /* hash + x*r */
655 s
= modmul(kinv
, hxr
, dss
->q
); /* s = k^-1 * (hash + x*r) mod q */
665 * string two 20-byte numbers r and s, end to end
667 * i.e. 4+7 + 4+40 bytes.
669 nbytes
= 4 + 7 + 4 + 40;
670 bytes
= snewn(nbytes
, unsigned char);
672 memcpy(bytes
+ 4, "ssh-dss", 7);
673 PUT_32BIT(bytes
+ 4 + 7, 40);
674 for (i
= 0; i
< 20; i
++) {
675 bytes
[4 + 7 + 4 + i
] = bignum_byte(r
, 19 - i
);
676 bytes
[4 + 7 + 4 + 20 + i
] = bignum_byte(s
, 19 - i
);
685 const struct ssh_signkey ssh_dss
= {
692 dss_openssh_createkey
,