2 * RSA implementation just sufficient for ssh client-side
5 * Rewritten for more speed by Joris van Rantwijk, Jun 1999.
16 int makekey(unsigned char *data, struct RSAKey *result,
17 unsigned char **keystr, int order) {
18 unsigned char *p = data;
24 result->bits = (result->bits << 8) + *p++;
29 * order=0 means exponent then modulus (the keys sent by the
30 * server). order=1 means modulus then exponent (the keys
31 * stored in a keyfile).
35 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
37 result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
38 if (keystr) *keystr = p+2;
39 p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
41 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
46 int makeprivate(unsigned char *data, struct RSAKey *result) {
47 return ssh1_read_bignum(data, &result->private_exponent);
50 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key) {
55 memmove(data+key->bytes-length, data, length);
59 for (i = 2; i < key->bytes-length-1; i++) {
61 data[i] = random_byte();
62 } while (data[i] == 0);
64 data[key->bytes-length-1] = 0;
66 b1 = bignum_from_bytes(data, key->bytes);
68 b2 = modpow(b1, key->exponent, key->modulus);
71 for (i=key->bytes; i-- ;) {
72 *p++ = bignum_byte(b2, i);
79 Bignum rsadecrypt(Bignum input, struct RSAKey *key) {
81 ret = modpow(input, key->private_exponent, key->modulus);
85 int rsastr_len(struct RSAKey *key) {
91 mdlen = (ssh1_bignum_bitcount(md)+15) / 16;
92 exlen = (ssh1_bignum_bitcount(ex)+15) / 16;
93 return 4 * (mdlen+exlen) + 20;
96 void rsastr_fmt(char *str, struct RSAKey *key) {
98 int len = 0, i, nibbles;
99 static const char hex[] = "0123456789abcdef";
104 len += sprintf(str+len, "0x");
106 nibbles = (3 + ssh1_bignum_bitcount(ex))/4; if (nibbles<1) nibbles=1;
107 for (i=nibbles; i-- ;)
108 str[len++] = hex[(bignum_byte(ex, i/2) >> (4*(i%2))) & 0xF];
110 len += sprintf(str+len, ",0x");
112 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
113 for (i=nibbles; i-- ;)
114 str[len++] = hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF];
120 * Generate a fingerprint string for the key. Compatible with the
121 * OpenSSH fingerprint code.
123 void rsa_fingerprint(char *str, int len, struct RSAKey *key) {
124 struct MD5Context md5c;
125 unsigned char digest[16];
126 char buffer[16*3+40];
130 numlen = ssh1_bignum_length(key->modulus) - 2;
131 for (i = numlen; i-- ;) {
132 unsigned char c = bignum_byte(key->modulus, i);
133 MD5Update(&md5c, &c, 1);
135 numlen = ssh1_bignum_length(key->exponent) - 2;
136 for (i = numlen; i-- ;) {
137 unsigned char c = bignum_byte(key->exponent, i);
138 MD5Update(&md5c, &c, 1);
140 MD5Final(digest, &md5c);
142 sprintf(buffer, "%d ", ssh1_bignum_bitcount(key->modulus));
143 for (i = 0; i < 16; i++)
144 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
145 strncpy(str, buffer, len); str[len-1] = '\0';
147 if (key->comment && slen < len-1) {
149 strncpy(str+slen+1, key->comment, len-slen-1);
155 * Verify that the public data in an RSA key matches the private
156 * data. We also check the private data itself: we ensure that p >
157 * q and that iqmp really is the inverse of q mod p.
159 int rsa_verify(struct RSAKey *key) {
160 Bignum n, ed, pm1, qm1;
163 /* n must equal pq. */
164 n = bigmul(key->p, key->q);
165 cmp = bignum_cmp(n, key->modulus);
170 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
171 pm1 = copybn(key->p);
173 ed = modmul(key->exponent, key->private_exponent, pm1);
174 cmp = bignum_cmp(ed, One);
179 qm1 = copybn(key->q);
181 ed = modmul(key->exponent, key->private_exponent, qm1);
182 cmp = bignum_cmp(ed, One);
190 if (bignum_cmp(key->p, key->q) <= 0)
194 * Ensure iqmp * q is congruent to 1, modulo p.
196 n = modmul(key->iqmp, key->q, key->p);
197 cmp = bignum_cmp(n, One);
205 void freersakey(struct RSAKey *key) {
206 if (key->modulus) freebn(key->modulus);
207 if (key->exponent) freebn(key->exponent);
208 if (key->private_exponent) freebn(key->private_exponent);
209 if (key->comment) sfree(key->comment);
212 /* ----------------------------------------------------------------------
213 * Implementation of the ssh-rsa signing key type.
216 #define GET_32BIT(cp) \
217 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
218 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
219 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
220 ((unsigned long)(unsigned char)(cp)[3]))
222 #define PUT_32BIT(cp, value) { \
223 (cp)[0] = (unsigned char)((value) >> 24); \
224 (cp)[1] = (unsigned char)((value) >> 16); \
225 (cp)[2] = (unsigned char)((value) >> 8); \
226 (cp)[3] = (unsigned char)(value); }
228 static void getstring(char **data, int *datalen, char **p, int *length) {
232 *length = GET_32BIT(*data);
233 *datalen -= 4; *data += 4;
234 if (*datalen < *length)
237 *data += *length; *datalen -= *length;
239 static Bignum getmp(char **data, int *datalen) {
244 getstring(data, datalen, &p, &length);
247 b = bignum_from_bytes(p, length);
251 static void *rsa2_newkey(char *data, int len) {
256 rsa = smalloc(sizeof(struct RSAKey));
257 if (!rsa) return NULL;
258 getstring(&data, &len, &p, &slen);
260 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
264 rsa->exponent = getmp(&data, &len);
265 rsa->modulus = getmp(&data, &len);
266 rsa->private_exponent = NULL;
272 static void rsa2_freekey(void *key) {
273 struct RSAKey *rsa = (struct RSAKey *)key;
278 static char *rsa2_fmtkey(void *key) {
279 struct RSAKey *rsa = (struct RSAKey *)key;
283 len = rsastr_len(rsa);
289 static unsigned char *rsa2_public_blob(void *key, int *len) {
290 struct RSAKey *rsa = (struct RSAKey *)key;
291 int elen, mlen, bloblen;
293 unsigned char *blob, *p;
295 elen = (ssh1_bignum_bitcount(rsa->exponent)+8)/8;
296 mlen = (ssh1_bignum_bitcount(rsa->modulus)+8)/8;
299 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
300 * (three length fields, 12+7=19).
302 bloblen = 19+elen+mlen;
303 blob = smalloc(bloblen);
305 PUT_32BIT(p, 7); p += 4;
306 memcpy(p, "ssh-rsa", 7); p += 7;
307 PUT_32BIT(p, elen); p += 4;
308 for (i = elen; i-- ;) *p++ = bignum_byte(rsa->exponent, i);
309 PUT_32BIT(p, mlen); p += 4;
310 for (i = mlen; i-- ;) *p++ = bignum_byte(rsa->modulus, i);
311 assert(p == blob + bloblen);
316 static unsigned char *rsa2_private_blob(void *key, int *len) {
317 struct RSAKey *rsa = (struct RSAKey *)key;
318 int dlen, plen, qlen, ulen, bloblen;
320 unsigned char *blob, *p;
322 dlen = (ssh1_bignum_bitcount(rsa->private_exponent)+8)/8;
323 plen = (ssh1_bignum_bitcount(rsa->p)+8)/8;
324 qlen = (ssh1_bignum_bitcount(rsa->q)+8)/8;
325 ulen = (ssh1_bignum_bitcount(rsa->iqmp)+8)/8;
328 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
331 bloblen = 16+dlen+plen+qlen+ulen;
332 blob = smalloc(bloblen);
334 PUT_32BIT(p, dlen); p += 4;
335 for (i = dlen; i-- ;) *p++ = bignum_byte(rsa->private_exponent, i);
336 PUT_32BIT(p, plen); p += 4;
337 for (i = plen; i-- ;) *p++ = bignum_byte(rsa->p, i);
338 PUT_32BIT(p, qlen); p += 4;
339 for (i = qlen; i-- ;) *p++ = bignum_byte(rsa->q, i);
340 PUT_32BIT(p, ulen); p += 4;
341 for (i = ulen; i-- ;) *p++ = bignum_byte(rsa->iqmp, i);
342 assert(p == blob + bloblen);
347 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
348 unsigned char *priv_blob, int priv_len) {
350 char *pb = (char *)priv_blob;
352 rsa = rsa2_newkey((char *)pub_blob, pub_len);
353 rsa->private_exponent = getmp(&pb, &priv_len);
354 rsa->p = getmp(&pb, &priv_len);
355 rsa->q = getmp(&pb, &priv_len);
356 rsa->iqmp = getmp(&pb, &priv_len);
358 if (!rsa_verify(rsa)) {
366 static void *rsa2_openssh_createkey(unsigned char **blob, int *len) {
367 char **b = (char **)blob;
370 rsa = smalloc(sizeof(struct RSAKey));
371 if (!rsa) return NULL;
374 rsa->modulus = getmp(b, len);
375 rsa->exponent = getmp(b, len);
376 rsa->private_exponent = getmp(b, len);
377 rsa->iqmp = getmp(b, len);
378 rsa->p = getmp(b, len);
379 rsa->q = getmp(b, len);
381 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
382 !rsa->iqmp || !rsa->p || !rsa->q) {
384 sfree(rsa->exponent);
385 sfree(rsa->private_exponent);
396 static char *rsa2_fingerprint(void *key) {
397 struct RSAKey *rsa = (struct RSAKey *)key;
398 struct MD5Context md5c;
399 unsigned char digest[16], lenbuf[4];
400 char buffer[16*3+40];
405 MD5Update(&md5c, "\0\0\0\7ssh-rsa", 11);
407 #define ADD_BIGNUM(bignum) \
408 numlen = (ssh1_bignum_bitcount(bignum)+8)/8; \
409 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
410 for (i = numlen; i-- ;) { \
411 unsigned char c = bignum_byte(bignum, i); \
412 MD5Update(&md5c, &c, 1); \
414 ADD_BIGNUM(rsa->exponent);
415 ADD_BIGNUM(rsa->modulus);
418 MD5Final(digest, &md5c);
420 sprintf(buffer, "ssh-rsa %d ", ssh1_bignum_bitcount(rsa->modulus));
421 for (i = 0; i < 16; i++)
422 sprintf(buffer+strlen(buffer), "%s%02x", i?":":"", digest[i]);
423 ret = smalloc(strlen(buffer)+1);
430 * This is the magic ASN.1/DER prefix that goes in the decoded
431 * signature, between the string of FFs and the actual SHA hash
432 * value. The meaning of it is:
434 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
436 * 30 21 -- a constructed SEQUENCE of length 0x21
437 * 30 09 -- a constructed sub-SEQUENCE of length 9
438 * 06 05 -- an object identifier, length 5
439 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
440 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
442 * 04 14 -- a primitive OCTET STRING of length 0x14
443 * [0x14 bytes of hash data follows]
445 * The object id in the middle there is listed as `id-sha1' in
446 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
447 * ASN module for PKCS #1) and its expanded form is as follows:
449 * id-sha1 OBJECT IDENTIFIER ::= {
450 * iso(1) identified-organization(3) oiw(14) secsig(3)
453 static unsigned char asn1_weird_stuff[] = {
454 0x00,0x30,0x21,0x30,0x09,0x06,0x05,0x2B,
455 0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14,
458 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
460 static int rsa2_verifysig(void *key, char *sig, int siglen,
461 char *data, int datalen) {
462 struct RSAKey *rsa = (struct RSAKey *)key;
466 int bytes, i, j, ret;
467 unsigned char hash[20];
469 getstring(&sig, &siglen, &p, &slen);
470 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
473 in = getmp(&sig, &siglen);
474 out = modpow(in, rsa->exponent, rsa->modulus);
479 bytes = ssh1_bignum_bitcount(rsa->modulus) / 8;
480 /* Top (partial) byte should be zero. */
481 if (bignum_byte(out, bytes-1) != 0)
483 /* First whole byte should be 1. */
484 if (bignum_byte(out, bytes-2) != 1)
486 /* Most of the rest should be FF. */
487 for (i = bytes-3; i >= 20 + ASN1_LEN; i--) {
488 if (bignum_byte(out, i) != 0xFF)
491 /* Then we expect to see the asn1_weird_stuff. */
492 for (i = 20 + ASN1_LEN - 1, j=0; i >= 20; i--,j++) {
493 if (bignum_byte(out, i) != asn1_weird_stuff[j])
496 /* Finally, we expect to see the SHA-1 hash of the signed data. */
497 SHA_Simple(data, datalen, hash);
498 for (i = 19, j=0; i >= 0; i--,j++) {
499 if (bignum_byte(out, i) != hash[j])
506 unsigned char *rsa2_sign(void *key, char *data, int datalen, int *siglen) {
507 struct RSAKey *rsa = (struct RSAKey *)key;
508 unsigned char *bytes;
510 unsigned char hash[20];
514 SHA_Simple(data, datalen, hash);
516 nbytes = (ssh1_bignum_bitcount(rsa->modulus)-1) / 8;
517 bytes = smalloc(nbytes);
520 for (i = 1; i < nbytes-20-ASN1_LEN; i++)
522 for (i = nbytes-20-ASN1_LEN, j=0; i < nbytes-20; i++,j++)
523 bytes[i] = asn1_weird_stuff[j];
524 for (i = nbytes-20, j=0; i < nbytes; i++,j++)
527 in = bignum_from_bytes(bytes, nbytes);
530 out = modpow(in, rsa->private_exponent, rsa->modulus);
533 nbytes = (ssh1_bignum_bitcount(out)+7)/8;
534 bytes = smalloc(4+7+4+nbytes);
536 memcpy(bytes+4, "ssh-rsa", 7);
537 PUT_32BIT(bytes+4+7, nbytes);
538 for (i = 0; i < nbytes; i++)
539 bytes[4+7+4+i] = bignum_byte(out, nbytes-1-i);
542 *siglen = 4+7+4+nbytes;
546 const struct ssh_signkey ssh_rsa = {
553 rsa2_openssh_createkey,