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