]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshrsa.c
Implement RSA blinding, to defeat Brumley and Boneh's RSA timing
[PuTTY.git] / sshrsa.c
1 /*
2  * RSA implementation for PuTTY.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9
10 #include "ssh.h"
11 #include "misc.h"
12
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]))
18
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); }
24
25 int makekey(unsigned char *data, struct RSAKey *result,
26             unsigned char **keystr, int order)
27 {
28     unsigned char *p = data;
29     int i;
30
31     if (result) {
32         result->bits = 0;
33         for (i = 0; i < 4; i++)
34             result->bits = (result->bits << 8) + *p++;
35     } else
36         p += 4;
37
38     /*
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).
42      */
43
44     if (order == 0)
45         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
46     if (result)
47         result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
48     if (keystr)
49         *keystr = p + 2;
50     p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
51     if (order == 1)
52         p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
53
54     return p - data;
55 }
56
57 int makeprivate(unsigned char *data, struct RSAKey *result)
58 {
59     return ssh1_read_bignum(data, &result->private_exponent);
60 }
61
62 void rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
63 {
64     Bignum b1, b2;
65     int i;
66     unsigned char *p;
67
68     memmove(data + key->bytes - length, data, length);
69     data[0] = 0;
70     data[1] = 2;
71
72     for (i = 2; i < key->bytes - length - 1; i++) {
73         do {
74             data[i] = random_byte();
75         } while (data[i] == 0);
76     }
77     data[key->bytes - length - 1] = 0;
78
79     b1 = bignum_from_bytes(data, key->bytes);
80
81     b2 = modpow(b1, key->exponent, key->modulus);
82
83     p = data;
84     for (i = key->bytes; i--;) {
85         *p++ = bignum_byte(b2, i);
86     }
87
88     freebn(b1);
89     freebn(b2);
90 }
91
92 /*
93  * This function is a wrapper on modpow(). It has the same effect
94  * as modpow(), but employs RSA blinding to protect against timing
95  * attacks.
96  */
97 static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
98 {
99     Bignum random, random_encrypted, random_inverse;
100     Bignum input_blinded, ret_blinded;
101     Bignum ret;
102
103     /*
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
112      * already knew.)
113      */
114     while (1) {
115         int bits, byte, bitsleft, v;
116         random = copybn(key->modulus);
117         /*
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.
121          */
122         bits = bignum_bitcount(random);
123         byte = 0;
124         bitsleft = 0;
125         while (bits--) {
126             if (bitsleft <= 0)
127                 bitsleft = 8, byte = random_byte();
128             v = byte & 1;
129             byte >>= 1;
130             bitsleft--;
131             bignum_set_bit(random, bits, v);
132         }
133
134         /*
135          * Now check that this number is strictly greater than
136          * zero, and strictly less than modulus.
137          */
138         if (bignum_cmp(random, Zero) <= 0 ||
139             bignum_cmp(random, key->modulus) >= 0) {
140             freebn(random);
141             continue;
142         } else {
143             break;
144         }
145     }
146
147     /*
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.
154      * 
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.
159      */
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);
165
166     freebn(ret_blinded);
167     freebn(input_blinded);
168     freebn(random_inverse);
169     freebn(random_encrypted);
170     freebn(random);
171
172     return ret;
173 }
174
175 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
176 {
177     return rsa_privkey_op(input, key);
178 }
179
180 int rsastr_len(struct RSAKey *key)
181 {
182     Bignum md, ex;
183     int mdlen, exlen;
184
185     md = key->modulus;
186     ex = key->exponent;
187     mdlen = (bignum_bitcount(md) + 15) / 16;
188     exlen = (bignum_bitcount(ex) + 15) / 16;
189     return 4 * (mdlen + exlen) + 20;
190 }
191
192 void rsastr_fmt(char *str, struct RSAKey *key)
193 {
194     Bignum md, ex;
195     int len = 0, i, nibbles;
196     static const char hex[] = "0123456789abcdef";
197
198     md = key->modulus;
199     ex = key->exponent;
200
201     len += sprintf(str + len, "0x");
202
203     nibbles = (3 + bignum_bitcount(ex)) / 4;
204     if (nibbles < 1)
205         nibbles = 1;
206     for (i = nibbles; i--;)
207         str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
208
209     len += sprintf(str + len, ",0x");
210
211     nibbles = (3 + bignum_bitcount(md)) / 4;
212     if (nibbles < 1)
213         nibbles = 1;
214     for (i = nibbles; i--;)
215         str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
216
217     str[len] = '\0';
218 }
219
220 /*
221  * Generate a fingerprint string for the key. Compatible with the
222  * OpenSSH fingerprint code.
223  */
224 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
225 {
226     struct MD5Context md5c;
227     unsigned char digest[16];
228     char buffer[16 * 3 + 40];
229     int numlen, slen, i;
230
231     MD5Init(&md5c);
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);
236     }
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);
241     }
242     MD5Final(digest, &md5c);
243
244     sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
245     for (i = 0; i < 16; i++)
246         sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
247                 digest[i]);
248     strncpy(str, buffer, len);
249     str[len - 1] = '\0';
250     slen = strlen(str);
251     if (key->comment && slen < len - 1) {
252         str[slen] = ' ';
253         strncpy(str + slen + 1, key->comment, len - slen - 1);
254         str[len - 1] = '\0';
255     }
256 }
257
258 /*
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.
262  */
263 int rsa_verify(struct RSAKey *key)
264 {
265     Bignum n, ed, pm1, qm1;
266     int cmp;
267
268     /* n must equal pq. */
269     n = bigmul(key->p, key->q);
270     cmp = bignum_cmp(n, key->modulus);
271     freebn(n);
272     if (cmp != 0)
273         return 0;
274
275     /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
276     pm1 = copybn(key->p);
277     decbn(pm1);
278     ed = modmul(key->exponent, key->private_exponent, pm1);
279     cmp = bignum_cmp(ed, One);
280     sfree(ed);
281     if (cmp != 0)
282         return 0;
283
284     qm1 = copybn(key->q);
285     decbn(qm1);
286     ed = modmul(key->exponent, key->private_exponent, qm1);
287     cmp = bignum_cmp(ed, One);
288     sfree(ed);
289     if (cmp != 0)
290         return 0;
291
292     /*
293      * Ensure p > q.
294      */
295     if (bignum_cmp(key->p, key->q) <= 0)
296         return 0;
297
298     /*
299      * Ensure iqmp * q is congruent to 1, modulo p.
300      */
301     n = modmul(key->iqmp, key->q, key->p);
302     cmp = bignum_cmp(n, One);
303     sfree(n);
304     if (cmp != 0)
305         return 0;
306
307     return 1;
308 }
309
310 /* Public key blob as used by Pageant: exponent before modulus. */
311 unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
312 {
313     int length, pos;
314     unsigned char *ret;
315
316     length = (ssh1_bignum_length(key->modulus) +
317               ssh1_bignum_length(key->exponent) + 4);
318     ret = smalloc(length);
319
320     PUT_32BIT(ret, bignum_bitcount(key->modulus));
321     pos = 4;
322     pos += ssh1_write_bignum(ret + pos, key->exponent);
323     pos += ssh1_write_bignum(ret + pos, key->modulus);
324
325     *len = length;
326     return ret;
327 }
328
329 /* Given a public blob, determine its length. */
330 int rsa_public_blob_len(void *data)
331 {
332     unsigned char *p = (unsigned char *)data;
333
334     p += 4;                            /* length word */
335     p += ssh1_read_bignum(p, NULL);    /* exponent */
336     p += ssh1_read_bignum(p, NULL);    /* modulus */
337
338     return p - (unsigned char *)data;
339 }
340
341 void freersakey(struct RSAKey *key)
342 {
343     if (key->modulus)
344         freebn(key->modulus);
345     if (key->exponent)
346         freebn(key->exponent);
347     if (key->private_exponent)
348         freebn(key->private_exponent);
349     if (key->comment)
350         sfree(key->comment);
351 }
352
353 /* ----------------------------------------------------------------------
354  * Implementation of the ssh-rsa signing key type. 
355  */
356
357 static void getstring(char **data, int *datalen, char **p, int *length)
358 {
359     *p = NULL;
360     if (*datalen < 4)
361         return;
362     *length = GET_32BIT(*data);
363     *datalen -= 4;
364     *data += 4;
365     if (*datalen < *length)
366         return;
367     *p = *data;
368     *data += *length;
369     *datalen -= *length;
370 }
371 static Bignum getmp(char **data, int *datalen)
372 {
373     char *p;
374     int length;
375     Bignum b;
376
377     getstring(data, datalen, &p, &length);
378     if (!p)
379         return NULL;
380     b = bignum_from_bytes((unsigned char *)p, length);
381     return b;
382 }
383
384 static void *rsa2_newkey(char *data, int len)
385 {
386     char *p;
387     int slen;
388     struct RSAKey *rsa;
389
390     rsa = smalloc(sizeof(struct RSAKey));
391     if (!rsa)
392         return NULL;
393     getstring(&data, &len, &p, &slen);
394
395     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
396         sfree(rsa);
397         return NULL;
398     }
399     rsa->exponent = getmp(&data, &len);
400     rsa->modulus = getmp(&data, &len);
401     rsa->private_exponent = NULL;
402     rsa->comment = NULL;
403
404     return rsa;
405 }
406
407 static void rsa2_freekey(void *key)
408 {
409     struct RSAKey *rsa = (struct RSAKey *) key;
410     freersakey(rsa);
411     sfree(rsa);
412 }
413
414 static char *rsa2_fmtkey(void *key)
415 {
416     struct RSAKey *rsa = (struct RSAKey *) key;
417     char *p;
418     int len;
419
420     len = rsastr_len(rsa);
421     p = smalloc(len);
422     rsastr_fmt(p, rsa);
423     return p;
424 }
425
426 static unsigned char *rsa2_public_blob(void *key, int *len)
427 {
428     struct RSAKey *rsa = (struct RSAKey *) key;
429     int elen, mlen, bloblen;
430     int i;
431     unsigned char *blob, *p;
432
433     elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
434     mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
435
436     /*
437      * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
438      * (three length fields, 12+7=19).
439      */
440     bloblen = 19 + elen + mlen;
441     blob = smalloc(bloblen);
442     p = blob;
443     PUT_32BIT(p, 7);
444     p += 4;
445     memcpy(p, "ssh-rsa", 7);
446     p += 7;
447     PUT_32BIT(p, elen);
448     p += 4;
449     for (i = elen; i--;)
450         *p++ = bignum_byte(rsa->exponent, i);
451     PUT_32BIT(p, mlen);
452     p += 4;
453     for (i = mlen; i--;)
454         *p++ = bignum_byte(rsa->modulus, i);
455     assert(p == blob + bloblen);
456     *len = bloblen;
457     return blob;
458 }
459
460 static unsigned char *rsa2_private_blob(void *key, int *len)
461 {
462     struct RSAKey *rsa = (struct RSAKey *) key;
463     int dlen, plen, qlen, ulen, bloblen;
464     int i;
465     unsigned char *blob, *p;
466
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;
471
472     /*
473      * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
474      * sum of lengths.
475      */
476     bloblen = 16 + dlen + plen + qlen + ulen;
477     blob = smalloc(bloblen);
478     p = blob;
479     PUT_32BIT(p, dlen);
480     p += 4;
481     for (i = dlen; i--;)
482         *p++ = bignum_byte(rsa->private_exponent, i);
483     PUT_32BIT(p, plen);
484     p += 4;
485     for (i = plen; i--;)
486         *p++ = bignum_byte(rsa->p, i);
487     PUT_32BIT(p, qlen);
488     p += 4;
489     for (i = qlen; i--;)
490         *p++ = bignum_byte(rsa->q, i);
491     PUT_32BIT(p, ulen);
492     p += 4;
493     for (i = ulen; i--;)
494         *p++ = bignum_byte(rsa->iqmp, i);
495     assert(p == blob + bloblen);
496     *len = bloblen;
497     return blob;
498 }
499
500 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
501                             unsigned char *priv_blob, int priv_len)
502 {
503     struct RSAKey *rsa;
504     char *pb = (char *) priv_blob;
505
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);
511
512     if (!rsa_verify(rsa)) {
513         rsa2_freekey(rsa);
514         return NULL;
515     }
516
517     return rsa;
518 }
519
520 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
521 {
522     char **b = (char **) blob;
523     struct RSAKey *rsa;
524
525     rsa = smalloc(sizeof(struct RSAKey));
526     if (!rsa)
527         return NULL;
528     rsa->comment = NULL;
529
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);
536
537     if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
538         !rsa->iqmp || !rsa->p || !rsa->q) {
539         sfree(rsa->modulus);
540         sfree(rsa->exponent);
541         sfree(rsa->private_exponent);
542         sfree(rsa->iqmp);
543         sfree(rsa->p);
544         sfree(rsa->q);
545         sfree(rsa);
546         return NULL;
547     }
548
549     return rsa;
550 }
551
552 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
553 {
554     struct RSAKey *rsa = (struct RSAKey *) key;
555     int bloblen, i;
556
557     bloblen =
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);
563
564     if (bloblen > len)
565         return bloblen;
566
567     bloblen = 0;
568 #define ENC(x) \
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);
571     ENC(rsa->modulus);
572     ENC(rsa->exponent);
573     ENC(rsa->private_exponent);
574     ENC(rsa->iqmp);
575     ENC(rsa->p);
576     ENC(rsa->q);
577
578     return bloblen;
579 }
580
581 static char *rsa2_fingerprint(void *key)
582 {
583     struct RSAKey *rsa = (struct RSAKey *) key;
584     struct MD5Context md5c;
585     unsigned char digest[16], lenbuf[4];
586     char buffer[16 * 3 + 40];
587     char *ret;
588     int numlen, i;
589
590     MD5Init(&md5c);
591     MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
592
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); \
599     }
600     ADD_BIGNUM(rsa->exponent);
601     ADD_BIGNUM(rsa->modulus);
602 #undef ADD_BIGNUM
603
604     MD5Final(digest, &md5c);
605
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 ? ":" : "",
609                 digest[i]);
610     ret = smalloc(strlen(buffer) + 1);
611     if (ret)
612         strcpy(ret, buffer);
613     return ret;
614 }
615
616 /*
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:
620  * 
621  * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
622  * 
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)
628  *       05 00 -- NULL
629  *    04 14 -- a primitive OCTET STRING of length 0x14
630  *       [0x14 bytes of hash data follows]
631  * 
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:
635  * 
636  * id-sha1                OBJECT IDENTIFIER ::= {
637  *    iso(1) identified-organization(3) oiw(14) secsig(3)
638  *    algorithms(2) 26 }
639  */
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,
643 };
644
645 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
646
647 static int rsa2_verifysig(void *key, char *sig, int siglen,
648                           char *data, int datalen)
649 {
650     struct RSAKey *rsa = (struct RSAKey *) key;
651     Bignum in, out;
652     char *p;
653     int slen;
654     int bytes, i, j, ret;
655     unsigned char hash[20];
656
657     getstring(&sig, &siglen, &p, &slen);
658     if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
659         return 0;
660     }
661     in = getmp(&sig, &siglen);
662     out = modpow(in, rsa->exponent, rsa->modulus);
663     freebn(in);
664
665     ret = 1;
666
667     bytes = bignum_bitcount(rsa->modulus) / 8;
668     /* Top (partial) byte should be zero. */
669     if (bignum_byte(out, bytes - 1) != 0)
670         ret = 0;
671     /* First whole byte should be 1. */
672     if (bignum_byte(out, bytes - 2) != 1)
673         ret = 0;
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)
677             ret = 0;
678     }
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])
682             ret = 0;
683     }
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])
688             ret = 0;
689     }
690
691     return ret;
692 }
693
694 static unsigned char *rsa2_sign(void *key, char *data, int datalen,
695                                 int *siglen)
696 {
697     struct RSAKey *rsa = (struct RSAKey *) key;
698     unsigned char *bytes;
699     int nbytes;
700     unsigned char hash[20];
701     Bignum in, out;
702     int i, j;
703
704     SHA_Simple(data, datalen, hash);
705
706     nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
707     bytes = smalloc(nbytes);
708
709     bytes[0] = 1;
710     for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
711         bytes[i] = 0xFF;
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++)
715         bytes[i] = hash[j];
716
717     in = bignum_from_bytes(bytes, nbytes);
718     sfree(bytes);
719
720     out = rsa_privkey_op(in, rsa);
721     freebn(in);
722
723     nbytes = (bignum_bitcount(out) + 7) / 8;
724     bytes = smalloc(4 + 7 + 4 + nbytes);
725     PUT_32BIT(bytes, 7);
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);
730     freebn(out);
731
732     *siglen = 4 + 7 + 4 + nbytes;
733     return bytes;
734 }
735
736 const struct ssh_signkey ssh_rsa = {
737     rsa2_newkey,
738     rsa2_freekey,
739     rsa2_fmtkey,
740     rsa2_public_blob,
741     rsa2_private_blob,
742     rsa2_createkey,
743     rsa2_openssh_createkey,
744     rsa2_openssh_fmtkey,
745     rsa2_fingerprint,
746     rsa2_verifysig,
747     rsa2_sign,
748     "ssh-rsa",
749     "rsa2"
750 };