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