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