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
);
93 * This function is a wrapper on modpow(). It has the same effect
94 * as modpow(), but employs RSA blinding to protect against timing
97 static Bignum
rsa_privkey_op(Bignum input
, struct RSAKey
*key
)
99 Bignum random
, random_encrypted
, random_inverse
;
100 Bignum input_blinded
, ret_blinded
;
104 * Start by inventing a random number chosen uniformly from the
105 * range 2..modulus-1. (We do this by preparing a random number
106 * of the right length and retrying if it's greater than the
107 * modulus, to prevent any potential Bleichenbacher-like
108 * attacks making use of the uneven distribution within the
109 * range that would arise from just reducing our number mod n.
110 * There are timing implications to the potential retries, of
111 * course, but all they tell you is the modulus, which you
115 int bits
, byte
, bitsleft
, v
;
116 random
= copybn(key
->modulus
);
118 * Find the topmost set bit. (This function will return its
119 * index plus one.) Then we'll set all bits from that one
120 * downwards randomly.
122 bits
= bignum_bitcount(random
);
127 bitsleft
= 8, byte
= random_byte();
131 bignum_set_bit(random
, bits
, v
);
135 * Now check that this number is strictly greater than
136 * zero, and strictly less than modulus.
138 if (bignum_cmp(random
, Zero
) <= 0 ||
139 bignum_cmp(random
, key
->modulus
) >= 0) {
148 * RSA blinding relies on the fact that (xy)^d mod n is equal
149 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
150 * y and y^d; then we multiply x by y, raise to the power e mod
151 * n as usual, and divide by y^d to recover x^d. Thus the
152 * timing of the modpow does not reveal information about x,
153 * but only about xy, which is unpredictable to an attacker.
155 * The clever bit is that we don't have to do a huge modpow to
156 * get y and y^d; we will use the number we just invented as
157 * _y^d_, and use the RSA public exponent to compute y from it,
158 * which is much faster.
160 random_encrypted
= modpow(random
, key
->exponent
, key
->modulus
);
161 random_inverse
= modinv(random
, key
->modulus
);
162 input_blinded
= modmul(input
, random_encrypted
, key
->modulus
);
163 ret_blinded
= modpow(input_blinded
, key
->private_exponent
, key
->modulus
);
164 ret
= modmul(ret_blinded
, random_inverse
, key
->modulus
);
167 freebn(input_blinded
);
168 freebn(random_inverse
);
169 freebn(random_encrypted
);
175 Bignum
rsadecrypt(Bignum input
, struct RSAKey
*key
)
177 return rsa_privkey_op(input
, key
);
180 int rsastr_len(struct RSAKey
*key
)
187 mdlen
= (bignum_bitcount(md
) + 15) / 16;
188 exlen
= (bignum_bitcount(ex
) + 15) / 16;
189 return 4 * (mdlen
+ exlen
) + 20;
192 void rsastr_fmt(char *str
, struct RSAKey
*key
)
195 int len
= 0, i
, nibbles
;
196 static const char hex
[] = "0123456789abcdef";
201 len
+= sprintf(str
+ len
, "0x");
203 nibbles
= (3 + bignum_bitcount(ex
)) / 4;
206 for (i
= nibbles
; i
--;)
207 str
[len
++] = hex
[(bignum_byte(ex
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
209 len
+= sprintf(str
+ len
, ",0x");
211 nibbles
= (3 + bignum_bitcount(md
)) / 4;
214 for (i
= nibbles
; i
--;)
215 str
[len
++] = hex
[(bignum_byte(md
, i
/ 2) >> (4 * (i
% 2))) & 0xF];
221 * Generate a fingerprint string for the key. Compatible with the
222 * OpenSSH fingerprint code.
224 void rsa_fingerprint(char *str
, int len
, struct RSAKey
*key
)
226 struct MD5Context md5c
;
227 unsigned char digest
[16];
228 char buffer
[16 * 3 + 40];
232 numlen
= ssh1_bignum_length(key
->modulus
) - 2;
233 for (i
= numlen
; i
--;) {
234 unsigned char c
= bignum_byte(key
->modulus
, i
);
235 MD5Update(&md5c
, &c
, 1);
237 numlen
= ssh1_bignum_length(key
->exponent
) - 2;
238 for (i
= numlen
; i
--;) {
239 unsigned char c
= bignum_byte(key
->exponent
, i
);
240 MD5Update(&md5c
, &c
, 1);
242 MD5Final(digest
, &md5c
);
244 sprintf(buffer
, "%d ", bignum_bitcount(key
->modulus
));
245 for (i
= 0; i
< 16; i
++)
246 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
248 strncpy(str
, buffer
, len
);
251 if (key
->comment
&& slen
< len
- 1) {
253 strncpy(str
+ slen
+ 1, key
->comment
, len
- slen
- 1);
259 * Verify that the public data in an RSA key matches the private
260 * data. We also check the private data itself: we ensure that p >
261 * q and that iqmp really is the inverse of q mod p.
263 int rsa_verify(struct RSAKey
*key
)
265 Bignum n
, ed
, pm1
, qm1
;
268 /* n must equal pq. */
269 n
= bigmul(key
->p
, key
->q
);
270 cmp
= bignum_cmp(n
, key
->modulus
);
275 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
276 pm1
= copybn(key
->p
);
278 ed
= modmul(key
->exponent
, key
->private_exponent
, pm1
);
279 cmp
= bignum_cmp(ed
, One
);
284 qm1
= copybn(key
->q
);
286 ed
= modmul(key
->exponent
, key
->private_exponent
, qm1
);
287 cmp
= bignum_cmp(ed
, One
);
295 if (bignum_cmp(key
->p
, key
->q
) <= 0)
299 * Ensure iqmp * q is congruent to 1, modulo p.
301 n
= modmul(key
->iqmp
, key
->q
, key
->p
);
302 cmp
= bignum_cmp(n
, One
);
310 /* Public key blob as used by Pageant: exponent before modulus. */
311 unsigned char *rsa_public_blob(struct RSAKey
*key
, int *len
)
316 length
= (ssh1_bignum_length(key
->modulus
) +
317 ssh1_bignum_length(key
->exponent
) + 4);
318 ret
= smalloc(length
);
320 PUT_32BIT(ret
, bignum_bitcount(key
->modulus
));
322 pos
+= ssh1_write_bignum(ret
+ pos
, key
->exponent
);
323 pos
+= ssh1_write_bignum(ret
+ pos
, key
->modulus
);
329 /* Given a public blob, determine its length. */
330 int rsa_public_blob_len(void *data
)
332 unsigned char *p
= (unsigned char *)data
;
334 p
+= 4; /* length word */
335 p
+= ssh1_read_bignum(p
, NULL
); /* exponent */
336 p
+= ssh1_read_bignum(p
, NULL
); /* modulus */
338 return p
- (unsigned char *)data
;
341 void freersakey(struct RSAKey
*key
)
344 freebn(key
->modulus
);
346 freebn(key
->exponent
);
347 if (key
->private_exponent
)
348 freebn(key
->private_exponent
);
353 /* ----------------------------------------------------------------------
354 * Implementation of the ssh-rsa signing key type.
357 static void getstring(char **data
, int *datalen
, char **p
, int *length
)
362 *length
= GET_32BIT(*data
);
365 if (*datalen
< *length
)
371 static Bignum
getmp(char **data
, int *datalen
)
377 getstring(data
, datalen
, &p
, &length
);
380 b
= bignum_from_bytes((unsigned char *)p
, length
);
384 static void *rsa2_newkey(char *data
, int len
)
390 rsa
= smalloc(sizeof(struct RSAKey
));
393 getstring(&data
, &len
, &p
, &slen
);
395 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
399 rsa
->exponent
= getmp(&data
, &len
);
400 rsa
->modulus
= getmp(&data
, &len
);
401 rsa
->private_exponent
= NULL
;
407 static void rsa2_freekey(void *key
)
409 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
414 static char *rsa2_fmtkey(void *key
)
416 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
420 len
= rsastr_len(rsa
);
426 static unsigned char *rsa2_public_blob(void *key
, int *len
)
428 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
429 int elen
, mlen
, bloblen
;
431 unsigned char *blob
, *p
;
433 elen
= (bignum_bitcount(rsa
->exponent
) + 8) / 8;
434 mlen
= (bignum_bitcount(rsa
->modulus
) + 8) / 8;
437 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
438 * (three length fields, 12+7=19).
440 bloblen
= 19 + elen
+ mlen
;
441 blob
= smalloc(bloblen
);
445 memcpy(p
, "ssh-rsa", 7);
450 *p
++ = bignum_byte(rsa
->exponent
, i
);
454 *p
++ = bignum_byte(rsa
->modulus
, i
);
455 assert(p
== blob
+ bloblen
);
460 static unsigned char *rsa2_private_blob(void *key
, int *len
)
462 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
463 int dlen
, plen
, qlen
, ulen
, bloblen
;
465 unsigned char *blob
, *p
;
467 dlen
= (bignum_bitcount(rsa
->private_exponent
) + 8) / 8;
468 plen
= (bignum_bitcount(rsa
->p
) + 8) / 8;
469 qlen
= (bignum_bitcount(rsa
->q
) + 8) / 8;
470 ulen
= (bignum_bitcount(rsa
->iqmp
) + 8) / 8;
473 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
476 bloblen
= 16 + dlen
+ plen
+ qlen
+ ulen
;
477 blob
= smalloc(bloblen
);
482 *p
++ = bignum_byte(rsa
->private_exponent
, i
);
486 *p
++ = bignum_byte(rsa
->p
, i
);
490 *p
++ = bignum_byte(rsa
->q
, i
);
494 *p
++ = bignum_byte(rsa
->iqmp
, i
);
495 assert(p
== blob
+ bloblen
);
500 static void *rsa2_createkey(unsigned char *pub_blob
, int pub_len
,
501 unsigned char *priv_blob
, int priv_len
)
504 char *pb
= (char *) priv_blob
;
506 rsa
= rsa2_newkey((char *) pub_blob
, pub_len
);
507 rsa
->private_exponent
= getmp(&pb
, &priv_len
);
508 rsa
->p
= getmp(&pb
, &priv_len
);
509 rsa
->q
= getmp(&pb
, &priv_len
);
510 rsa
->iqmp
= getmp(&pb
, &priv_len
);
512 if (!rsa_verify(rsa
)) {
520 static void *rsa2_openssh_createkey(unsigned char **blob
, int *len
)
522 char **b
= (char **) blob
;
525 rsa
= smalloc(sizeof(struct RSAKey
));
530 rsa
->modulus
= getmp(b
, len
);
531 rsa
->exponent
= getmp(b
, len
);
532 rsa
->private_exponent
= getmp(b
, len
);
533 rsa
->iqmp
= getmp(b
, len
);
534 rsa
->p
= getmp(b
, len
);
535 rsa
->q
= getmp(b
, len
);
537 if (!rsa
->modulus
|| !rsa
->exponent
|| !rsa
->private_exponent
||
538 !rsa
->iqmp
|| !rsa
->p
|| !rsa
->q
) {
540 sfree(rsa
->exponent
);
541 sfree(rsa
->private_exponent
);
552 static int rsa2_openssh_fmtkey(void *key
, unsigned char *blob
, int len
)
554 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
558 ssh2_bignum_length(rsa
->modulus
) +
559 ssh2_bignum_length(rsa
->exponent
) +
560 ssh2_bignum_length(rsa
->private_exponent
) +
561 ssh2_bignum_length(rsa
->iqmp
) +
562 ssh2_bignum_length(rsa
->p
) + ssh2_bignum_length(rsa
->q
);
569 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
570 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
573 ENC(rsa
->private_exponent
);
581 static char *rsa2_fingerprint(void *key
)
583 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
584 struct MD5Context md5c
;
585 unsigned char digest
[16], lenbuf
[4];
586 char buffer
[16 * 3 + 40];
591 MD5Update(&md5c
, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
593 #define ADD_BIGNUM(bignum) \
594 numlen = (bignum_bitcount(bignum)+8)/8; \
595 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
596 for (i = numlen; i-- ;) { \
597 unsigned char c = bignum_byte(bignum, i); \
598 MD5Update(&md5c, &c, 1); \
600 ADD_BIGNUM(rsa
->exponent
);
601 ADD_BIGNUM(rsa
->modulus
);
604 MD5Final(digest
, &md5c
);
606 sprintf(buffer
, "ssh-rsa %d ", bignum_bitcount(rsa
->modulus
));
607 for (i
= 0; i
< 16; i
++)
608 sprintf(buffer
+ strlen(buffer
), "%s%02x", i ?
":" : "",
610 ret
= smalloc(strlen(buffer
) + 1);
617 * This is the magic ASN.1/DER prefix that goes in the decoded
618 * signature, between the string of FFs and the actual SHA hash
619 * value. The meaning of it is:
621 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
623 * 30 21 -- a constructed SEQUENCE of length 0x21
624 * 30 09 -- a constructed sub-SEQUENCE of length 9
625 * 06 05 -- an object identifier, length 5
626 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
627 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
629 * 04 14 -- a primitive OCTET STRING of length 0x14
630 * [0x14 bytes of hash data follows]
632 * The object id in the middle there is listed as `id-sha1' in
633 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
634 * ASN module for PKCS #1) and its expanded form is as follows:
636 * id-sha1 OBJECT IDENTIFIER ::= {
637 * iso(1) identified-organization(3) oiw(14) secsig(3)
640 static const unsigned char asn1_weird_stuff
[] = {
641 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
642 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
645 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
647 static int rsa2_verifysig(void *key
, char *sig
, int siglen
,
648 char *data
, int datalen
)
650 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
654 int bytes
, i
, j
, ret
;
655 unsigned char hash
[20];
657 getstring(&sig
, &siglen
, &p
, &slen
);
658 if (!p
|| slen
!= 7 || memcmp(p
, "ssh-rsa", 7)) {
661 in
= getmp(&sig
, &siglen
);
662 out
= modpow(in
, rsa
->exponent
, rsa
->modulus
);
667 bytes
= bignum_bitcount(rsa
->modulus
) / 8;
668 /* Top (partial) byte should be zero. */
669 if (bignum_byte(out
, bytes
- 1) != 0)
671 /* First whole byte should be 1. */
672 if (bignum_byte(out
, bytes
- 2) != 1)
674 /* Most of the rest should be FF. */
675 for (i
= bytes
- 3; i
>= 20 + ASN1_LEN
; i
--) {
676 if (bignum_byte(out
, i
) != 0xFF)
679 /* Then we expect to see the asn1_weird_stuff. */
680 for (i
= 20 + ASN1_LEN
- 1, j
= 0; i
>= 20; i
--, j
++) {
681 if (bignum_byte(out
, i
) != asn1_weird_stuff
[j
])
684 /* Finally, we expect to see the SHA-1 hash of the signed data. */
685 SHA_Simple(data
, datalen
, hash
);
686 for (i
= 19, j
= 0; i
>= 0; i
--, j
++) {
687 if (bignum_byte(out
, i
) != hash
[j
])
694 static unsigned char *rsa2_sign(void *key
, char *data
, int datalen
,
697 struct RSAKey
*rsa
= (struct RSAKey
*) key
;
698 unsigned char *bytes
;
700 unsigned char hash
[20];
704 SHA_Simple(data
, datalen
, hash
);
706 nbytes
= (bignum_bitcount(rsa
->modulus
) - 1) / 8;
707 bytes
= smalloc(nbytes
);
710 for (i
= 1; i
< nbytes
- 20 - ASN1_LEN
; i
++)
712 for (i
= nbytes
- 20 - ASN1_LEN
, j
= 0; i
< nbytes
- 20; i
++, j
++)
713 bytes
[i
] = asn1_weird_stuff
[j
];
714 for (i
= nbytes
- 20, j
= 0; i
< nbytes
; i
++, j
++)
717 in
= bignum_from_bytes(bytes
, nbytes
);
720 out
= rsa_privkey_op(in
, rsa
);
723 nbytes
= (bignum_bitcount(out
) + 7) / 8;
724 bytes
= smalloc(4 + 7 + 4 + nbytes
);
726 memcpy(bytes
+ 4, "ssh-rsa", 7);
727 PUT_32BIT(bytes
+ 4 + 7, nbytes
);
728 for (i
= 0; i
< nbytes
; i
++)
729 bytes
[4 + 7 + 4 + i
] = bignum_byte(out
, nbytes
- 1 - i
);
732 *siglen
= 4 + 7 + 4 + nbytes
;
736 const struct ssh_signkey ssh_rsa
= {
743 rsa2_openssh_createkey
,