blob: 6fdc349c5efe507d2467956f5f72b8eb61743fdc [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
Adam Langley839b8812015-05-26 11:36:46 -0700117void RSA_additional_prime_free(RSA_additional_prime *ap) {
118 if (ap == NULL) {
119 return;
120 }
121
122 BN_clear_free(ap->prime);
123 BN_clear_free(ap->exp);
124 BN_clear_free(ap->coeff);
125 BN_clear_free(ap->r);
126 OPENSSL_free(ap);
127}
128
Adam Langley95c29f32014-06-20 12:00:00 -0700129void RSA_free(RSA *rsa) {
130 unsigned u;
131
132 if (rsa == NULL) {
133 return;
134 }
135
Adam Langley0da323a2015-05-15 12:49:30 -0700136 if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700137 return;
138 }
139
140 if (rsa->meth->finish) {
141 rsa->meth->finish(rsa);
142 }
143 METHOD_unref(rsa->meth);
144
David Benjamin9f33fc62015-04-15 17:29:53 -0400145 CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamind8b65c82015-04-22 16:09:09 -0400147 BN_clear_free(rsa->n);
148 BN_clear_free(rsa->e);
149 BN_clear_free(rsa->d);
150 BN_clear_free(rsa->p);
151 BN_clear_free(rsa->q);
152 BN_clear_free(rsa->dmp1);
153 BN_clear_free(rsa->dmq1);
154 BN_clear_free(rsa->iqmp);
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 Benjamind8b65c82015-04-22 16:09:09 -0400158 OPENSSL_free(rsa->blindings);
159 OPENSSL_free(rsa->blindings_inuse);
Adam Langley839b8812015-05-26 11:36:46 -0700160 if (rsa->additional_primes != NULL) {
161 sk_RSA_additional_prime_pop_free(rsa->additional_primes,
162 RSA_additional_prime_free);
163 }
Adam Langley683d7bd2015-04-13 11:04:14 -0700164 CRYPTO_MUTEX_cleanup(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700165 OPENSSL_free(rsa);
166}
167
168int RSA_up_ref(RSA *rsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700169 CRYPTO_refcount_inc(&rsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700170 return 1;
171}
172
173int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
174 if (rsa->meth->keygen) {
175 return rsa->meth->keygen(rsa, bits, e_value, cb);
176 }
177
178 return RSA_default_method.keygen(rsa, bits, e_value, cb);
179}
180
Adam Langley839b8812015-05-26 11:36:46 -0700181int RSA_generate_multi_prime_key(RSA *rsa, int bits, int num_primes,
182 BIGNUM *e_value, BN_GENCB *cb) {
183 if (rsa->meth->multi_prime_keygen) {
184 return rsa->meth->multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
185 }
186
187 return RSA_default_method.multi_prime_keygen(rsa, bits, num_primes, e_value,
188 cb);
189}
190
Adam Langley95c29f32014-06-20 12:00:00 -0700191int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
192 const uint8_t *in, size_t in_len, int padding) {
193 if (rsa->meth->encrypt) {
194 return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
195 }
196
197 return RSA_default_method.encrypt(rsa, out_len, out, max_out, in, in_len,
198 padding);
199}
200
201int RSA_public_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
202 int padding) {
203 size_t out_len;
204
205 if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
206 return -1;
207 }
208
209 return out_len;
210}
211
212int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
213 const uint8_t *in, size_t in_len, int padding) {
214 if (rsa->meth->sign_raw) {
215 return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
216 }
217
218 return RSA_default_method.sign_raw(rsa, out_len, out, max_out, in, in_len,
219 padding);
220}
221
222int RSA_private_encrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
223 int padding) {
224 size_t out_len;
225
226 if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
227 return -1;
228 }
229
230 return out_len;
231}
232
233int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
234 const uint8_t *in, size_t in_len, int padding) {
235 if (rsa->meth->decrypt) {
236 return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
237 }
238
239 return RSA_default_method.decrypt(rsa, out_len, out, max_out, in, in_len,
240 padding);
241}
242
243int RSA_private_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
244 int padding) {
245 size_t out_len;
246
247 if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
248 return -1;
249 }
250
251 return out_len;
252}
253
254int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
255 const uint8_t *in, size_t in_len, int padding) {
256 if (rsa->meth->verify_raw) {
257 return rsa->meth->verify_raw(rsa, out_len, out, max_out, in, in_len, padding);
258 }
259
260 return RSA_default_method.verify_raw(rsa, out_len, out, max_out, in, in_len,
261 padding);
262}
263
264int RSA_public_decrypt(int flen, const uint8_t *from, uint8_t *to, RSA *rsa,
265 int padding) {
266 size_t out_len;
267
268 if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
269 return -1;
270 }
271
272 return out_len;
273}
274
275unsigned RSA_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400276 if (rsa->meth->size) {
277 return rsa->meth->size(rsa);
278 }
279
280 return RSA_default_method.size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700281}
282
David Benjaminecc0ce72014-07-18 18:39:42 -0400283int RSA_is_opaque(const RSA *rsa) {
284 return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
285}
286
David Benjaminc20febe2014-11-11 23:47:50 -0500287int RSA_supports_digest(const RSA *rsa, const EVP_MD *md) {
288 if (rsa->meth && rsa->meth->supports_digest) {
289 return rsa->meth->supports_digest(rsa, md);
290 }
291 return 1;
292}
293
Adam Langley95c29f32014-06-20 12:00:00 -0700294int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
295 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400296 int index;
297 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
298 dup_func, free_func)) {
299 return -1;
300 }
301 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700302}
303
304int RSA_set_ex_data(RSA *d, int idx, void *arg) {
305 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
306}
307
308void *RSA_get_ex_data(const RSA *d, int idx) {
309 return CRYPTO_get_ex_data(&d->ex_data, idx);
310}
311
312/* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
313 * the length of an MD5 and SHA1 hash. */
314static const unsigned SSL_SIG_LENGTH = 36;
315
316/* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
317 * to be signed with PKCS#1. */
318struct pkcs1_sig_prefix {
319 /* nid identifies the hash function. */
320 int nid;
321 /* len is the number of bytes of |bytes| which are valid. */
322 uint8_t len;
323 /* bytes contains the DER bytes. */
324 uint8_t bytes[19];
325};
326
327/* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
328 * different hash functions. */
329static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
330 {
331 NID_md5,
332 18,
333 {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
334 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
335 },
336 {
337 NID_sha1,
338 15,
339 {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
340 0x00, 0x04, 0x14},
341 },
342 {
343 NID_sha224,
344 19,
345 {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
346 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
347 },
348 {
349 NID_sha256,
350 19,
351 {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
352 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
353 },
354 {
355 NID_sha384,
356 19,
357 {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
358 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
359 },
360 {
361 NID_sha512,
362 19,
363 {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
364 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
365 },
366 {
Adam Langley95c29f32014-06-20 12:00:00 -0700367 NID_undef, 0, {0},
368 },
369};
370
David Benjaminb0acb772015-05-28 16:53:31 -0400371int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
372 int *is_alloced, int hash_nid, const uint8_t *msg,
373 size_t msg_len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700374 unsigned i;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
376 if (hash_nid == NID_md5_sha1) {
377 /* Special case: SSL signature, just check the length. */
378 if (msg_len != SSL_SIG_LENGTH) {
David Benjaminb0acb772015-05-28 16:53:31 -0400379 OPENSSL_PUT_ERROR(RSA, RSA_add_pkcs1_prefix,
380 RSA_R_INVALID_MESSAGE_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700381 return 0;
382 }
383
384 *out_msg = (uint8_t*) msg;
385 *out_msg_len = SSL_SIG_LENGTH;
386 *is_alloced = 0;
387 return 1;
388 }
389
390 for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
391 const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
Brian Smitha039d702015-01-29 15:03:18 -0800392 if (sig_prefix->nid != hash_nid) {
393 continue;
Adam Langley95c29f32014-06-20 12:00:00 -0700394 }
Brian Smitha039d702015-01-29 15:03:18 -0800395
396 const uint8_t* prefix = sig_prefix->bytes;
397 unsigned prefix_len = sig_prefix->len;
398 unsigned signed_msg_len;
399 uint8_t *signed_msg;
400
401 signed_msg_len = prefix_len + msg_len;
402 if (signed_msg_len < prefix_len) {
David Benjaminb0acb772015-05-28 16:53:31 -0400403 OPENSSL_PUT_ERROR(RSA, RSA_add_pkcs1_prefix, RSA_R_TOO_LONG);
Brian Smitha039d702015-01-29 15:03:18 -0800404 return 0;
405 }
406
407 signed_msg = OPENSSL_malloc(signed_msg_len);
408 if (!signed_msg) {
David Benjaminb0acb772015-05-28 16:53:31 -0400409 OPENSSL_PUT_ERROR(RSA, RSA_add_pkcs1_prefix, ERR_R_MALLOC_FAILURE);
Brian Smitha039d702015-01-29 15:03:18 -0800410 return 0;
411 }
412
413 memcpy(signed_msg, prefix, prefix_len);
414 memcpy(signed_msg + prefix_len, msg, msg_len);
415
416 *out_msg = signed_msg;
417 *out_msg_len = signed_msg_len;
418 *is_alloced = 1;
419
420 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700421 }
422
David Benjaminb0acb772015-05-28 16:53:31 -0400423 OPENSSL_PUT_ERROR(RSA, RSA_add_pkcs1_prefix, RSA_R_UNKNOWN_ALGORITHM_TYPE);
Brian Smitha039d702015-01-29 15:03:18 -0800424 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700425}
426
427int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
428 unsigned *out_len, RSA *rsa) {
429 const unsigned rsa_size = RSA_size(rsa);
430 int ret = 0;
431 uint8_t *signed_msg;
432 size_t signed_msg_len;
433 int signed_msg_is_alloced = 0;
434 size_t size_t_out_len;
435
436 if (rsa->meth->sign) {
437 return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
438 }
439
David Benjaminb0acb772015-05-28 16:53:31 -0400440 if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
441 &signed_msg_is_alloced, hash_nid, in, in_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700442 return 0;
443 }
444
445 if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
446 signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
447 OPENSSL_PUT_ERROR(RSA, RSA_sign, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
448 goto finish;
449 }
450
451 if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
452 signed_msg_len, RSA_PKCS1_PADDING)) {
453 *out_len = size_t_out_len;
454 ret = 1;
455 }
456
457finish:
458 if (signed_msg_is_alloced) {
459 OPENSSL_free(signed_msg);
460 }
461 return ret;
462}
463
464int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
465 const uint8_t *sig, size_t sig_len, RSA *rsa) {
466 const size_t rsa_size = RSA_size(rsa);
467 uint8_t *buf = NULL;
468 int ret = 0;
469 uint8_t *signed_msg = NULL;
470 size_t signed_msg_len, len;
471 int signed_msg_is_alloced = 0;
472
473 if (rsa->meth->verify) {
474 return rsa->meth->verify(hash_nid, msg, msg_len, sig, sig_len, rsa);
475 }
476
477 if (sig_len != rsa_size) {
478 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_WRONG_SIGNATURE_LENGTH);
479 return 0;
480 }
481
482 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
483 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_INVALID_MESSAGE_LENGTH);
484 return 0;
485 }
486
487 buf = OPENSSL_malloc(rsa_size);
488 if (!buf) {
489 OPENSSL_PUT_ERROR(RSA, RSA_verify, ERR_R_MALLOC_FAILURE);
490 return 0;
491 }
492
493 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
494 RSA_PKCS1_PADDING)) {
495 goto out;
496 }
497
David Benjaminb0acb772015-05-28 16:53:31 -0400498 if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
499 &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700500 goto out;
501 }
502
503 if (len != signed_msg_len || CRYPTO_memcmp(buf, signed_msg, len) != 0) {
504 OPENSSL_PUT_ERROR(RSA, RSA_verify, RSA_R_BAD_SIGNATURE);
505 goto out;
506 }
507
508 ret = 1;
509
510out:
David Benjamind8b65c82015-04-22 16:09:09 -0400511 OPENSSL_free(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700512 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) {
Adam Langley409766d2014-06-20 12:00:00 -0700519 BN_free(*bn);
520 *bn = NULL;
521}
522
Adam Langley05b73772014-07-25 12:03:51 -0700523int RSA_check_key(const RSA *key) {
524 BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp;
525 BN_CTX *ctx;
526 int ok = 0, has_crt_values;
527
528 if (RSA_is_opaque(key)) {
529 /* Opaque keys can't be checked. */
530 return 1;
531 }
532
533 if ((key->p != NULL) != (key->q != NULL)) {
534 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
535 return 0;
536 }
537
538 if (!key->n || !key->e) {
539 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_VALUE_MISSING);
540 return 0;
541 }
542
543 if (!key->d || !key->p) {
544 /* For a public key, or without p and q, there's nothing that can be
545 * checked. */
546 return 1;
547 }
548
549 ctx = BN_CTX_new();
550 if (ctx == NULL) {
551 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_R_MALLOC_FAILURE);
552 return 0;
553 }
554
555 BN_init(&n);
556 BN_init(&pm1);
557 BN_init(&qm1);
558 BN_init(&lcm);
559 BN_init(&gcd);
560 BN_init(&de);
561 BN_init(&dmp1);
562 BN_init(&dmq1);
563 BN_init(&iqmp);
564
Adam Langley839b8812015-05-26 11:36:46 -0700565 if (!BN_mul(&n, key->p, key->q, ctx) ||
566 /* lcm = lcm(prime-1, for all primes) */
Adam Langley05b73772014-07-25 12:03:51 -0700567 !BN_sub(&pm1, key->p, BN_value_one()) ||
568 !BN_sub(&qm1, key->q, BN_value_one()) ||
569 !BN_mul(&lcm, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700570 !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
571 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
572 goto out;
573 }
574
575 size_t num_additional_primes = 0;
576 if (key->additional_primes != NULL) {
577 num_additional_primes = sk_RSA_additional_prime_num(key->additional_primes);
578 }
579
580 size_t i;
581 for (i = 0; i < num_additional_primes; i++) {
582 const RSA_additional_prime *ap =
583 sk_RSA_additional_prime_value(key->additional_primes, i);
584 if (!BN_mul(&n, &n, ap->prime, ctx) ||
585 !BN_sub(&pm1, ap->prime, BN_value_one()) ||
586 !BN_mul(&lcm, &lcm, &pm1, ctx) ||
587 !BN_gcd(&gcd, &gcd, &pm1, ctx)) {
588 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
589 goto out;
590 }
591 }
592
593 if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
Adam Langley05b73772014-07-25 12:03:51 -0700594 !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700595 /* de = d*e mod lcm(prime-1, for all primes). */
Adam Langley05b73772014-07-25 12:03:51 -0700596 !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
597 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
598 goto out;
599 }
600
601 if (BN_cmp(&n, key->n) != 0) {
602 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_N_NOT_EQUAL_P_Q);
603 goto out;
604 }
605
606 if (!BN_is_one(&de)) {
607 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_D_E_NOT_CONGRUENT_TO_1);
608 goto out;
609 }
610
611 has_crt_values = key->dmp1 != NULL;
612 if (has_crt_values != (key->dmq1 != NULL) ||
613 has_crt_values != (key->iqmp != NULL)) {
614 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
615 goto out;
616 }
617
Adam Langley839b8812015-05-26 11:36:46 -0700618 if (has_crt_values && num_additional_primes == 0) {
Adam Langley05b73772014-07-25 12:03:51 -0700619 if (/* dmp1 = d mod (p-1) */
620 !BN_mod(&dmp1, key->d, &pm1, ctx) ||
621 /* dmq1 = d mod (q-1) */
622 !BN_mod(&dmq1, key->d, &qm1, ctx) ||
623 /* iqmp = q^-1 mod p */
624 !BN_mod_inverse(&iqmp, key->q, key->p, ctx)) {
625 OPENSSL_PUT_ERROR(RSA, RSA_check_key, ERR_LIB_BN);
626 goto out;
627 }
628
629 if (BN_cmp(&dmp1, key->dmp1) != 0 ||
630 BN_cmp(&dmq1, key->dmq1) != 0 ||
631 BN_cmp(&iqmp, key->iqmp) != 0) {
632 OPENSSL_PUT_ERROR(RSA, RSA_check_key, RSA_R_CRT_VALUES_INCORRECT);
633 goto out;
634 }
635 }
636
637 ok = 1;
638
639out:
640 BN_free(&n);
641 BN_free(&pm1);
642 BN_free(&qm1);
643 BN_free(&lcm);
644 BN_free(&gcd);
645 BN_free(&de);
646 BN_free(&dmp1);
647 BN_free(&dmq1);
648 BN_free(&iqmp);
649 BN_CTX_free(ctx);
650
651 return ok;
652}
653
Adam Langley409766d2014-06-20 12:00:00 -0700654int RSA_recover_crt_params(RSA *rsa) {
655 BN_CTX *ctx;
656 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
657 int ok = 0;
658
659 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
660 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_EMPTY_PUBLIC_KEY);
661 return 0;
662 }
663
664 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
665 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params,
666 RSA_R_CRT_PARAMS_ALREADY_GIVEN);
667 return 0;
668 }
669
Adam Langley839b8812015-05-26 11:36:46 -0700670 if (rsa->additional_primes != NULL) {
671 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params,
672 RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY);
673 return 0;
674 }
675
Adam Langley409766d2014-06-20 12:00:00 -0700676 /* This uses the algorithm from section 9B of the RSA paper:
677 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
678
679 ctx = BN_CTX_new();
680 if (ctx == NULL) {
681 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
682 return 0;
683 }
684
685 BN_CTX_start(ctx);
686 totient = BN_CTX_get(ctx);
687 rem = BN_CTX_get(ctx);
688 multiple = BN_CTX_get(ctx);
689 p_plus_q = BN_CTX_get(ctx);
690 p_minus_q = BN_CTX_get(ctx);
691
692 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
693 p_minus_q == NULL) {
694 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
695 goto err;
696 }
697
698 /* ed-1 is a small multiple of φ(n). */
699 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
700 !BN_sub_word(totient, 1) ||
701 /* φ(n) =
702 * pq - p - q + 1 =
703 * n - (p + q) + 1
704 *
705 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
706 * close. But, when we calculate the quotient, we'll be truncating it
707 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
708 * which the totient cannot be. So we add one to the estimate.
709 *
710 * Consider ed-1 as:
711 *
712 * multiple * (n - (p+q) + 1) =
713 * multiple*n - multiple*(p+q) + multiple
714 *
715 * When we divide by n, the first term becomes multiple and, since
716 * multiple and p+q is tiny compared to n, the second and third terms can
717 * be ignored. Thus I claim that subtracting one from the estimate is
718 * sufficient. */
719 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
720 !BN_add_word(multiple, 1) ||
721 !BN_div(totient, rem, totient, multiple, ctx)) {
722 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
723 goto err;
724 }
725
726 if (!BN_is_zero(rem)) {
727 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_BAD_RSA_PARAMETERS);
728 goto err;
729 }
730
731 rsa->p = BN_new();
732 rsa->q = BN_new();
733 rsa->dmp1 = BN_new();
734 rsa->dmq1 = BN_new();
735 rsa->iqmp = BN_new();
736 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
737 NULL || rsa->iqmp == NULL) {
738 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_MALLOC_FAILURE);
739 goto err;
740 }
741
742 /* φ(n) = n - (p + q) + 1 =>
743 * n - totient + 1 = p + q */
744 if (!BN_sub(p_plus_q, rsa->n, totient) ||
745 !BN_add_word(p_plus_q, 1) ||
746 /* p - q = sqrt((p+q)^2 - 4n) */
747 !BN_sqr(rem, p_plus_q, ctx) ||
748 !BN_lshift(multiple, rsa->n, 2) ||
749 !BN_sub(rem, rem, multiple) ||
750 !BN_sqrt(p_minus_q, rem, ctx) ||
751 /* q is 1/2 (p+q)-(p-q) */
752 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
753 !BN_rshift1(rsa->q, rsa->q) ||
754 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
755 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
756 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
757 goto err;
758 }
759
760 if (BN_cmp(multiple, rsa->n) != 0) {
761 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, RSA_R_INTERNAL_ERROR);
762 goto err;
763 }
764
765 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
766 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
767 !BN_sub(rem, rsa->q, BN_value_one()) ||
768 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
769 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
770 OPENSSL_PUT_ERROR(RSA, RSA_recover_crt_params, ERR_R_BN_LIB);
771 goto err;
772 }
773
774 ok = 1;
775
776err:
777 BN_CTX_end(ctx);
778 BN_CTX_free(ctx);
779 if (!ok) {
780 bn_free_and_null(&rsa->p);
781 bn_free_and_null(&rsa->q);
782 bn_free_and_null(&rsa->dmp1);
783 bn_free_and_null(&rsa->dmq1);
784 bn_free_and_null(&rsa->iqmp);
785 }
786 return ok;
787}
Adam Langley6bc658d2014-08-18 13:29:45 -0700788
789int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
790 size_t len) {
791 if (rsa->meth->private_transform) {
792 return rsa->meth->private_transform(rsa, out, in, len);
793 }
794
795 return RSA_default_method.private_transform(rsa, out, in, len);
796}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700797
798int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
799 return 1;
800}