Fix off-by-one in selection update while scrolling. Thanks Richard B.
[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
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
033a3ded 150 * y and y^d; then we multiply x by y, raise to the power d mod
151 * n as usual, and divide by y^d to recover x^d. Thus an
152 * attacker can't correlate the timing of the modpow with the
153 * input, because they don't know anything about the number
154 * that was input to the actual modpow.
8671a580 155 *
156 * The clever bit is that we don't have to do a huge modpow to
157 * get y and y^d; we will use the number we just invented as
033a3ded 158 * _y^d_, and use the _public_ exponent to compute (y^d)^e = y
159 * from it, which is much faster to do.
8671a580 160 */
161 random_encrypted = modpow(random, key->exponent, key->modulus);
162 random_inverse = modinv(random, key->modulus);
163 input_blinded = modmul(input, random_encrypted, key->modulus);
164 ret_blinded = modpow(input_blinded, key->private_exponent, key->modulus);
165 ret = modmul(ret_blinded, random_inverse, key->modulus);
166
167 freebn(ret_blinded);
168 freebn(input_blinded);
169 freebn(random_inverse);
170 freebn(random_encrypted);
171 freebn(random);
172
7cca0d81 173 return ret;
174}
175
8671a580 176Bignum rsadecrypt(Bignum input, struct RSAKey *key)
177{
178 return rsa_privkey_op(input, key);
179}
180
32874aea 181int rsastr_len(struct RSAKey *key)
182{
374330e2 183 Bignum md, ex;
3709bfe9 184 int mdlen, exlen;
374330e2 185
186 md = key->modulus;
187 ex = key->exponent;
32874aea 188 mdlen = (bignum_bitcount(md) + 15) / 16;
189 exlen = (bignum_bitcount(ex) + 15) / 16;
190 return 4 * (mdlen + exlen) + 20;
374330e2 191}
192
32874aea 193void rsastr_fmt(char *str, struct RSAKey *key)
194{
374330e2 195 Bignum md, ex;
d5859615 196 int len = 0, i, nibbles;
197 static const char hex[] = "0123456789abcdef";
374330e2 198
199 md = key->modulus;
200 ex = key->exponent;
201
32874aea 202 len += sprintf(str + len, "0x");
d5859615 203
32874aea 204 nibbles = (3 + bignum_bitcount(ex)) / 4;
205 if (nibbles < 1)
206 nibbles = 1;
207 for (i = nibbles; i--;)
208 str[len++] = hex[(bignum_byte(ex, i / 2) >> (4 * (i % 2))) & 0xF];
d5859615 209
32874aea 210 len += sprintf(str + len, ",0x");
d5859615 211
32874aea 212 nibbles = (3 + bignum_bitcount(md)) / 4;
213 if (nibbles < 1)
214 nibbles = 1;
215 for (i = nibbles; i--;)
216 str[len++] = hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF];
d5859615 217
374330e2 218 str[len] = '\0';
219}
220
1c2a93c4 221/*
222 * Generate a fingerprint string for the key. Compatible with the
223 * OpenSSH fingerprint code.
224 */
32874aea 225void rsa_fingerprint(char *str, int len, struct RSAKey *key)
226{
1c2a93c4 227 struct MD5Context md5c;
228 unsigned char digest[16];
32874aea 229 char buffer[16 * 3 + 40];
1c2a93c4 230 int numlen, slen, i;
231
232 MD5Init(&md5c);
233 numlen = ssh1_bignum_length(key->modulus) - 2;
32874aea 234 for (i = numlen; i--;) {
235 unsigned char c = bignum_byte(key->modulus, i);
236 MD5Update(&md5c, &c, 1);
1c2a93c4 237 }
238 numlen = ssh1_bignum_length(key->exponent) - 2;
32874aea 239 for (i = numlen; i--;) {
240 unsigned char c = bignum_byte(key->exponent, i);
241 MD5Update(&md5c, &c, 1);
1c2a93c4 242 }
243 MD5Final(digest, &md5c);
244
ddecd643 245 sprintf(buffer, "%d ", bignum_bitcount(key->modulus));
1c2a93c4 246 for (i = 0; i < 16; i++)
32874aea 247 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
248 digest[i]);
249 strncpy(str, buffer, len);
250 str[len - 1] = '\0';
1c2a93c4 251 slen = strlen(str);
32874aea 252 if (key->comment && slen < len - 1) {
253 str[slen] = ' ';
254 strncpy(str + slen + 1, key->comment, len - slen - 1);
255 str[len - 1] = '\0';
1c2a93c4 256 }
257}
258
98f022f5 259/*
260 * Verify that the public data in an RSA key matches the private
60fe6ff7 261 * data. We also check the private data itself: we ensure that p >
262 * q and that iqmp really is the inverse of q mod p.
98f022f5 263 */
32874aea 264int rsa_verify(struct RSAKey *key)
265{
60fe6ff7 266 Bignum n, ed, pm1, qm1;
98f022f5 267 int cmp;
268
269 /* n must equal pq. */
270 n = bigmul(key->p, key->q);
271 cmp = bignum_cmp(n, key->modulus);
272 freebn(n);
273 if (cmp != 0)
274 return 0;
275
60fe6ff7 276 /* e * d must be congruent to 1, modulo (p-1) and modulo (q-1). */
98f022f5 277 pm1 = copybn(key->p);
278 decbn(pm1);
60fe6ff7 279 ed = modmul(key->exponent, key->private_exponent, pm1);
280 cmp = bignum_cmp(ed, One);
281 sfree(ed);
282 if (cmp != 0)
283 return 0;
284
98f022f5 285 qm1 = copybn(key->q);
286 decbn(qm1);
60fe6ff7 287 ed = modmul(key->exponent, key->private_exponent, qm1);
98f022f5 288 cmp = bignum_cmp(ed, One);
289 sfree(ed);
290 if (cmp != 0)
291 return 0;
014970c8 292
60fe6ff7 293 /*
294 * Ensure p > q.
295 */
296 if (bignum_cmp(key->p, key->q) <= 0)
32874aea 297 return 0;
60fe6ff7 298
299 /*
300 * Ensure iqmp * q is congruent to 1, modulo p.
301 */
302 n = modmul(key->iqmp, key->q, key->p);
303 cmp = bignum_cmp(n, One);
304 sfree(n);
305 if (cmp != 0)
32874aea 306 return 0;
60fe6ff7 307
014970c8 308 return 1;
98f022f5 309}
310
3f2d010c 311/* Public key blob as used by Pageant: exponent before modulus. */
312unsigned char *rsa_public_blob(struct RSAKey *key, int *len)
313{
314 int length, pos;
315 unsigned char *ret;
316
317 length = (ssh1_bignum_length(key->modulus) +
318 ssh1_bignum_length(key->exponent) + 4);
319 ret = smalloc(length);
320
321 PUT_32BIT(ret, bignum_bitcount(key->modulus));
322 pos = 4;
323 pos += ssh1_write_bignum(ret + pos, key->exponent);
324 pos += ssh1_write_bignum(ret + pos, key->modulus);
325
326 *len = length;
327 return ret;
328}
329
330/* Given a public blob, determine its length. */
331int rsa_public_blob_len(void *data)
332{
333 unsigned char *p = (unsigned char *)data;
3f2d010c 334
335 p += 4; /* length word */
336 p += ssh1_read_bignum(p, NULL); /* exponent */
337 p += ssh1_read_bignum(p, NULL); /* modulus */
338
339 return p - (unsigned char *)data;
340}
341
32874aea 342void freersakey(struct RSAKey *key)
343{
344 if (key->modulus)
345 freebn(key->modulus);
346 if (key->exponent)
347 freebn(key->exponent);
348 if (key->private_exponent)
349 freebn(key->private_exponent);
350 if (key->comment)
351 sfree(key->comment);
5c58ad2d 352}
85cc02bb 353
354/* ----------------------------------------------------------------------
355 * Implementation of the ssh-rsa signing key type.
356 */
357
32874aea 358static void getstring(char **data, int *datalen, char **p, int *length)
359{
85cc02bb 360 *p = NULL;
361 if (*datalen < 4)
32874aea 362 return;
85cc02bb 363 *length = GET_32BIT(*data);
32874aea 364 *datalen -= 4;
365 *data += 4;
85cc02bb 366 if (*datalen < *length)
32874aea 367 return;
85cc02bb 368 *p = *data;
32874aea 369 *data += *length;
370 *datalen -= *length;
85cc02bb 371}
32874aea 372static Bignum getmp(char **data, int *datalen)
373{
85cc02bb 374 char *p;
375 int length;
376 Bignum b;
377
378 getstring(data, datalen, &p, &length);
379 if (!p)
32874aea 380 return NULL;
9bf430c9 381 b = bignum_from_bytes((unsigned char *)p, length);
85cc02bb 382 return b;
383}
384
32874aea 385static void *rsa2_newkey(char *data, int len)
386{
85cc02bb 387 char *p;
388 int slen;
389 struct RSAKey *rsa;
390
391 rsa = smalloc(sizeof(struct RSAKey));
32874aea 392 if (!rsa)
393 return NULL;
85cc02bb 394 getstring(&data, &len, &p, &slen);
395
45cebe79 396 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
85cc02bb 397 sfree(rsa);
398 return NULL;
399 }
400 rsa->exponent = getmp(&data, &len);
401 rsa->modulus = getmp(&data, &len);
402 rsa->private_exponent = NULL;
403 rsa->comment = NULL;
404
405 return rsa;
406}
407
32874aea 408static void rsa2_freekey(void *key)
409{
410 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 411 freersakey(rsa);
412 sfree(rsa);
413}
414
32874aea 415static char *rsa2_fmtkey(void *key)
416{
417 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 418 char *p;
419 int len;
32874aea 420
85cc02bb 421 len = rsastr_len(rsa);
422 p = smalloc(len);
32874aea 423 rsastr_fmt(p, rsa);
85cc02bb 424 return p;
425}
426
32874aea 427static unsigned char *rsa2_public_blob(void *key, int *len)
428{
429 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 430 int elen, mlen, bloblen;
431 int i;
432 unsigned char *blob, *p;
433
32874aea 434 elen = (bignum_bitcount(rsa->exponent) + 8) / 8;
435 mlen = (bignum_bitcount(rsa->modulus) + 8) / 8;
65a22376 436
437 /*
438 * string "ssh-rsa", mpint exp, mpint mod. Total 19+elen+mlen.
439 * (three length fields, 12+7=19).
440 */
32874aea 441 bloblen = 19 + elen + mlen;
65a22376 442 blob = smalloc(bloblen);
443 p = blob;
32874aea 444 PUT_32BIT(p, 7);
445 p += 4;
446 memcpy(p, "ssh-rsa", 7);
447 p += 7;
448 PUT_32BIT(p, elen);
449 p += 4;
450 for (i = elen; i--;)
451 *p++ = bignum_byte(rsa->exponent, i);
452 PUT_32BIT(p, mlen);
453 p += 4;
454 for (i = mlen; i--;)
455 *p++ = bignum_byte(rsa->modulus, i);
65a22376 456 assert(p == blob + bloblen);
457 *len = bloblen;
458 return blob;
459}
460
32874aea 461static unsigned char *rsa2_private_blob(void *key, int *len)
462{
463 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 464 int dlen, plen, qlen, ulen, bloblen;
465 int i;
466 unsigned char *blob, *p;
467
32874aea 468 dlen = (bignum_bitcount(rsa->private_exponent) + 8) / 8;
469 plen = (bignum_bitcount(rsa->p) + 8) / 8;
470 qlen = (bignum_bitcount(rsa->q) + 8) / 8;
471 ulen = (bignum_bitcount(rsa->iqmp) + 8) / 8;
65a22376 472
473 /*
474 * mpint private_exp, mpint p, mpint q, mpint iqmp. Total 16 +
475 * sum of lengths.
476 */
32874aea 477 bloblen = 16 + dlen + plen + qlen + ulen;
65a22376 478 blob = smalloc(bloblen);
479 p = blob;
32874aea 480 PUT_32BIT(p, dlen);
481 p += 4;
482 for (i = dlen; i--;)
483 *p++ = bignum_byte(rsa->private_exponent, i);
484 PUT_32BIT(p, plen);
485 p += 4;
486 for (i = plen; i--;)
487 *p++ = bignum_byte(rsa->p, i);
488 PUT_32BIT(p, qlen);
489 p += 4;
490 for (i = qlen; i--;)
491 *p++ = bignum_byte(rsa->q, i);
492 PUT_32BIT(p, ulen);
493 p += 4;
494 for (i = ulen; i--;)
495 *p++ = bignum_byte(rsa->iqmp, i);
65a22376 496 assert(p == blob + bloblen);
497 *len = bloblen;
498 return blob;
499}
500
501static void *rsa2_createkey(unsigned char *pub_blob, int pub_len,
32874aea 502 unsigned char *priv_blob, int priv_len)
503{
65a22376 504 struct RSAKey *rsa;
32874aea 505 char *pb = (char *) priv_blob;
506
507 rsa = rsa2_newkey((char *) pub_blob, pub_len);
65a22376 508 rsa->private_exponent = getmp(&pb, &priv_len);
509 rsa->p = getmp(&pb, &priv_len);
510 rsa->q = getmp(&pb, &priv_len);
511 rsa->iqmp = getmp(&pb, &priv_len);
512
98f022f5 513 if (!rsa_verify(rsa)) {
514 rsa2_freekey(rsa);
515 return NULL;
516 }
517
65a22376 518 return rsa;
519}
520
32874aea 521static void *rsa2_openssh_createkey(unsigned char **blob, int *len)
522{
523 char **b = (char **) blob;
45cebe79 524 struct RSAKey *rsa;
45cebe79 525
526 rsa = smalloc(sizeof(struct RSAKey));
32874aea 527 if (!rsa)
528 return NULL;
45cebe79 529 rsa->comment = NULL;
530
531 rsa->modulus = getmp(b, len);
532 rsa->exponent = getmp(b, len);
533 rsa->private_exponent = getmp(b, len);
534 rsa->iqmp = getmp(b, len);
535 rsa->p = getmp(b, len);
536 rsa->q = getmp(b, len);
537
538 if (!rsa->modulus || !rsa->exponent || !rsa->private_exponent ||
539 !rsa->iqmp || !rsa->p || !rsa->q) {
540 sfree(rsa->modulus);
541 sfree(rsa->exponent);
542 sfree(rsa->private_exponent);
543 sfree(rsa->iqmp);
544 sfree(rsa->p);
545 sfree(rsa->q);
546 sfree(rsa);
547 return NULL;
548 }
549
550 return rsa;
551}
552
32874aea 553static int rsa2_openssh_fmtkey(void *key, unsigned char *blob, int len)
554{
555 struct RSAKey *rsa = (struct RSAKey *) key;
ddecd643 556 int bloblen, i;
557
558 bloblen =
559 ssh2_bignum_length(rsa->modulus) +
560 ssh2_bignum_length(rsa->exponent) +
561 ssh2_bignum_length(rsa->private_exponent) +
562 ssh2_bignum_length(rsa->iqmp) +
32874aea 563 ssh2_bignum_length(rsa->p) + ssh2_bignum_length(rsa->q);
ddecd643 564
565 if (bloblen > len)
566 return bloblen;
567
568 bloblen = 0;
569#define ENC(x) \
570 PUT_32BIT(blob+bloblen, ssh2_bignum_length((x))-4); bloblen += 4; \
571 for (i = ssh2_bignum_length((x))-4; i-- ;) blob[bloblen++]=bignum_byte((x),i);
572 ENC(rsa->modulus);
573 ENC(rsa->exponent);
574 ENC(rsa->private_exponent);
575 ENC(rsa->iqmp);
576 ENC(rsa->p);
577 ENC(rsa->q);
578
579 return bloblen;
580}
581
32874aea 582static char *rsa2_fingerprint(void *key)
583{
584 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 585 struct MD5Context md5c;
586 unsigned char digest[16], lenbuf[4];
32874aea 587 char buffer[16 * 3 + 40];
85cc02bb 588 char *ret;
589 int numlen, i;
590
591 MD5Init(&md5c);
9bf430c9 592 MD5Update(&md5c, (unsigned char *)"\0\0\0\7ssh-rsa", 11);
85cc02bb 593
594#define ADD_BIGNUM(bignum) \
ddecd643 595 numlen = (bignum_bitcount(bignum)+8)/8; \
85cc02bb 596 PUT_32BIT(lenbuf, numlen); MD5Update(&md5c, lenbuf, 4); \
597 for (i = numlen; i-- ;) { \
598 unsigned char c = bignum_byte(bignum, i); \
599 MD5Update(&md5c, &c, 1); \
600 }
601 ADD_BIGNUM(rsa->exponent);
602 ADD_BIGNUM(rsa->modulus);
603#undef ADD_BIGNUM
604
605 MD5Final(digest, &md5c);
606
ddecd643 607 sprintf(buffer, "ssh-rsa %d ", bignum_bitcount(rsa->modulus));
85cc02bb 608 for (i = 0; i < 16; i++)
32874aea 609 sprintf(buffer + strlen(buffer), "%s%02x", i ? ":" : "",
610 digest[i]);
611 ret = smalloc(strlen(buffer) + 1);
85cc02bb 612 if (ret)
32874aea 613 strcpy(ret, buffer);
85cc02bb 614 return ret;
615}
616
617/*
618 * This is the magic ASN.1/DER prefix that goes in the decoded
619 * signature, between the string of FFs and the actual SHA hash
96a73db9 620 * value. The meaning of it is:
85cc02bb 621 *
622 * 00 -- this marks the end of the FFs; not part of the ASN.1 bit itself
623 *
624 * 30 21 -- a constructed SEQUENCE of length 0x21
625 * 30 09 -- a constructed sub-SEQUENCE of length 9
626 * 06 05 -- an object identifier, length 5
96a73db9 627 * 2B 0E 03 02 1A -- object id { 1 3 14 3 2 26 }
628 * (the 1,3 comes from 0x2B = 43 = 40*1+3)
85cc02bb 629 * 05 00 -- NULL
630 * 04 14 -- a primitive OCTET STRING of length 0x14
631 * [0x14 bytes of hash data follows]
96a73db9 632 *
633 * The object id in the middle there is listed as `id-sha1' in
634 * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1d2.asn (the
635 * ASN module for PKCS #1) and its expanded form is as follows:
636 *
637 * id-sha1 OBJECT IDENTIFIER ::= {
638 * iso(1) identified-organization(3) oiw(14) secsig(3)
639 * algorithms(2) 26 }
85cc02bb 640 */
b5864f2c 641static const unsigned char asn1_weird_stuff[] = {
32874aea 642 0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B,
643 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14,
85cc02bb 644};
645
d8770b12 646#define ASN1_LEN ( (int) sizeof(asn1_weird_stuff) )
647
85cc02bb 648static int rsa2_verifysig(void *key, char *sig, int siglen,
32874aea 649 char *data, int datalen)
650{
651 struct RSAKey *rsa = (struct RSAKey *) key;
85cc02bb 652 Bignum in, out;
653 char *p;
654 int slen;
655 int bytes, i, j, ret;
656 unsigned char hash[20];
657
658 getstring(&sig, &siglen, &p, &slen);
659 if (!p || slen != 7 || memcmp(p, "ssh-rsa", 7)) {
32874aea 660 return 0;
85cc02bb 661 }
662 in = getmp(&sig, &siglen);
663 out = modpow(in, rsa->exponent, rsa->modulus);
664 freebn(in);
665
666 ret = 1;
667
ddecd643 668 bytes = bignum_bitcount(rsa->modulus) / 8;
85cc02bb 669 /* Top (partial) byte should be zero. */
32874aea 670 if (bignum_byte(out, bytes - 1) != 0)
671 ret = 0;
85cc02bb 672 /* First whole byte should be 1. */
32874aea 673 if (bignum_byte(out, bytes - 2) != 1)
674 ret = 0;
85cc02bb 675 /* Most of the rest should be FF. */
32874aea 676 for (i = bytes - 3; i >= 20 + ASN1_LEN; i--) {
677 if (bignum_byte(out, i) != 0xFF)
678 ret = 0;
85cc02bb 679 }
680 /* Then we expect to see the asn1_weird_stuff. */
32874aea 681 for (i = 20 + ASN1_LEN - 1, j = 0; i >= 20; i--, j++) {
682 if (bignum_byte(out, i) != asn1_weird_stuff[j])
683 ret = 0;
85cc02bb 684 }
685 /* Finally, we expect to see the SHA-1 hash of the signed data. */
686 SHA_Simple(data, datalen, hash);
32874aea 687 for (i = 19, j = 0; i >= 0; i--, j++) {
688 if (bignum_byte(out, i) != hash[j])
689 ret = 0;
85cc02bb 690 }
691
692 return ret;
693}
694
164feb13 695static unsigned char *rsa2_sign(void *key, char *data, int datalen,
696 int *siglen)
32874aea 697{
698 struct RSAKey *rsa = (struct RSAKey *) key;
65a22376 699 unsigned char *bytes;
700 int nbytes;
701 unsigned char hash[20];
702 Bignum in, out;
703 int i, j;
704
705 SHA_Simple(data, datalen, hash);
706
32874aea 707 nbytes = (bignum_bitcount(rsa->modulus) - 1) / 8;
65a22376 708 bytes = smalloc(nbytes);
709
710 bytes[0] = 1;
32874aea 711 for (i = 1; i < nbytes - 20 - ASN1_LEN; i++)
65a22376 712 bytes[i] = 0xFF;
32874aea 713 for (i = nbytes - 20 - ASN1_LEN, j = 0; i < nbytes - 20; i++, j++)
65a22376 714 bytes[i] = asn1_weird_stuff[j];
32874aea 715 for (i = nbytes - 20, j = 0; i < nbytes; i++, j++)
65a22376 716 bytes[i] = hash[j];
717
718 in = bignum_from_bytes(bytes, nbytes);
719 sfree(bytes);
720
8671a580 721 out = rsa_privkey_op(in, rsa);
65a22376 722 freebn(in);
723
32874aea 724 nbytes = (bignum_bitcount(out) + 7) / 8;
725 bytes = smalloc(4 + 7 + 4 + nbytes);
65a22376 726 PUT_32BIT(bytes, 7);
32874aea 727 memcpy(bytes + 4, "ssh-rsa", 7);
728 PUT_32BIT(bytes + 4 + 7, nbytes);
65a22376 729 for (i = 0; i < nbytes; i++)
32874aea 730 bytes[4 + 7 + 4 + i] = bignum_byte(out, nbytes - 1 - i);
65a22376 731 freebn(out);
732
32874aea 733 *siglen = 4 + 7 + 4 + nbytes;
65a22376 734 return bytes;
85cc02bb 735}
736
65a22376 737const struct ssh_signkey ssh_rsa = {
85cc02bb 738 rsa2_newkey,
739 rsa2_freekey,
740 rsa2_fmtkey,
65a22376 741 rsa2_public_blob,
742 rsa2_private_blob,
743 rsa2_createkey,
45cebe79 744 rsa2_openssh_createkey,
ddecd643 745 rsa2_openssh_fmtkey,
85cc02bb 746 rsa2_fingerprint,
747 rsa2_verifysig,
748 rsa2_sign,
749 "ssh-rsa",
750 "rsa2"
751};