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 smemclr(lenbuf, sizeof(lenbuf));
\r
117 * Compute (base ^ exp) % mod, provided mod == p * q, with p,q
\r
118 * distinct primes, and iqmp is the multiplicative inverse of q mod p.
\r
119 * Uses Chinese Remainder Theorem to speed computation up over the
\r
120 * obvious implementation of a single big modpow.
\r
122 Bignum crt_modpow(Bignum base, Bignum exp, Bignum mod,
\r
123 Bignum p, Bignum q, Bignum iqmp)
\r
125 Bignum pm1, qm1, pexp, qexp, presult, qresult, diff, multiplier, ret0, ret;
\r
128 * Reduce the exponent mod phi(p) and phi(q), to save time when
\r
129 * exponentiating mod p and mod q respectively. Of course, since p
\r
130 * and q are prime, phi(p) == p-1 and similarly for q.
\r
136 pexp = bigmod(exp, pm1);
\r
137 qexp = bigmod(exp, qm1);
\r
140 * Do the two modpows.
\r
142 presult = modpow(base, pexp, p);
\r
143 qresult = modpow(base, qexp, q);
\r
146 * Recombine the results. We want a value which is congruent to
\r
147 * qresult mod q, and to presult mod p.
\r
149 * We know that iqmp * q is congruent to 1 * mod p (by definition
\r
150 * of iqmp) and to 0 mod q (obviously). So we start with qresult
\r
151 * (which is congruent to qresult mod both primes), and add on
\r
152 * (presult-qresult) * (iqmp * q) which adjusts it to be congruent
\r
153 * to presult mod p without affecting its value mod q.
\r
155 if (bignum_cmp(presult, qresult) < 0) {
\r
157 * Can't subtract presult from qresult without first adding on
\r
160 Bignum tmp = presult;
\r
161 presult = bigadd(presult, p);
\r
164 diff = bigsub(presult, qresult);
\r
165 multiplier = bigmul(iqmp, q);
\r
166 ret0 = bigmuladd(multiplier, diff, qresult);
\r
169 * Finally, reduce the result mod n.
\r
171 ret = bigmod(ret0, mod);
\r
174 * Free all the intermediate results before returning.
\r
183 freebn(multiplier);
\r
190 * This function is a wrapper on modpow(). It has the same effect as
\r
191 * modpow(), but employs RSA blinding to protect against timing
\r
192 * attacks and also uses the Chinese Remainder Theorem (implemented
\r
193 * above, in crt_modpow()) to speed up the main operation.
\r
195 static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
\r
197 Bignum random, random_encrypted, random_inverse;
\r
198 Bignum input_blinded, ret_blinded;
\r
202 unsigned char digest512[64];
\r
203 int digestused = lenof(digest512);
\r
207 * Start by inventing a random number chosen uniformly from the
\r
208 * range 2..modulus-1. (We do this by preparing a random number
\r
209 * of the right length and retrying if it's greater than the
\r
210 * modulus, to prevent any potential Bleichenbacher-like
\r
211 * attacks making use of the uneven distribution within the
\r
212 * range that would arise from just reducing our number mod n.
\r
213 * There are timing implications to the potential retries, of
\r
214 * course, but all they tell you is the modulus, which you
\r
217 * To preserve determinism and avoid Pageant needing to share
\r
218 * the random number pool, we actually generate this `random'
\r
219 * number by hashing stuff with the private key.
\r
222 int bits, byte, bitsleft, v;
\r
223 random = copybn(key->modulus);
\r
225 * Find the topmost set bit. (This function will return its
\r
226 * index plus one.) Then we'll set all bits from that one
\r
227 * downwards randomly.
\r
229 bits = bignum_bitcount(random);
\r
233 if (bitsleft <= 0) {
\r
236 * Conceptually the following few lines are equivalent to
\r
237 * byte = random_byte();
\r
239 if (digestused >= lenof(digest512)) {
\r
240 unsigned char seqbuf[4];
\r
241 PUT_32BIT(seqbuf, hashseq);
\r
243 SHA512_Bytes(&ss, "RSA deterministic blinding", 26);
\r
244 SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf));
\r
245 sha512_mpint(&ss, key->private_exponent);
\r
246 SHA512_Final(&ss, digest512);
\r
250 * Now hash that digest plus the signature
\r
254 SHA512_Bytes(&ss, digest512, sizeof(digest512));
\r
255 sha512_mpint(&ss, input);
\r
256 SHA512_Final(&ss, digest512);
\r
260 byte = digest512[digestused++];
\r
265 bignum_set_bit(random, bits, v);
\r
267 bn_restore_invariant(random);
\r
270 * Now check that this number is strictly greater than
\r
271 * zero, and strictly less than modulus.
\r
273 if (bignum_cmp(random, Zero) <= 0 ||
\r
274 bignum_cmp(random, key->modulus) >= 0) {
\r
280 * Also, make sure it has an inverse mod modulus.
\r
282 random_inverse = modinv(random, key->modulus);
\r
283 if (!random_inverse) {
\r
292 * RSA blinding relies on the fact that (xy)^d mod n is equal
\r
293 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
\r
294 * y and y^d; then we multiply x by y, raise to the power d mod
\r
295 * n as usual, and divide by y^d to recover x^d. Thus an
\r
296 * attacker can't correlate the timing of the modpow with the
\r
297 * input, because they don't know anything about the number
\r
298 * that was input to the actual modpow.
\r
300 * The clever bit is that we don't have to do a huge modpow to
\r
301 * get y and y^d; we will use the number we just invented as
\r
302 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
\r
303 * from it, which is much faster to do.
\r
305 random_encrypted = crt_modpow(random, key->exponent,
\r
306 key->modulus, key->p, key->q, key->iqmp);
\r
307 input_blinded = modmul(input, random_encrypted, key->modulus);
\r
308 ret_blinded = crt_modpow(input_blinded, key->private_exponent,
\r
309 key->modulus, key->p, key->q, key->iqmp);
\r
310 ret = modmul(ret_blinded, random_inverse, key->modulus);
\r
312 freebn(ret_blinded);
\r
313 freebn(input_blinded);
\r
314 freebn(random_inverse);
\r
315 freebn(random_encrypted);
\r
321 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
\r
323 return rsa_privkey_op(input, key);
\r
326 int rsastr_len(struct RSAKey *key)
\r
332 ex = key->exponent;
\r
333 mdlen = (bignum_bitcount(md) + 15) / 16;
\r
334 exlen = (bignum_bitcount(ex) + 15) / 16;
\r
335 return 4 * (mdlen + exlen) + 20;
\r
338 void rsastr_fmt(char *str, struct RSAKey *key)
\r
341 int len = 0, i, nibbles;
\r
342 static const char hex[] = "0123456789abcdef";
\r
345 ex = key->exponent;
\r
347 len += sprintf(str + len, "0x");
\r
349 nibbles = (3 + bignum_bitcount(ex)) / 4;
\r
352 for (i = nibbles; i--;)
\r
353 str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
\r
355 len += sprintf(str + len, ",0x");
\r
357 nibbles = (3 + bignum_bitcount(md)) / 4;
\r
360 for (i = nibbles; i--;)
\r
361 str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
\r
367 * Generate a fingerprint string for the key. Compatible with the
\r
368 * OpenSSH fingerprint code.
\r
370 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
\r
372 struct MD5Context md5c;
\r
373 unsigned char digest[16];
\r
374 char buffer[16 * 3 + 40];
\r
375 int numlen, slen, i;
\r
378 numlen = ssh1_bignum_length(key->modulus) - 2;
\r
379 for (i = numlen; i--;) {
\r
380 unsigned char c = bignum_byte(key->modulus, i);
\r
381 MD5Update(&md5c, &c, 1);
\r
383 numlen = ssh1_bignum_length(key->exponent) - 2;
\r
384 for (i = numlen; i--;) {
\r
385 unsigned char c = bignum_byte(key->exponent, i);
\r
386 MD5Update(&md5c, &c, 1);
\r
388 MD5Final(digest, &md5c);
\r
390 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
\r
391 for (i = 0; i < 16; i++)
\r
392 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
\r
394 strncpy(str, buffer, len);
\r
395 str[len - 1] = '\0';
\r
396 slen = strlen(str);
\r
397 if (key->comment && slen < len - 1) {
\r
399 strncpy(str + slen + 1, key->comment, len - slen - 1);
\r
400 str[len - 1] = '\0';
\r
405 * Verify that the public data in an RSA key matches the private
\r
406 * data. We also check the private data itself: we ensure that p >
\r
407 * q and that iqmp really is the inverse of q mod p.
\r
409 int rsa_verify(struct RSAKey *key)
\r
411 Bignum n, ed, pm1, qm1;
\r
414 /* n must equal pq. */
\r
415 n = bigmul(key->p, key->q);
\r
416 cmp = bignum_cmp(n, key->modulus);
\r
421 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
\r
422 pm1 = copybn(key->p);
\r
424 ed = modmul(key->exponent, key->private_exponent, pm1);
\r
426 cmp = bignum_cmp(ed, One);
\r
431 qm1 = copybn(key->q);
\r
433 ed = modmul(key->exponent, key->private_exponent, qm1);
\r
435 cmp = bignum_cmp(ed, One);
\r
443 * I have seen key blobs in the wild which were generated with
\r
444 * p < q, so instead of rejecting the key in this case we
\r
445 * should instead flip them round into the canonical order of
\r
446 * p > q. This also involves regenerating iqmp.
\r
448 if (bignum_cmp(key->p, key->q) <= 0) {
\r
449 Bignum tmp = key->p;
\r
454 key->iqmp = modinv(key->q, key->p);
\r
460 * Ensure iqmp * q is congruent to 1, modulo p.
\r
462 n = modmul(key->iqmp, key->q, key->p);
\r
463 cmp = bignum_cmp(n, One);
\r
471 /* Public key blob as used by Pageant: exponent before modulus. */
\r
472 unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
\r
475 unsigned char *ret;
\r
477 length = (ssh1_bignum_length(key->modulus) +
\r
478 ssh1_bignum_length(key->exponent) + 4);
\r
479 ret = snewn(length, unsigned char);
\r
481 PUT_32BIT(ret, bignum_bitcount(key->modulus));
\r
483 pos += ssh1_write_bignum(ret + pos, key->exponent);
\r
484 pos += ssh1_write_bignum(ret + pos, key->modulus);
\r
490 /* Given a public blob, determine its length. */
\r
491 int rsa_public_blob_len(void *data, int maxlen)
\r
493 unsigned char *p = (unsigned char *)data;
\r
498 p += 4; /* length word */
\r
501 n = ssh1_read_bignum(p, maxlen, NULL); /* exponent */
\r
506 n = ssh1_read_bignum(p, maxlen, NULL); /* modulus */
\r
511 return p - (unsigned char *)data;
\r
514 void freersakey(struct RSAKey *key)
\r
517 freebn(key->modulus);
\r
519 freebn(key->exponent);
\r
520 if (key->private_exponent)
\r
521 freebn(key->private_exponent);
\r
529 sfree(key->comment);
\r
532 /* ----------------------------------------------------------------------
\r
533 * Implementation of the ssh-rsa signing key type.
\r
536 static void getstring(char **data, int *datalen, char **p, int *length)
\r
541 *length = toint(GET_32BIT(*data));
\r
546 if (*datalen < *length)
\r
550 *datalen -= *length;
\r
552 static Bignum getmp(char **data, int *datalen)
\r
558 getstring(data, datalen, &p, &length);
\r
561 b = bignum_from_bytes((unsigned char *)p, length);
\r
565 static void rsa2_freekey(void *key); /* forward reference */
\r
567 static void *rsa2_newkey(char *data, int len)
\r
571 struct RSAKey *rsa;
\r
573 rsa = snew(struct RSAKey);
\r
574 getstring(&data, &len, &p, &slen);
\r
576 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
\r
580 rsa->exponent = getmp(&data, &len);
\r
581 rsa->modulus = getmp(&data, &len);
\r
582 rsa->private_exponent = NULL;
\r
583 rsa->p = rsa->q = rsa->iqmp = NULL;
\r
584 rsa->comment = NULL;
\r
586 if (!rsa->exponent || !rsa->modulus) {
\r
594 static void rsa2_freekey(void *key)
\r
596 struct RSAKey *rsa = (struct RSAKey *) key;
\r
601 static char *rsa2_fmtkey(void *key)
\r
603 struct RSAKey *rsa = (struct RSAKey *) key;
\r
607 len = rsastr_len(rsa);
\r
608 p = snewn(len, char);
\r
609 rsastr_fmt(p, rsa);
\r
613 static unsigned char *rsa2_public_blob(void *key, int *len)
\r
615 struct RSAKey *rsa = (struct RSAKey *) key;
\r
616 int elen, mlen, bloblen;
\r
618 unsigned char *blob, *p;
\r
620 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
\r
621 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
\r
624 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
\r
625 * (three length fields, 12+7=19).
\r
627 bloblen = 19 + elen + mlen;
\r
628 blob = snewn(bloblen, unsigned char);
\r
632 memcpy(p, "ssh-rsa", 7);
\r
634 PUT_32BIT(p, elen);
\r
636 for (i = elen; i--;)
\r
637 *p++ = bignum_byte(rsa->exponent, i);
\r
638 PUT_32BIT(p, mlen);
\r
640 for (i = mlen; i--;)
\r
641 *p++ = bignum_byte(rsa->modulus, i);
\r
642 assert(p == blob + bloblen);
\r
647 static unsigned char *rsa2_private_blob(void *key, int *len)
\r
649 struct RSAKey *rsa = (struct RSAKey *) key;
\r
650 int dlen, plen, qlen, ulen, bloblen;
\r
652 unsigned char *blob, *p;
\r
654 dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
\r
655 plen = (bignum_bitcount(rsa->p) + 8) / 8;
\r
656 qlen = (bignum_bitcount(rsa->q) + 8) / 8;
\r
657 ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
\r
660 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
\r
663 bloblen = 16 + dlen + plen + qlen + ulen;
\r
664 blob = snewn(bloblen, unsigned char);
\r
666 PUT_32BIT(p, dlen);
\r
668 for (i = dlen; i--;)
\r
669 *p++ = bignum_byte(rsa->private_exponent, i);
\r
670 PUT_32BIT(p, plen);
\r
672 for (i = plen; i--;)
\r
673 *p++ = bignum_byte(rsa->p, i);
\r
674 PUT_32BIT(p, qlen);
\r
676 for (i = qlen; i--;)
\r
677 *p++ = bignum_byte(rsa->q, i);
\r
678 PUT_32BIT(p, ulen);
\r
680 for (i = ulen; i--;)
\r
681 *p++ = bignum_byte(rsa->iqmp, i);
\r
682 assert(p == blob + bloblen);
\r
687 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
\r
688 unsigned char *priv_blob, int priv_len)
\r
690 struct RSAKey *rsa;
\r
691 char *pb = (char *) priv_blob;
\r
693 rsa = rsa2_newkey((char *) pub_blob, pub_len);
\r
694 rsa->private_exponent = getmp(&pb, &priv_len);
\r
695 rsa->p = getmp(&pb, &priv_len);
\r
696 rsa->q = getmp(&pb, &priv_len);
\r
697 rsa->iqmp = getmp(&pb, &priv_len);
\r
699 if (!rsa_verify(rsa)) {
\r
707 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
\r
709 char **b = (char **) blob;
\r
710 struct RSAKey *rsa;
\r
712 rsa = snew(struct RSAKey);
\r
713 rsa->comment = NULL;
\r
715 rsa->modulus = getmp(b, len);
\r
716 rsa->exponent = getmp(b, len);
\r
717 rsa->private_exponent = getmp(b, len);
\r
718 rsa->iqmp = getmp(b, len);
\r
719 rsa->p = getmp(b, len);
\r
720 rsa->q = getmp(b, len);
\r
722 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
\r
723 !rsa->iqmp || !rsa->p || !rsa->q) {
\r
728 if (!rsa_verify(rsa)) {
\r
736 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
\r
738 struct RSAKey *rsa = (struct RSAKey *) key;
\r
742 ssh2_bignum_length(rsa->modulus) +
\r
743 ssh2_bignum_length(rsa->exponent) +
\r
744 ssh2_bignum_length(rsa->private_exponent) +
\r
745 ssh2_bignum_length(rsa->iqmp) +
\r
746 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
\r
753 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
\r
754 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
\r
756 ENC(rsa->exponent);
\r
757 ENC(rsa->private_exponent);
\r
765 static int rsa2_pubkey_bits(void *blob, int len)
\r
767 struct RSAKey *rsa;
\r
770 rsa = rsa2_newkey((char *) blob, len);
\r
773 ret = bignum_bitcount(rsa->modulus);
\r
779 static char *rsa2_fingerprint(void *key)
\r
781 struct RSAKey *rsa = (struct RSAKey *) key;
\r
782 struct MD5Context md5c;
\r
783 unsigned char digest[16], lenbuf[4];
\r
784 char buffer[16 * 3 + 40];
\r
789 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
\r
791 #define ADD_BIGNUM(bignum) \
\r
792 numlen = (bignum_bitcount(bignum)+8)/8; \
\r
793 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
\r
794 for (i = numlen; i-- ;) { \
\r
795 unsigned char c = bignum_byte(bignum, i); \
\r
796 MD5Update(&md5c, &c, 1); \
\r
798 ADD_BIGNUM(rsa->exponent);
\r
799 ADD_BIGNUM(rsa->modulus);
\r
802 MD5Final(digest, &md5c);
\r
804 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
\r
805 for (i = 0; i < 16; i++)
\r
806 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
\r
808 ret = snewn(strlen(buffer) + 1, char);
\r
810 strcpy(ret, buffer);
\r
815 * This is the magic ASN.1/DER prefix that goes in the decoded
\r
816 * signature, between the string of FFs and the actual SHA hash
\r
817 * value. The meaning of it is:
\r
819 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
\r
821 * 30 21 -- a constructed SEQUENCE of length 0x21
\r
822 * 30 09 -- a constructed sub-SEQUENCE of length 9
\r
823 * 06 05 -- an object identifier, length 5
\r
824 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
\r
825 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
\r
827 * 04 14 -- a primitive OCTET STRING of length 0x14
\r
828 * [0x14 bytes of hash data follows]
\r
830 * The object id in the middle there is listed as `id-sha1' in
\r
831 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
\r
832 * ASN module for PKCS #1) and its expanded form is as follows:
\r
834 * id-sha1 OBJECT IDENTIFIER ::= {
\r
835 * iso(1) identified-organization(3) oiw(14) secsig(3)
\r
836 * algorithms(2) 26 }
\r
838 static const unsigned char asn1_weird_stuff[] = {
\r
839 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
\r
840 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
\r
843 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
\r
845 static int rsa2_verifysig(void *key, char *sig, int siglen,
\r
846 char *data, int datalen)
\r
848 struct RSAKey *rsa = (struct RSAKey *) key;
\r
852 int bytes, i, j, ret;
\r
853 unsigned char hash[20];
\r
855 getstring(&sig, &siglen, &p, &slen);
\r
856 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
\r
859 in = getmp(&sig, &siglen);
\r
862 out = modpow(in, rsa->exponent, rsa->modulus);
\r
867 bytes = (bignum_bitcount(rsa->modulus)+7) / 8;
\r
868 /* Top (partial) byte should be zero. */
\r
869 if (bignum_byte(out, bytes - 1) != 0)
\r
871 /* First whole byte should be 1. */
\r
872 if (bignum_byte(out, bytes - 2) != 1)
\r
874 /* Most of the rest should be FF. */
\r
875 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
\r
876 if (bignum_byte(out, i) != 0xFF)
\r
879 /* Then we expect to see the asn1_weird_stuff. */
\r
880 for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
\r
881 if (bignum_byte(out, i) != asn1_weird_stuff[j])
\r
884 /* Finally, we expect to see the SHA-1 hash of the signed data. */
\r
885 SHA_Simple(data, datalen, hash);
\r
886 for (i = 19, j = 0; i >= 0; i--, j++) {
\r
887 if (bignum_byte(out, i) != hash[j])
\r
895 static unsigned char *rsa2_sign(void *key, char *data, int datalen,
\r
898 struct RSAKey *rsa = (struct RSAKey *) key;
\r
899 unsigned char *bytes;
\r
901 unsigned char hash[20];
\r
905 SHA_Simple(data, datalen, hash);
\r
907 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
\r
908 assert(1 <= nbytes - 20 - ASN1_LEN);
\r
909 bytes = snewn(nbytes, unsigned char);
\r
912 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
\r
914 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
\r
915 bytes[i] = asn1_weird_stuff[j];
\r
916 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
\r
917 bytes[i] = hash[j];
\r
919 in = bignum_from_bytes(bytes, nbytes);
\r
922 out = rsa_privkey_op(in, rsa);
\r
925 nbytes = (bignum_bitcount(out) + 7) / 8;
\r
926 bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
\r
927 PUT_32BIT(bytes, 7);
\r
928 memcpy(bytes + 4, "ssh-rsa", 7);
\r
929 PUT_32BIT(bytes + 4 + 7, nbytes);
\r
930 for (i = 0; i < nbytes; i++)
\r
931 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
\r
934 *siglen = 4 + 7 + 4 + nbytes;
\r
938 const struct ssh_signkey ssh_rsa = {
\r
945 rsa2_openssh_createkey,
\r
946 rsa2_openssh_fmtkey,
\r
955 void *ssh_rsakex_newkey(char *data, int len)
\r
957 return rsa2_newkey(data, len);
\r
960 void ssh_rsakex_freekey(void *key)
\r
965 int ssh_rsakex_klen(void *key)
\r
967 struct RSAKey *rsa = (struct RSAKey *) key;
\r
969 return bignum_bitcount(rsa->modulus);
\r
972 static void oaep_mask(const struct ssh_hash *h, void *seed, int seedlen,
\r
973 void *vdata, int datalen)
\r
975 unsigned char *data = (unsigned char *)vdata;
\r
976 unsigned count = 0;
\r
978 while (datalen > 0) {
\r
979 int i, max = (datalen > h->hlen ? h->hlen : datalen);
\r
981 unsigned char counter[4], hash[SSH2_KEX_MAX_HASH_LEN];
\r
983 assert(h->hlen <= SSH2_KEX_MAX_HASH_LEN);
\r
984 PUT_32BIT(counter, count);
\r
986 h->bytes(s, seed, seedlen);
\r
987 h->bytes(s, counter, 4);
\r
991 for (i = 0; i < max; i++)
\r
992 data[i] ^= hash[i];
\r
999 void ssh_rsakex_encrypt(const struct ssh_hash *h, unsigned char *in, int inlen,
\r
1000 unsigned char *out, int outlen,
\r
1004 struct RSAKey *rsa = (struct RSAKey *) key;
\r
1007 const int HLEN = h->hlen;
\r
1010 * Here we encrypt using RSAES-OAEP. Essentially this means:
\r
1012 * - we have a SHA-based `mask generation function' which
\r
1013 * creates a pseudo-random stream of mask data
\r
1014 * deterministically from an input chunk of data.
\r
1016 * - we have a random chunk of data called a seed.
\r
1018 * - we use the seed to generate a mask which we XOR with our
\r
1021 * - then we use _the masked plaintext_ to generate a mask
\r
1022 * which we XOR with the seed.
\r
1024 * - then we concatenate the masked seed and the masked
\r
1025 * plaintext, and RSA-encrypt that lot.
\r
1027 * The result is that the data input to the encryption function
\r
1028 * is random-looking and (hopefully) contains no exploitable
\r
1029 * structure such as PKCS1-v1_5 does.
\r
1031 * For a precise specification, see RFC 3447, section 7.1.1.
\r
1032 * Some of the variable names below are derived from that, so
\r
1033 * it'd probably help to read it anyway.
\r
1036 /* k denotes the length in octets of the RSA modulus. */
\r
1037 k = (7 + bignum_bitcount(rsa->modulus)) / 8;
\r
1039 /* The length of the input data must be at most k - 2hLen - 2. */
\r
1040 assert(inlen > 0 && inlen <= k - 2*HLEN - 2);
\r
1042 /* The length of the output data wants to be precisely k. */
\r
1043 assert(outlen == k);
\r
1046 * Now perform EME-OAEP encoding. First set up all the unmasked
\r
1049 /* Leading byte zero. */
\r
1051 /* At position 1, the seed: HLEN bytes of random data. */
\r
1052 for (i = 0; i < HLEN; i++)
\r
1053 out[i + 1] = random_byte();
\r
1054 /* At position 1+HLEN, the data block DB, consisting of: */
\r
1055 /* The hash of the label (we only support an empty label here) */
\r
1056 h->final(h->init(), out + HLEN + 1);
\r
1057 /* A bunch of zero octets */
\r
1058 memset(out + 2*HLEN + 1, 0, outlen - (2*HLEN + 1));
\r
1059 /* A single 1 octet, followed by the input message data. */
\r
1060 out[outlen - inlen - 1] = 1;
\r
1061 memcpy(out + outlen - inlen, in, inlen);
\r
1064 * Now use the seed data to mask the block DB.
\r
1066 oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
\r
1069 * And now use the masked DB to mask the seed itself.
\r
1071 oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
\r
1074 * Now `out' contains precisely the data we want to
\r
1077 b1 = bignum_from_bytes(out, outlen);
\r
1078 b2 = modpow(b1, rsa->exponent, rsa->modulus);
\r
1080 for (i = outlen; i--;) {
\r
1081 *p++ = bignum_byte(b2, i);
\r
1091 static const struct ssh_kex ssh_rsa_kex_sha1 = {
\r
1092 "rsa1024-sha1", NULL, KEXTYPE_RSA, NULL, NULL, 0, 0, &ssh_sha1
\r
1095 static const struct ssh_kex ssh_rsa_kex_sha256 = {
\r
1096 "rsa2048-sha256", NULL, KEXTYPE_RSA, NULL, NULL, 0, 0, &ssh_sha256
\r
1099 static const struct ssh_kex *const rsa_kex_list[] = {
\r
1100 &ssh_rsa_kex_sha256,
\r
1104 const struct ssh_kexes ssh_rsa_kex = {
\r
1105 sizeof(rsa_kex_list) / sizeof(*rsa_kex_list),
\r