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