Added a command-line key generation tool. Currently builds and runs
[u/mdw/putty] / 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 static void sha512_mpint(SHA512_State * s, Bignum b)
93 {
94 unsigned char lenbuf[4];
95 int len;
96 len = (bignum_bitcount(b) + 8) / 8;
97 PUT_32BIT(lenbuf, len);
98 SHA512_Bytes(s, lenbuf, 4);
99 while (len-- > 0) {
100 lenbuf[0] = bignum_byte(b, len);
101 SHA512_Bytes(s, lenbuf, 1);
102 }
103 memset(lenbuf, 0, sizeof(lenbuf));
104 }
105
106 /*
107 * This function is a wrapper on modpow(). It has the same effect
108 * as modpow(), but employs RSA blinding to protect against timing
109 * attacks.
110 */
111 static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
112 {
113 Bignum random, random_encrypted, random_inverse;
114 Bignum input_blinded, ret_blinded;
115 Bignum ret;
116
117 SHA512_State ss;
118 unsigned char digest512[64];
119 int digestused = lenof(digest512);
120 int hashseq = 0;
121
122 /*
123 * Start by inventing a random number chosen uniformly from the
124 * range 2..modulus-1. (We do this by preparing a random number
125 * of the right length and retrying if it's greater than the
126 * modulus, to prevent any potential Bleichenbacher-like
127 * attacks making use of the uneven distribution within the
128 * range that would arise from just reducing our number mod n.
129 * There are timing implications to the potential retries, of
130 * course, but all they tell you is the modulus, which you
131 * already knew.)
132 *
133 * To preserve determinism and avoid Pageant needing to share
134 * the random number pool, we actually generate this `random'
135 * number by hashing stuff with the private key.
136 */
137 while (1) {
138 int bits, byte, bitsleft, v;
139 random = copybn(key->modulus);
140 /*
141 * Find the topmost set bit. (This function will return its
142 * index plus one.) Then we'll set all bits from that one
143 * downwards randomly.
144 */
145 bits = bignum_bitcount(random);
146 byte = 0;
147 bitsleft = 0;
148 while (bits--) {
149 if (bitsleft <= 0) {
150 bitsleft = 8;
151 /*
152 * Conceptually the following few lines are equivalent to
153 * byte = random_byte();
154 */
155 if (digestused >= lenof(digest512)) {
156 unsigned char seqbuf[4];
157 PUT_32BIT(seqbuf, hashseq);
158 SHA512_Init(&ss);
159 SHA512_Bytes(&ss, "RSA deterministic blinding", 26);
160 SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf));
161 sha512_mpint(&ss, key->private_exponent);
162 SHA512_Final(&ss, digest512);
163 hashseq++;
164
165 /*
166 * Now hash that digest plus the signature
167 * input.
168 */
169 SHA512_Init(&ss);
170 SHA512_Bytes(&ss, digest512, sizeof(digest512));
171 sha512_mpint(&ss, input);
172 SHA512_Final(&ss, digest512);
173
174 digestused = 0;
175 }
176 byte = digest512[digestused++];
177 }
178 v = byte & 1;
179 byte >>= 1;
180 bitsleft--;
181 bignum_set_bit(random, bits, v);
182 }
183
184 /*
185 * Now check that this number is strictly greater than
186 * zero, and strictly less than modulus.
187 */
188 if (bignum_cmp(random, Zero) <= 0 ||
189 bignum_cmp(random, key->modulus) >= 0) {
190 freebn(random);
191 continue;
192 } else {
193 break;
194 }
195 }
196
197 /*
198 * RSA blinding relies on the fact that (xy)^d mod n is equal
199 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
200 * y and y^d; then we multiply x by y, raise to the power d mod
201 * n as usual, and divide by y^d to recover x^d. Thus an
202 * attacker can't correlate the timing of the modpow with the
203 * input, because they don't know anything about the number
204 * that was input to the actual modpow.
205 *
206 * The clever bit is that we don't have to do a huge modpow to
207 * get y and y^d; we will use the number we just invented as
208 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
209 * from it, which is much faster to do.
210 */
211 random_encrypted = modpow(random, key->exponent, key->modulus);
212 random_inverse = modinv(random, key->modulus);
213 input_blinded = modmul(input, random_encrypted, key->modulus);
214 ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus);
215 ret = modmul(ret_blinded, random_inverse, key->modulus);
216
217 freebn(ret_blinded);
218 freebn(input_blinded);
219 freebn(random_inverse);
220 freebn(random_encrypted);
221 freebn(random);
222
223 return ret;
224 }
225
226 Bignum rsadecrypt(Bignum input, struct RSAKey *key)
227 {
228 return rsa_privkey_op(input, key);
229 }
230
231 int rsastr_len(struct RSAKey *key)
232 {
233 Bignum md, ex;
234 int mdlen, exlen;
235
236 md = key->modulus;
237 ex = key->exponent;
238 mdlen = (bignum_bitcount(md) + 15) / 16;
239 exlen = (bignum_bitcount(ex) + 15) / 16;
240 return 4 * (mdlen + exlen) + 20;
241 }
242
243 void rsastr_fmt(char *str, struct RSAKey *key)
244 {
245 Bignum md, ex;
246 int len = 0, i, nibbles;
247 static const char hex[] = "0123456789abcdef";
248
249 md = key->modulus;
250 ex = key->exponent;
251
252 len += sprintf(str + len, "0x");
253
254 nibbles = (3 + bignum_bitcount(ex)) / 4;
255 if (nibbles < 1)
256 nibbles = 1;
257 for (i = nibbles; i--;)
258 str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
259
260 len += sprintf(str + len, ",0x");
261
262 nibbles = (3 + bignum_bitcount(md)) / 4;
263 if (nibbles < 1)
264 nibbles = 1;
265 for (i = nibbles; i--;)
266 str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
267
268 str[len] = '\0';
269 }
270
271 /*
272 * Generate a fingerprint string for the key. Compatible with the
273 * OpenSSH fingerprint code.
274 */
275 void rsa_fingerprint(char *str, int len, struct RSAKey *key)
276 {
277 struct MD5Context md5c;
278 unsigned char digest[16];
279 char buffer[16 * 3 + 40];
280 int numlen, slen, i;
281
282 MD5Init(&md5c);
283 numlen = ssh1_bignum_length(key->modulus) - 2;
284 for (i = numlen; i--;) {
285 unsigned char c = bignum_byte(key->modulus, i);
286 MD5Update(&md5c, &c, 1);
287 }
288 numlen = ssh1_bignum_length(key->exponent) - 2;
289 for (i = numlen; i--;) {
290 unsigned char c = bignum_byte(key->exponent, i);
291 MD5Update(&md5c, &c, 1);
292 }
293 MD5Final(digest, &md5c);
294
295 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
296 for (i = 0; i < 16; i++)
297 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
298 digest[i]);
299 strncpy(str, buffer, len);
300 str[len - 1] = '\0';
301 slen = strlen(str);
302 if (key->comment && slen < len - 1) {
303 str[slen] = ' ';
304 strncpy(str + slen + 1, key->comment, len - slen - 1);
305 str[len - 1] = '\0';
306 }
307 }
308
309 /*
310 * Verify that the public data in an RSA key matches the private
311 * data. We also check the private data itself: we ensure that p >
312 * q and that iqmp really is the inverse of q mod p.
313 */
314 int rsa_verify(struct RSAKey *key)
315 {
316 Bignum n, ed, pm1, qm1;
317 int cmp;
318
319 /* n must equal pq. */
320 n = bigmul(key->p, key->q);
321 cmp = bignum_cmp(n, key->modulus);
322 freebn(n);
323 if (cmp != 0)
324 return 0;
325
326 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
327 pm1 = copybn(key->p);
328 decbn(pm1);
329 ed = modmul(key->exponent, key->private_exponent, pm1);
330 cmp = bignum_cmp(ed, One);
331 sfree(ed);
332 if (cmp != 0)
333 return 0;
334
335 qm1 = copybn(key->q);
336 decbn(qm1);
337 ed = modmul(key->exponent, key->private_exponent, qm1);
338 cmp = bignum_cmp(ed, One);
339 sfree(ed);
340 if (cmp != 0)
341 return 0;
342
343 /*
344 * Ensure p > q.
345 */
346 if (bignum_cmp(key->p, key->q) <= 0)
347 return 0;
348
349 /*
350 * Ensure iqmp * q is congruent to 1, modulo p.
351 */
352 n = modmul(key->iqmp, key->q, key->p);
353 cmp = bignum_cmp(n, One);
354 sfree(n);
355 if (cmp != 0)
356 return 0;
357
358 return 1;
359 }
360
361 /* Public key blob as used by Pageant: exponent before modulus. */
362 unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
363 {
364 int length, pos;
365 unsigned char *ret;
366
367 length = (ssh1_bignum_length(key->modulus) +
368 ssh1_bignum_length(key->exponent) + 4);
369 ret = snewn(length, unsigned char);
370
371 PUT_32BIT(ret, bignum_bitcount(key->modulus));
372 pos = 4;
373 pos += ssh1_write_bignum(ret + pos, key->exponent);
374 pos += ssh1_write_bignum(ret + pos, key->modulus);
375
376 *len = length;
377 return ret;
378 }
379
380 /* Given a public blob, determine its length. */
381 int rsa_public_blob_len(void *data)
382 {
383 unsigned char *p = (unsigned char *)data;
384
385 p += 4; /* length word */
386 p += ssh1_read_bignum(p, NULL); /* exponent */
387 p += ssh1_read_bignum(p, NULL); /* modulus */
388
389 return p - (unsigned char *)data;
390 }
391
392 void freersakey(struct RSAKey *key)
393 {
394 if (key->modulus)
395 freebn(key->modulus);
396 if (key->exponent)
397 freebn(key->exponent);
398 if (key->private_exponent)
399 freebn(key->private_exponent);
400 if (key->comment)
401 sfree(key->comment);
402 }
403
404 /* ----------------------------------------------------------------------
405 * Implementation of the ssh-rsa signing key type.
406 */
407
408 static void getstring(char **data, int *datalen, char **p, int *length)
409 {
410 *p = NULL;
411 if (*datalen < 4)
412 return;
413 *length = GET_32BIT(*data);
414 *datalen -= 4;
415 *data += 4;
416 if (*datalen < *length)
417 return;
418 *p = *data;
419 *data += *length;
420 *datalen -= *length;
421 }
422 static Bignum getmp(char **data, int *datalen)
423 {
424 char *p;
425 int length;
426 Bignum b;
427
428 getstring(data, datalen, &p, &length);
429 if (!p)
430 return NULL;
431 b = bignum_from_bytes((unsigned char *)p, length);
432 return b;
433 }
434
435 static void *rsa2_newkey(char *data, int len)
436 {
437 char *p;
438 int slen;
439 struct RSAKey *rsa;
440
441 rsa = snew(struct RSAKey);
442 if (!rsa)
443 return NULL;
444 getstring(&data, &len, &p, &slen);
445
446 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
447 sfree(rsa);
448 return NULL;
449 }
450 rsa->exponent = getmp(&data, &len);
451 rsa->modulus = getmp(&data, &len);
452 rsa->private_exponent = NULL;
453 rsa->comment = NULL;
454
455 return rsa;
456 }
457
458 static void rsa2_freekey(void *key)
459 {
460 struct RSAKey *rsa = (struct RSAKey *) key;
461 freersakey(rsa);
462 sfree(rsa);
463 }
464
465 static char *rsa2_fmtkey(void *key)
466 {
467 struct RSAKey *rsa = (struct RSAKey *) key;
468 char *p;
469 int len;
470
471 len = rsastr_len(rsa);
472 p = snewn(len, char);
473 rsastr_fmt(p, rsa);
474 return p;
475 }
476
477 static unsigned char *rsa2_public_blob(void *key, int *len)
478 {
479 struct RSAKey *rsa = (struct RSAKey *) key;
480 int elen, mlen, bloblen;
481 int i;
482 unsigned char *blob, *p;
483
484 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
485 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
486
487 /*
488 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
489 * (three length fields, 12+7=19).
490 */
491 bloblen = 19 + elen + mlen;
492 blob = snewn(bloblen, unsigned char);
493 p = blob;
494 PUT_32BIT(p, 7);
495 p += 4;
496 memcpy(p, "ssh-rsa", 7);
497 p += 7;
498 PUT_32BIT(p, elen);
499 p += 4;
500 for (i = elen; i--;)
501 *p++ = bignum_byte(rsa->exponent, i);
502 PUT_32BIT(p, mlen);
503 p += 4;
504 for (i = mlen; i--;)
505 *p++ = bignum_byte(rsa->modulus, i);
506 assert(p == blob + bloblen);
507 *len = bloblen;
508 return blob;
509 }
510
511 static unsigned char *rsa2_private_blob(void *key, int *len)
512 {
513 struct RSAKey *rsa = (struct RSAKey *) key;
514 int dlen, plen, qlen, ulen, bloblen;
515 int i;
516 unsigned char *blob, *p;
517
518 dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
519 plen = (bignum_bitcount(rsa->p) + 8) / 8;
520 qlen = (bignum_bitcount(rsa->q) + 8) / 8;
521 ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
522
523 /*
524 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
525 * sum of lengths.
526 */
527 bloblen = 16 + dlen + plen + qlen + ulen;
528 blob = snewn(bloblen, unsigned char);
529 p = blob;
530 PUT_32BIT(p, dlen);
531 p += 4;
532 for (i = dlen; i--;)
533 *p++ = bignum_byte(rsa->private_exponent, i);
534 PUT_32BIT(p, plen);
535 p += 4;
536 for (i = plen; i--;)
537 *p++ = bignum_byte(rsa->p, i);
538 PUT_32BIT(p, qlen);
539 p += 4;
540 for (i = qlen; i--;)
541 *p++ = bignum_byte(rsa->q, i);
542 PUT_32BIT(p, ulen);
543 p += 4;
544 for (i = ulen; i--;)
545 *p++ = bignum_byte(rsa->iqmp, i);
546 assert(p == blob + bloblen);
547 *len = bloblen;
548 return blob;
549 }
550
551 static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
552 unsigned char *priv_blob, int priv_len)
553 {
554 struct RSAKey *rsa;
555 char *pb = (char *) priv_blob;
556
557 rsa = rsa2_newkey((char *) pub_blob, pub_len);
558 rsa->private_exponent = getmp(&pb, &priv_len);
559 rsa->p = getmp(&pb, &priv_len);
560 rsa->q = getmp(&pb, &priv_len);
561 rsa->iqmp = getmp(&pb, &priv_len);
562
563 if (!rsa_verify(rsa)) {
564 rsa2_freekey(rsa);
565 return NULL;
566 }
567
568 return rsa;
569 }
570
571 static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
572 {
573 char **b = (char **) blob;
574 struct RSAKey *rsa;
575
576 rsa = snew(struct RSAKey);
577 if (!rsa)
578 return NULL;
579 rsa->comment = NULL;
580
581 rsa->modulus = getmp(b, len);
582 rsa->exponent = getmp(b, len);
583 rsa->private_exponent = getmp(b, len);
584 rsa->iqmp = getmp(b, len);
585 rsa->p = getmp(b, len);
586 rsa->q = getmp(b, len);
587
588 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
589 !rsa->iqmp || !rsa->p || !rsa->q) {
590 sfree(rsa->modulus);
591 sfree(rsa->exponent);
592 sfree(rsa->private_exponent);
593 sfree(rsa->iqmp);
594 sfree(rsa->p);
595 sfree(rsa->q);
596 sfree(rsa);
597 return NULL;
598 }
599
600 return rsa;
601 }
602
603 static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
604 {
605 struct RSAKey *rsa = (struct RSAKey *) key;
606 int bloblen, i;
607
608 bloblen =
609 ssh2_bignum_length(rsa->modulus) +
610 ssh2_bignum_length(rsa->exponent) +
611 ssh2_bignum_length(rsa->private_exponent) +
612 ssh2_bignum_length(rsa->iqmp) +
613 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
614
615 if (bloblen > len)
616 return bloblen;
617
618 bloblen = 0;
619 #define ENC(x) \
620 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
621 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
622 ENC(rsa->modulus);
623 ENC(rsa->exponent);
624 ENC(rsa->private_exponent);
625 ENC(rsa->iqmp);
626 ENC(rsa->p);
627 ENC(rsa->q);
628
629 return bloblen;
630 }
631
632 static int rsa2_pubkey_bits(void *blob, int len)
633 {
634 struct RSAKey *rsa;
635 int ret;
636
637 rsa = rsa2_newkey((char *) blob, len);
638 ret = bignum_bitcount(rsa->modulus);
639 rsa2_freekey(rsa);
640
641 return ret;
642 }
643
644 static char *rsa2_fingerprint(void *key)
645 {
646 struct RSAKey *rsa = (struct RSAKey *) key;
647 struct MD5Context md5c;
648 unsigned char digest[16], lenbuf[4];
649 char buffer[16 * 3 + 40];
650 char *ret;
651 int numlen, i;
652
653 MD5Init(&md5c);
654 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
655
656 #define ADD_BIGNUM(bignum) \
657 numlen = (bignum_bitcount(bignum)+8)/8; \
658 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
659 for (i = numlen; i-- ;) { \
660 unsigned char c = bignum_byte(bignum, i); \
661 MD5Update(&md5c, &c, 1); \
662 }
663 ADD_BIGNUM(rsa->exponent);
664 ADD_BIGNUM(rsa->modulus);
665 #undef ADD_BIGNUM
666
667 MD5Final(digest, &md5c);
668
669 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
670 for (i = 0; i < 16; i++)
671 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
672 digest[i]);
673 ret = snewn(strlen(buffer) + 1, char);
674 if (ret)
675 strcpy(ret, buffer);
676 return ret;
677 }
678
679 /*
680 * This is the magic ASN.1/DER prefix that goes in the decoded
681 * signature, between the string of FFs and the actual SHA hash
682 * value. The meaning of it is:
683 *
684 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
685 *
686 * 30 21 -- a constructed SEQUENCE of length 0x21
687 * 30 09 -- a constructed sub-SEQUENCE of length 9
688 * 06 05 -- an object identifier, length 5
689 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
690 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
691 * 05 00 -- NULL
692 * 04 14 -- a primitive OCTET STRING of length 0x14
693 * [0x14 bytes of hash data follows]
694 *
695 * The object id in the middle there is listed as `id-sha1' in
696 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
697 * ASN module for PKCS #1) and its expanded form is as follows:
698 *
699 * id-sha1 OBJECT IDENTIFIER ::= {
700 * iso(1) identified-organization(3) oiw(14) secsig(3)
701 * algorithms(2) 26 }
702 */
703 static const unsigned char asn1_weird_stuff[] = {
704 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
705 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
706 };
707
708 #define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
709
710 static int rsa2_verifysig(void *key, char *sig, int siglen,
711 char *data, int datalen)
712 {
713 struct RSAKey *rsa = (struct RSAKey *) key;
714 Bignum in, out;
715 char *p;
716 int slen;
717 int bytes, i, j, ret;
718 unsigned char hash[20];
719
720 getstring(&sig, &siglen, &p, &slen);
721 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
722 return 0;
723 }
724 in = getmp(&sig, &siglen);
725 out = modpow(in, rsa->exponent, rsa->modulus);
726 freebn(in);
727
728 ret = 1;
729
730 bytes = bignum_bitcount(rsa->modulus) / 8;
731 /* Top (partial) byte should be zero. */
732 if (bignum_byte(out, bytes - 1) != 0)
733 ret = 0;
734 /* First whole byte should be 1. */
735 if (bignum_byte(out, bytes - 2) != 1)
736 ret = 0;
737 /* Most of the rest should be FF. */
738 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
739 if (bignum_byte(out, i) != 0xFF)
740 ret = 0;
741 }
742 /* Then we expect to see the asn1_weird_stuff. */
743 for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
744 if (bignum_byte(out, i) != asn1_weird_stuff[j])
745 ret = 0;
746 }
747 /* Finally, we expect to see the SHA-1 hash of the signed data. */
748 SHA_Simple(data, datalen, hash);
749 for (i = 19, j = 0; i >= 0; i--, j++) {
750 if (bignum_byte(out, i) != hash[j])
751 ret = 0;
752 }
753 freebn(out);
754
755 return ret;
756 }
757
758 static unsigned char *rsa2_sign(void *key, char *data, int datalen,
759 int *siglen)
760 {
761 struct RSAKey *rsa = (struct RSAKey *) key;
762 unsigned char *bytes;
763 int nbytes;
764 unsigned char hash[20];
765 Bignum in, out;
766 int i, j;
767
768 SHA_Simple(data, datalen, hash);
769
770 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
771 bytes = snewn(nbytes, unsigned char);
772
773 bytes[0] = 1;
774 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
775 bytes[i] = 0xFF;
776 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
777 bytes[i] = asn1_weird_stuff[j];
778 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
779 bytes[i] = hash[j];
780
781 in = bignum_from_bytes(bytes, nbytes);
782 sfree(bytes);
783
784 out = rsa_privkey_op(in, rsa);
785 freebn(in);
786
787 nbytes = (bignum_bitcount(out) + 7) / 8;
788 bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
789 PUT_32BIT(bytes, 7);
790 memcpy(bytes + 4, "ssh-rsa", 7);
791 PUT_32BIT(bytes + 4 + 7, nbytes);
792 for (i = 0; i < nbytes; i++)
793 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
794 freebn(out);
795
796 *siglen = 4 + 7 + 4 + nbytes;
797 return bytes;
798 }
799
800 const struct ssh_signkey ssh_rsa = {
801 rsa2_newkey,
802 rsa2_freekey,
803 rsa2_fmtkey,
804 rsa2_public_blob,
805 rsa2_private_blob,
806 rsa2_createkey,
807 rsa2_openssh_createkey,
808 rsa2_openssh_fmtkey,
809 rsa2_pubkey_bits,
810 rsa2_fingerprint,
811 rsa2_verifysig,
812 rsa2_sign,
813 "ssh-rsa",
814 "rsa2"
815 };