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