2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This package is an SSL implementation written
|
|
|
|
* by Eric Young (eay@cryptsoft.com).
|
|
|
|
* The implementation was written so as to conform with Netscapes SSL.
|
|
|
|
*
|
|
|
|
* This library is free for commercial and non-commercial use as long as
|
|
|
|
* the following conditions are aheared to. The following conditions
|
|
|
|
* apply to all code found in this distribution, be it the RC4, RSA,
|
|
|
|
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
|
|
|
* included with this distribution is covered by the same copyright terms
|
|
|
|
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
|
|
|
*
|
|
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
|
|
|
* the code are not to be removed.
|
|
|
|
* If this package is used in a product, Eric Young should be given attribution
|
|
|
|
* as the author of the parts of the library used.
|
|
|
|
* This can be in the form of a textual message at program startup or
|
|
|
|
* in documentation (online or textual) provided with the package.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* "This product includes cryptographic software written by
|
|
|
|
* Eric Young (eay@cryptsoft.com)"
|
|
|
|
* The word 'cryptographic' can be left out if the rouines from the library
|
|
|
|
* being used are not cryptographic related :-).
|
|
|
|
* 4. If you include any Windows specific code (or a derivative thereof) from
|
|
|
|
* the apps directory (application code) you must include an acknowledgement:
|
|
|
|
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* The licence and distribution terms for any publically available version or
|
|
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
|
|
|
* copied and put under another distribution licence
|
|
|
|
* [including the GNU Public Licence.] */
|
|
|
|
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
2016-03-14 18:19:41 +00:00
|
|
|
#include <assert.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/mem.h>
|
2015-03-28 07:12:01 +00:00
|
|
|
#include <openssl/thread.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
#include "internal.h"
|
2015-04-13 19:04:14 +01:00
|
|
|
#include "../internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
|
2016-01-12 20:47:25 +00:00
|
|
|
static int check_modulus_and_exponent_sizes(const RSA *rsa) {
|
|
|
|
unsigned rsa_bits = BN_num_bits(rsa->n);
|
2016-03-14 18:19:41 +00:00
|
|
|
|
2016-01-12 20:47:25 +00:00
|
|
|
if (rsa_bits > 16 * 1024) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-03-14 18:19:41 +00:00
|
|
|
/* Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as
|
|
|
|
* the limit based on the recommendations in [1] and [2]. Windows CryptoAPI
|
|
|
|
* doesn't support values larger than 32 bits [3], so it is unlikely that
|
|
|
|
* exponents larger than 32 bits are being used for anything Windows commonly
|
|
|
|
* does.
|
|
|
|
*
|
|
|
|
* [1] https://www.imperialviolet.org/2012/03/16/rsae.html
|
|
|
|
* [2] https://www.imperialviolet.org/2012/03/17/rsados.html
|
|
|
|
* [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx */
|
|
|
|
static const unsigned kMaxExponentBits = 33;
|
|
|
|
|
|
|
|
if (BN_num_bits(rsa->e) > kMaxExponentBits) {
|
2016-01-12 20:47:25 +00:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:19:41 +00:00
|
|
|
/* Verify |n > e|. Comparing |rsa_bits| to |kMaxExponentBits| is a small
|
|
|
|
* shortcut to comparing |n| and |e| directly. In reality, |kMaxExponentBits|
|
|
|
|
* is much smaller than the minimum RSA key size that any application should
|
|
|
|
* accept. */
|
|
|
|
if (rsa_bits <= kMaxExponentBits) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
|
2016-01-12 20:47:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-14 18:19:41 +00:00
|
|
|
assert(BN_ucmp(rsa->n, rsa->e) > 0);
|
2016-01-12 20:47:25 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
size_t rsa_default_size(const RSA *rsa) {
|
2014-07-11 19:14:08 +01:00
|
|
|
return BN_num_bytes(rsa->n);
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
|
|
|
const uint8_t *in, size_t in_len, int padding) {
|
2014-06-20 20:00:00 +01:00
|
|
|
const unsigned rsa_size = RSA_size(rsa);
|
|
|
|
BIGNUM *f, *result;
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
2014-06-20 20:00:00 +01:00
|
|
|
int i, ret = 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (max_out < rsa_size) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-12 20:47:25 +00:00
|
|
|
if (!check_modulus_and_exponent_sizes(rsa)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
f = BN_CTX_get(ctx);
|
|
|
|
result = BN_CTX_get(ctx);
|
|
|
|
buf = OPENSSL_malloc(rsa_size);
|
|
|
|
if (!f || !result || !buf) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (padding) {
|
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
|
|
|
|
break;
|
|
|
|
case RSA_PKCS1_OAEP_PADDING:
|
2014-06-20 20:00:00 +01:00
|
|
|
/* Use the default parameters: SHA-1 for both hashes and no label. */
|
|
|
|
i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
|
|
|
|
NULL, 0, NULL, NULL);
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
case RSA_NO_PADDING:
|
|
|
|
i = RSA_padding_add_none(buf, rsa_size, in, in_len);
|
|
|
|
break;
|
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i <= 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_bin2bn(buf, rsa_size, f) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_ucmp(f, rsa->n) >= 0) {
|
|
|
|
/* usually the padding functions would catch this */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
|
2016-03-25 19:12:48 +00:00
|
|
|
!BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* put in leading 0 bytes if the number is less than the length of the
|
|
|
|
* modulus */
|
2014-06-20 20:00:00 +01:00
|
|
|
if (!BN_bn2bin_padded(out, rsa_size, result)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = rsa_size;
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
if (buf != NULL) {
|
|
|
|
OPENSSL_cleanse(buf, rsa_size);
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
|
|
|
|
* RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
|
|
|
|
* destroyed as needed. */
|
|
|
|
#define MAX_BLINDINGS_PER_RSA 1024
|
|
|
|
|
|
|
|
/* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
|
|
|
|
* allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
|
|
|
|
* none are free, the cache will be extended by a extra element and the new
|
|
|
|
* BN_BLINDING is returned.
|
|
|
|
*
|
|
|
|
* On success, the index of the assigned BN_BLINDING is written to
|
|
|
|
* |*index_used| and must be passed to |rsa_blinding_release| when finished. */
|
|
|
|
static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
|
|
|
|
BN_CTX *ctx) {
|
2016-03-18 02:10:04 +00:00
|
|
|
assert(ctx != NULL);
|
2016-03-21 21:25:39 +00:00
|
|
|
assert(rsa->mont_n != NULL);
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
BN_BLINDING *ret = NULL;
|
|
|
|
BN_BLINDING **new_blindings;
|
|
|
|
uint8_t *new_blindings_inuse;
|
|
|
|
char overflow = 0;
|
|
|
|
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_lock_write(&rsa->lock);
|
2015-04-01 02:55:53 +01:00
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < rsa->num_blindings; i++) {
|
|
|
|
if (rsa->blindings_inuse[i] == 0) {
|
|
|
|
rsa->blindings_inuse[i] = 1;
|
|
|
|
ret = rsa->blindings[i];
|
|
|
|
*index_used = i;
|
|
|
|
break;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != NULL) {
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_unlock(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
|
|
|
|
|
|
|
|
/* We didn't find a free BN_BLINDING to use so increase the length of
|
|
|
|
* the arrays by one and use the newly created element. */
|
|
|
|
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_unlock(&rsa->lock);
|
Require the public exponent to be available in RSA blinding.
Require the public exponent to be available unless
|RSA_FLAG_NO_BLINDING| is set on the key. Also, document this.
If the public exponent |e| is not available, then we could compute it
from |p|, |q|, and |d|. However, there's no reasonable situation in
which we'd have |p| or |q| but not |e|; either we have all the CRT
parameters, or we have (e, d, n), or we have only (d, n). The
calculation to compute |e| exposes the private key to risk of side
channel attacks.
Also, it was particularly wasteful to compute |e| for each
|BN_BLINDING| created, instead of just once before the first
|BN_BLINDING| was created.
|BN_BLINDING| now no longer needs to contain a duplicate copy of |e|,
so it is now more space-efficient.
Note that the condition |b->e != NULL| in |bn_blinding_update| was
always true since commit cbf56a5683ddda831ff91c46ea48d1fba545db66.
Change-Id: Ic2fd6980e0d359dcd53772a7c31bdd0267e316b4
Reviewed-on: https://boringssl-review.googlesource.com/7594
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-27 06:42:31 +01:00
|
|
|
ret = BN_BLINDING_new();
|
2014-06-20 20:00:00 +01:00
|
|
|
if (ret == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overflow) {
|
|
|
|
/* We cannot add any more cached BN_BLINDINGs so we use |ret|
|
|
|
|
* and mark it for destruction in |rsa_blinding_release|. */
|
|
|
|
*index_used = MAX_BLINDINGS_PER_RSA;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_lock_write(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
new_blindings =
|
|
|
|
OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
|
|
|
|
if (new_blindings == NULL) {
|
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
memcpy(new_blindings, rsa->blindings,
|
|
|
|
sizeof(BN_BLINDING *) * rsa->num_blindings);
|
|
|
|
new_blindings[rsa->num_blindings] = ret;
|
|
|
|
|
|
|
|
new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
|
|
|
|
if (new_blindings_inuse == NULL) {
|
|
|
|
goto err2;
|
|
|
|
}
|
|
|
|
memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
|
|
|
|
new_blindings_inuse[rsa->num_blindings] = 1;
|
|
|
|
*index_used = rsa->num_blindings;
|
|
|
|
|
2015-04-22 21:09:09 +01:00
|
|
|
OPENSSL_free(rsa->blindings);
|
2014-06-20 20:00:00 +01:00
|
|
|
rsa->blindings = new_blindings;
|
2015-04-22 21:09:09 +01:00
|
|
|
OPENSSL_free(rsa->blindings_inuse);
|
2014-06-20 20:00:00 +01:00
|
|
|
rsa->blindings_inuse = new_blindings_inuse;
|
|
|
|
rsa->num_blindings++;
|
|
|
|
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_unlock(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
err2:
|
|
|
|
OPENSSL_free(new_blindings);
|
|
|
|
|
|
|
|
err1:
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_unlock(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
BN_BLINDING_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
|
|
|
|
* for other threads to use. */
|
|
|
|
static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
|
|
|
|
unsigned blinding_index) {
|
|
|
|
if (blinding_index == MAX_BLINDINGS_PER_RSA) {
|
|
|
|
/* This blinding wasn't cached. */
|
|
|
|
BN_BLINDING_free(blinding);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_lock_write(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
rsa->blindings_inuse[blinding_index] = 0;
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_unlock(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* signing */
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
|
|
|
|
size_t max_out, const uint8_t *in, size_t in_len,
|
|
|
|
int padding) {
|
2014-06-20 20:00:00 +01:00
|
|
|
const unsigned rsa_size = RSA_size(rsa);
|
|
|
|
uint8_t *buf = NULL;
|
2014-06-20 20:00:00 +01:00
|
|
|
int i, ret = 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (max_out < rsa_size) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = OPENSSL_malloc(rsa_size);
|
2014-08-18 21:29:45 +01:00
|
|
|
if (buf == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (padding) {
|
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
|
|
|
|
break;
|
|
|
|
case RSA_NO_PADDING:
|
|
|
|
i = RSA_padding_add_none(buf, rsa_size, in, in_len);
|
|
|
|
break;
|
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
if (i <= 0) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
|
2015-02-24 21:49:41 +00:00
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = rsa_size;
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (buf != NULL) {
|
|
|
|
OPENSSL_cleanse(buf, rsa_size);
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
|
|
|
const uint8_t *in, size_t in_len, int padding) {
|
2014-06-20 20:00:00 +01:00
|
|
|
const unsigned rsa_size = RSA_size(rsa);
|
|
|
|
int r = -1;
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (max_out < rsa_size) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:09:19 +01:00
|
|
|
if (padding == RSA_NO_PADDING) {
|
|
|
|
buf = out;
|
|
|
|
} else {
|
|
|
|
/* Allocate a temporary buffer to hold the padded plaintext. */
|
|
|
|
buf = OPENSSL_malloc(rsa_size);
|
|
|
|
if (buf == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
if (in_len != rsa_size) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
switch (padding) {
|
|
|
|
case RSA_PKCS1_PADDING:
|
2014-06-20 20:00:00 +01:00
|
|
|
r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
case RSA_PKCS1_OAEP_PADDING:
|
2014-06-20 20:00:00 +01:00
|
|
|
/* Use the default parameters: SHA-1 for both hashes and no label. */
|
|
|
|
r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
|
|
|
|
NULL, 0, NULL, NULL);
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
case RSA_NO_PADDING:
|
2014-07-24 18:09:19 +01:00
|
|
|
r = rsa_size;
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
|
2014-06-20 20:00:00 +01:00
|
|
|
} else {
|
|
|
|
*out_len = r;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2014-07-24 18:09:19 +01:00
|
|
|
if (padding != RSA_NO_PADDING && buf != NULL) {
|
2014-06-20 20:00:00 +01:00
|
|
|
OPENSSL_cleanse(buf, rsa_size);
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-25 23:24:46 +00:00
|
|
|
static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
|
|
|
|
|
2016-03-04 18:54:07 +00:00
|
|
|
int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
|
|
|
const uint8_t *in, size_t in_len, int padding) {
|
|
|
|
if (rsa->n == NULL || rsa->e == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
const unsigned rsa_size = RSA_size(rsa);
|
|
|
|
BIGNUM *f, *result;
|
2014-06-20 20:00:00 +01:00
|
|
|
int r = -1;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (max_out < rsa_size) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:20:07 +00:00
|
|
|
if (in_len != rsa_size) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-12 20:47:25 +00:00
|
|
|
if (!check_modulus_and_exponent_sizes(rsa)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:42:47 +00:00
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
2014-06-20 20:00:00 +01:00
|
|
|
if (ctx == NULL) {
|
2016-03-04 23:42:47 +00:00
|
|
|
return 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:42:47 +00:00
|
|
|
int ret = 0;
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
BN_CTX_start(ctx);
|
|
|
|
f = BN_CTX_get(ctx);
|
|
|
|
result = BN_CTX_get(ctx);
|
2016-03-04 23:42:47 +00:00
|
|
|
if (f == NULL || result == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:09:19 +01:00
|
|
|
if (padding == RSA_NO_PADDING) {
|
|
|
|
buf = out;
|
|
|
|
} else {
|
|
|
|
/* Allocate a temporary buffer to hold the padded plaintext. */
|
|
|
|
buf = OPENSSL_malloc(rsa_size);
|
|
|
|
if (buf == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (BN_bin2bn(in, in_len, f) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_ucmp(f, rsa->n) >= 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
|
2016-03-25 19:12:48 +00:00
|
|
|
!BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
if (!BN_bn2bin_padded(buf, rsa_size, result)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
switch (padding) {
|
|
|
|
case RSA_PKCS1_PADDING:
|
2014-06-20 20:00:00 +01:00
|
|
|
r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
case RSA_NO_PADDING:
|
2014-07-24 18:09:19 +01:00
|
|
|
r = rsa_size;
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
|
2014-06-20 20:00:00 +01:00
|
|
|
} else {
|
|
|
|
*out_len = r;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
2016-03-04 23:42:47 +00:00
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
if (buf != out) {
|
2014-06-20 20:00:00 +01:00
|
|
|
OPENSSL_free(buf);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
|
|
|
|
size_t len) {
|
2014-08-18 21:29:45 +01:00
|
|
|
BIGNUM *f, *result;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
unsigned blinding_index = 0;
|
|
|
|
BN_BLINDING *blinding = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
f = BN_CTX_get(ctx);
|
|
|
|
result = BN_CTX_get(ctx);
|
|
|
|
|
|
|
|
if (f == NULL || result == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_bin2bn(in, len, f) == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_ucmp(f, rsa->n) >= 0) {
|
|
|
|
/* Usually the padding functions would catch this. */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Verify RSA private key operation regardless of whether CRT is used.
Previously, the verification was only done when using the CRT method,
as the CRT method has been shown to be extremely sensitive to fault
attacks. However, there's no reason to avoid doing the verification
when the non-CRT method is used (performance-sensitive applications
should always be using the CRT-capable keys).
Previously, when we detected a fault (attack) through this verification,
libcrypto would fall back to the non-CRT method and assume that the
non-CRT method would give a correct result, despite having just
detecting corruption that is likely from an attack. Instead, just give
up, like NSS does.
Previously, the code tried to handle the case where the input was not
reduced mod rsa->n. This is (was) not possible, so avoid trying to
handle that. This simplifies the equality check and lets us use
|CRYPTO_memcmp|.
Change-Id: I78d1e55520a1c8c280cae2b7256e12ff6290507d
Reviewed-on: https://boringssl-review.googlesource.com/7582
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-25 22:23:16 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
|
Require the public exponent to be available in RSA blinding.
Require the public exponent to be available unless
|RSA_FLAG_NO_BLINDING| is set on the key. Also, document this.
If the public exponent |e| is not available, then we could compute it
from |p|, |q|, and |d|. However, there's no reasonable situation in
which we'd have |p| or |q| but not |e|; either we have all the CRT
parameters, or we have (e, d, n), or we have only (d, n). The
calculation to compute |e| exposes the private key to risk of side
channel attacks.
Also, it was particularly wasteful to compute |e| for each
|BN_BLINDING| created, instead of just once before the first
|BN_BLINDING| was created.
|BN_BLINDING| now no longer needs to contain a duplicate copy of |e|,
so it is now more space-efficient.
Note that the condition |b->e != NULL| in |bn_blinding_update| was
always true since commit cbf56a5683ddda831ff91c46ea48d1fba545db66.
Change-Id: Ic2fd6980e0d359dcd53772a7c31bdd0267e316b4
Reviewed-on: https://boringssl-review.googlesource.com/7594
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-27 06:42:31 +01:00
|
|
|
/* Keys without public exponents must have blinding explicitly disabled to
|
|
|
|
* be used. */
|
|
|
|
if (rsa->e == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
|
|
|
|
if (blinding == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
Require the public exponent to be available in RSA blinding.
Require the public exponent to be available unless
|RSA_FLAG_NO_BLINDING| is set on the key. Also, document this.
If the public exponent |e| is not available, then we could compute it
from |p|, |q|, and |d|. However, there's no reasonable situation in
which we'd have |p| or |q| but not |e|; either we have all the CRT
parameters, or we have (e, d, n), or we have only (d, n). The
calculation to compute |e| exposes the private key to risk of side
channel attacks.
Also, it was particularly wasteful to compute |e| for each
|BN_BLINDING| created, instead of just once before the first
|BN_BLINDING| was created.
|BN_BLINDING| now no longer needs to contain a duplicate copy of |e|,
so it is now more space-efficient.
Note that the condition |b->e != NULL| in |bn_blinding_update| was
always true since commit cbf56a5683ddda831ff91c46ea48d1fba545db66.
Change-Id: Ic2fd6980e0d359dcd53772a7c31bdd0267e316b4
Reviewed-on: https://boringssl-review.googlesource.com/7594
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-27 06:42:31 +01:00
|
|
|
if (!BN_BLINDING_convert(f, blinding, rsa->e, rsa->mont_n, ctx)) {
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 23:15:39 +00:00
|
|
|
if (rsa->p != NULL && rsa->q != NULL && rsa->e != NULL && rsa->dmp1 != NULL &&
|
|
|
|
rsa->dmq1 != NULL && rsa->iqmp != NULL) {
|
2016-03-25 23:24:46 +00:00
|
|
|
if (!mod_exp(result, f, rsa, ctx)) {
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BIGNUM local_d;
|
|
|
|
BIGNUM *d = NULL;
|
|
|
|
|
|
|
|
BN_init(&local_d);
|
|
|
|
d = &local_d;
|
|
|
|
BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
|
|
|
|
|
Verify RSA private key operation regardless of whether CRT is used.
Previously, the verification was only done when using the CRT method,
as the CRT method has been shown to be extremely sensitive to fault
attacks. However, there's no reason to avoid doing the verification
when the non-CRT method is used (performance-sensitive applications
should always be using the CRT-capable keys).
Previously, when we detected a fault (attack) through this verification,
libcrypto would fall back to the non-CRT method and assume that the
non-CRT method would give a correct result, despite having just
detecting corruption that is likely from an attack. Instead, just give
up, like NSS does.
Previously, the code tried to handle the case where the input was not
reduced mod rsa->n. This is (was) not possible, so avoid trying to
handle that. This simplifies the equality check and lets us use
|CRYPTO_memcmp|.
Change-Id: I78d1e55520a1c8c280cae2b7256e12ff6290507d
Reviewed-on: https://boringssl-review.googlesource.com/7582
Reviewed-by: David Benjamin <davidben@google.com>
2016-03-25 22:23:16 +00:00
|
|
|
if (!BN_mod_exp_mont_consttime(result, f, d, rsa->n, ctx, rsa->mont_n)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the result to protect against fault attacks as described in the
|
|
|
|
* 1997 paper "On the Importance of Checking Cryptographic Protocols for
|
|
|
|
* Faults" by Dan Boneh, Richard A. DeMillo, and Richard J. Lipton. Some
|
|
|
|
* implementations do this only when the CRT is used, but we do it in all
|
|
|
|
* cases. Section 6 of the aforementioned paper describes an attack that
|
|
|
|
* works when the CRT isn't used. That attack is much less likely to succeed
|
|
|
|
* than the CRT attack, but there have likely been improvements since 1997.
|
|
|
|
*
|
|
|
|
* This check is very cheap assuming |e| is small; it almost always is.
|
|
|
|
*
|
|
|
|
* XXX: It's unfortunate that we don't do this check when |rsa->e == NULL|. */
|
|
|
|
if (rsa->e != NULL) {
|
|
|
|
BIGNUM *vrfy = BN_CTX_get(ctx);
|
|
|
|
if (vrfy == NULL ||
|
|
|
|
!BN_mod_exp_mont(vrfy, result, rsa->e, rsa->n, ctx, rsa->mont_n) ||
|
|
|
|
!BN_equal_consttime(vrfy, f)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blinding) {
|
2016-03-18 02:10:04 +00:00
|
|
|
if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) {
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_bn2bin_padded(out, len, result)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2014-08-18 21:29:45 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
if (blinding != NULL) {
|
|
|
|
rsa_blinding_release(rsa, blinding, blinding_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
|
2016-03-25 23:24:46 +00:00
|
|
|
assert(ctx != NULL);
|
|
|
|
|
2016-03-25 23:15:39 +00:00
|
|
|
assert(rsa->n != NULL);
|
|
|
|
assert(rsa->e != NULL);
|
|
|
|
assert(rsa->d != NULL);
|
|
|
|
assert(rsa->p != NULL);
|
|
|
|
assert(rsa->q != NULL);
|
|
|
|
assert(rsa->dmp1 != NULL);
|
|
|
|
assert(rsa->dmq1 != NULL);
|
|
|
|
assert(rsa->iqmp != NULL);
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
BIGNUM *r1, *m1, *vrfy;
|
|
|
|
BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
|
|
|
|
BIGNUM *dmp1, *dmq1, *c, *pr1;
|
|
|
|
int ret = 0;
|
2015-05-26 19:36:46 +01:00
|
|
|
size_t i, num_additional_primes = 0;
|
|
|
|
|
|
|
|
if (rsa->additional_primes != NULL) {
|
|
|
|
num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
r1 = BN_CTX_get(ctx);
|
|
|
|
m1 = BN_CTX_get(ctx);
|
|
|
|
vrfy = BN_CTX_get(ctx);
|
2016-03-20 08:39:37 +00:00
|
|
|
if (r1 == NULL ||
|
|
|
|
m1 == NULL ||
|
|
|
|
vrfy == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
BIGNUM local_p, local_q;
|
|
|
|
BIGNUM *p = NULL, *q = NULL;
|
|
|
|
|
|
|
|
/* Make sure BN_mod_inverse in Montgomery intialization uses the
|
2015-11-19 03:44:11 +00:00
|
|
|
* BN_FLG_CONSTTIME flag. */
|
2014-06-20 20:00:00 +01:00
|
|
|
BN_init(&local_p);
|
|
|
|
p = &local_p;
|
|
|
|
BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
|
|
|
|
|
|
|
|
BN_init(&local_q);
|
|
|
|
q = &local_q;
|
|
|
|
BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, p, ctx) ||
|
|
|
|
!BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, q, ctx)) {
|
2016-03-25 19:12:48 +00:00
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
|
2016-03-25 19:12:48 +00:00
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compute I mod q */
|
|
|
|
c = &local_c;
|
|
|
|
BN_with_flags(c, I, BN_FLG_CONSTTIME);
|
|
|
|
if (!BN_mod(r1, c, rsa->q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute r1^dmq1 mod q */
|
|
|
|
dmq1 = &local_dmq1;
|
|
|
|
BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
|
2016-02-09 06:36:51 +00:00
|
|
|
if (!BN_mod_exp_mont_consttime(m1, r1, dmq1, rsa->q, ctx, rsa->mont_q)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute I mod p */
|
|
|
|
c = &local_c;
|
|
|
|
BN_with_flags(c, I, BN_FLG_CONSTTIME);
|
|
|
|
if (!BN_mod(r1, c, rsa->p, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute r1^dmp1 mod p */
|
|
|
|
dmp1 = &local_dmp1;
|
|
|
|
BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
|
2016-02-09 06:36:51 +00:00
|
|
|
if (!BN_mod_exp_mont_consttime(r0, r1, dmp1, rsa->p, ctx, rsa->mont_p)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_sub(r0, r0, m1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
/* This will help stop the size of r0 increasing, which does
|
|
|
|
* affect the multiply if it optimised for a power of 2 size */
|
|
|
|
if (BN_is_negative(r0)) {
|
|
|
|
if (!BN_add(r0, r0, rsa->p)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn BN_FLG_CONSTTIME flag on before division operation */
|
|
|
|
pr1 = &local_r1;
|
|
|
|
BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
|
|
|
|
|
|
|
|
if (!BN_mod(r0, pr1, rsa->p, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If p < q it is occasionally possible for the correction of
|
|
|
|
* adding 'p' if r0 is negative above to leave the result still
|
|
|
|
* negative. This can break the private key operations: the following
|
|
|
|
* second correction should *always* correct this rare occurrence.
|
|
|
|
* This will *never* happen with OpenSSL generated keys because
|
|
|
|
* they ensure p > q [steve] */
|
|
|
|
if (BN_is_negative(r0)) {
|
|
|
|
if (!BN_add(r0, r0, rsa->p)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!BN_mul(r1, r0, rsa->q, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!BN_add(r0, r1, m1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-05-26 19:36:46 +01:00
|
|
|
for (i = 0; i < num_additional_primes; i++) {
|
|
|
|
/* multi-prime RSA. */
|
|
|
|
BIGNUM local_exp, local_prime;
|
|
|
|
BIGNUM *exp = &local_exp, *prime = &local_prime;
|
|
|
|
RSA_additional_prime *ap =
|
|
|
|
sk_RSA_additional_prime_value(rsa->additional_primes, i);
|
|
|
|
|
|
|
|
BN_with_flags(exp, ap->exp, BN_FLG_CONSTTIME);
|
|
|
|
BN_with_flags(prime, ap->prime, BN_FLG_CONSTTIME);
|
|
|
|
|
|
|
|
/* c will already point to a BIGNUM with the correct flags. */
|
|
|
|
if (!BN_mod(r1, c, prime, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:11:04 +00:00
|
|
|
if (!BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, prime, ctx) ||
|
2016-03-25 19:12:48 +00:00
|
|
|
!BN_mod_exp_mont_consttime(m1, r1, exp, prime, ctx, ap->mont)) {
|
2015-05-26 19:36:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_set_flags(m1, BN_FLG_CONSTTIME);
|
|
|
|
|
|
|
|
if (!BN_sub(m1, m1, r0) ||
|
|
|
|
!BN_mul(m1, m1, ap->coeff, ctx) ||
|
|
|
|
!BN_mod(m1, m1, prime, ctx) ||
|
|
|
|
(BN_is_negative(m1) && !BN_add(m1, m1, prime)) ||
|
|
|
|
!BN_mul(m1, m1, ap->r, ctx) ||
|
|
|
|
!BN_add(r0, r0, m1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
|
|
|
|
BIGNUM *e_value, BN_GENCB *cb) {
|
2014-06-20 20:00:00 +01:00
|
|
|
BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
|
|
|
|
BIGNUM local_r0, local_d, local_p;
|
|
|
|
BIGNUM *pr0, *d, *p;
|
2015-05-26 19:36:46 +01:00
|
|
|
int prime_bits, ok = -1, n = 0, i, j;
|
2014-06-20 20:00:00 +01:00
|
|
|
BN_CTX *ctx = NULL;
|
2015-05-26 19:36:46 +01:00
|
|
|
STACK_OF(RSA_additional_prime) *additional_primes = NULL;
|
|
|
|
|
|
|
|
if (num_primes < 2) {
|
|
|
|
ok = 0; /* we set our own err */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
|
2015-05-26 19:36:46 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
BN_CTX_start(ctx);
|
|
|
|
r0 = BN_CTX_get(ctx);
|
|
|
|
r1 = BN_CTX_get(ctx);
|
|
|
|
r2 = BN_CTX_get(ctx);
|
|
|
|
r3 = BN_CTX_get(ctx);
|
2015-08-06 15:42:27 +01:00
|
|
|
if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-05-26 19:36:46 +01:00
|
|
|
if (num_primes > 2) {
|
|
|
|
additional_primes = sk_RSA_additional_prime_new_null();
|
|
|
|
if (additional_primes == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 2; i < num_primes; i++) {
|
|
|
|
RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
|
|
|
|
if (ap == NULL) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memset(ap, 0, sizeof(RSA_additional_prime));
|
|
|
|
ap->prime = BN_new();
|
|
|
|
ap->exp = BN_new();
|
|
|
|
ap->coeff = BN_new();
|
|
|
|
ap->r = BN_new();
|
|
|
|
if (ap->prime == NULL ||
|
|
|
|
ap->exp == NULL ||
|
|
|
|
ap->coeff == NULL ||
|
|
|
|
ap->r == NULL ||
|
|
|
|
!sk_RSA_additional_prime_push(additional_primes, ap)) {
|
|
|
|
RSA_additional_prime_free(ap);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* We need the RSA components non-NULL */
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-06-12 02:42:14 +01:00
|
|
|
if (!BN_copy(rsa->e, e_value)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* generate p and q */
|
2015-05-26 19:36:46 +01:00
|
|
|
prime_bits = (bits + (num_primes - 1)) / num_primes;
|
2014-06-20 20:00:00 +01:00
|
|
|
for (;;) {
|
2015-05-26 19:36:46 +01:00
|
|
|
if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
|
2015-02-11 06:17:41 +00:00
|
|
|
!BN_sub(r2, rsa->p, BN_value_one()) ||
|
|
|
|
!BN_gcd(r1, r2, rsa->e, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (BN_is_one(r1)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!BN_GENCB_call(cb, 2, n++)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_GENCB_call(cb, 3, 0)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2015-05-26 19:36:46 +01:00
|
|
|
prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
|
2014-06-20 20:00:00 +01:00
|
|
|
for (;;) {
|
|
|
|
/* When generating ridiculously small keys, we can get stuck
|
|
|
|
* continually regenerating the same prime values. Check for
|
|
|
|
* this and bail if it happens 3 times. */
|
|
|
|
unsigned int degenerate = 0;
|
|
|
|
do {
|
2015-05-26 19:36:46 +01:00
|
|
|
if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
} while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
|
|
|
|
if (degenerate == 3) {
|
|
|
|
ok = 0; /* we set our own err */
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
|
|
|
}
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_sub(r2, rsa->q, BN_value_one()) ||
|
|
|
|
!BN_gcd(r1, r2, rsa->e, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (BN_is_one(r1)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!BN_GENCB_call(cb, 2, n++)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-05-26 19:36:46 +01:00
|
|
|
|
|
|
|
if (!BN_GENCB_call(cb, 3, 1) ||
|
|
|
|
!BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2015-05-26 19:36:46 +01:00
|
|
|
|
|
|
|
for (i = 2; i < num_primes; i++) {
|
|
|
|
RSA_additional_prime *ap =
|
|
|
|
sk_RSA_additional_prime_value(additional_primes, i - 2);
|
|
|
|
prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
|
|
|
|
(num_primes - i);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (BN_cmp(rsa->p, ap->prime) == 0 ||
|
|
|
|
BN_cmp(rsa->q, ap->prime) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < i - 2; j++) {
|
|
|
|
if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
|
|
|
|
ap->prime) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j != i - 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_sub(r2, ap->prime, BN_value_one()) ||
|
|
|
|
!BN_gcd(r1, r2, rsa->e, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_is_one(r1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i != num_primes - 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For the last prime we'll check that it makes n large enough. In the
|
|
|
|
* two prime case this isn't a problem because we generate primes with
|
|
|
|
* the top two bits set and so the product is always of the expected
|
|
|
|
* size. In the multi prime case, this doesn't follow. */
|
|
|
|
if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
2015-06-02 22:16:44 +01:00
|
|
|
if (BN_num_bits(r1) == (unsigned) bits) {
|
2015-05-26 19:36:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_GENCB_call(cb, 2, n++)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ap->r is is the product of all the primes prior to the current one
|
|
|
|
* (including p and q). */
|
|
|
|
if (!BN_copy(ap->r, rsa->n)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (i == num_primes - 1) {
|
|
|
|
/* In the case of the last prime, we calculated n as |r1| in the loop
|
|
|
|
* above. */
|
|
|
|
if (!BN_copy(rsa->n, r1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_GENCB_call(cb, 3, 1)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
if (BN_cmp(rsa->p, rsa->q) < 0) {
|
|
|
|
tmp = rsa->p;
|
|
|
|
rsa->p = rsa->q;
|
|
|
|
rsa->q = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate d */
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_sub(r1, rsa->p, BN_value_one())) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err; /* p-1 */
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!BN_sub(r2, rsa->q, BN_value_one())) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err; /* q-1 */
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
|
|
|
if (!BN_mul(r0, r1, r2, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err; /* (p-1)(q-1) */
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2015-05-26 19:36:46 +01:00
|
|
|
for (i = 2; i < num_primes; i++) {
|
|
|
|
RSA_additional_prime *ap =
|
|
|
|
sk_RSA_additional_prime_value(additional_primes, i - 2);
|
|
|
|
if (!BN_sub(r3, ap->prime, BN_value_one()) ||
|
|
|
|
!BN_mul(r0, r0, r3, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
pr0 = &local_r0;
|
|
|
|
BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err; /* d */
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* set up d for correct BN_FLG_CONSTTIME flag */
|
|
|
|
d = &local_d;
|
|
|
|
BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
|
|
|
|
|
|
|
|
/* calculate d mod (p-1) */
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* calculate d mod (q-1) */
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* calculate inverse of q mod p */
|
|
|
|
p = &local_p;
|
|
|
|
BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
|
|
|
|
|
2015-02-11 06:17:41 +00:00
|
|
|
if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto err;
|
2015-02-11 06:17:41 +00:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-05-26 19:36:46 +01:00
|
|
|
for (i = 2; i < num_primes; i++) {
|
|
|
|
RSA_additional_prime *ap =
|
|
|
|
sk_RSA_additional_prime_value(additional_primes, i - 2);
|
|
|
|
if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
|
|
|
|
!BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
|
|
|
|
!BN_mod_inverse(ap->coeff, ap->r, ap->prime, ctx)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
ok = 1;
|
2015-05-26 19:36:46 +01:00
|
|
|
rsa->additional_primes = additional_primes;
|
|
|
|
additional_primes = NULL;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (ok == -1) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
|
2014-06-20 20:00:00 +01:00
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
if (ctx != NULL) {
|
|
|
|
BN_CTX_end(ctx);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
2015-05-26 19:36:46 +01:00
|
|
|
sk_RSA_additional_prime_pop_free(additional_primes,
|
|
|
|
RSA_additional_prime_free);
|
2014-06-20 20:00:00 +01:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
|
|
|
|
return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
|
|
|
|
cb);
|
2015-05-26 19:36:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-25 23:24:46 +00:00
|
|
|
/* All of the methods are NULL to make it easier for the compiler/linker to drop
|
|
|
|
* unused functions. The wrapper functions will select the appropriate
|
|
|
|
* |rsa_default_*| implementation. */
|
2015-10-29 17:19:12 +00:00
|
|
|
const RSA_METHOD RSA_default_method = {
|
2014-06-20 20:00:00 +01:00
|
|
|
{
|
|
|
|
0 /* references */,
|
|
|
|
1 /* is_static */,
|
|
|
|
},
|
|
|
|
NULL /* app_data */,
|
|
|
|
|
|
|
|
NULL /* init */,
|
2015-10-29 17:19:12 +00:00
|
|
|
NULL /* finish (defaults to rsa_default_finish) */,
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
NULL /* size (defaults to rsa_default_size) */,
|
2014-07-11 19:14:08 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
NULL /* sign */,
|
|
|
|
NULL /* verify */,
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
NULL /* encrypt (defaults to rsa_default_encrypt) */,
|
|
|
|
NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
|
|
|
|
NULL /* decrypt (defaults to rsa_default_decrypt) */,
|
|
|
|
NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
NULL /* private_transform (defaults to rsa_default_private_transform) */,
|
2014-08-18 21:29:45 +01:00
|
|
|
|
2016-03-25 23:24:46 +00:00
|
|
|
NULL /* mod_exp (ignored) */,
|
|
|
|
NULL /* bn_mod_exp (ignored) */,
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
NULL /* keygen (defaults to rsa_default_keygen) */,
|
|
|
|
NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
|
2015-09-12 00:17:44 +01:00
|
|
|
|
|
|
|
NULL /* supports_digest */,
|
2014-06-20 20:00:00 +01:00
|
|
|
};
|