blob: 1ea5fef70e8914af55a233e89e77117fe1248218 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#include <openssl/rsa.h>
58
59#include <openssl/bn.h>
60#include <openssl/engine.h>
61#include <openssl/err.h>
62#include <openssl/ex_data.h>
63#include <openssl/mem.h>
64#include <openssl/obj.h>
65
66#include "internal.h"
67
68
69extern const RSA_METHOD RSA_default_method;
70
71RSA *RSA_new(void) { return RSA_new_method(NULL); }
72
73RSA *RSA_new_method(const ENGINE *engine) {
74 RSA *rsa = (RSA *)OPENSSL_malloc(sizeof(RSA));
75 if (rsa == NULL) {
76 OPENSSL_PUT_ERROR(RSA, RSA_new_method, ERR_R_MALLOC_FAILURE);
77 return NULL;
78 }
79
80 memset(rsa, 0, sizeof(RSA));
81
82 if (engine) {
83 rsa->meth = ENGINE_get_RSA_method(engine);
84 }
85
86 if (rsa->meth == NULL) {
87 rsa->meth = (RSA_METHOD*) &RSA_default_method;
88 }
89 METHOD_ref(rsa->meth);
90
91 rsa->references = 1;
92 rsa->flags = rsa->meth->flags;
93
94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data)) {
95 METHOD_unref(rsa->meth);
96 OPENSSL_free(rsa);
97 return NULL;
98 }
99
100 if (rsa->meth->init && !rsa->meth->init(rsa)) {
101 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data);
102 METHOD_unref(rsa->meth);
103 OPENSSL_free(rsa);
104 return NULL;
105 }
106
107 return rsa;
108}
109
110void RSA_free(RSA *rsa) {
111 unsigned u;
112
113 if (rsa == NULL) {
114 return;
115 }
116
117 if (CRYPTO_add(&rsa->references, -1, CRYPTO_LOCK_RSA) > 0) {
118 return;
119 }
120
121 if (rsa->meth->finish) {
122 rsa->meth->finish(rsa);
123 }
124 METHOD_unref(rsa->meth);
125
126 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, rsa, &rsa->ex_data);
127
128 if (rsa->n != NULL)
129 BN_clear_free(rsa->n);
130 if (rsa->e != NULL)
131 BN_clear_free(rsa->e);
132 if (rsa->d != NULL)
133 BN_clear_free(rsa->d);
134 if (rsa->p != NULL)
135 BN_clear_free(rsa->p);
136 if (rsa->q != NULL)
137 BN_clear_free(rsa->q);
138 if (rsa->dmp1 != NULL)
139 BN_clear_free(rsa->dmp1);
140 if (rsa->dmq1 != NULL)
141 BN_clear_free(rsa->dmq1);
142 if (rsa->iqmp != NULL)
143 BN_clear_free(rsa->iqmp);
144 for (u = 0; u < rsa->num_blindings; u++) {
145 BN_BLINDING_free(rsa->blindings[u]);
146 }
147 if (rsa->blindings != NULL)
148 OPENSSL_free(rsa->blindings);
149 if (rsa->blindings_inuse != NULL)
150 OPENSSL_free(rsa->blindings_inuse);
151 OPENSSL_free(rsa);
152}
153
154int RSA_up_ref(RSA *rsa) {
155 CRYPTO_add(&rsa->references, 1, CRYPTO_LOCK_RSA);
156 return 1;
157}
158
159int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
160 if (rsa->meth->keygen) {
161 return rsa->meth->keygen(rsa, bits, e_value, cb);
162 }
163
164 return RSA_default_method.keygen(rsa, bits, e_value, cb);
165}
166
167int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
168 const uint8_t *in, size_t in_len, int padding) {
169 if (rsa->meth->encrypt) {
170 return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
171 }
172
173 return RSA_default_method.encrypt(rsa, out_len, out, max_out, in, in_len,
174 padding);
175}
176
177int RSA_public_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
178 int padding) {
179 size_t out_len;
180
181 if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
182 return -1;
183 }
184
185 return out_len;
186}
187
188int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
189 const uint8_t *in, size_t in_len, int padding) {
190 if (rsa->meth->sign_raw) {
191 return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
192 }
193
194 return RSA_default_method.sign_raw(rsa, out_len, out, max_out, in, in_len,
195 padding);
196}
197
198int RSA_private_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
199 int padding) {
200 size_t out_len;
201
202 if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
203 return -1;
204 }
205
206 return out_len;
207}
208
209int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
210 const uint8_t *in, size_t in_len, int padding) {
211 if (rsa->meth->decrypt) {
212 return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
213 }
214
215 return RSA_default_method.decrypt(rsa, out_len, out, max_out, in, in_len,
216 padding);
217}
218
219int RSA_private_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
220 int padding) {
221 size_t out_len;
222
223 if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
224 return -1;
225 }
226
227 return out_len;
228}
229
230int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
231 const uint8_t *in, size_t in_len, int padding) {
232 if (rsa->meth->verify_raw) {
233 return rsa->meth->verify_raw(rsa, out_len, out, max_out, in, in_len, padding);
234 }
235
236 return RSA_default_method.verify_raw(rsa, out_len, out, max_out, in, in_len,
237 padding);
238}
239
240int RSA_public_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
241 int padding) {
242 size_t out_len;
243
244 if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
245 return -1;
246 }
247
248 return out_len;
249}
250
251unsigned RSA_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400252 if (rsa->meth->size) {
253 return rsa->meth->size(rsa);
254 }
255
256 return RSA_default_method.size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700257}
258
David Benjaminecc0ce72014-07-18 18:39:42 -0400259int RSA_is_opaque(const RSA *rsa) {
260 return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
261}
262
David Benjaminc20febe2014-11-11 23:47:50 -0500263int RSA_supports_digest(const RSA *rsa, const EVP_MD *md) {
264 if (rsa->meth && rsa->meth->supports_digest) {
265 return rsa->meth->supports_digest(rsa, md);
266 }
267 return 1;
268}
269
Adam Langley95c29f32014-06-20 12:00:00 -0700270int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
271 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
272 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, new_func,
273 dup_func, free_func);
274}
275
276int RSA_set_ex_data(RSA *d, int idx, void *arg) {
277 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
278}
279
280void *RSA_get_ex_data(const RSA *d, int idx) {
281 return CRYPTO_get_ex_data(&d->ex_data, idx);
282}
283
284/* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
285 * the length of an MD5 and SHA1 hash. */
286static const unsigned SSL_SIG_LENGTH = 36;
287
288/* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
289 * to be signed with PKCS#1. */
290struct pkcs1_sig_prefix {
291 /* nid identifies the hash function. */
292 int nid;
293 /* len is the number of bytes of |bytes| which are valid. */
294 uint8_t len;
295 /* bytes contains the DER bytes. */
296 uint8_t bytes[19];
297};
298
299/* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
300 * different hash functions. */
301static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
302 {
303 NID_md5,
304 18,
305 {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
306 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
307 },
308 {
309 NID_sha1,
310 15,
311 {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
312 0x00, 0x04, 0x14},
313 },
314 {
315 NID_sha224,
316 19,
317 {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
318 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
319 },
320 {
321 NID_sha256,
322 19,
323 {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
324 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
325 },
326 {
327 NID_sha384,
328 19,
329 {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
330 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
331 },
332 {
333 NID_sha512,
334 19,
335 {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
336 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
337 },
338 {
339 NID_ripemd160,
340 14,
341 {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31,
342 0x04, 0x14},
343 },
344 {
345 NID_undef, 0, {0},
346 },
347};
348
349/* TODO(fork): mostly new code, needs careful review. */
350
351/* pkcs1_prefixed_msg builds a PKCS#1, prefixed version of |msg| for the given
352 * hash function and sets |out_msg| to point to it. On successful return,
353 * |*out_msg| may be allocated memory and, if so, |*is_alloced| will be 1. */
354static int pkcs1_prefixed_msg(uint8_t **out_msg, size_t *out_msg_len,
355 int *is_alloced, int hash_nid, const uint8_t *msg,
356 size_t msg_len) {
357 unsigned i;
358 const uint8_t* prefix = NULL;
359 unsigned prefix_len;
360 uint8_t *signed_msg;
361 unsigned signed_msg_len;
362
363 if (hash_nid == NID_md5_sha1) {
364 /* Special case: SSL signature, just check the length. */
365 if (msg_len != SSL_SIG_LENGTH) {
366 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_INVALID_MESSAGE_LENGTH);
367 return 0;
368 }
369
370 *out_msg = (uint8_t*) msg;
371 *out_msg_len = SSL_SIG_LENGTH;
372 *is_alloced = 0;
373 return 1;
374 }
375
376 for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
377 const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
378 if (sig_prefix->nid == hash_nid) {
379 prefix = sig_prefix->bytes;
380 prefix_len = sig_prefix->len;
381 break;
382 }
383 }
384
385 if (prefix == NULL) {
386 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_UNKNOWN_ALGORITHM_TYPE);
387 return 0;
388 }
389
390 signed_msg_len = prefix_len + msg_len;
391 if (signed_msg_len < prefix_len) {
392 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_TOO_LONG);
393 return 0;
394 }
395
396 signed_msg = OPENSSL_malloc(signed_msg_len);
397 if (!signed_msg) {
398 OPENSSL_PUT_ERROR(RSA, RSA_sign, ERR_R_MALLOC_FAILURE);
399 return 0;
400 }
401
402 memcpy(signed_msg, prefix, prefix_len);
403 memcpy(signed_msg + prefix_len, msg, msg_len);
404
405 *out_msg = signed_msg;
406 *out_msg_len = signed_msg_len;
407 *is_alloced = 1;
408
409 return 1;
410}
411
412int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
413 unsigned *out_len, RSA *rsa) {
414 const unsigned rsa_size = RSA_size(rsa);
415 int ret = 0;
416 uint8_t *signed_msg;
417 size_t signed_msg_len;
418 int signed_msg_is_alloced = 0;
419 size_t size_t_out_len;
420
421 if (rsa->meth->sign) {
422 return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
423 }
424
425 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
426 hash_nid, in, in_len)) {
427 return 0;
428 }
429
430 if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
431 signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
432 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
433 goto finish;
434 }
435
436 if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
437 signed_msg_len, RSA_PKCS1_PADDING)) {
438 *out_len = size_t_out_len;
439 ret = 1;
440 }
441
442finish:
443 if (signed_msg_is_alloced) {
444 OPENSSL_free(signed_msg);
445 }
446 return ret;
447}
448
449int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
450 const uint8_t *sig, size_t sig_len, RSA *rsa) {
451 const size_t rsa_size = RSA_size(rsa);
452 uint8_t *buf = NULL;
453 int ret = 0;
454 uint8_t *signed_msg = NULL;
455 size_t signed_msg_len, len;
456 int signed_msg_is_alloced = 0;
457
458 if (rsa->meth->verify) {
459 return rsa->meth->verify(hash_nid, msg, msg_len, sig, sig_len, rsa);
460 }
461
462 if (sig_len != rsa_size) {
463 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_WRONG_SIGNATURE_LENGTH);
464 return 0;
465 }
466
467 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
468 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_INVALID_MESSAGE_LENGTH);
469 return 0;
470 }
471
472 buf = OPENSSL_malloc(rsa_size);
473 if (!buf) {
474 OPENSSL_PUT_ERROR(RSA, RSA_verify, ERR_R_MALLOC_FAILURE);
475 return 0;
476 }
477
478 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
479 RSA_PKCS1_PADDING)) {
480 goto out;
481 }
482
483 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
484 hash_nid, msg, msg_len)) {
485 goto out;
486 }
487
488 if (len != signed_msg_len || CRYPTO_memcmp(buf, signed_msg, len) != 0) {
489 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_BAD_SIGNATURE);
490 goto out;
491 }
492
493 ret = 1;
494
495out:
496 if (buf != NULL) {
497 OPENSSL_free(buf);
498 }
499 if (signed_msg_is_alloced) {
500 OPENSSL_free(signed_msg);
501 }
502 return ret;
503}
Adam Langley409766d2014-06-20 12:00:00 -0700504
505static void bn_free_and_null(BIGNUM **bn) {
506 if (*bn == NULL) {
507 return;
508 }
509
510 BN_free(*bn);
511 *bn = NULL;
512}
513
Adam Langley05b73772014-07-25 12:03:51 -0700514int RSA_check_key(const RSA *key) {
515 BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp;
516 BN_CTX *ctx;
517 int ok = 0, has_crt_values;
518
519 if (RSA_is_opaque(key)) {
520 /* Opaque keys can't be checked. */
521 return 1;
522 }
523
524 if ((key->p != NULL) != (key->q != NULL)) {
525 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
526 return 0;
527 }
528
529 if (!key->n || !key->e) {
530 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_VALUE_MISSING);
531 return 0;
532 }
533
534 if (!key->d || !key->p) {
535 /* For a public key, or without p and q, there's nothing that can be
536 * checked. */
537 return 1;
538 }
539
540 ctx = BN_CTX_new();
541 if (ctx == NULL) {
542 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_R_MALLOC_FAILURE);
543 return 0;
544 }
545
546 BN_init(&n);
547 BN_init(&pm1);
548 BN_init(&qm1);
549 BN_init(&lcm);
550 BN_init(&gcd);
551 BN_init(&de);
552 BN_init(&dmp1);
553 BN_init(&dmq1);
554 BN_init(&iqmp);
555
556 if (/* n = pq */
557 !BN_mul(&n, key->p, key->q, ctx) ||
558 /* lcm = lcm(p-1, q-1) */
559 !BN_sub(&pm1, key->p, BN_value_one()) ||
560 !BN_sub(&qm1, key->q, BN_value_one()) ||
561 !BN_mul(&lcm, &pm1, &qm1, ctx) ||
562 !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
563 !BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
564 /* de = d*e mod lcm(p-1, q-1) */
565 !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
566 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
567 goto out;
568 }
569
570 if (BN_cmp(&n, key->n) != 0) {
571 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_N_NOT_EQUAL_P_Q);
572 goto out;
573 }
574
575 if (!BN_is_one(&de)) {
576 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_D_E_NOT_CONGRUENT_TO_1);
577 goto out;
578 }
579
580 has_crt_values = key->dmp1 != NULL;
581 if (has_crt_values != (key->dmq1 != NULL) ||
582 has_crt_values != (key->iqmp != NULL)) {
583 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
584 goto out;
585 }
586
587 if (has_crt_values) {
588 if (/* dmp1 = d mod (p-1) */
589 !BN_mod(&dmp1, key->d, &pm1, ctx) ||
590 /* dmq1 = d mod (q-1) */
591 !BN_mod(&dmq1, key->d, &qm1, ctx) ||
592 /* iqmp = q^-1 mod p */
593 !BN_mod_inverse(&iqmp, key->q, key->p, ctx)) {
594 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
595 goto out;
596 }
597
598 if (BN_cmp(&dmp1, key->dmp1) != 0 ||
599 BN_cmp(&dmq1, key->dmq1) != 0 ||
600 BN_cmp(&iqmp, key->iqmp) != 0) {
601 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_CRT_VALUES_INCORRECT);
602 goto out;
603 }
604 }
605
606 ok = 1;
607
608out:
609 BN_free(&n);
610 BN_free(&pm1);
611 BN_free(&qm1);
612 BN_free(&lcm);
613 BN_free(&gcd);
614 BN_free(&de);
615 BN_free(&dmp1);
616 BN_free(&dmq1);
617 BN_free(&iqmp);
618 BN_CTX_free(ctx);
619
620 return ok;
621}
622
Adam Langley409766d2014-06-20 12:00:00 -0700623int RSA_recover_crt_params(RSA *rsa) {
624 BN_CTX *ctx;
625 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
626 int ok = 0;
627
628 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
629 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_EMPTY_PUBLIC_KEY);
630 return 0;
631 }
632
633 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
634 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params,
635 RSA_R_CRT_PARAMS_ALREADY_GIVEN);
636 return 0;
637 }
638
639 /* This uses the algorithm from section 9B of the RSA paper:
640 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
641
642 ctx = BN_CTX_new();
643 if (ctx == NULL) {
644 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
645 return 0;
646 }
647
648 BN_CTX_start(ctx);
649 totient = BN_CTX_get(ctx);
650 rem = BN_CTX_get(ctx);
651 multiple = BN_CTX_get(ctx);
652 p_plus_q = BN_CTX_get(ctx);
653 p_minus_q = BN_CTX_get(ctx);
654
655 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
656 p_minus_q == NULL) {
657 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
658 goto err;
659 }
660
661 /* ed-1 is a small multiple of φ(n). */
662 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
663 !BN_sub_word(totient, 1) ||
664 /* φ(n) =
665 * pq - p - q + 1 =
666 * n - (p + q) + 1
667 *
668 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
669 * close. But, when we calculate the quotient, we'll be truncating it
670 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
671 * which the totient cannot be. So we add one to the estimate.
672 *
673 * Consider ed-1 as:
674 *
675 * multiple * (n - (p+q) + 1) =
676 * multiple*n - multiple*(p+q) + multiple
677 *
678 * When we divide by n, the first term becomes multiple and, since
679 * multiple and p+q is tiny compared to n, the second and third terms can
680 * be ignored. Thus I claim that subtracting one from the estimate is
681 * sufficient. */
682 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
683 !BN_add_word(multiple, 1) ||
684 !BN_div(totient, rem, totient, multiple, ctx)) {
685 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
686 goto err;
687 }
688
689 if (!BN_is_zero(rem)) {
690 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_BAD_RSA_PARAMETERS);
691 goto err;
692 }
693
694 rsa->p = BN_new();
695 rsa->q = BN_new();
696 rsa->dmp1 = BN_new();
697 rsa->dmq1 = BN_new();
698 rsa->iqmp = BN_new();
699 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
700 NULL || rsa->iqmp == NULL) {
701 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
702 goto err;
703 }
704
705 /* φ(n) = n - (p + q) + 1 =>
706 * n - totient + 1 = p + q */
707 if (!BN_sub(p_plus_q, rsa->n, totient) ||
708 !BN_add_word(p_plus_q, 1) ||
709 /* p - q = sqrt((p+q)^2 - 4n) */
710 !BN_sqr(rem, p_plus_q, ctx) ||
711 !BN_lshift(multiple, rsa->n, 2) ||
712 !BN_sub(rem, rem, multiple) ||
713 !BN_sqrt(p_minus_q, rem, ctx) ||
714 /* q is 1/2 (p+q)-(p-q) */
715 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
716 !BN_rshift1(rsa->q, rsa->q) ||
717 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
718 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
719 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
720 goto err;
721 }
722
723 if (BN_cmp(multiple, rsa->n) != 0) {
724 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_INTERNAL_ERROR);
725 goto err;
726 }
727
728 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
729 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
730 !BN_sub(rem, rsa->q, BN_value_one()) ||
731 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
732 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
733 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
734 goto err;
735 }
736
737 ok = 1;
738
739err:
740 BN_CTX_end(ctx);
741 BN_CTX_free(ctx);
742 if (!ok) {
743 bn_free_and_null(&rsa->p);
744 bn_free_and_null(&rsa->q);
745 bn_free_and_null(&rsa->dmp1);
746 bn_free_and_null(&rsa->dmq1);
747 bn_free_and_null(&rsa->iqmp);
748 }
749 return ok;
750}
Adam Langley6bc658d2014-08-18 13:29:45 -0700751
752int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
753 size_t len) {
754 if (rsa->meth->private_transform) {
755 return rsa->meth->private_transform(rsa, out, in, len);
756 }
757
758 return RSA_default_method.private_transform(rsa, out, in, len);
759}