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
)
22 unsigned char lenbuf
[4];
24 len
= (bignum_bitcount(b
) + 8) / 8;
25 PUT_32BIT(lenbuf
, len
);
26 SHA_Bytes(s
, lenbuf
, 4);
28 lenbuf
[0] = bignum_byte(b
, len
);
29 SHA_Bytes(s
, lenbuf
, 1);
31 memset(lenbuf
, 0, sizeof(lenbuf
));
34 static void sha512_mpint(SHA512_State
* s
, Bignum b
)
36 unsigned char lenbuf
[4];
38 len
= (bignum_bitcount(b
) + 8) / 8;
39 PUT_32BIT(lenbuf
, len
);
40 SHA512_Bytes(s
, lenbuf
, 4);
42 lenbuf
[0] = bignum_byte(b
, len
);
43 SHA512_Bytes(s
, lenbuf
, 1);
45 memset(lenbuf
, 0, sizeof(lenbuf
));
48 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
53 *length
= GET_32BIT(*data
);
56 if (*datalen
< *length
)
62 static Bignum
getmp(char **data
, int *datalen
)
68 getstring(data
, datalen
, &p
, &length
);
72 return NULL
; /* negative mp */
73 b
= bignum_from_bytes((unsigned char *)p
, length
);
77 static Bignum
get160(char **data
, int *datalen
)
81 b
= bignum_from_bytes((unsigned char *)*data
, 20);
88 static void *dss_newkey(char *data
, int len
)
94 dss
= smalloc(sizeof(struct dss_key
));
97 getstring(&data
, &len
, &p
, &slen
);
103 for (i
= 0; i
< len
; i
++)
104 printf(" %02x", (unsigned char) (data
[i
]));
109 if (!p
|| memcmp(p
, "ssh-dss", 7)) {
113 dss
->p
= getmp(&data
, &len
);
114 dss
->q
= getmp(&data
, &len
);
115 dss
->g
= getmp(&data
, &len
);
116 dss
->y
= getmp(&data
, &len
);
121 static void dss_freekey(void *key
)
123 struct dss_key
*dss
= (struct dss_key
*) key
;
131 static char *dss_fmtkey(void *key
)
133 struct dss_key
*dss
= (struct dss_key
*) key
;
135 int len
, i
, pos
, nibbles
;
136 static const char hex
[] = "0123456789abcdef";
139 len
= 8 + 4 + 1; /* 4 x "0x", punctuation, \0 */
140 len
+= 4 * (bignum_bitcount(dss
->p
) + 15) / 16;
141 len
+= 4 * (bignum_bitcount(dss
->q
) + 15) / 16;
142 len
+= 4 * (bignum_bitcount(dss
->g
) + 15) / 16;
143 len
+= 4 * (bignum_bitcount(dss
->y
) + 15) / 16;
149 pos
+= sprintf(p
+ pos
, "0x");
150 nibbles
= (3 + bignum_bitcount(dss
->p
)) / 4;
153 for (i
= nibbles
; i
--;)
155 hex
[(bignum_byte(dss
->p
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
156 pos
+= sprintf(p
+ pos
, ",0x");
157 nibbles
= (3 + bignum_bitcount(dss
->q
)) / 4;
160 for (i
= nibbles
; i
--;)
162 hex
[(bignum_byte(dss
->q
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
163 pos
+= sprintf(p
+ pos
, ",0x");
164 nibbles
= (3 + bignum_bitcount(dss
->g
)) / 4;
167 for (i
= nibbles
; i
--;)
169 hex
[(bignum_byte(dss
->g
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
170 pos
+= sprintf(p
+ pos
, ",0x");
171 nibbles
= (3 + bignum_bitcount(dss
->y
)) / 4;
174 for (i
= nibbles
; i
--;)
176 hex
[(bignum_byte(dss
->y
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
181 static char *dss_fingerprint(void *key
)
183 struct dss_key
*dss
= (struct dss_key
*) key
;
184 struct MD5Context md5c
;
185 unsigned char digest
[16], lenbuf
[4];
186 char buffer
[16 * 3 + 40];
191 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-dss", 11);
193 #define ADD_BIGNUM(bignum) \
194 numlen = (bignum_bitcount(bignum)+8)/8; \
195 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
196 for (i = numlen; i-- ;) { \
197 unsigned char c = bignum_byte(bignum, i); \
198 MD5Update(&md5c, &c, 1); \
206 MD5Final(digest
, &md5c
);
208 sprintf(buffer
, "ssh-dss %d ", bignum_bitcount(dss
->p
));
209 for (i
= 0; i
< 16; i
++)
210 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
212 ret
= smalloc(strlen(buffer
) + 1);
218 static int dss_verifysig(void *key
, char *sig
, int siglen
,
219 char *data
, int datalen
)
221 struct dss_key
*dss
= (struct dss_key
*) key
;
225 Bignum r
, s
, w
, gu1p
, yu2p
, gu1yu2p
, u1
, u2
, sha
, v
;
235 for (i
= 0; i
< siglen
; i
++)
236 printf(" %02x", (unsigned char) (sig
[i
]));
241 * Commercial SSH (2.0.13) and OpenSSH disagree over the format
242 * of a DSA signature. OpenSSH is in line with the IETF drafts:
243 * it uses a string "ssh-dss", followed by a 40-byte string
244 * containing two 160-bit integers end-to-end. Commercial SSH
245 * can't be bothered with the header bit, and considers a DSA
246 * signature blob to be _just_ the 40-byte string containing
247 * the two 160-bit integers. We tell them apart by measuring
248 * the length: length 40 means the commercial-SSH bug, anything
249 * else is assumed to be IETF-compliant.
251 if (siglen
!= 40) { /* bug not present; read admin fields */
252 getstring(&sig
, &siglen
, &p
, &slen
);
253 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-dss", 7)) {
256 sig
+= 4, siglen
-= 4; /* skip yet another length field */
258 r
= get160(&sig
, &siglen
);
259 s
= get160(&sig
, &siglen
);
264 * Step 1. w <- s^-1 mod q.
266 w
= modinv(s
, dss
->q
);
269 * Step 2. u1 <- SHA(message) * w mod q.
271 SHA_Simple(data
, datalen
, (unsigned char *)hash
);
274 sha
= get160(&p
, &slen
);
275 u1
= modmul(sha
, w
, dss
->q
);
278 * Step 3. u2 <- r * w mod q.
280 u2
= modmul(r
, w
, dss
->q
);
283 * Step 4. v <- (g^u1 * y^u2 mod p) mod q.
285 gu1p
= modpow(dss
->g
, u1
, dss
->p
);
286 yu2p
= modpow(dss
->y
, u2
, dss
->p
);
287 gu1yu2p
= modmul(gu1p
, yu2p
, dss
->p
);
288 v
= modmul(gu1yu2p
, One
, dss
->q
);
291 * Step 5. v should now be equal to r.
294 ret
= !bignum_cmp(v
, r
);
308 static unsigned char *dss_public_blob(void *key
, int *len
)
310 struct dss_key
*dss
= (struct dss_key
*) key
;
311 int plen
, qlen
, glen
, ylen
, bloblen
;
313 unsigned char *blob
, *p
;
315 plen
= (bignum_bitcount(dss
->p
) + 8) / 8;
316 qlen
= (bignum_bitcount(dss
->q
) + 8) / 8;
317 glen
= (bignum_bitcount(dss
->g
) + 8) / 8;
318 ylen
= (bignum_bitcount(dss
->y
) + 8) / 8;
321 * string "ssh-dss", mpint p, mpint q, mpint g, mpint y. Total
322 * 27 + sum of lengths. (five length fields, 20+7=27).
324 bloblen
= 27 + plen
+ qlen
+ glen
+ ylen
;
325 blob
= smalloc(bloblen
);
329 memcpy(p
, "ssh-dss", 7);
334 *p
++ = bignum_byte(dss
->p
, i
);
338 *p
++ = bignum_byte(dss
->q
, i
);
342 *p
++ = bignum_byte(dss
->g
, i
);
346 *p
++ = bignum_byte(dss
->y
, i
);
347 assert(p
== blob
+ bloblen
);
352 static unsigned char *dss_private_blob(void *key
, int *len
)
354 struct dss_key
*dss
= (struct dss_key
*) key
;
357 unsigned char *blob
, *p
;
359 xlen
= (bignum_bitcount(dss
->x
) + 8) / 8;
362 * mpint x, string[20] the SHA of p||q||g. Total 4 + xlen.
365 blob
= smalloc(bloblen
);
370 *p
++ = bignum_byte(dss
->x
, i
);
371 assert(p
== blob
+ bloblen
);
376 static void *dss_createkey(unsigned char *pub_blob
, int pub_len
,
377 unsigned char *priv_blob
, int priv_len
)
380 char *pb
= (char *) priv_blob
;
384 unsigned char digest
[20];
387 dss
= dss_newkey((char *) pub_blob
, pub_len
);
388 dss
->x
= getmp(&pb
, &priv_len
);
391 * Check the obsolete hash in the old DSS key format.
394 getstring(&pb
, &priv_len
, &hash
, &hashlen
);
397 sha_mpint(&s
, dss
->p
);
398 sha_mpint(&s
, dss
->q
);
399 sha_mpint(&s
, dss
->g
);
400 SHA_Final(&s
, digest
);
401 if (0 != memcmp(hash
, digest
, 20)) {
408 * Now ensure g^x mod p really is y.
410 ytest
= modpow(dss
->g
, dss
->x
, dss
->p
);
411 if (0 != bignum_cmp(ytest
, dss
->y
)) {
420 static void *dss_openssh_createkey(unsigned char **blob
, int *len
)
422 char **b
= (char **) blob
;
425 dss
= smalloc(sizeof(struct dss_key
));
429 dss
->p
= getmp(b
, len
);
430 dss
->q
= getmp(b
, len
);
431 dss
->g
= getmp(b
, len
);
432 dss
->y
= getmp(b
, len
);
433 dss
->x
= getmp(b
, len
);
435 if (!dss
->p
|| !dss
->q
|| !dss
->g
|| !dss
->y
|| !dss
->x
) {
448 static int dss_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
450 struct dss_key
*dss
= (struct dss_key
*) key
;
454 ssh2_bignum_length(dss
->p
) +
455 ssh2_bignum_length(dss
->q
) +
456 ssh2_bignum_length(dss
->g
) +
457 ssh2_bignum_length(dss
->y
) +
458 ssh2_bignum_length(dss
->x
);
465 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
466 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
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
= smalloc(nbytes
);
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
,