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