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