blob: 0823929f4f5ec33d117daa173315566693f2e44a [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
Brian Smith28120a12015-03-13 02:26:34 +0800128 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, rsa, &rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700129
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 {
Adam Langley95c29f32014-06-20 12:00:00 -0700351 NID_undef, 0, {0},
352 },
353};
354
355/* TODO(fork): mostly new code, needs careful review. */
356
357/* pkcs1_prefixed_msg builds a PKCS#1, prefixed version of |msg| for the given
358 * hash function and sets |out_msg| to point to it. On successful return,
359 * |*out_msg| may be allocated memory and, if so, |*is_alloced| will be 1. */
360static int pkcs1_prefixed_msg(uint8_t **out_msg, size_t *out_msg_len,
361 int *is_alloced, int hash_nid, const uint8_t *msg,
362 size_t msg_len) {
363 unsigned i;
364 const uint8_t* prefix = NULL;
365 unsigned prefix_len;
366 uint8_t *signed_msg;
367 unsigned signed_msg_len;
368
369 if (hash_nid == NID_md5_sha1) {
370 /* Special case: SSL signature, just check the length. */
371 if (msg_len != SSL_SIG_LENGTH) {
Håvard Mollandab2479a2015-03-20 13:15:39 +0100372 OPENSSL_PUT_ERROR(RSA, pkcs1_prefixed_msg, RSA_R_INVALID_MESSAGE_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700373 return 0;
374 }
375
376 *out_msg = (uint8_t*) msg;
377 *out_msg_len = SSL_SIG_LENGTH;
378 *is_alloced = 0;
379 return 1;
380 }
381
382 for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
383 const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
384 if (sig_prefix->nid == hash_nid) {
385 prefix = sig_prefix->bytes;
386 prefix_len = sig_prefix->len;
387 break;
388 }
389 }
390
391 if (prefix == NULL) {
Håvard Mollandab2479a2015-03-20 13:15:39 +0100392 OPENSSL_PUT_ERROR(RSA, pkcs1_prefixed_msg, RSA_R_UNKNOWN_ALGORITHM_TYPE);
Adam Langley95c29f32014-06-20 12:00:00 -0700393 return 0;
394 }
395
396 signed_msg_len = prefix_len + msg_len;
397 if (signed_msg_len < prefix_len) {
Håvard Mollandab2479a2015-03-20 13:15:39 +0100398 OPENSSL_PUT_ERROR(RSA, pkcs1_prefixed_msg, RSA_R_TOO_LONG);
Adam Langley95c29f32014-06-20 12:00:00 -0700399 return 0;
400 }
401
402 signed_msg = OPENSSL_malloc(signed_msg_len);
403 if (!signed_msg) {
Håvard Mollandab2479a2015-03-20 13:15:39 +0100404 OPENSSL_PUT_ERROR(RSA, pkcs1_prefixed_msg, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700405 return 0;
406 }
407
408 memcpy(signed_msg, prefix, prefix_len);
409 memcpy(signed_msg + prefix_len, msg, msg_len);
410
411 *out_msg = signed_msg;
412 *out_msg_len = signed_msg_len;
413 *is_alloced = 1;
414
415 return 1;
416}
417
418int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
419 unsigned *out_len, RSA *rsa) {
420 const unsigned rsa_size = RSA_size(rsa);
421 int ret = 0;
422 uint8_t *signed_msg;
423 size_t signed_msg_len;
424 int signed_msg_is_alloced = 0;
425 size_t size_t_out_len;
426
427 if (rsa->meth->sign) {
428 return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
429 }
430
431 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
432 hash_nid, in, in_len)) {
433 return 0;
434 }
435
436 if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
437 signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
438 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
439 goto finish;
440 }
441
442 if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
443 signed_msg_len, RSA_PKCS1_PADDING)) {
444 *out_len = size_t_out_len;
445 ret = 1;
446 }
447
448finish:
449 if (signed_msg_is_alloced) {
450 OPENSSL_free(signed_msg);
451 }
452 return ret;
453}
454
455int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
456 const uint8_t *sig, size_t sig_len, RSA *rsa) {
457 const size_t rsa_size = RSA_size(rsa);
458 uint8_t *buf = NULL;
459 int ret = 0;
460 uint8_t *signed_msg = NULL;
461 size_t signed_msg_len, len;
462 int signed_msg_is_alloced = 0;
463
464 if (rsa->meth->verify) {
465 return rsa->meth->verify(hash_nid, msg, msg_len, sig, sig_len, rsa);
466 }
467
468 if (sig_len != rsa_size) {
469 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_WRONG_SIGNATURE_LENGTH);
470 return 0;
471 }
472
473 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
474 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_INVALID_MESSAGE_LENGTH);
475 return 0;
476 }
477
478 buf = OPENSSL_malloc(rsa_size);
479 if (!buf) {
480 OPENSSL_PUT_ERROR(RSA, RSA_verify, ERR_R_MALLOC_FAILURE);
481 return 0;
482 }
483
484 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
485 RSA_PKCS1_PADDING)) {
486 goto out;
487 }
488
489 if (!pkcs1_prefixed_msg(&signed_msg, &signed_msg_len, &signed_msg_is_alloced,
490 hash_nid, msg, msg_len)) {
491 goto out;
492 }
493
494 if (len != signed_msg_len || CRYPTO_memcmp(buf, signed_msg, len) != 0) {
495 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_BAD_SIGNATURE);
496 goto out;
497 }
498
499 ret = 1;
500
501out:
502 if (buf != NULL) {
503 OPENSSL_free(buf);
504 }
505 if (signed_msg_is_alloced) {
506 OPENSSL_free(signed_msg);
507 }
508 return ret;
509}
Adam Langley409766d2014-06-20 12:00:00 -0700510
511static void bn_free_and_null(BIGNUM **bn) {
512 if (*bn == NULL) {
513 return;
514 }
515
516 BN_free(*bn);
517 *bn = NULL;
518}
519
Adam Langley05b73772014-07-25 12:03:51 -0700520int RSA_check_key(const RSA *key) {
521 BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp;
522 BN_CTX *ctx;
523 int ok = 0, has_crt_values;
524
525 if (RSA_is_opaque(key)) {
526 /* Opaque keys can't be checked. */
527 return 1;
528 }
529
530 if ((key->p != NULL) != (key->q != NULL)) {
531 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
532 return 0;
533 }
534
535 if (!key->n || !key->e) {
536 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_VALUE_MISSING);
537 return 0;
538 }
539
540 if (!key->d || !key->p) {
541 /* For a public key, or without p and q, there's nothing that can be
542 * checked. */
543 return 1;
544 }
545
546 ctx = BN_CTX_new();
547 if (ctx == NULL) {
548 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_R_MALLOC_FAILURE);
549 return 0;
550 }
551
552 BN_init(&n);
553 BN_init(&pm1);
554 BN_init(&qm1);
555 BN_init(&lcm);
556 BN_init(&gcd);
557 BN_init(&de);
558 BN_init(&dmp1);
559 BN_init(&dmq1);
560 BN_init(&iqmp);
561
562 if (/* n = pq */
563 !BN_mul(&n, key->p, key->q, ctx) ||
564 /* lcm = lcm(p-1, q-1) */
565 !BN_sub(&pm1, key->p, BN_value_one()) ||
566 !BN_sub(&qm1, key->q, BN_value_one()) ||
567 !BN_mul(&lcm, &pm1, &qm1, ctx) ||
568 !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
569 !BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
570 /* de = d*e mod lcm(p-1, q-1) */
571 !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
572 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
573 goto out;
574 }
575
576 if (BN_cmp(&n, key->n) != 0) {
577 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_N_NOT_EQUAL_P_Q);
578 goto out;
579 }
580
581 if (!BN_is_one(&de)) {
582 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_D_E_NOT_CONGRUENT_TO_1);
583 goto out;
584 }
585
586 has_crt_values = key->dmp1 != NULL;
587 if (has_crt_values != (key->dmq1 != NULL) ||
588 has_crt_values != (key->iqmp != NULL)) {
589 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
590 goto out;
591 }
592
593 if (has_crt_values) {
594 if (/* dmp1 = d mod (p-1) */
595 !BN_mod(&dmp1, key->d, &pm1, ctx) ||
596 /* dmq1 = d mod (q-1) */
597 !BN_mod(&dmq1, key->d, &qm1, ctx) ||
598 /* iqmp = q^-1 mod p */
599 !BN_mod_inverse(&iqmp, key->q, key->p, ctx)) {
600 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
601 goto out;
602 }
603
604 if (BN_cmp(&dmp1, key->dmp1) != 0 ||
605 BN_cmp(&dmq1, key->dmq1) != 0 ||
606 BN_cmp(&iqmp, key->iqmp) != 0) {
607 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_CRT_VALUES_INCORRECT);
608 goto out;
609 }
610 }
611
612 ok = 1;
613
614out:
615 BN_free(&n);
616 BN_free(&pm1);
617 BN_free(&qm1);
618 BN_free(&lcm);
619 BN_free(&gcd);
620 BN_free(&de);
621 BN_free(&dmp1);
622 BN_free(&dmq1);
623 BN_free(&iqmp);
624 BN_CTX_free(ctx);
625
626 return ok;
627}
628
Adam Langley409766d2014-06-20 12:00:00 -0700629int RSA_recover_crt_params(RSA *rsa) {
630 BN_CTX *ctx;
631 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
632 int ok = 0;
633
634 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
635 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_EMPTY_PUBLIC_KEY);
636 return 0;
637 }
638
639 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
640 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params,
641 RSA_R_CRT_PARAMS_ALREADY_GIVEN);
642 return 0;
643 }
644
645 /* This uses the algorithm from section 9B of the RSA paper:
646 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
647
648 ctx = BN_CTX_new();
649 if (ctx == NULL) {
650 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
651 return 0;
652 }
653
654 BN_CTX_start(ctx);
655 totient = BN_CTX_get(ctx);
656 rem = BN_CTX_get(ctx);
657 multiple = BN_CTX_get(ctx);
658 p_plus_q = BN_CTX_get(ctx);
659 p_minus_q = BN_CTX_get(ctx);
660
661 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
662 p_minus_q == NULL) {
663 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
664 goto err;
665 }
666
667 /* ed-1 is a small multiple of φ(n). */
668 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
669 !BN_sub_word(totient, 1) ||
670 /* φ(n) =
671 * pq - p - q + 1 =
672 * n - (p + q) + 1
673 *
674 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
675 * close. But, when we calculate the quotient, we'll be truncating it
676 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
677 * which the totient cannot be. So we add one to the estimate.
678 *
679 * Consider ed-1 as:
680 *
681 * multiple * (n - (p+q) + 1) =
682 * multiple*n - multiple*(p+q) + multiple
683 *
684 * When we divide by n, the first term becomes multiple and, since
685 * multiple and p+q is tiny compared to n, the second and third terms can
686 * be ignored. Thus I claim that subtracting one from the estimate is
687 * sufficient. */
688 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
689 !BN_add_word(multiple, 1) ||
690 !BN_div(totient, rem, totient, multiple, ctx)) {
691 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
692 goto err;
693 }
694
695 if (!BN_is_zero(rem)) {
696 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_BAD_RSA_PARAMETERS);
697 goto err;
698 }
699
700 rsa->p = BN_new();
701 rsa->q = BN_new();
702 rsa->dmp1 = BN_new();
703 rsa->dmq1 = BN_new();
704 rsa->iqmp = BN_new();
705 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
706 NULL || rsa->iqmp == NULL) {
707 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
708 goto err;
709 }
710
711 /* φ(n) = n - (p + q) + 1 =>
712 * n - totient + 1 = p + q */
713 if (!BN_sub(p_plus_q, rsa->n, totient) ||
714 !BN_add_word(p_plus_q, 1) ||
715 /* p - q = sqrt((p+q)^2 - 4n) */
716 !BN_sqr(rem, p_plus_q, ctx) ||
717 !BN_lshift(multiple, rsa->n, 2) ||
718 !BN_sub(rem, rem, multiple) ||
719 !BN_sqrt(p_minus_q, rem, ctx) ||
720 /* q is 1/2 (p+q)-(p-q) */
721 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
722 !BN_rshift1(rsa->q, rsa->q) ||
723 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
724 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
725 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
726 goto err;
727 }
728
729 if (BN_cmp(multiple, rsa->n) != 0) {
730 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_INTERNAL_ERROR);
731 goto err;
732 }
733
734 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
735 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
736 !BN_sub(rem, rsa->q, BN_value_one()) ||
737 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
738 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
739 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
740 goto err;
741 }
742
743 ok = 1;
744
745err:
746 BN_CTX_end(ctx);
747 BN_CTX_free(ctx);
748 if (!ok) {
749 bn_free_and_null(&rsa->p);
750 bn_free_and_null(&rsa->q);
751 bn_free_and_null(&rsa->dmp1);
752 bn_free_and_null(&rsa->dmq1);
753 bn_free_and_null(&rsa->iqmp);
754 }
755 return ok;
756}
Adam Langley6bc658d2014-08-18 13:29:45 -0700757
758int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
759 size_t len) {
760 if (rsa->meth->private_transform) {
761 return rsa->meth->private_transform(rsa, out, in, len);
762 }
763
764 return RSA_default_method.private_transform(rsa, out, in, len);
765}