sshrsa.c now obeys the RFC793 Robustness Principle when it comes to
[u/mdw/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
0016d70b 13int makekey(unsigned char *data, int len, struct RSAKey *result,
32874aea 14 unsigned char **keystr, int order)
15{
374330e2 16 unsigned char *p = data;
0016d70b 17 int i, n;
18
19 if (len < 4)
20 return -1;
374330e2 21
a52f067e 22 if (result) {
32874aea 23 result->bits = 0;
24 for (i = 0; i < 4; i++)
25 result->bits = (result->bits << 8) + *p++;
a52f067e 26 } else
32874aea 27 p += 4;
374330e2 28
0016d70b 29 len -= 4;
30
7cca0d81 31 /*
32 * order=0 means exponent then modulus (the keys sent by the
33 * server). order=1 means modulus then exponent (the keys
34 * stored in a keyfile).
35 */
374330e2 36
0016d70b 37 if (order == 0) {
38 n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL);
39 if (n < 0) return -1;
40 p += n;
41 len -= n;
42 }
43
44 n = ssh1_read_bignum(p, len, result ? &result->modulus : NULL);
26d98fc6 45 if (n < 0 || (result && bignum_bitcount(result->modulus) == 0)) return -1;
a52f067e 46 if (result)
0016d70b 47 result->bytes = n - 2;
32874aea 48 if (keystr)
49 *keystr = p + 2;
0016d70b 50 p += n;
51 len -= n;
52
53 if (order == 1) {
54 n = ssh1_read_bignum(p, len, result ? &result->exponent : NULL);
55 if (n < 0) return -1;
56 p += n;
57 len -= n;
58 }
374330e2 59 return p - data;
60}
61
0016d70b 62int makeprivate(unsigned char *data, int len, struct RSAKey *result)
32874aea 63{
0016d70b 64 return ssh1_read_bignum(data, len, &result->private_exponent);
7cca0d81 65}
66
0016d70b 67int rsaencrypt(unsigned char *data, int length, struct RSAKey *key)
32874aea 68{
374330e2 69 Bignum b1, b2;
3709bfe9 70 int i;
374330e2 71 unsigned char *p;
72
0016d70b 73 if (key->bytes < length + 4)
74 return 0; /* RSA key too short! */
75
32874aea 76 memmove(data + key->bytes - length, data, length);
374330e2 77 data[0] = 0;
78 data[1] = 2;
79
32874aea 80 for (i = 2; i < key->bytes - length - 1; i++) {
374330e2 81 do {
82 data[i] = random_byte();
83 } while (data[i] == 0);
84 }
32874aea 85 data[key->bytes - length - 1] = 0;
374330e2 86
3709bfe9 87 b1 = bignum_from_bytes(data, key->bytes);
374330e2 88
59600f67 89 b2 = modpow(b1, key->exponent, key->modulus);
374330e2 90
374330e2 91 p = data;
32874aea 92 for (i = key->bytes; i--;) {
93 *p++ = bignum_byte(b2, i);
374330e2 94 }
95
96 freebn(b1);
97 freebn(b2);
0016d70b 98
99 return 1;
374330e2 100}
101
b492c4d7 102static void sha512_mpint(SHA512_State * s, Bignum b)
103{
104 unsigned char lenbuf[4];
105 int len;
106 len = (bignum_bitcount(b) + 8) / 8;
107 PUT_32BIT(lenbuf, len);
108 SHA512_Bytes(s, lenbuf, 4);
109 while (len-- > 0) {
110 lenbuf[0] = bignum_byte(b, len);
111 SHA512_Bytes(s, lenbuf, 1);
112 }
113 memset(lenbuf, 0, sizeof(lenbuf));
114}
115
8671a580 116/*
117 * This function is a wrapper on modpow(). It has the same effect
118 * as modpow(), but employs RSA blinding to protect against timing
119 * attacks.
120 */
121static Bignum rsa_privkey_op(Bignum input, struct RSAKey *key)
32874aea 122{
8671a580 123 Bignum random, random_encrypted, random_inverse;
124 Bignum input_blinded, ret_blinded;
7cca0d81 125 Bignum ret;
8671a580 126
b492c4d7 127 SHA512_State ss;
128 unsigned char digest512[64];
129 int digestused = lenof(digest512);
130 int hashseq = 0;
131
8671a580 132 /*
133 * Start by inventing a random number chosen uniformly from the
134 * range 2..modulus-1. (We do this by preparing a random number
135 * of the right length and retrying if it's greater than the
136 * modulus, to prevent any potential Bleichenbacher-like
137 * attacks making use of the uneven distribution within the
138 * range that would arise from just reducing our number mod n.
139 * There are timing implications to the potential retries, of
140 * course, but all they tell you is the modulus, which you
141 * already knew.)
b492c4d7 142 *
143 * To preserve determinism and avoid Pageant needing to share
144 * the random number pool, we actually generate this `random'
145 * number by hashing stuff with the private key.
8671a580 146 */
147 while (1) {
148 int bits, byte, bitsleft, v;
149 random = copybn(key->modulus);
150 /*
151 * Find the topmost set bit. (This function will return its
152 * index plus one.) Then we'll set all bits from that one
153 * downwards randomly.
154 */
155 bits = bignum_bitcount(random);
156 byte = 0;
157 bitsleft = 0;
158 while (bits--) {
b492c4d7 159 if (bitsleft <= 0) {
160 bitsleft = 8;
161 /*
162 * Conceptually the following few lines are equivalent to
163 * byte = random_byte();
164 */
165 if (digestused >= lenof(digest512)) {
166 unsigned char seqbuf[4];
167 PUT_32BIT(seqbuf, hashseq);
168 SHA512_Init(&ss);
169 SHA512_Bytes(&ss, "RSA deterministic blinding", 26);
170 SHA512_Bytes(&ss, seqbuf, sizeof(seqbuf));
171 sha512_mpint(&ss, key->private_exponent);
172 SHA512_Final(&ss, digest512);
173 hashseq++;
174
175 /*
176 * Now hash that digest plus the signature
177 * input.
178 */
179 SHA512_Init(&ss);
180 SHA512_Bytes(&ss, digest512, sizeof(digest512));
181 sha512_mpint(&ss, input);
182 SHA512_Final(&ss, digest512);
183
184 digestused = 0;
185 }
186 byte = digest512[digestused++];
187 }
8671a580 188 v = byte & 1;
189 byte >>= 1;
190 bitsleft--;
191 bignum_set_bit(random, bits, v);
192 }
193
194 /*
195 * Now check that this number is strictly greater than
196 * zero, and strictly less than modulus.
197 */
198 if (bignum_cmp(random, Zero) <= 0 ||
199 bignum_cmp(random, key->modulus) >= 0) {
200 freebn(random);
201 continue;
202 } else {
203 break;
204 }
205 }
206
207 /*
208 * RSA blinding relies on the fact that (xy)^d mod n is equal
209 * to (x^d mod n) * (y^d mod n) mod n. We invent a random pair
033a3ded 210 * y and y^d; then we multiply x by y, raise to the power d mod
211 * n as usual, and divide by y^d to recover x^d. Thus an
212 * attacker can't correlate the timing of the modpow with the
213 * input, because they don't know anything about the number
214 * that was input to the actual modpow.
8671a580 215 *
216 * The clever bit is that we don't have to do a huge modpow to
217 * get y and y^d; we will use the number we just invented as
033a3ded 218 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
219 * from it, which is much faster to do.
8671a580 220 */
221 random_encrypted = modpow(random, key->exponent, key->modulus);
222 random_inverse = modinv(random, key->modulus);
223 input_blinded = modmul(input, random_encrypted, key->modulus);
224 ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus);
225 ret = modmul(ret_blinded, random_inverse, key->modulus);
226
227 freebn(ret_blinded);
228 freebn(input_blinded);
229 freebn(random_inverse);
230 freebn(random_encrypted);
231 freebn(random);
232
7cca0d81 233 return ret;
234}
235
8671a580 236Bignum rsadecrypt(Bignum input, struct RSAKey *key)
237{
238 return rsa_privkey_op(input, key);
239}
240
32874aea 241int rsastr_len(struct RSAKey *key)
242{
374330e2 243 Bignum md, ex;
3709bfe9 244 int mdlen, exlen;
374330e2 245
246 md = key->modulus;
247 ex = key->exponent;
32874aea 248 mdlen = (bignum_bitcount(md) + 15) / 16;
249 exlen = (bignum_bitcount(ex) + 15) / 16;
250 return 4 * (mdlen + exlen) + 20;
374330e2 251}
252
32874aea 253void rsastr_fmt(char *str, struct RSAKey *key)
254{
374330e2 255 Bignum md, ex;
d5859615 256 int len = 0, i, nibbles;
257 static const char hex[] = "0123456789abcdef";
374330e2 258
259 md = key->modulus;
260 ex = key->exponent;
261
32874aea 262 len += sprintf(str + len, "0x");
d5859615 263
32874aea 264 nibbles = (3 + bignum_bitcount(ex)) / 4;
265 if (nibbles < 1)
266 nibbles = 1;
267 for (i = nibbles; i--;)
268 str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
d5859615 269
32874aea 270 len += sprintf(str + len, ",0x");
d5859615 271
32874aea 272 nibbles = (3 + bignum_bitcount(md)) / 4;
273 if (nibbles < 1)
274 nibbles = 1;
275 for (i = nibbles; i--;)
276 str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
d5859615 277
374330e2 278 str[len] = '\0';
279}
280
1c2a93c4 281/*
282 * Generate a fingerprint string for the key. Compatible with the
283 * OpenSSH fingerprint code.
284 */
32874aea 285void rsa_fingerprint(char *str, int len, struct RSAKey *key)
286{
1c2a93c4 287 struct MD5Context md5c;
288 unsigned char digest[16];
32874aea 289 char buffer[16 * 3 + 40];
1c2a93c4 290 int numlen, slen, i;
291
292 MD5Init(&md5c);
293 numlen = ssh1_bignum_length(key->modulus) - 2;
32874aea 294 for (i = numlen; i--;) {
295 unsigned char c = bignum_byte(key->modulus, i);
296 MD5Update(&md5c, &c, 1);
1c2a93c4 297 }
298 numlen = ssh1_bignum_length(key->exponent) - 2;
32874aea 299 for (i = numlen; i--;) {
300 unsigned char c = bignum_byte(key->exponent, i);
301 MD5Update(&md5c, &c, 1);
1c2a93c4 302 }
303 MD5Final(digest, &md5c);
304
ddecd643 305 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
1c2a93c4 306 for (i = 0; i < 16; i++)
32874aea 307 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
308 digest[i]);
309 strncpy(str, buffer, len);
310 str[len - 1] = '\0';
1c2a93c4 311 slen = strlen(str);
32874aea 312 if (key->comment && slen < len - 1) {
313 str[slen] = ' ';
314 strncpy(str + slen + 1, key->comment, len - slen - 1);
315 str[len - 1] = '\0';
1c2a93c4 316 }
317}
318
98f022f5 319/*
320 * Verify that the public data in an RSA key matches the private
60fe6ff7 321 * data. We also check the private data itself: we ensure that p >
322 * q and that iqmp really is the inverse of q mod p.
98f022f5 323 */
32874aea 324int rsa_verify(struct RSAKey *key)
325{
60fe6ff7 326 Bignum n, ed, pm1, qm1;
98f022f5 327 int cmp;
328
329 /* n must equal pq. */
330 n = bigmul(key->p, key->q);
331 cmp = bignum_cmp(n, key->modulus);
332 freebn(n);
333 if (cmp != 0)
334 return 0;
335
60fe6ff7 336 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
98f022f5 337 pm1 = copybn(key->p);
338 decbn(pm1);
60fe6ff7 339 ed = modmul(key->exponent, key->private_exponent, pm1);
340 cmp = bignum_cmp(ed, One);
341 sfree(ed);
342 if (cmp != 0)
343 return 0;
344
98f022f5 345 qm1 = copybn(key->q);
346 decbn(qm1);
60fe6ff7 347 ed = modmul(key->exponent, key->private_exponent, qm1);
98f022f5 348 cmp = bignum_cmp(ed, One);
349 sfree(ed);
350 if (cmp != 0)
351 return 0;
014970c8 352
60fe6ff7 353 /*
354 * Ensure p > q.
f5bcbcc2 355 *
356 * I have seen key blobs in the wild which were generated with
357 * p < q, so instead of rejecting the key in this case we
358 * should instead flip them round into the canonical order of
359 * p > q. This also involves regenerating iqmp.
60fe6ff7 360 */
f5bcbcc2 361 if (bignum_cmp(key->p, key->q) <= 0) {
362 Bignum tmp = key->p;
363 key->p = key->q;
364 key->q = tmp;
365
366 freebn(key->iqmp);
367 key->iqmp = modinv(key->q, key->p);
368 }
60fe6ff7 369
370 /*
371 * Ensure iqmp * q is congruent to 1, modulo p.
372 */
373 n = modmul(key->iqmp, key->q, key->p);
374 cmp = bignum_cmp(n, One);
375 sfree(n);
376 if (cmp != 0)
32874aea 377 return 0;
60fe6ff7 378
014970c8 379 return 1;
98f022f5 380}
381
3f2d010c 382/* Public key blob as used by Pageant: exponent before modulus. */
383unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
384{
385 int length, pos;
386 unsigned char *ret;
387
388 length = (ssh1_bignum_length(key->modulus) +
389 ssh1_bignum_length(key->exponent) + 4);
3d88e64d 390 ret = snewn(length, unsigned char);
3f2d010c 391
392 PUT_32BIT(ret, bignum_bitcount(key->modulus));
393 pos = 4;
394 pos += ssh1_write_bignum(ret + pos, key->exponent);
395 pos += ssh1_write_bignum(ret + pos, key->modulus);
396
397 *len = length;
398 return ret;
399}
400
401/* Given a public blob, determine its length. */
0016d70b 402int rsa_public_blob_len(void *data, int maxlen)
3f2d010c 403{
404 unsigned char *p = (unsigned char *)data;
0016d70b 405 int n;
3f2d010c 406
0016d70b 407 if (maxlen < 4)
408 return -1;
3f2d010c 409 p += 4; /* length word */
0016d70b 410 maxlen -= 4;
411
412 n = ssh1_read_bignum(p, maxlen, NULL); /* exponent */
413 if (n < 0)
414 return -1;
415 p += n;
416
417 n = ssh1_read_bignum(p, maxlen, NULL); /* modulus */
418 if (n < 0)
419 return -1;
420 p += n;
3f2d010c 421
422 return p - (unsigned char *)data;
423}
424
32874aea 425void freersakey(struct RSAKey *key)
426{
427 if (key->modulus)
428 freebn(key->modulus);
429 if (key->exponent)
430 freebn(key->exponent);
431 if (key->private_exponent)
432 freebn(key->private_exponent);
f5bcbcc2 433 if (key->p)
434 freebn(key->p);
435 if (key->q)
436 freebn(key->q);
437 if (key->iqmp)
438 freebn(key->iqmp);
32874aea 439 if (key->comment)
440 sfree(key->comment);
5c58ad2d 441}
85cc02bb 442
443/* ----------------------------------------------------------------------
444 * Implementation of the ssh-rsa signing key type.
445 */
446
32874aea 447static void getstring(char **data, int *datalen, char **p, int *length)
448{
85cc02bb 449 *p = NULL;
450 if (*datalen < 4)
32874aea 451 return;
85cc02bb 452 *length = GET_32BIT(*data);
32874aea 453 *datalen -= 4;
454 *data += 4;
85cc02bb 455 if (*datalen < *length)
32874aea 456 return;
85cc02bb 457 *p = *data;
32874aea 458 *data += *length;
459 *datalen -= *length;
85cc02bb 460}
32874aea 461static Bignum getmp(char **data, int *datalen)
462{
85cc02bb 463 char *p;
464 int length;
465 Bignum b;
466
467 getstring(data, datalen, &p, &length);
468 if (!p)
32874aea 469 return NULL;
9bf430c9 470 b = bignum_from_bytes((unsigned char *)p, length);
85cc02bb 471 return b;
472}
473
32874aea 474static void *rsa2_newkey(char *data, int len)
475{
85cc02bb 476 char *p;
477 int slen;
478 struct RSAKey *rsa;
479
3d88e64d 480 rsa = snew(struct RSAKey);
32874aea 481 if (!rsa)
482 return NULL;
85cc02bb 483 getstring(&data, &len, &p, &slen);
484
45cebe79 485 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
85cc02bb 486 sfree(rsa);
487 return NULL;
488 }
489 rsa->exponent = getmp(&data, &len);
490 rsa->modulus = getmp(&data, &len);
491 rsa->private_exponent = NULL;
492 rsa->comment = NULL;
493
494 return rsa;
495}
496
32874aea 497static void rsa2_freekey(void *key)
498{
499 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 500 freersakey(rsa);
501 sfree(rsa);
502}
503
32874aea 504static char *rsa2_fmtkey(void *key)
505{
506 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 507 char *p;
508 int len;
32874aea 509
85cc02bb 510 len = rsastr_len(rsa);
3d88e64d 511 p = snewn(len, char);
32874aea 512 rsastr_fmt(p, rsa);
85cc02bb 513 return p;
514}
515
32874aea 516static unsigned char *rsa2_public_blob(void *key, int *len)
517{
518 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 519 int elen, mlen, bloblen;
520 int i;
521 unsigned char *blob, *p;
522
32874aea 523 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
524 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
65a22376 525
526 /*
527 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
528 * (three length fields, 12+7=19).
529 */
32874aea 530 bloblen = 19 + elen + mlen;
3d88e64d 531 blob = snewn(bloblen, unsigned char);
65a22376 532 p = blob;
32874aea 533 PUT_32BIT(p, 7);
534 p += 4;
535 memcpy(p, "ssh-rsa", 7);
536 p += 7;
537 PUT_32BIT(p, elen);
538 p += 4;
539 for (i = elen; i--;)
540 *p++ = bignum_byte(rsa->exponent, i);
541 PUT_32BIT(p, mlen);
542 p += 4;
543 for (i = mlen; i--;)
544 *p++ = bignum_byte(rsa->modulus, i);
65a22376 545 assert(p == blob + bloblen);
546 *len = bloblen;
547 return blob;
548}
549
32874aea 550static unsigned char *rsa2_private_blob(void *key, int *len)
551{
552 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 553 int dlen, plen, qlen, ulen, bloblen;
554 int i;
555 unsigned char *blob, *p;
556
32874aea 557 dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
558 plen = (bignum_bitcount(rsa->p) + 8) / 8;
559 qlen = (bignum_bitcount(rsa->q) + 8) / 8;
560 ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
65a22376 561
562 /*
563 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
564 * sum of lengths.
565 */
32874aea 566 bloblen = 16 + dlen + plen + qlen + ulen;
3d88e64d 567 blob = snewn(bloblen, unsigned char);
65a22376 568 p = blob;
32874aea 569 PUT_32BIT(p, dlen);
570 p += 4;
571 for (i = dlen; i--;)
572 *p++ = bignum_byte(rsa->private_exponent, i);
573 PUT_32BIT(p, plen);
574 p += 4;
575 for (i = plen; i--;)
576 *p++ = bignum_byte(rsa->p, i);
577 PUT_32BIT(p, qlen);
578 p += 4;
579 for (i = qlen; i--;)
580 *p++ = bignum_byte(rsa->q, i);
581 PUT_32BIT(p, ulen);
582 p += 4;
583 for (i = ulen; i--;)
584 *p++ = bignum_byte(rsa->iqmp, i);
65a22376 585 assert(p == blob + bloblen);
586 *len = bloblen;
587 return blob;
588}
589
590static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
32874aea 591 unsigned char *priv_blob, int priv_len)
592{
65a22376 593 struct RSAKey *rsa;
32874aea 594 char *pb = (char *) priv_blob;
595
596 rsa = rsa2_newkey((char *) pub_blob, pub_len);
65a22376 597 rsa->private_exponent = getmp(&pb, &priv_len);
598 rsa->p = getmp(&pb, &priv_len);
599 rsa->q = getmp(&pb, &priv_len);
600 rsa->iqmp = getmp(&pb, &priv_len);
601
98f022f5 602 if (!rsa_verify(rsa)) {
603 rsa2_freekey(rsa);
604 return NULL;
605 }
606
65a22376 607 return rsa;
608}
609
32874aea 610static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
611{
612 char **b = (char **) blob;
45cebe79 613 struct RSAKey *rsa;
45cebe79 614
3d88e64d 615 rsa = snew(struct RSAKey);
32874aea 616 if (!rsa)
617 return NULL;
45cebe79 618 rsa->comment = NULL;
619
620 rsa->modulus = getmp(b, len);
621 rsa->exponent = getmp(b, len);
622 rsa->private_exponent = getmp(b, len);
623 rsa->iqmp = getmp(b, len);
624 rsa->p = getmp(b, len);
625 rsa->q = getmp(b, len);
626
627 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
628 !rsa->iqmp || !rsa->p || !rsa->q) {
629 sfree(rsa->modulus);
630 sfree(rsa->exponent);
631 sfree(rsa->private_exponent);
632 sfree(rsa->iqmp);
633 sfree(rsa->p);
634 sfree(rsa->q);
635 sfree(rsa);
636 return NULL;
637 }
638
639 return rsa;
640}
641
32874aea 642static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
643{
644 struct RSAKey *rsa = (struct RSAKey *) key;
ddecd643 645 int bloblen, i;
646
647 bloblen =
648 ssh2_bignum_length(rsa->modulus) +
649 ssh2_bignum_length(rsa->exponent) +
650 ssh2_bignum_length(rsa->private_exponent) +
651 ssh2_bignum_length(rsa->iqmp) +
32874aea 652 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
ddecd643 653
654 if (bloblen > len)
655 return bloblen;
656
657 bloblen = 0;
658#define ENC(x) \
659 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
660 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
661 ENC(rsa->modulus);
662 ENC(rsa->exponent);
663 ENC(rsa->private_exponent);
664 ENC(rsa->iqmp);
665 ENC(rsa->p);
666 ENC(rsa->q);
667
668 return bloblen;
669}
670
47a6b94c 671static int rsa2_pubkey_bits(void *blob, int len)
672{
673 struct RSAKey *rsa;
674 int ret;
675
676 rsa = rsa2_newkey((char *) blob, len);
677 ret = bignum_bitcount(rsa->modulus);
678 rsa2_freekey(rsa);
679
680 return ret;
681}
682
32874aea 683static char *rsa2_fingerprint(void *key)
684{
685 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 686 struct MD5Context md5c;
687 unsigned char digest[16], lenbuf[4];
32874aea 688 char buffer[16 * 3 + 40];
85cc02bb 689 char *ret;
690 int numlen, i;
691
692 MD5Init(&md5c);
9bf430c9 693 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
85cc02bb 694
695#define ADD_BIGNUM(bignum) \
ddecd643 696 numlen = (bignum_bitcount(bignum)+8)/8; \
85cc02bb 697 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
698 for (i = numlen; i-- ;) { \
699 unsigned char c = bignum_byte(bignum, i); \
700 MD5Update(&md5c, &c, 1); \
701 }
702 ADD_BIGNUM(rsa->exponent);
703 ADD_BIGNUM(rsa->modulus);
704#undef ADD_BIGNUM
705
706 MD5Final(digest, &md5c);
707
ddecd643 708 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
85cc02bb 709 for (i = 0; i < 16; i++)
32874aea 710 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
711 digest[i]);
3d88e64d 712 ret = snewn(strlen(buffer) + 1, char);
85cc02bb 713 if (ret)
32874aea 714 strcpy(ret, buffer);
85cc02bb 715 return ret;
716}
717
718/*
719 * This is the magic ASN.1/DER prefix that goes in the decoded
720 * signature, between the string of FFs and the actual SHA hash
96a73db9 721 * value. The meaning of it is:
85cc02bb 722 *
723 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
724 *
725 * 30 21 -- a constructed SEQUENCE of length 0x21
726 * 30 09 -- a constructed sub-SEQUENCE of length 9
727 * 06 05 -- an object identifier, length 5
96a73db9 728 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
729 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
85cc02bb 730 * 05 00 -- NULL
731 * 04 14 -- a primitive OCTET STRING of length 0x14
732 * [0x14 bytes of hash data follows]
96a73db9 733 *
734 * The object id in the middle there is listed as `id-sha1' in
735 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
736 * ASN module for PKCS #1) and its expanded form is as follows:
737 *
738 * id-sha1 OBJECT IDENTIFIER ::= {
739 * iso(1) identified-organization(3) oiw(14) secsig(3)
740 * algorithms(2) 26 }
85cc02bb 741 */
b5864f2c 742static const unsigned char asn1_weird_stuff[] = {
32874aea 743 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
744 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
85cc02bb 745};
746
d8770b12 747#define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
748
85cc02bb 749static int rsa2_verifysig(void *key, char *sig, int siglen,
32874aea 750 char *data, int datalen)
751{
752 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 753 Bignum in, out;
754 char *p;
755 int slen;
756 int bytes, i, j, ret;
757 unsigned char hash[20];
758
759 getstring(&sig, &siglen, &p, &slen);
760 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
32874aea 761 return 0;
85cc02bb 762 }
763 in = getmp(&sig, &siglen);
764 out = modpow(in, rsa->exponent, rsa->modulus);
765 freebn(in);
766
767 ret = 1;
768
7bd33644 769 bytes = (bignum_bitcount(rsa->modulus)+7) / 8;
85cc02bb 770 /* Top (partial) byte should be zero. */
32874aea 771 if (bignum_byte(out, bytes - 1) != 0)
772 ret = 0;
85cc02bb 773 /* First whole byte should be 1. */
32874aea 774 if (bignum_byte(out, bytes - 2) != 1)
775 ret = 0;
85cc02bb 776 /* Most of the rest should be FF. */
32874aea 777 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
778 if (bignum_byte(out, i) != 0xFF)
779 ret = 0;
85cc02bb 780 }
781 /* Then we expect to see the asn1_weird_stuff. */
32874aea 782 for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
783 if (bignum_byte(out, i) != asn1_weird_stuff[j])
784 ret = 0;
85cc02bb 785 }
786 /* Finally, we expect to see the SHA-1 hash of the signed data. */
787 SHA_Simple(data, datalen, hash);
32874aea 788 for (i = 19, j = 0; i >= 0; i--, j++) {
789 if (bignum_byte(out, i) != hash[j])
790 ret = 0;
85cc02bb 791 }
679539d7 792 freebn(out);
85cc02bb 793
794 return ret;
795}
796
164feb13 797static unsigned char *rsa2_sign(void *key, char *data, int datalen,
798 int *siglen)
32874aea 799{
800 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 801 unsigned char *bytes;
802 int nbytes;
803 unsigned char hash[20];
804 Bignum in, out;
805 int i, j;
806
807 SHA_Simple(data, datalen, hash);
808
32874aea 809 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
e99cd73f 810 assert(1 <= nbytes - 20 - ASN1_LEN);
3d88e64d 811 bytes = snewn(nbytes, unsigned char);
65a22376 812
813 bytes[0] = 1;
32874aea 814 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
65a22376 815 bytes[i] = 0xFF;
32874aea 816 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
65a22376 817 bytes[i] = asn1_weird_stuff[j];
32874aea 818 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
65a22376 819 bytes[i] = hash[j];
820
821 in = bignum_from_bytes(bytes, nbytes);
822 sfree(bytes);
823
8671a580 824 out = rsa_privkey_op(in, rsa);
65a22376 825 freebn(in);
826
32874aea 827 nbytes = (bignum_bitcount(out) + 7) / 8;
3d88e64d 828 bytes = snewn(4 + 7 + 4 + nbytes, unsigned char);
65a22376 829 PUT_32BIT(bytes, 7);
32874aea 830 memcpy(bytes + 4, "ssh-rsa", 7);
831 PUT_32BIT(bytes + 4 + 7, nbytes);
65a22376 832 for (i = 0; i < nbytes; i++)
32874aea 833 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
65a22376 834 freebn(out);
835
32874aea 836 *siglen = 4 + 7 + 4 + nbytes;
65a22376 837 return bytes;
85cc02bb 838}
839
65a22376 840const struct ssh_signkey ssh_rsa = {
85cc02bb 841 rsa2_newkey,
842 rsa2_freekey,
843 rsa2_fmtkey,
65a22376 844 rsa2_public_blob,
845 rsa2_private_blob,
846 rsa2_createkey,
45cebe79 847 rsa2_openssh_createkey,
ddecd643 848 rsa2_openssh_fmtkey,
47a6b94c 849 rsa2_pubkey_bits,
85cc02bb 850 rsa2_fingerprint,
851 rsa2_verifysig,
852 rsa2_sign,
853 "ssh-rsa",
854 "rsa2"
855};
fae1a71b 856
857void *ssh_rsakex_newkey(char *data, int len)
858{
859 return rsa2_newkey(data, len);
860}
861
862void ssh_rsakex_freekey(void *key)
863{
864 rsa2_freekey(key);
865}
866
867int ssh_rsakex_klen(void *key)
868{
869 struct RSAKey *rsa = (struct RSAKey *) key;
870
871 return bignum_bitcount(rsa->modulus);
872}
873
874static void oaep_mask(const struct ssh_hash *h, void *seed, int seedlen,
875 void *vdata, int datalen)
876{
877 unsigned char *data = (unsigned char *)vdata;
878 unsigned count = 0;
879
880 while (datalen > 0) {
881 int i, max = (datalen > h->hlen ? h->hlen : datalen);
882 void *s;
143ec28a 883 unsigned char counter[4], hash[SSH2_KEX_MAX_HASH_LEN];
fae1a71b 884
143ec28a 885 assert(h->hlen <= SSH2_KEX_MAX_HASH_LEN);
fae1a71b 886 PUT_32BIT(counter, count);
887 s = h->init();
888 h->bytes(s, seed, seedlen);
889 h->bytes(s, counter, 4);
890 h->final(s, hash);
891 count++;
892
893 for (i = 0; i < max; i++)
894 data[i] ^= hash[i];
895
896 data += max;
897 datalen -= max;
898 }
899}
900
901void ssh_rsakex_encrypt(const struct ssh_hash *h, unsigned char *in, int inlen,
902 unsigned char *out, int outlen,
903 void *key)
904{
905 Bignum b1, b2;
906 struct RSAKey *rsa = (struct RSAKey *) key;
907 int k, i;
908 char *p;
909 const int HLEN = h->hlen;
910
911 /*
912 * Here we encrypt using RSAES-OAEP. Essentially this means:
913 *
914 * - we have a SHA-based `mask generation function' which
915 * creates a pseudo-random stream of mask data
916 * deterministically from an input chunk of data.
917 *
918 * - we have a random chunk of data called a seed.
919 *
920 * - we use the seed to generate a mask which we XOR with our
921 * plaintext.
922 *
923 * - then we use _the masked plaintext_ to generate a mask
924 * which we XOR with the seed.
925 *
926 * - then we concatenate the masked seed and the masked
927 * plaintext, and RSA-encrypt that lot.
928 *
929 * The result is that the data input to the encryption function
930 * is random-looking and (hopefully) contains no exploitable
931 * structure such as PKCS1-v1_5 does.
932 *
933 * For a precise specification, see RFC 3447, section 7.1.1.
934 * Some of the variable names below are derived from that, so
935 * it'd probably help to read it anyway.
936 */
937
938 /* k denotes the length in octets of the RSA modulus. */
939 k = (7 + bignum_bitcount(rsa->modulus)) / 8;
940
941 /* The length of the input data must be at most k - 2hLen - 2. */
942 assert(inlen > 0 && inlen <= k - 2*HLEN - 2);
943
944 /* The length of the output data wants to be precisely k. */
945 assert(outlen == k);
946
947 /*
948 * Now perform EME-OAEP encoding. First set up all the unmasked
949 * output data.
950 */
951 /* Leading byte zero. */
952 out[0] = 0;
953 /* At position 1, the seed: HLEN bytes of random data. */
954 for (i = 0; i < HLEN; i++)
955 out[i + 1] = random_byte();
956 /* At position 1+HLEN, the data block DB, consisting of: */
957 /* The hash of the label (we only support an empty label here) */
958 h->final(h->init(), out + HLEN + 1);
959 /* A bunch of zero octets */
960 memset(out + 2*HLEN + 1, 0, outlen - (2*HLEN + 1));
961 /* A single 1 octet, followed by the input message data. */
962 out[outlen - inlen - 1] = 1;
963 memcpy(out + outlen - inlen, in, inlen);
964
965 /*
966 * Now use the seed data to mask the block DB.
967 */
968 oaep_mask(h, out+1, HLEN, out+HLEN+1, outlen-HLEN-1);
969
970 /*
971 * And now use the masked DB to mask the seed itself.
972 */
973 oaep_mask(h, out+HLEN+1, outlen-HLEN-1, out+1, HLEN);
974
975 /*
976 * Now `out' contains precisely the data we want to
977 * RSA-encrypt.
978 */
979 b1 = bignum_from_bytes(out, outlen);
980 b2 = modpow(b1, rsa->exponent, rsa->modulus);
7108a872 981 p = (char *)out;
fae1a71b 982 for (i = outlen; i--;) {
983 *p++ = bignum_byte(b2, i);
984 }
985 freebn(b1);
986 freebn(b2);
987
988 /*
989 * And we're done.
990 */
991}
992
993static const struct ssh_kex ssh_rsa_kex_sha1 = {
994 "rsa1024-sha1", NULL, KEXTYPE_RSA, NULL, NULL, 0, 0, &ssh_sha1
995};
996
997static const struct ssh_kex ssh_rsa_kex_sha256 = {
998 "rsa2048-sha256", NULL, KEXTYPE_RSA, NULL, NULL, 0, 0, &ssh_sha256
999};
1000
1001static const struct ssh_kex *const rsa_kex_list[] = {
1002 &ssh_rsa_kex_sha256,
1003 &ssh_rsa_kex_sha1
1004};
1005
1006const struct ssh_kexes ssh_rsa_kex = {
1007 sizeof(rsa_kex_list) / sizeof(*rsa_kex_list),
1008 rsa_kex_list
1009};