2 * RSA implementation for PuTTY.
\r
13 int makekey(unsigned char *data, int len, struct RSAKey *result,
\r
14 unsigned char **keystr, int order)
\r
16 unsigned char *p = data;
\r
24 for (i = 0; i < 4; i++)
\r
25 result->bits = (result->bits << 8) + *p++;
\r
32 * order=0 means exponent then modulus (the keys sent by the
\r
33 * server). order=1 means modulus then exponent (the keys
\r
34 * stored in a keyfile).
\r
38 n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL);
\r
39 if (n < 0) return -1;
\r
44 n = ssh1_read_bignum(p, len, result ? &result->modulus : NULL);
\r
45 if (n < 0 || (result && bignum_bitcount(result->modulus) == 0)) return -1;
\r
47 result->bytes = n - 2;
\r
54 n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL);
\r
55 if (n < 0) return -1;
\r
62 int makeprivate(unsigned char *data, int len, struct RSAKey *result)
\r
64 return ssh1_read_bignum(data, len, &result->private_exponent);
\r
67 int rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
\r
73 if (key->bytes < length + 4)
\r
74 return 0; /* RSA key too short! */
\r
76 memmove(data + key->bytes - length, data, length);
\r
80 for (i = 2; i < key->bytes - length - 1; i++) {
\r
82 data[i] = random_byte();
\r
83 } while (data[i] == 0);
\r
85 data[key->bytes - length - 1] = 0;
\r
87 b1 = bignum_from_bytes(data, key->bytes);
\r
89 b2 = modpow(b1, key->exponent, key->modulus);
\r
92 for (i = key->bytes; i--;) {
\r
93 *p++ = bignum_byte(b2, i);
\r
102 static void sha512_mpint(SHA512_State * s, Bignum b)
\r
104 unsigned char lenbuf[4];
\r
106 len = (bignum_bitcount(b) + 8) / 8;
\r
107 PUT_32BIT(lenbuf, len);
\r
108 SHA512_Bytes(s, lenbuf, 4);
\r
109 while (len-- > 0) {
\r
110 lenbuf[0] = bignum_byte(b, len);
\r
111 SHA512_Bytes(s, lenbuf, 1);
\r
113 memset(lenbuf, 0, sizeof(lenbuf));
\r
117 * This function is a wrapper on modpow(). It has the same effect
\r
118 * as modpow(), but employs RSA blinding to protect against timing
\r
121 static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
\r
123 Bignum random, random_encrypted, random_inverse;
\r
124 Bignum input_blinded, ret_blinded;
\r
128 unsigned char digest512[64];
\r
129 int digestused = lenof(digest512);
\r
133 * Start by inventing a random number chosen uniformly from the
\r
134 * range 2..modulus-1. (We do this by preparing a random number
\r
135 * of the right length and retrying if it's greater than the
\r
136 * modulus, to prevent any potential Bleichenbacher-like
\r
137 * attacks making use of the uneven distribution within the
\r
138 * range that would arise from just reducing our number mod n.
\r
139 * There are timing implications to the potential retries, of
\r
140 * course, but all they tell you is the modulus, which you
\r
143 * To preserve determinism and avoid Pageant needing to share
\r
144 * the random number pool, we actually generate this `random'
\r
145 * number by hashing stuff with the private key.
\r
148 int bits, byte, bitsleft, v;
\r
149 random = copybn(key->modulus);
\r
151 * Find the topmost set bit. (This function will return its
\r
152 * index plus one.) Then we'll set all bits from that one
\r
153 * downwards randomly.
\r
155 bits = bignum_bitcount(random);
\r
159 if (bitsleft <= 0) {
\r
162 * Conceptually the following few lines are equivalent to
\r
163 * byte = random_byte();
\r
165 if (digestused >= lenof(digest512)) {
\r
166 unsigned char seqbuf[4];
\r
167 PUT_32BIT(seqbuf, hashseq);
\r
169 SHA512_Bytes(&ss, "RSA deterministic blinding", 26);
\r
170 SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf));
\r
171 sha512_mpint(&ss, key->private_exponent);
\r
172 SHA512_Final(&ss, digest512);
\r
176 * Now hash that digest plus the signature
\r
180 SHA512_Bytes(&ss, digest512, sizeof(digest512));
\r
181 sha512_mpint(&ss, input);
\r
182 SHA512_Final(&ss, digest512);
\r
186 byte = digest512[digestused++];
\r
191 bignum_set_bit(random, bits, v);
\r
195 * Now check that this number is strictly greater than
\r
196 * zero, and strictly less than modulus.
\r
198 if (bignum_cmp(random, Zero) <= 0 ||
\r
199 bignum_cmp(random, key->modulus) >= 0) {
\r
208 * RSA blinding relies on the fact that (xy)^d mod n is equal
\r
209 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
\r
210 * y and y^d; then we multiply x by y, raise to the power d mod
\r
211 * n as usual, and divide by y^d to recover x^d. Thus an
\r
212 * attacker can't correlate the timing of the modpow with the
\r
213 * input, because they don't know anything about the number
\r
214 * that was input to the actual modpow.
\r
216 * The clever bit is that we don't have to do a huge modpow to
\r
217 * get y and y^d; we will use the number we just invented as
\r
218 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
\r
219 * from it, which is much faster to do.
\r
221 random_encrypted = modpow(random, key->exponent, key->modulus);
\r
222 random_inverse = modinv(random, key->modulus);
\r
223 input_blinded = modmul(input, random_encrypted, key->modulus);
\r
224 ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus);
\r
225 ret = modmul(ret_blinded, random_inverse, key->modulus);
\r
227 freebn(ret_blinded);
\r
228 freebn(input_blinded);
\r
229 freebn(random_inverse);
\r
230 freebn(random_encrypted);
\r
236 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
\r
238 return rsa_privkey_op(input, key);
\r
241 int rsastr_len(struct RSAKey *key)
\r
247 ex = key->exponent;
\r
248 mdlen = (bignum_bitcount(md) + 15) / 16;
\r
249 exlen = (bignum_bitcount(ex) + 15) / 16;
\r
250 return 4 * (mdlen + exlen) + 20;
\r
253 void rsastr_fmt(char *str, struct RSAKey *key)
\r
256 int len = 0, i, nibbles;
\r
257 static const char hex[] = "0123456789abcdef";
\r
260 ex = key->exponent;
\r
262 len += sprintf(str + len, "0x");
\r
264 nibbles = (3 + bignum_bitcount(ex)) / 4;
\r
267 for (i = nibbles; i--;)
\r
268 str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
\r
270 len += sprintf(str + len, ",0x");
\r
272 nibbles = (3 + bignum_bitcount(md)) / 4;
\r
275 for (i = nibbles; i--;)
\r
276 str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
\r
282 * Generate a fingerprint string for the key. Compatible with the
\r
283 * OpenSSH fingerprint code.
\r
285 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
\r
287 struct MD5Context md5c;
\r
288 unsigned char digest[16];
\r
289 char buffer[16 * 3 + 40];
\r
290 int numlen, slen, i;
\r
293 numlen = ssh1_bignum_length(key->modulus) - 2;
\r
294 for (i = numlen; i--;) {
\r
295 unsigned char c = bignum_byte(key->modulus, i);
\r
296 MD5Update(&md5c, &c, 1);
\r
298 numlen = ssh1_bignum_length(key->exponent) - 2;
\r
299 for (i = numlen; i--;) {
\r
300 unsigned char c = bignum_byte(key->exponent, i);
\r
301 MD5Update(&md5c, &c, 1);
\r
303 MD5Final(digest, &md5c);
\r
305 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
\r
306 for (i = 0; i < 16; i++)
\r
307 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
\r
309 strncpy(str, buffer, len);
\r
310 str[len - 1] = '\0';
\r
311 slen = strlen(str);
\r
312 if (key->comment && slen < len - 1) {
\r
314 strncpy(str + slen + 1, key->comment, len - slen - 1);
\r
315 str[len - 1] = '\0';
\r
320 * Verify that the public data in an RSA key matches the private
\r
321 * data. We also check the private data itself: we ensure that p >
\r
322 * q and that iqmp really is the inverse of q mod p.
\r
324 int rsa_verify(struct RSAKey *key)
\r
326 Bignum n, ed, pm1, qm1;
\r
329 /* n must equal pq. */
\r
330 n = bigmul(key->p, key->q);
\r
331 cmp = bignum_cmp(n, key->modulus);
\r
336 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
\r
337 pm1 = copybn(key->p);
\r
339 ed = modmul(key->exponent, key->private_exponent, pm1);
\r
340 cmp = bignum_cmp(ed, One);
\r
345 qm1 = copybn(key->q);
\r
347 ed = modmul(key->exponent, key->private_exponent, qm1);
\r
348 cmp = bignum_cmp(ed, One);
\r
356 if (bignum_cmp(key->p, key->q) <= 0)
\r
360 * Ensure iqmp * q is congruent to 1, modulo p.
\r
362 n = modmul(key->iqmp, key->q, key->p);
\r
363 cmp = bignum_cmp(n, One);
\r
371 /* Public key blob as used by Pageant: exponent before modulus. */
\r
372 unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
\r
375 unsigned char *ret;
\r
377 length = (ssh1_bignum_length(key->modulus) +
\r
378 ssh1_bignum_length(key->exponent) + 4);
\r
379 ret = snewn(length, unsigned char);
\r
381 PUT_32BIT(ret, bignum_bitcount(key->modulus));
\r
383 pos += ssh1_write_bignum(ret + pos, key->exponent);
\r
384 pos += ssh1_write_bignum(ret + pos, key->modulus);
\r
390 /* Given a public blob, determine its length. */
\r
391 int rsa_public_blob_len(void *data, int maxlen)
\r
393 unsigned char *p = (unsigned char *)data;
\r
398 p += 4; /* length word */
\r
401 n = ssh1_read_bignum(p, maxlen, NULL); /* exponent */
\r
406 n = ssh1_read_bignum(p, maxlen, NULL); /* modulus */
\r
411 return p - (unsigned char *)data;
\r
414 void freersakey(struct RSAKey *key)
\r
417 freebn(key->modulus);
\r
419 freebn(key->exponent);
\r
420 if (key->private_exponent)
\r
421 freebn(key->private_exponent);
\r
423 sfree(key->comment);
\r
426 /* ----------------------------------------------------------------------
\r
427 * Implementation of the ssh-rsa signing key type.
\r
430 static void getstring(char **data, int *datalen, char **p, int *length)
\r
435 *length = GET_32BIT(*data);
\r
438 if (*datalen < *length)
\r
442 *datalen -= *length;
\r
444 static Bignum getmp(char **data, int *datalen)
\r
450 getstring(data, datalen, &p, &length);
\r
453 b = bignum_from_bytes((unsigned char *)p, length);
\r
457 static void *rsa2_newkey(char *data, int len)
\r
461 struct RSAKey *rsa;
\r
463 rsa = snew(struct RSAKey);
\r
466 getstring(&data, &len, &p, &slen);
\r
468 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
\r
472 rsa->exponent = getmp(&data, &len);
\r
473 rsa->modulus = getmp(&data, &len);
\r
474 rsa->private_exponent = NULL;
\r
475 rsa->comment = NULL;
\r
480 static void rsa2_freekey(void *key)
\r
482 struct RSAKey *rsa = (struct RSAKey *) key;
\r
487 static char *rsa2_fmtkey(void *key)
\r
489 struct RSAKey *rsa = (struct RSAKey *) key;
\r
493 len = rsastr_len(rsa);
\r
494 p = snewn(len, char);
\r
495 rsastr_fmt(p, rsa);
\r
499 static unsigned char *rsa2_public_blob(void *key, int *len)
\r
501 struct RSAKey *rsa = (struct RSAKey *) key;
\r
502 int elen, mlen, bloblen;
\r
504 unsigned char *blob, *p;
\r
506 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
\r
507 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
\r
510 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
\r
511 * (three length fields, 12+7=19).
\r
513 bloblen = 19 + elen + mlen;
\r
514 blob = snewn(bloblen, unsigned char);
\r
518 memcpy(p, "ssh-rsa", 7);
\r
520 PUT_32BIT(p, elen);
\r
522 for (i = elen; i--;)
\r
523 *p++ = bignum_byte(rsa->exponent, i);
\r
524 PUT_32BIT(p, mlen);
\r
526 for (i = mlen; i--;)
\r
527 *p++ = bignum_byte(rsa->modulus, i);
\r
528 assert(p == blob + bloblen);
\r
533 static unsigned char *rsa2_private_blob(void *key, int *len)
\r
535 struct RSAKey *rsa = (struct RSAKey *) key;
\r
536 int dlen, plen, qlen, ulen, bloblen;
\r
538 unsigned char *blob, *p;
\r
540 dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
\r
541 plen = (bignum_bitcount(rsa->p) + 8) / 8;
\r
542 qlen = (bignum_bitcount(rsa->q) + 8) / 8;
\r
543 ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
\r
546 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
\r
549 bloblen = 16 + dlen + plen + qlen + ulen;
\r
550 blob = snewn(bloblen, unsigned char);
\r
552 PUT_32BIT(p, dlen);
\r
554 for (i = dlen; i--;)
\r
555 *p++ = bignum_byte(rsa->private_exponent, i);
\r
556 PUT_32BIT(p, plen);
\r
558 for (i = plen; i--;)
\r
559 *p++ = bignum_byte(rsa->p, i);
\r
560 PUT_32BIT(p, qlen);
\r
562 for (i = qlen; i--;)
\r
563 *p++ = bignum_byte(rsa->q, i);
\r
564 PUT_32BIT(p, ulen);
\r
566 for (i = ulen; i--;)
\r
567 *p++ = bignum_byte(rsa->iqmp, i);
\r
568 assert(p == blob + bloblen);
\r
573 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
\r
574 unsigned char *priv_blob, int priv_len)
\r
576 struct RSAKey *rsa;
\r
577 char *pb = (char *) priv_blob;
\r
579 rsa = rsa2_newkey((char *) pub_blob, pub_len);
\r
580 rsa->private_exponent = getmp(&pb, &priv_len);
\r
581 rsa->p = getmp(&pb, &priv_len);
\r
582 rsa->q = getmp(&pb, &priv_len);
\r
583 rsa->iqmp = getmp(&pb, &priv_len);
\r
585 if (!rsa_verify(rsa)) {
\r
593 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
\r
595 char **b = (char **) blob;
\r
596 struct RSAKey *rsa;
\r
598 rsa = snew(struct RSAKey);
\r
601 rsa->comment = NULL;
\r
603 rsa->modulus = getmp(b, len);
\r
604 rsa->exponent = getmp(b, len);
\r
605 rsa->private_exponent = getmp(b, len);
\r
606 rsa->iqmp = getmp(b, len);
\r
607 rsa->p = getmp(b, len);
\r
608 rsa->q = getmp(b, len);
\r
610 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
\r
611 !rsa->iqmp || !rsa->p || !rsa->q) {
\r
612 sfree(rsa->modulus);
\r
613 sfree(rsa->exponent);
\r
614 sfree(rsa->private_exponent);
\r
625 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
\r
627 struct RSAKey *rsa = (struct RSAKey *) key;
\r
631 ssh2_bignum_length(rsa->modulus) +
\r
632 ssh2_bignum_length(rsa->exponent) +
\r
633 ssh2_bignum_length(rsa->private_exponent) +
\r
634 ssh2_bignum_length(rsa->iqmp) +
\r
635 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
\r
642 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
\r
643 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
\r
645 ENC(rsa->exponent);
\r
646 ENC(rsa->private_exponent);
\r
654 static int rsa2_pubkey_bits(void *blob, int len)
\r
656 struct RSAKey *rsa;
\r
659 rsa = rsa2_newkey((char *) blob, len);
\r
660 ret = bignum_bitcount(rsa->modulus);
\r
666 static char *rsa2_fingerprint(void *key)
\r
668 struct RSAKey *rsa = (struct RSAKey *) key;
\r
669 struct MD5Context md5c;
\r
670 unsigned char digest[16], lenbuf[4];
\r
671 char buffer[16 * 3 + 40];
\r
676 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
\r
678 #define ADD_BIGNUM(bignum) \
\r
679 numlen = (bignum_bitcount(bignum)+8)/8; \
\r
680 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
\r
681 for (i = numlen; i-- ;) { \
\r
682 unsigned char c = bignum_byte(bignum, i); \
\r
683 MD5Update(&md5c, &c, 1); \
\r
685 ADD_BIGNUM(rsa->exponent);
\r
686 ADD_BIGNUM(rsa->modulus);
\r
689 MD5Final(digest, &md5c);
\r
691 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
\r
692 for (i = 0; i < 16; i++)
\r
693 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
\r
695 ret = snewn(strlen(buffer) + 1, char);
\r
697 strcpy(ret, buffer);
\r
702 * This is the magic ASN.1/DER prefix that goes in the decoded
\r
703 * signature, between the string of FFs and the actual SHA hash
\r
704 * value. The meaning of it is:
\r
706 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
\r
708 * 30 21 -- a constructed SEQUENCE of length 0x21
\r
709 * 30 09 -- a constructed sub-SEQUENCE of length 9
\r
710 * 06 05 -- an object identifier, length 5
\r
711 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
\r
712 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
\r
714 * 04 14 -- a primitive OCTET STRING of length 0x14
\r
715 * [0x14 bytes of hash data follows]
\r
717 * The object id in the middle there is listed as `id-sha1' in
\r
718 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
\r
719 * ASN module for PKCS #1) and its expanded form is as follows:
\r
721 * id-sha1 OBJECT IDENTIFIER ::= {
\r
722 * iso(1) identified-organization(3) oiw(14) secsig(3)
\r
723 * algorithms(2) 26 }
\r
725 static const unsigned char asn1_weird_stuff[] = {
\r
726 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
\r
727 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
\r
730 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
\r
732 static int rsa2_verifysig(void *key, char *sig, int siglen,
\r
733 char *data, int datalen)
\r
735 struct RSAKey *rsa = (struct RSAKey *) key;
\r
739 int bytes, i, j, ret;
\r
740 unsigned char hash[20];
\r
742 getstring(&sig, &siglen, &p, &slen);
\r
743 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
\r
746 in = getmp(&sig, &siglen);
\r
747 out = modpow(in, rsa->exponent, rsa->modulus);
\r
752 bytes = (bignum_bitcount(rsa->modulus)+7) / 8;
\r
753 /* Top (partial) byte should be zero. */
\r
754 if (bignum_byte(out, bytes - 1) != 0)
\r
756 /* First whole byte should be 1. */
\r
757 if (bignum_byte(out, bytes - 2) != 1)
\r
759 /* Most of the rest should be FF. */
\r
760 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
\r
761 if (bignum_byte(out, i) != 0xFF)
\r
764 /* Then we expect to see the asn1_weird_stuff. */
\r
765 for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
\r
766 if (bignum_byte(out, i) != asn1_weird_stuff[j])
\r
769 /* Finally, we expect to see the SHA-1 hash of the signed data. */
\r
770 SHA_Simple(data, datalen, hash);
\r
771 for (i = 19, j = 0; i >= 0; i--, j++) {
\r
772 if (bignum_byte(out, i) != hash[j])
\r
780 static unsigned char *rsa2_sign(void *key, char *data, int datalen,
\r
783 struct RSAKey *rsa = (struct RSAKey *) key;
\r
784 unsigned char *bytes;
\r
786 unsigned char hash[20];
\r
790 SHA_Simple(data, datalen, hash);
\r
792 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
\r
793 assert(1 <= nbytes - 20 - ASN1_LEN);
\r
794 bytes = snewn(nbytes, unsigned char);
\r
797 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
\r
799 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
\r
800 bytes[i] = asn1_weird_stuff[j];
\r
801 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
\r
802 bytes[i] = hash[j];
\r
804 in = bignum_from_bytes(bytes, nbytes);
\r
807 out = rsa_privkey_op(in, rsa);
\r
810 nbytes = (bignum_bitcount(out) + 7) / 8;
\r
811 bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
\r
812 PUT_32BIT(bytes, 7);
\r
813 memcpy(bytes + 4, "ssh-rsa", 7);
\r
814 PUT_32BIT(bytes + 4 + 7, nbytes);
\r
815 for (i = 0; i < nbytes; i++)
\r
816 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
\r
819 *siglen = 4 + 7 + 4 + nbytes;
\r
823 const struct ssh_signkey ssh_rsa = {
\r
830 rsa2_openssh_createkey,
\r
831 rsa2_openssh_fmtkey,
\r