Implement RSA blinding, to defeat Brumley and Boneh's RSA timing
[sgt/putty] / sshrsa.c
CommitLineData
374330e2 1/*
8671a580 2 * RSA implementation for PuTTY.
374330e2 3 */
4
374330e2 5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
65a22376 8#include <assert.h>
374330e2 9
e5574168 10#include "ssh.h"
8365990c 11#include "misc.h"
374330e2 12
3f2d010c 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); }
1c2a93c4 24
374330e2 25int makekey(unsigned char *data, struct RSAKey *result,
32874aea 26 unsigned char **keystr, int order)
27{
374330e2 28 unsigned char *p = data;
7cca0d81 29 int i;
374330e2 30
a52f067e 31 if (result) {
32874aea 32 result->bits = 0;
33 for (i = 0; i < 4; i++)
34 result->bits = (result->bits << 8) + *p++;
a52f067e 35 } else
32874aea 36 p += 4;
374330e2 37
7cca0d81 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 */
374330e2 43
7cca0d81 44 if (order == 0)
32874aea 45 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
a52f067e 46 if (result)
32874aea 47 result->bytes = (((p[0] << 8) + p[1]) + 7) / 8;
48 if (keystr)
49 *keystr = p + 2;
a52f067e 50 p += ssh1_read_bignum(p, result ? &result->modulus : NULL);
7cca0d81 51 if (order == 1)
32874aea 52 p += ssh1_read_bignum(p, result ? &result->exponent : NULL);
374330e2 53
54 return p - data;
55}
56
32874aea 57int makeprivate(unsigned char *data, struct RSAKey *result)
58{
7cca0d81 59 return ssh1_read_bignum(data, &result->private_exponent);
60}
61
32874aea 62void rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
63{
374330e2 64 Bignum b1, b2;
3709bfe9 65 int i;
374330e2 66 unsigned char *p;
67
32874aea 68 memmove(data + key->bytes - length, data, length);
374330e2 69 data[0] = 0;
70 data[1] = 2;
71
32874aea 72 for (i = 2; i < key->bytes - length - 1; i++) {
374330e2 73 do {
74 data[i] = random_byte();
75 } while (data[i] == 0);
76 }
32874aea 77 data[key->bytes - length - 1] = 0;
374330e2 78
3709bfe9 79 b1 = bignum_from_bytes(data, key->bytes);
374330e2 80
59600f67 81 b2 = modpow(b1, key->exponent, key->modulus);
374330e2 82
374330e2 83 p = data;
32874aea 84 for (i = key->bytes; i--;) {
85 *p++ = bignum_byte(b2, i);
374330e2 86 }
87
88 freebn(b1);
89 freebn(b2);
90}
91
8671a580 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 */
97static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
32874aea 98{
8671a580 99 Bignum random, random_encrypted, random_inverse;
100 Bignum input_blinded, ret_blinded;
7cca0d81 101 Bignum ret;
8671a580 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
7cca0d81 172 return ret;
173}
174
8671a580 175Bignum rsadecrypt(Bignum input, struct RSAKey *key)
176{
177 return rsa_privkey_op(input, key);
178}
179
32874aea 180int rsastr_len(struct RSAKey *key)
181{
374330e2 182 Bignum md, ex;
3709bfe9 183 int mdlen, exlen;
374330e2 184
185 md = key->modulus;
186 ex = key->exponent;
32874aea 187 mdlen = (bignum_bitcount(md) + 15) / 16;
188 exlen = (bignum_bitcount(ex) + 15) / 16;
189 return 4 * (mdlen + exlen) + 20;
374330e2 190}
191
32874aea 192void rsastr_fmt(char *str, struct RSAKey *key)
193{
374330e2 194 Bignum md, ex;
d5859615 195 int len = 0, i, nibbles;
196 static const char hex[] = "0123456789abcdef";
374330e2 197
198 md = key->modulus;
199 ex = key->exponent;
200
32874aea 201 len += sprintf(str + len, "0x");
d5859615 202
32874aea 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];
d5859615 208
32874aea 209 len += sprintf(str + len, ",0x");
d5859615 210
32874aea 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];
d5859615 216
374330e2 217 str[len] = '\0';
218}
219
1c2a93c4 220/*
221 * Generate a fingerprint string for the key. Compatible with the
222 * OpenSSH fingerprint code.
223 */
32874aea 224void rsa_fingerprint(char *str, int len, struct RSAKey *key)
225{
1c2a93c4 226 struct MD5Context md5c;
227 unsigned char digest[16];
32874aea 228 char buffer[16 * 3 + 40];
1c2a93c4 229 int numlen, slen, i;
230
231 MD5Init(&md5c);
232 numlen = ssh1_bignum_length(key->modulus) - 2;
32874aea 233 for (i = numlen; i--;) {
234 unsigned char c = bignum_byte(key->modulus, i);
235 MD5Update(&md5c, &c, 1);
1c2a93c4 236 }
237 numlen = ssh1_bignum_length(key->exponent) - 2;
32874aea 238 for (i = numlen; i--;) {
239 unsigned char c = bignum_byte(key->exponent, i);
240 MD5Update(&md5c, &c, 1);
1c2a93c4 241 }
242 MD5Final(digest, &md5c);
243
ddecd643 244 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
1c2a93c4 245 for (i = 0; i < 16; i++)
32874aea 246 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
247 digest[i]);
248 strncpy(str, buffer, len);
249 str[len - 1] = '\0';
1c2a93c4 250 slen = strlen(str);
32874aea 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';
1c2a93c4 255 }
256}
257
98f022f5 258/*
259 * Verify that the public data in an RSA key matches the private
60fe6ff7 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.
98f022f5 262 */
32874aea 263int rsa_verify(struct RSAKey *key)
264{
60fe6ff7 265 Bignum n, ed, pm1, qm1;
98f022f5 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
60fe6ff7 275 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
98f022f5 276 pm1 = copybn(key->p);
277 decbn(pm1);
60fe6ff7 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
98f022f5 284 qm1 = copybn(key->q);
285 decbn(qm1);
60fe6ff7 286 ed = modmul(key->exponent, key->private_exponent, qm1);
98f022f5 287 cmp = bignum_cmp(ed, One);
288 sfree(ed);
289 if (cmp != 0)
290 return 0;
014970c8 291
60fe6ff7 292 /*
293 * Ensure p > q.
294 */
295 if (bignum_cmp(key->p, key->q) <= 0)
32874aea 296 return 0;
60fe6ff7 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)
32874aea 305 return 0;
60fe6ff7 306
014970c8 307 return 1;
98f022f5 308}
309
3f2d010c 310/* Public key blob as used by Pageant: exponent before modulus. */
311unsigned 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. */
330int rsa_public_blob_len(void *data)
331{
332 unsigned char *p = (unsigned char *)data;
3f2d010c 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
32874aea 341void 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);
5c58ad2d 351}
85cc02bb 352
353/* ----------------------------------------------------------------------
354 * Implementation of the ssh-rsa signing key type.
355 */
356
32874aea 357static void getstring(char **data, int *datalen, char **p, int *length)
358{
85cc02bb 359 *p = NULL;
360 if (*datalen < 4)
32874aea 361 return;
85cc02bb 362 *length = GET_32BIT(*data);
32874aea 363 *datalen -= 4;
364 *data += 4;
85cc02bb 365 if (*datalen < *length)
32874aea 366 return;
85cc02bb 367 *p = *data;
32874aea 368 *data += *length;
369 *datalen -= *length;
85cc02bb 370}
32874aea 371static Bignum getmp(char **data, int *datalen)
372{
85cc02bb 373 char *p;
374 int length;
375 Bignum b;
376
377 getstring(data, datalen, &p, &length);
378 if (!p)
32874aea 379 return NULL;
9bf430c9 380 b = bignum_from_bytes((unsigned char *)p, length);
85cc02bb 381 return b;
382}
383
32874aea 384static void *rsa2_newkey(char *data, int len)
385{
85cc02bb 386 char *p;
387 int slen;
388 struct RSAKey *rsa;
389
390 rsa = smalloc(sizeof(struct RSAKey));
32874aea 391 if (!rsa)
392 return NULL;
85cc02bb 393 getstring(&data, &len, &p, &slen);
394
45cebe79 395 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
85cc02bb 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
32874aea 407static void rsa2_freekey(void *key)
408{
409 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 410 freersakey(rsa);
411 sfree(rsa);
412}
413
32874aea 414static char *rsa2_fmtkey(void *key)
415{
416 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 417 char *p;
418 int len;
32874aea 419
85cc02bb 420 len = rsastr_len(rsa);
421 p = smalloc(len);
32874aea 422 rsastr_fmt(p, rsa);
85cc02bb 423 return p;
424}
425
32874aea 426static unsigned char *rsa2_public_blob(void *key, int *len)
427{
428 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 429 int elen, mlen, bloblen;
430 int i;
431 unsigned char *blob, *p;
432
32874aea 433 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
434 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
65a22376 435
436 /*
437 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
438 * (three length fields, 12+7=19).
439 */
32874aea 440 bloblen = 19 + elen + mlen;
65a22376 441 blob = smalloc(bloblen);
442 p = blob;
32874aea 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);
65a22376 455 assert(p == blob + bloblen);
456 *len = bloblen;
457 return blob;
458}
459
32874aea 460static unsigned char *rsa2_private_blob(void *key, int *len)
461{
462 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 463 int dlen, plen, qlen, ulen, bloblen;
464 int i;
465 unsigned char *blob, *p;
466
32874aea 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;
65a22376 471
472 /*
473 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
474 * sum of lengths.
475 */
32874aea 476 bloblen = 16 + dlen + plen + qlen + ulen;
65a22376 477 blob = smalloc(bloblen);
478 p = blob;
32874aea 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);
65a22376 495 assert(p == blob + bloblen);
496 *len = bloblen;
497 return blob;
498}
499
500static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
32874aea 501 unsigned char *priv_blob, int priv_len)
502{
65a22376 503 struct RSAKey *rsa;
32874aea 504 char *pb = (char *) priv_blob;
505
506 rsa = rsa2_newkey((char *) pub_blob, pub_len);
65a22376 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
98f022f5 512 if (!rsa_verify(rsa)) {
513 rsa2_freekey(rsa);
514 return NULL;
515 }
516
65a22376 517 return rsa;
518}
519
32874aea 520static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
521{
522 char **b = (char **) blob;
45cebe79 523 struct RSAKey *rsa;
45cebe79 524
525 rsa = smalloc(sizeof(struct RSAKey));
32874aea 526 if (!rsa)
527 return NULL;
45cebe79 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
32874aea 552static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
553{
554 struct RSAKey *rsa = (struct RSAKey *) key;
ddecd643 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) +
32874aea 562 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
ddecd643 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
32874aea 581static char *rsa2_fingerprint(void *key)
582{
583 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 584 struct MD5Context md5c;
585 unsigned char digest[16], lenbuf[4];
32874aea 586 char buffer[16 * 3 + 40];
85cc02bb 587 char *ret;
588 int numlen, i;
589
590 MD5Init(&md5c);
9bf430c9 591 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
85cc02bb 592
593#define ADD_BIGNUM(bignum) \
ddecd643 594 numlen = (bignum_bitcount(bignum)+8)/8; \
85cc02bb 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
ddecd643 606 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
85cc02bb 607 for (i = 0; i < 16; i++)
32874aea 608 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
609 digest[i]);
610 ret = smalloc(strlen(buffer) + 1);
85cc02bb 611 if (ret)
32874aea 612 strcpy(ret, buffer);
85cc02bb 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
96a73db9 619 * value. The meaning of it is:
85cc02bb 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
96a73db9 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)
85cc02bb 628 * 05 00 -- NULL
629 * 04 14 -- a primitive OCTET STRING of length 0x14
630 * [0x14 bytes of hash data follows]
96a73db9 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 }
85cc02bb 639 */
b5864f2c 640static const unsigned char asn1_weird_stuff[] = {
32874aea 641 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
642 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
85cc02bb 643};
644
d8770b12 645#define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
646
85cc02bb 647static int rsa2_verifysig(void *key, char *sig, int siglen,
32874aea 648 char *data, int datalen)
649{
650 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 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)) {
32874aea 659 return 0;
85cc02bb 660 }
661 in = getmp(&sig, &siglen);
662 out = modpow(in, rsa->exponent, rsa->modulus);
663 freebn(in);
664
665 ret = 1;
666
ddecd643 667 bytes = bignum_bitcount(rsa->modulus) / 8;
85cc02bb 668 /* Top (partial) byte should be zero. */
32874aea 669 if (bignum_byte(out, bytes - 1) != 0)
670 ret = 0;
85cc02bb 671 /* First whole byte should be 1. */
32874aea 672 if (bignum_byte(out, bytes - 2) != 1)
673 ret = 0;
85cc02bb 674 /* Most of the rest should be FF. */
32874aea 675 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
676 if (bignum_byte(out, i) != 0xFF)
677 ret = 0;
85cc02bb 678 }
679 /* Then we expect to see the asn1_weird_stuff. */
32874aea 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;
85cc02bb 683 }
684 /* Finally, we expect to see the SHA-1 hash of the signed data. */
685 SHA_Simple(data, datalen, hash);
32874aea 686 for (i = 19, j = 0; i >= 0; i--, j++) {
687 if (bignum_byte(out, i) != hash[j])
688 ret = 0;
85cc02bb 689 }
690
691 return ret;
692}
693
164feb13 694static unsigned char *rsa2_sign(void *key, char *data, int datalen,
695 int *siglen)
32874aea 696{
697 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 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
32874aea 706 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
65a22376 707 bytes = smalloc(nbytes);
708
709 bytes[0] = 1;
32874aea 710 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
65a22376 711 bytes[i] = 0xFF;
32874aea 712 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
65a22376 713 bytes[i] = asn1_weird_stuff[j];
32874aea 714 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
65a22376 715 bytes[i] = hash[j];
716
717 in = bignum_from_bytes(bytes, nbytes);
718 sfree(bytes);
719
8671a580 720 out = rsa_privkey_op(in, rsa);
65a22376 721 freebn(in);
722
32874aea 723 nbytes = (bignum_bitcount(out) + 7) / 8;
724 bytes = smalloc(4 + 7 + 4 + nbytes);
65a22376 725 PUT_32BIT(bytes, 7);
32874aea 726 memcpy(bytes + 4, "ssh-rsa", 7);
727 PUT_32BIT(bytes + 4 + 7, nbytes);
65a22376 728 for (i = 0; i < nbytes; i++)
32874aea 729 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
65a22376 730 freebn(out);
731
32874aea 732 *siglen = 4 + 7 + 4 + nbytes;
65a22376 733 return bytes;
85cc02bb 734}
735
65a22376 736const struct ssh_signkey ssh_rsa = {
85cc02bb 737 rsa2_newkey,
738 rsa2_freekey,
739 rsa2_fmtkey,
65a22376 740 rsa2_public_blob,
741 rsa2_private_blob,
742 rsa2_createkey,
45cebe79 743 rsa2_openssh_createkey,
ddecd643 744 rsa2_openssh_fmtkey,
85cc02bb 745 rsa2_fingerprint,
746 rsa2_verifysig,
747 rsa2_sign,
748 "ssh-rsa",
749 "rsa2"
750};