2 * RSA implementation for PuTTY.
13 #define GET_32BIT(cp) \
14 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
15 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
16 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
17 ((unsigned long)(unsigned char)(cp)[3]))
19 #define PUT_32BIT(cp, value) { \
20 (cp)[0] = (unsigned char)((value) >> 24); \
21 (cp)[1] = (unsigned char)((value) >> 16); \
22 (cp)[2] = (unsigned char)((value) >> 8); \
23 (cp)[3] = (unsigned char)(value); }
25 int makekey(unsigned char *data
, struct RSAKey
*result
,
26 unsigned char **keystr
, int order
)
28 unsigned char *p
= data
;
33 for (i
= 0; i
< 4; i
++)
34 result
->bits
= (result
->bits
<< 8) + *p
++;
39 * order=0 means exponent then modulus (the keys sent by the
40 * server). order=1 means modulus then exponent (the keys
41 * stored in a keyfile).
45 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
47 result
->bytes
= (((p
[0] << 8) + p
[1]) + 7) / 8;
50 p
+= ssh1_read_bignum(p
, result ?
&result
->modulus
: NULL
);
52 p
+= ssh1_read_bignum(p
, result ?
&result
->exponent
: NULL
);
57 int makeprivate(unsigned char *data
, struct RSAKey
*result
)
59 return ssh1_read_bignum(data
, &result
->private_exponent
);
62 void rsaencrypt(unsigned char *data
, int length
, struct RSAKey
*key
)
68 memmove(data
+ key
->bytes
- length
, data
, length
);
72 for (i
= 2; i
< key
->bytes
- length
- 1; i
++) {
74 data
[i
] = random_byte();
75 } while (data
[i
] == 0);
77 data
[key
->bytes
- length
- 1] = 0;
79 b1
= bignum_from_bytes(data
, key
->bytes
);
81 b2
= modpow(b1
, key
->exponent
, key
->modulus
);
84 for (i
= key
->bytes
; i
--;) {
85 *p
++ = bignum_byte(b2
, i
);
92 static void sha512_mpint(SHA512_State
* s
, Bignum b
)
94 unsigned char lenbuf
[4];
96 len
= (bignum_bitcount(b
) + 8) / 8;
97 PUT_32BIT(lenbuf
, len
);
98 SHA512_Bytes(s
, lenbuf
, 4);
100 lenbuf
[0] = bignum_byte(b
, len
);
101 SHA512_Bytes(s
, lenbuf
, 1);
103 memset(lenbuf
, 0, sizeof(lenbuf
));
107 * This function is a wrapper on modpow(). It has the same effect
108 * as modpow(), but employs RSA blinding to protect against timing
111 static Bignum
rsa_privkey_op(Bignum input
, struct RSAKey
*key
)
113 Bignum random
, random_encrypted
, random_inverse
;
114 Bignum input_blinded
, ret_blinded
;
118 unsigned char digest512
[64];
119 int digestused
= lenof(digest512
);
123 * Start by inventing a random number chosen uniformly from the
124 * range 2..modulus-1. (We do this by preparing a random number
125 * of the right length and retrying if it's greater than the
126 * modulus, to prevent any potential Bleichenbacher-like
127 * attacks making use of the uneven distribution within the
128 * range that would arise from just reducing our number mod n.
129 * There are timing implications to the potential retries, of
130 * course, but all they tell you is the modulus, which you
133 * To preserve determinism and avoid Pageant needing to share
134 * the random number pool, we actually generate this `random'
135 * number by hashing stuff with the private key.
138 int bits
, byte
, bitsleft
, v
;
139 random
= copybn(key
->modulus
);
141 * Find the topmost set bit. (This function will return its
142 * index plus one.) Then we'll set all bits from that one
143 * downwards randomly.
145 bits
= bignum_bitcount(random
);
152 * Conceptually the following few lines are equivalent to
153 * byte = random_byte();
155 if (digestused
>= lenof(digest512
)) {
156 unsigned char seqbuf
[4];
157 PUT_32BIT(seqbuf
, hashseq
);
159 SHA512_Bytes(&ss
, "RSA deterministic blinding", 26);
160 SHA512_Bytes(&ss
, seqbuf
, sizeof(seqbuf
));
161 sha512_mpint(&ss
, key
->private_exponent
);
162 SHA512_Final(&ss
, digest512
);
166 * Now hash that digest plus the signature
170 SHA512_Bytes(&ss
, digest512
, sizeof(digest512
));
171 sha512_mpint(&ss
, input
);
172 SHA512_Final(&ss
, digest512
);
176 byte
= digest512
[digestused
++];
181 bignum_set_bit(random
, bits
, v
);
185 * Now check that this number is strictly greater than
186 * zero, and strictly less than modulus.
188 if (bignum_cmp(random
, Zero
) <= 0 ||
189 bignum_cmp(random
, key
->modulus
) >= 0) {
198 * RSA blinding relies on the fact that (xy)^d mod n is equal
199 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
200 * y and y^d; then we multiply x by y, raise to the power d mod
201 * n as usual, and divide by y^d to recover x^d. Thus an
202 * attacker can't correlate the timing of the modpow with the
203 * input, because they don't know anything about the number
204 * that was input to the actual modpow.
206 * The clever bit is that we don't have to do a huge modpow to
207 * get y and y^d; we will use the number we just invented as
208 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
209 * from it, which is much faster to do.
211 random_encrypted
= modpow(random
, key
->exponent
, key
->modulus
);
212 random_inverse
= modinv(random
, key
->modulus
);
213 input_blinded
= modmul(input
, random_encrypted
, key
->modulus
);
214 ret_blinded
= modpow(input_blinded
, key
->private_exponent
, key
->modulus
);
215 ret
= modmul(ret_blinded
, random_inverse
, key
->modulus
);
218 freebn(input_blinded
);
219 freebn(random_inverse
);
220 freebn(random_encrypted
);
226 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
)
228 return rsa_privkey_op(input
, key
);
231 int rsastr_len(struct RSAKey
*key
)
238 mdlen
= (bignum_bitcount(md
) + 15) / 16;
239 exlen
= (bignum_bitcount(ex
) + 15) / 16;
240 return 4 * (mdlen
+ exlen
) + 20;
243 void rsastr_fmt(char *str
, struct RSAKey
*key
)
246 int len
= 0, i
, nibbles
;
247 static const char hex
[] = "0123456789abcdef";
252 len
+= sprintf(str
+ len
, "0x");
254 nibbles
= (3 + bignum_bitcount(ex
)) / 4;
257 for (i
= nibbles
; i
--;)
258 str
[len
++] = hex
[(bignum_byte(ex
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
260 len
+= sprintf(str
+ len
, ",0x");
262 nibbles
= (3 + bignum_bitcount(md
)) / 4;
265 for (i
= nibbles
; i
--;)
266 str
[len
++] = hex
[(bignum_byte(md
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
272 * Generate a fingerprint string for the key. Compatible with the
273 * OpenSSH fingerprint code.
275 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
)
277 struct MD5Context md5c
;
278 unsigned char digest
[16];
279 char buffer
[16 * 3 + 40];
283 numlen
= ssh1_bignum_length(key
->modulus
) - 2;
284 for (i
= numlen
; i
--;) {
285 unsigned char c
= bignum_byte(key
->modulus
, i
);
286 MD5Update(&md5c
, &c
, 1);
288 numlen
= ssh1_bignum_length(key
->exponent
) - 2;
289 for (i
= numlen
; i
--;) {
290 unsigned char c
= bignum_byte(key
->exponent
, i
);
291 MD5Update(&md5c
, &c
, 1);
293 MD5Final(digest
, &md5c
);
295 sprintf(buffer
, "%d ", bignum_bitcount(key
->modulus
));
296 for (i
= 0; i
< 16; i
++)
297 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
299 strncpy(str
, buffer
, len
);
302 if (key
->comment
&& slen
< len
- 1) {
304 strncpy(str
+ slen
+ 1, key
->comment
, len
- slen
- 1);
310 * Verify that the public data in an RSA key matches the private
311 * data. We also check the private data itself: we ensure that p >
312 * q and that iqmp really is the inverse of q mod p.
314 int rsa_verify(struct RSAKey
*key
)
316 Bignum n
, ed
, pm1
, qm1
;
319 /* n must equal pq. */
320 n
= bigmul(key
->p
, key
->q
);
321 cmp
= bignum_cmp(n
, key
->modulus
);
326 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
327 pm1
= copybn(key
->p
);
329 ed
= modmul(key
->exponent
, key
->private_exponent
, pm1
);
330 cmp
= bignum_cmp(ed
, One
);
335 qm1
= copybn(key
->q
);
337 ed
= modmul(key
->exponent
, key
->private_exponent
, qm1
);
338 cmp
= bignum_cmp(ed
, One
);
346 if (bignum_cmp(key
->p
, key
->q
) <= 0)
350 * Ensure iqmp * q is congruent to 1, modulo p.
352 n
= modmul(key
->iqmp
, key
->q
, key
->p
);
353 cmp
= bignum_cmp(n
, One
);
361 /* Public key blob as used by Pageant: exponent before modulus. */
362 unsigned char *rsa_public_blob(struct RSAKey
*key
, int *len
)
367 length
= (ssh1_bignum_length(key
->modulus
) +
368 ssh1_bignum_length(key
->exponent
) + 4);
369 ret
= snewn(length
, unsigned char);
371 PUT_32BIT(ret
, bignum_bitcount(key
->modulus
));
373 pos
+= ssh1_write_bignum(ret
+ pos
, key
->exponent
);
374 pos
+= ssh1_write_bignum(ret
+ pos
, key
->modulus
);
380 /* Given a public blob, determine its length. */
381 int rsa_public_blob_len(void *data
)
383 unsigned char *p
= (unsigned char *)data
;
385 p
+= 4; /* length word */
386 p
+= ssh1_read_bignum(p
, NULL
); /* exponent */
387 p
+= ssh1_read_bignum(p
, NULL
); /* modulus */
389 return p
- (unsigned char *)data
;
392 void freersakey(struct RSAKey
*key
)
395 freebn(key
->modulus
);
397 freebn(key
->exponent
);
398 if (key
->private_exponent
)
399 freebn(key
->private_exponent
);
404 /* ----------------------------------------------------------------------
405 * Implementation of the ssh-rsa signing key type.
408 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
413 *length
= GET_32BIT(*data
);
416 if (*datalen
< *length
)
422 static Bignum
getmp(char **data
, int *datalen
)
428 getstring(data
, datalen
, &p
, &length
);
431 b
= bignum_from_bytes((unsigned char *)p
, length
);
435 static void *rsa2_newkey(char *data
, int len
)
441 rsa
= snew(struct RSAKey
);
444 getstring(&data
, &len
, &p
, &slen
);
446 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
450 rsa
->exponent
= getmp(&data
, &len
);
451 rsa
->modulus
= getmp(&data
, &len
);
452 rsa
->private_exponent
= NULL
;
458 static void rsa2_freekey(void *key
)
460 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
465 static char *rsa2_fmtkey(void *key
)
467 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
471 len
= rsastr_len(rsa
);
472 p
= snewn(len
, char);
477 static unsigned char *rsa2_public_blob(void *key
, int *len
)
479 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
480 int elen
, mlen
, bloblen
;
482 unsigned char *blob
, *p
;
484 elen
= (bignum_bitcount(rsa
->exponent
) + 8) / 8;
485 mlen
= (bignum_bitcount(rsa
->modulus
) + 8) / 8;
488 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
489 * (three length fields, 12+7=19).
491 bloblen
= 19 + elen
+ mlen
;
492 blob
= snewn(bloblen
, unsigned char);
496 memcpy(p
, "ssh-rsa", 7);
501 *p
++ = bignum_byte(rsa
->exponent
, i
);
505 *p
++ = bignum_byte(rsa
->modulus
, i
);
506 assert(p
== blob
+ bloblen
);
511 static unsigned char *rsa2_private_blob(void *key
, int *len
)
513 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
514 int dlen
, plen
, qlen
, ulen
, bloblen
;
516 unsigned char *blob
, *p
;
518 dlen
= (bignum_bitcount(rsa
->private_exponent
) + 8) / 8;
519 plen
= (bignum_bitcount(rsa
->p
) + 8) / 8;
520 qlen
= (bignum_bitcount(rsa
->q
) + 8) / 8;
521 ulen
= (bignum_bitcount(rsa
->iqmp
) + 8) / 8;
524 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
527 bloblen
= 16 + dlen
+ plen
+ qlen
+ ulen
;
528 blob
= snewn(bloblen
, unsigned char);
533 *p
++ = bignum_byte(rsa
->private_exponent
, i
);
537 *p
++ = bignum_byte(rsa
->p
, i
);
541 *p
++ = bignum_byte(rsa
->q
, i
);
545 *p
++ = bignum_byte(rsa
->iqmp
, i
);
546 assert(p
== blob
+ bloblen
);
551 static void *rsa2_createkey(unsigned char *pub_blob
, int pub_len
,
552 unsigned char *priv_blob
, int priv_len
)
555 char *pb
= (char *) priv_blob
;
557 rsa
= rsa2_newkey((char *) pub_blob
, pub_len
);
558 rsa
->private_exponent
= getmp(&pb
, &priv_len
);
559 rsa
->p
= getmp(&pb
, &priv_len
);
560 rsa
->q
= getmp(&pb
, &priv_len
);
561 rsa
->iqmp
= getmp(&pb
, &priv_len
);
563 if (!rsa_verify(rsa
)) {
571 static void *rsa2_openssh_createkey(unsigned char **blob
, int *len
)
573 char **b
= (char **) blob
;
576 rsa
= snew(struct RSAKey
);
581 rsa
->modulus
= getmp(b
, len
);
582 rsa
->exponent
= getmp(b
, len
);
583 rsa
->private_exponent
= getmp(b
, len
);
584 rsa
->iqmp
= getmp(b
, len
);
585 rsa
->p
= getmp(b
, len
);
586 rsa
->q
= getmp(b
, len
);
588 if (!rsa
->modulus
|| !rsa
->exponent
|| !rsa
->private_exponent
||
589 !rsa
->iqmp
|| !rsa
->p
|| !rsa
->q
) {
591 sfree(rsa
->exponent
);
592 sfree(rsa
->private_exponent
);
603 static int rsa2_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
605 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
609 ssh2_bignum_length(rsa
->modulus
) +
610 ssh2_bignum_length(rsa
->exponent
) +
611 ssh2_bignum_length(rsa
->private_exponent
) +
612 ssh2_bignum_length(rsa
->iqmp
) +
613 ssh2_bignum_length(rsa
->p
) + ssh2_bignum_length(rsa
->q
);
620 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
621 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
624 ENC(rsa
->private_exponent
);
632 static char *rsa2_fingerprint(void *key
)
634 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
635 struct MD5Context md5c
;
636 unsigned char digest
[16], lenbuf
[4];
637 char buffer
[16 * 3 + 40];
642 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
644 #define ADD_BIGNUM(bignum) \
645 numlen = (bignum_bitcount(bignum)+8)/8; \
646 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
647 for (i = numlen; i-- ;) { \
648 unsigned char c = bignum_byte(bignum, i); \
649 MD5Update(&md5c, &c, 1); \
651 ADD_BIGNUM(rsa
->exponent
);
652 ADD_BIGNUM(rsa
->modulus
);
655 MD5Final(digest
, &md5c
);
657 sprintf(buffer
, "ssh-rsa %d ", bignum_bitcount(rsa
->modulus
));
658 for (i
= 0; i
< 16; i
++)
659 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
661 ret
= snewn(strlen(buffer
) + 1, char);
668 * This is the magic ASN.1/DER prefix that goes in the decoded
669 * signature, between the string of FFs and the actual SHA hash
670 * value. The meaning of it is:
672 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
674 * 30 21 -- a constructed SEQUENCE of length 0x21
675 * 30 09 -- a constructed sub-SEQUENCE of length 9
676 * 06 05 -- an object identifier, length 5
677 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
678 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
680 * 04 14 -- a primitive OCTET STRING of length 0x14
681 * [0x14 bytes of hash data follows]
683 * The object id in the middle there is listed as `id-sha1' in
684 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
685 * ASN module for PKCS #1) and its expanded form is as follows:
687 * id-sha1 OBJECT IDENTIFIER ::= {
688 * iso(1) identified-organization(3) oiw(14) secsig(3)
691 static const unsigned char asn1_weird_stuff
[] = {
692 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
693 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
696 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
698 static int rsa2_verifysig(void *key
, char *sig
, int siglen
,
699 char *data
, int datalen
)
701 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
705 int bytes
, i
, j
, ret
;
706 unsigned char hash
[20];
708 getstring(&sig
, &siglen
, &p
, &slen
);
709 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
712 in
= getmp(&sig
, &siglen
);
713 out
= modpow(in
, rsa
->exponent
, rsa
->modulus
);
718 bytes
= bignum_bitcount(rsa
->modulus
) / 8;
719 /* Top (partial) byte should be zero. */
720 if (bignum_byte(out
, bytes
- 1) != 0)
722 /* First whole byte should be 1. */
723 if (bignum_byte(out
, bytes
- 2) != 1)
725 /* Most of the rest should be FF. */
726 for (i
= bytes
- 3; i
>= 20 + ASN1_LEN
; i
--) {
727 if (bignum_byte(out
, i
) != 0xFF)
730 /* Then we expect to see the asn1_weird_stuff. */
731 for (i
= 20 + ASN1_LEN
- 1, j
= 0; i
>= 20; i
--, j
++) {
732 if (bignum_byte(out
, i
) != asn1_weird_stuff
[j
])
735 /* Finally, we expect to see the SHA-1 hash of the signed data. */
736 SHA_Simple(data
, datalen
, hash
);
737 for (i
= 19, j
= 0; i
>= 0; i
--, j
++) {
738 if (bignum_byte(out
, i
) != hash
[j
])
745 static unsigned char *rsa2_sign(void *key
, char *data
, int datalen
,
748 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
749 unsigned char *bytes
;
751 unsigned char hash
[20];
755 SHA_Simple(data
, datalen
, hash
);
757 nbytes
= (bignum_bitcount(rsa
->modulus
) - 1) / 8;
758 bytes
= snewn(nbytes
, unsigned char);
761 for (i
= 1; i
< nbytes
- 20 - ASN1_LEN
; i
++)
763 for (i
= nbytes
- 20 - ASN1_LEN
, j
= 0; i
< nbytes
- 20; i
++, j
++)
764 bytes
[i
] = asn1_weird_stuff
[j
];
765 for (i
= nbytes
- 20, j
= 0; i
< nbytes
; i
++, j
++)
768 in
= bignum_from_bytes(bytes
, nbytes
);
771 out
= rsa_privkey_op(in
, rsa
);
774 nbytes
= (bignum_bitcount(out
) + 7) / 8;
775 bytes
= snewn(4 + 7 + 4 + nbytes
, unsigned char);
777 memcpy(bytes
+ 4, "ssh-rsa", 7);
778 PUT_32BIT(bytes
+ 4 + 7, nbytes
);
779 for (i
= 0; i
< nbytes
; i
++)
780 bytes
[4 + 7 + 4 + i
] = bignum_byte(out
, nbytes
- 1 - i
);
783 *siglen
= 4 + 7 + 4 + nbytes
;
787 const struct ssh_signkey ssh_rsa
= {
794 rsa2_openssh_createkey
,