blob: 8aa9adc7c38af07069b13dfc6ce186d2ab8bdb6e [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
David Benjamin79c59a32015-09-19 13:35:39 -040059#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080060#include <string.h>
61
Adam Langley95c29f32014-06-20 12:00:00 -070062#include <openssl/bn.h>
63#include <openssl/engine.h>
64#include <openssl/err.h>
65#include <openssl/ex_data.h>
66#include <openssl/mem.h>
David Benjamin98193672016-03-25 18:07:11 -040067#include <openssl/nid.h>
Brian Smith054e6822015-03-27 21:12:01 -100068#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070069
70#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070071#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070072
73
David Benjamin9f33fc62015-04-15 17:29:53 -040074static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
75
Adam Langley95c29f32014-06-20 12:00:00 -070076RSA *RSA_new(void) { return RSA_new_method(NULL); }
77
78RSA *RSA_new_method(const ENGINE *engine) {
Brian Smith5ba06892016-02-07 09:36:04 -100079 RSA *rsa = OPENSSL_malloc(sizeof(RSA));
Adam Langley95c29f32014-06-20 12:00:00 -070080 if (rsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040081 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070082 return NULL;
83 }
84
85 memset(rsa, 0, sizeof(RSA));
86
87 if (engine) {
88 rsa->meth = ENGINE_get_RSA_method(engine);
89 }
90
91 if (rsa->meth == NULL) {
92 rsa->meth = (RSA_METHOD*) &RSA_default_method;
93 }
94 METHOD_ref(rsa->meth);
95
96 rsa->references = 1;
97 rsa->flags = rsa->meth->flags;
Adam Langley683d7bd2015-04-13 11:04:14 -070098 CRYPTO_MUTEX_init(&rsa->lock);
David Benjamin8a589332015-12-04 23:14:35 -050099 CRYPTO_new_ex_data(&rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700100
101 if (rsa->meth->init && !rsa->meth->init(rsa)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400102 CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
David Benjamin79680ff2015-10-23 18:46:16 -0400103 CRYPTO_MUTEX_cleanup(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700104 METHOD_unref(rsa->meth);
105 OPENSSL_free(rsa);
106 return NULL;
107 }
108
109 return rsa;
110}
111
Adam Langley839b8812015-05-26 11:36:46 -0700112void RSA_additional_prime_free(RSA_additional_prime *ap) {
113 if (ap == NULL) {
114 return;
115 }
116
117 BN_clear_free(ap->prime);
118 BN_clear_free(ap->exp);
119 BN_clear_free(ap->coeff);
120 BN_clear_free(ap->r);
David Benjamin8fb0f522015-11-03 18:24:07 -0500121 BN_MONT_CTX_free(ap->mont);
Adam Langley839b8812015-05-26 11:36:46 -0700122 OPENSSL_free(ap);
123}
124
Adam Langley95c29f32014-06-20 12:00:00 -0700125void RSA_free(RSA *rsa) {
126 unsigned u;
127
128 if (rsa == NULL) {
129 return;
130 }
131
Adam Langley0da323a2015-05-15 12:49:30 -0700132 if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700133 return;
134 }
135
David Benjamin8fb0f522015-11-03 18:24:07 -0500136 if (rsa->meth->finish) {
Adam Langley95c29f32014-06-20 12:00:00 -0700137 rsa->meth->finish(rsa);
138 }
139 METHOD_unref(rsa->meth);
140
David Benjamin9f33fc62015-04-15 17:29:53 -0400141 CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjamind8b65c82015-04-22 16:09:09 -0400143 BN_clear_free(rsa->n);
144 BN_clear_free(rsa->e);
145 BN_clear_free(rsa->d);
146 BN_clear_free(rsa->p);
147 BN_clear_free(rsa->q);
148 BN_clear_free(rsa->dmp1);
149 BN_clear_free(rsa->dmq1);
150 BN_clear_free(rsa->iqmp);
David Benjamin8fb0f522015-11-03 18:24:07 -0500151 BN_MONT_CTX_free(rsa->mont_n);
152 BN_MONT_CTX_free(rsa->mont_p);
153 BN_MONT_CTX_free(rsa->mont_q);
Adam Langley95c29f32014-06-20 12:00:00 -0700154 for (u = 0; u < rsa->num_blindings; u++) {
155 BN_BLINDING_free(rsa->blindings[u]);
156 }
David Benjamind8b65c82015-04-22 16:09:09 -0400157 OPENSSL_free(rsa->blindings);
158 OPENSSL_free(rsa->blindings_inuse);
Adam Langley839b8812015-05-26 11:36:46 -0700159 if (rsa->additional_primes != NULL) {
160 sk_RSA_additional_prime_pop_free(rsa->additional_primes,
161 RSA_additional_prime_free);
162 }
Adam Langley683d7bd2015-04-13 11:04:14 -0700163 CRYPTO_MUTEX_cleanup(&rsa->lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700164 OPENSSL_free(rsa);
165}
166
167int RSA_up_ref(RSA *rsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700168 CRYPTO_refcount_inc(&rsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700169 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
David Benjamind93831d2015-10-29 13:19:12 -0400177 return rsa_default_keygen(rsa, bits, e_value, cb);
Adam Langley95c29f32014-06-20 12:00:00 -0700178}
179
Adam Langley839b8812015-05-26 11:36:46 -0700180int RSA_generate_multi_prime_key(RSA *rsa, int bits, int num_primes,
181 BIGNUM *e_value, BN_GENCB *cb) {
182 if (rsa->meth->multi_prime_keygen) {
183 return rsa->meth->multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
184 }
185
David Benjamind93831d2015-10-29 13:19:12 -0400186 return rsa_default_multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
Adam Langley839b8812015-05-26 11:36:46 -0700187}
188
Adam Langley95c29f32014-06-20 12:00:00 -0700189int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
190 const uint8_t *in, size_t in_len, int padding) {
191 if (rsa->meth->encrypt) {
192 return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
193 }
194
David Benjamind93831d2015-10-29 13:19:12 -0400195 return rsa_default_encrypt(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700196}
197
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700198int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700199 int padding) {
200 size_t out_len;
201
202 if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
203 return -1;
204 }
205
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700206 if (out_len > INT_MAX) {
207 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
208 return -1;
209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210 return out_len;
211}
212
213int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
214 const uint8_t *in, size_t in_len, int padding) {
215 if (rsa->meth->sign_raw) {
216 return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
217 }
218
David Benjamind93831d2015-10-29 13:19:12 -0400219 return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700220}
221
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700222int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700223 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
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700230 if (out_len > INT_MAX) {
231 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
232 return -1;
233 }
Adam Langley95c29f32014-06-20 12:00:00 -0700234 return out_len;
235}
236
237int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
238 const uint8_t *in, size_t in_len, int padding) {
239 if (rsa->meth->decrypt) {
240 return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
241 }
242
David Benjamind93831d2015-10-29 13:19:12 -0400243 return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
Adam Langley95c29f32014-06-20 12:00:00 -0700244}
245
David Benjamin79c59a32015-09-19 13:35:39 -0400246int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700247 int padding) {
248 size_t out_len;
249
250 if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
251 return -1;
252 }
253
David Benjamin79c59a32015-09-19 13:35:39 -0400254 if (out_len > INT_MAX) {
255 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
256 return -1;
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258 return out_len;
259}
260
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700261int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
Adam Langley95c29f32014-06-20 12:00:00 -0700262 int padding) {
263 size_t out_len;
264
265 if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
266 return -1;
267 }
268
Matt Braithwaite978f16e2015-10-19 13:38:36 -0700269 if (out_len > INT_MAX) {
270 OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
271 return -1;
272 }
Adam Langley95c29f32014-06-20 12:00:00 -0700273 return out_len;
274}
275
276unsigned RSA_size(const RSA *rsa) {
David Benjamin925fee32014-07-11 14:14:08 -0400277 if (rsa->meth->size) {
278 return rsa->meth->size(rsa);
279 }
280
David Benjamind93831d2015-10-29 13:19:12 -0400281 return rsa_default_size(rsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700282}
283
David Benjaminecc0ce72014-07-18 18:39:42 -0400284int RSA_is_opaque(const RSA *rsa) {
285 return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
286}
287
David Benjaminc20febe2014-11-11 23:47:50 -0500288int RSA_supports_digest(const RSA *rsa, const EVP_MD *md) {
289 if (rsa->meth && rsa->meth->supports_digest) {
290 return rsa->meth->supports_digest(rsa, md);
291 }
292 return 1;
293}
294
David Benjamin8a589332015-12-04 23:14:35 -0500295int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langley95c29f32014-06-20 12:00:00 -0700296 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400297 int index;
David Benjamin8a589332015-12-04 23:14:35 -0500298 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
299 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400300 return -1;
301 }
302 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700303}
304
305int RSA_set_ex_data(RSA *d, int idx, void *arg) {
306 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
307}
308
309void *RSA_get_ex_data(const RSA *d, int idx) {
310 return CRYPTO_get_ex_data(&d->ex_data, idx);
311}
312
313/* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
314 * the length of an MD5 and SHA1 hash. */
315static const unsigned SSL_SIG_LENGTH = 36;
316
317/* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
318 * to be signed with PKCS#1. */
319struct pkcs1_sig_prefix {
320 /* nid identifies the hash function. */
321 int nid;
322 /* len is the number of bytes of |bytes| which are valid. */
323 uint8_t len;
324 /* bytes contains the DER bytes. */
325 uint8_t bytes[19];
326};
327
328/* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
329 * different hash functions. */
330static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
331 {
332 NID_md5,
333 18,
334 {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
335 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
336 },
337 {
338 NID_sha1,
339 15,
340 {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
341 0x00, 0x04, 0x14},
342 },
343 {
344 NID_sha224,
345 19,
346 {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
347 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
348 },
349 {
350 NID_sha256,
351 19,
352 {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
353 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
354 },
355 {
356 NID_sha384,
357 19,
358 {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
359 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
360 },
361 {
362 NID_sha512,
363 19,
364 {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
365 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
366 },
367 {
Adam Langley95c29f32014-06-20 12:00:00 -0700368 NID_undef, 0, {0},
369 },
370};
371
David Benjaminb0acb772015-05-28 16:53:31 -0400372int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
373 int *is_alloced, int hash_nid, const uint8_t *msg,
374 size_t msg_len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700375 unsigned i;
Adam Langley95c29f32014-06-20 12:00:00 -0700376
377 if (hash_nid == NID_md5_sha1) {
378 /* Special case: SSL signature, just check the length. */
379 if (msg_len != SSL_SIG_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400380 OPENSSL_PUT_ERROR(RSA, 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 Benjamin3570d732015-06-29 00:28:17 -0400403 OPENSSL_PUT_ERROR(RSA, 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 Benjamin3570d732015-06-29 00:28:17 -0400409 OPENSSL_PUT_ERROR(RSA, 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 Benjamin3570d732015-06-29 00:28:17 -0400423 OPENSSL_PUT_ERROR(RSA, 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) {
David Benjamin3570d732015-06-29 00:28:17 -0400447 OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
Adam Langley95c29f32014-06-20 12:00:00 -0700448 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) {
Brian Smithc0b196d2016-03-04 08:54:07 -1000466 if (rsa->n == NULL || rsa->e == NULL) {
467 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
468 return 0;
469 }
470
Adam Langley95c29f32014-06-20 12:00:00 -0700471 const size_t rsa_size = RSA_size(rsa);
472 uint8_t *buf = NULL;
473 int ret = 0;
474 uint8_t *signed_msg = NULL;
475 size_t signed_msg_len, len;
476 int signed_msg_is_alloced = 0;
477
Adam Langley95c29f32014-06-20 12:00:00 -0700478 if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400479 OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
Adam Langley95c29f32014-06-20 12:00:00 -0700480 return 0;
481 }
482
483 buf = OPENSSL_malloc(rsa_size);
484 if (!buf) {
David Benjamin3570d732015-06-29 00:28:17 -0400485 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700486 return 0;
487 }
488
489 if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
490 RSA_PKCS1_PADDING)) {
491 goto out;
492 }
493
David Benjaminb0acb772015-05-28 16:53:31 -0400494 if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
495 &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700496 goto out;
497 }
498
Brian Smith69f05322016-03-15 12:44:36 -1000499 if (len != signed_msg_len || memcmp(buf, signed_msg, len) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400500 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700501 goto out;
502 }
503
504 ret = 1;
505
506out:
David Benjamind8b65c82015-04-22 16:09:09 -0400507 OPENSSL_free(buf);
Adam Langley95c29f32014-06-20 12:00:00 -0700508 if (signed_msg_is_alloced) {
509 OPENSSL_free(signed_msg);
510 }
511 return ret;
512}
Adam Langley409766d2014-06-20 12:00:00 -0700513
514static void bn_free_and_null(BIGNUM **bn) {
Adam Langley409766d2014-06-20 12:00:00 -0700515 BN_free(*bn);
516 *bn = NULL;
517}
518
Adam Langley05b73772014-07-25 12:03:51 -0700519int RSA_check_key(const RSA *key) {
Brian Smith7241ca52016-07-25 16:01:10 -1000520 BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
Adam Langley05b73772014-07-25 12:03:51 -0700521 BN_CTX *ctx;
522 int ok = 0, has_crt_values;
523
524 if (RSA_is_opaque(key)) {
525 /* Opaque keys can't be checked. */
526 return 1;
527 }
528
529 if ((key->p != NULL) != (key->q != NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400530 OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
Adam Langley05b73772014-07-25 12:03:51 -0700531 return 0;
532 }
533
534 if (!key->n || !key->e) {
David Benjamin3570d732015-06-29 00:28:17 -0400535 OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
Adam Langley05b73772014-07-25 12:03:51 -0700536 return 0;
537 }
538
539 if (!key->d || !key->p) {
540 /* For a public key, or without p and q, there's nothing that can be
541 * checked. */
542 return 1;
543 }
544
545 ctx = BN_CTX_new();
546 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400547 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley05b73772014-07-25 12:03:51 -0700548 return 0;
549 }
550
551 BN_init(&n);
552 BN_init(&pm1);
553 BN_init(&qm1);
554 BN_init(&lcm);
555 BN_init(&gcd);
556 BN_init(&de);
557 BN_init(&dmp1);
558 BN_init(&dmq1);
Brian Smith7241ca52016-07-25 16:01:10 -1000559 BN_init(&iqmp_times_q);
Adam Langley05b73772014-07-25 12:03:51 -0700560
Adam Langley839b8812015-05-26 11:36:46 -0700561 if (!BN_mul(&n, key->p, key->q, ctx) ||
562 /* lcm = lcm(prime-1, for all primes) */
Adam Langley05b73772014-07-25 12:03:51 -0700563 !BN_sub(&pm1, key->p, BN_value_one()) ||
564 !BN_sub(&qm1, key->q, BN_value_one()) ||
565 !BN_mul(&lcm, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700566 !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400567 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley839b8812015-05-26 11:36:46 -0700568 goto out;
569 }
570
571 size_t num_additional_primes = 0;
572 if (key->additional_primes != NULL) {
573 num_additional_primes = sk_RSA_additional_prime_num(key->additional_primes);
574 }
575
576 size_t i;
577 for (i = 0; i < num_additional_primes; i++) {
578 const RSA_additional_prime *ap =
579 sk_RSA_additional_prime_value(key->additional_primes, i);
580 if (!BN_mul(&n, &n, ap->prime, ctx) ||
581 !BN_sub(&pm1, ap->prime, BN_value_one()) ||
582 !BN_mul(&lcm, &lcm, &pm1, ctx) ||
583 !BN_gcd(&gcd, &gcd, &pm1, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400584 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley839b8812015-05-26 11:36:46 -0700585 goto out;
586 }
587 }
588
589 if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
Adam Langley05b73772014-07-25 12:03:51 -0700590 !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
Adam Langley839b8812015-05-26 11:36:46 -0700591 /* de = d*e mod lcm(prime-1, for all primes). */
Adam Langley05b73772014-07-25 12:03:51 -0700592 !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400593 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley05b73772014-07-25 12:03:51 -0700594 goto out;
595 }
596
597 if (BN_cmp(&n, key->n) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400598 OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
Adam Langley05b73772014-07-25 12:03:51 -0700599 goto out;
600 }
601
602 if (!BN_is_one(&de)) {
David Benjamin3570d732015-06-29 00:28:17 -0400603 OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
Adam Langley05b73772014-07-25 12:03:51 -0700604 goto out;
605 }
606
607 has_crt_values = key->dmp1 != NULL;
608 if (has_crt_values != (key->dmq1 != NULL) ||
609 has_crt_values != (key->iqmp != NULL)) {
David Benjamin3570d732015-06-29 00:28:17 -0400610 OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
Adam Langley05b73772014-07-25 12:03:51 -0700611 goto out;
612 }
613
Adam Langley839b8812015-05-26 11:36:46 -0700614 if (has_crt_values && num_additional_primes == 0) {
Adam Langley05b73772014-07-25 12:03:51 -0700615 if (/* dmp1 = d mod (p-1) */
616 !BN_mod(&dmp1, key->d, &pm1, ctx) ||
617 /* dmq1 = d mod (q-1) */
618 !BN_mod(&dmq1, key->d, &qm1, ctx) ||
619 /* iqmp = q^-1 mod p */
Brian Smith7241ca52016-07-25 16:01:10 -1000620 !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400621 OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
Adam Langley05b73772014-07-25 12:03:51 -0700622 goto out;
623 }
624
625 if (BN_cmp(&dmp1, key->dmp1) != 0 ||
626 BN_cmp(&dmq1, key->dmq1) != 0 ||
Brian Smith7241ca52016-07-25 16:01:10 -1000627 BN_cmp(key->iqmp, key->p) >= 0 ||
628 !BN_is_one(&iqmp_times_q)) {
David Benjamin3570d732015-06-29 00:28:17 -0400629 OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
Adam Langley05b73772014-07-25 12:03:51 -0700630 goto out;
631 }
632 }
633
634 ok = 1;
635
636out:
637 BN_free(&n);
638 BN_free(&pm1);
639 BN_free(&qm1);
640 BN_free(&lcm);
641 BN_free(&gcd);
642 BN_free(&de);
643 BN_free(&dmp1);
644 BN_free(&dmq1);
Brian Smith7241ca52016-07-25 16:01:10 -1000645 BN_free(&iqmp_times_q);
Adam Langley05b73772014-07-25 12:03:51 -0700646 BN_CTX_free(ctx);
647
648 return ok;
649}
650
Adam Langley409766d2014-06-20 12:00:00 -0700651int RSA_recover_crt_params(RSA *rsa) {
652 BN_CTX *ctx;
653 BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
654 int ok = 0;
655
656 if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400657 OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
Adam Langley409766d2014-06-20 12:00:00 -0700658 return 0;
659 }
660
661 if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
David Benjamin3570d732015-06-29 00:28:17 -0400662 OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_PARAMS_ALREADY_GIVEN);
Adam Langley409766d2014-06-20 12:00:00 -0700663 return 0;
664 }
665
Adam Langley839b8812015-05-26 11:36:46 -0700666 if (rsa->additional_primes != NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400667 OPENSSL_PUT_ERROR(RSA, RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY);
Adam Langley839b8812015-05-26 11:36:46 -0700668 return 0;
669 }
670
Adam Langley409766d2014-06-20 12:00:00 -0700671 /* This uses the algorithm from section 9B of the RSA paper:
672 * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
673
674 ctx = BN_CTX_new();
675 if (ctx == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400676 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700677 return 0;
678 }
679
680 BN_CTX_start(ctx);
681 totient = BN_CTX_get(ctx);
682 rem = BN_CTX_get(ctx);
683 multiple = BN_CTX_get(ctx);
684 p_plus_q = BN_CTX_get(ctx);
685 p_minus_q = BN_CTX_get(ctx);
686
687 if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
688 p_minus_q == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400689 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700690 goto err;
691 }
692
693 /* ed-1 is a small multiple of φ(n). */
694 if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
695 !BN_sub_word(totient, 1) ||
696 /* φ(n) =
697 * pq - p - q + 1 =
698 * n - (p + q) + 1
699 *
700 * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
701 * close. But, when we calculate the quotient, we'll be truncating it
702 * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
703 * which the totient cannot be. So we add one to the estimate.
704 *
705 * Consider ed-1 as:
706 *
707 * multiple * (n - (p+q) + 1) =
708 * multiple*n - multiple*(p+q) + multiple
709 *
710 * When we divide by n, the first term becomes multiple and, since
711 * multiple and p+q is tiny compared to n, the second and third terms can
712 * be ignored. Thus I claim that subtracting one from the estimate is
713 * sufficient. */
714 !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
715 !BN_add_word(multiple, 1) ||
716 !BN_div(totient, rem, totient, multiple, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400717 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700718 goto err;
719 }
720
721 if (!BN_is_zero(rem)) {
David Benjamin3570d732015-06-29 00:28:17 -0400722 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
Adam Langley409766d2014-06-20 12:00:00 -0700723 goto err;
724 }
725
726 rsa->p = BN_new();
727 rsa->q = BN_new();
728 rsa->dmp1 = BN_new();
729 rsa->dmq1 = BN_new();
730 rsa->iqmp = BN_new();
731 if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
732 NULL || rsa->iqmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400733 OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
Adam Langley409766d2014-06-20 12:00:00 -0700734 goto err;
735 }
736
737 /* φ(n) = n - (p + q) + 1 =>
738 * n - totient + 1 = p + q */
739 if (!BN_sub(p_plus_q, rsa->n, totient) ||
740 !BN_add_word(p_plus_q, 1) ||
741 /* p - q = sqrt((p+q)^2 - 4n) */
742 !BN_sqr(rem, p_plus_q, ctx) ||
743 !BN_lshift(multiple, rsa->n, 2) ||
744 !BN_sub(rem, rem, multiple) ||
745 !BN_sqrt(p_minus_q, rem, ctx) ||
746 /* q is 1/2 (p+q)-(p-q) */
747 !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
748 !BN_rshift1(rsa->q, rsa->q) ||
749 !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
750 !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400751 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700752 goto err;
753 }
754
755 if (BN_cmp(multiple, rsa->n) != 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400756 OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
Adam Langley409766d2014-06-20 12:00:00 -0700757 goto err;
758 }
759
760 if (!BN_sub(rem, rsa->p, BN_value_one()) ||
761 !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
762 !BN_sub(rem, rsa->q, BN_value_one()) ||
763 !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
764 !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400765 OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
Adam Langley409766d2014-06-20 12:00:00 -0700766 goto err;
767 }
768
769 ok = 1;
770
771err:
772 BN_CTX_end(ctx);
773 BN_CTX_free(ctx);
774 if (!ok) {
775 bn_free_and_null(&rsa->p);
776 bn_free_and_null(&rsa->q);
777 bn_free_and_null(&rsa->dmp1);
778 bn_free_and_null(&rsa->dmq1);
779 bn_free_and_null(&rsa->iqmp);
780 }
781 return ok;
782}
Adam Langley6bc658d2014-08-18 13:29:45 -0700783
784int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
785 size_t len) {
786 if (rsa->meth->private_transform) {
787 return rsa->meth->private_transform(rsa, out, in, len);
788 }
789
David Benjamind93831d2015-10-29 13:19:12 -0400790 return rsa_default_private_transform(rsa, out, in, len);
Adam Langley6bc658d2014-08-18 13:29:45 -0700791}
Adam Langleyc3ef76f2015-04-13 14:34:17 -0700792
793int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
794 return 1;
795}