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>
|
|
|
|
|
2015-09-19 18:35:39 +01:00
|
|
|
#include <limits.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/bn.h>
|
2017-05-02 19:34:09 +01:00
|
|
|
#include <openssl/digest.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/engine.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/ex_data.h>
|
2017-05-02 19:15:21 +01:00
|
|
|
#include <openssl/md5.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/mem.h>
|
2016-03-25 22:07:11 +00:00
|
|
|
#include <openssl/nid.h>
|
2017-05-02 19:15:21 +01:00
|
|
|
#include <openssl/sha.h>
|
2015-03-28 07:12:01 +00:00
|
|
|
#include <openssl/thread.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-05-03 19:50:51 +01:00
|
|
|
#include "../bn/internal.h"
|
|
|
|
#include "../delocate.h"
|
|
|
|
#include "../../internal.h"
|
2014-06-20 20:00:00 +01:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
|
2017-05-03 19:50:51 +01:00
|
|
|
DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class);
|
2015-04-15 22:29:53 +01:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
RSA *RSA_new(void) { return RSA_new_method(NULL); }
|
|
|
|
|
|
|
|
RSA *RSA_new_method(const ENGINE *engine) {
|
2016-02-07 19:36:04 +00:00
|
|
|
RSA *rsa = OPENSSL_malloc(sizeof(RSA));
|
2014-06-20 20:00:00 +01:00
|
|
|
if (rsa == 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
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(rsa, 0, sizeof(RSA));
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (engine) {
|
|
|
|
rsa->meth = ENGINE_get_RSA_method(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rsa->meth == NULL) {
|
2017-05-03 19:50:51 +01:00
|
|
|
rsa->meth = (RSA_METHOD *) RSA_default_method();
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
METHOD_ref(rsa->meth);
|
|
|
|
|
|
|
|
rsa->references = 1;
|
|
|
|
rsa->flags = rsa->meth->flags;
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_init(&rsa->lock);
|
2015-12-05 04:14:35 +00:00
|
|
|
CRYPTO_new_ex_data(&rsa->ex_data);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
if (rsa->meth->init && !rsa->meth->init(rsa)) {
|
2017-05-03 19:50:51 +01:00
|
|
|
CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
|
2015-10-23 23:46:16 +01:00
|
|
|
CRYPTO_MUTEX_cleanup(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
METHOD_unref(rsa->meth);
|
|
|
|
OPENSSL_free(rsa);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsa;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_free(RSA *rsa) {
|
|
|
|
unsigned u;
|
|
|
|
|
|
|
|
if (rsa == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-15 20:49:30 +01:00
|
|
|
if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-03 23:24:07 +00:00
|
|
|
if (rsa->meth->finish) {
|
2014-06-20 20:00:00 +01:00
|
|
|
rsa->meth->finish(rsa);
|
|
|
|
}
|
|
|
|
METHOD_unref(rsa->meth);
|
|
|
|
|
2017-05-03 19:50:51 +01:00
|
|
|
CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
Make the rest of RSA CRT constant-time.
Alas, the existence of RSA keys with q > p is obnoxious, but we can
canonicalize it away. To my knowledge, the remaining leaks in RSA are:
- Key generation. This is kind of hopelessly non-constant-time but
perhaps deserves a more careful ponder. Though hopefully it does not
come in at a measurable point for practical purposes.
- Private key serialization. RSAPrivateKey inherently leaks the
magnitudes of d, dmp1, dmq1, and iqmp. This is unavoidable but
hopefully does not come in at a measurable point for practical
purposes.
- If p and q have different word widths, we currently fall back to the
variable-time BN_mod rather than Montgomery reduction at the start of
CRT. I can think of ways to apply Montgomery reduction, but it's
probably better to deny CRT to such keys, if not reject them outright.
- bn_mul_fixed and bn_sqr_fixed which affect the Montgomery
multiplication bn_mul_mont-less configurations, as well as the final
CRT multiplication. We should fix this.
Bug: 233
Change-Id: I8c2ecf8f8ec104e9f26299b66ac8cbb0cad04616
Reviewed-on: https://boringssl-review.googlesource.com/25263
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
2018-01-24 22:22:17 +00:00
|
|
|
BN_free(rsa->n);
|
|
|
|
BN_free(rsa->e);
|
|
|
|
BN_free(rsa->d);
|
|
|
|
BN_free(rsa->p);
|
|
|
|
BN_free(rsa->q);
|
|
|
|
BN_free(rsa->dmp1);
|
|
|
|
BN_free(rsa->dmq1);
|
|
|
|
BN_free(rsa->iqmp);
|
2015-11-03 23:24:07 +00:00
|
|
|
BN_MONT_CTX_free(rsa->mont_n);
|
|
|
|
BN_MONT_CTX_free(rsa->mont_p);
|
|
|
|
BN_MONT_CTX_free(rsa->mont_q);
|
Fix threading issues with RSA freeze_private_key.
OpenSSL's RSA API is poorly designed and does not have a single place to
properly initialize the key. See
https://github.com/openssl/openssl/issues/5158.
To workaround this flaw, we must lazily instantiate pre-computed
Montgomery bits with locking. This is a ton of complexity. More
importantly, it makes it very difficult to implement RSA without side
channels. The correct in-memory representation of d, dmp1, and dmq1
depend on n, p, and q, respectively. (Those values have private
magnitudes and must be sized relative to the respective moduli.)
08805fe27910e09d05e87d61bc5411a4e3b2d999 attempted to fix up the various
widths under lock, when we set up BN_MONT_CTX. However, this introduces
threading issues because other threads may access those exposed
components (RSA_get0_* also count as exposed for these purposes because
they are get0 functions), while a private key operation is in progress.
Instead, we do the following:
- There is no actual need to minimize n, p, and q, but we have minimized
copies in the BN_MONT_CTXs, so use those.
- Store additional copies of d, dmp1, and dmq1, at the cost of more
memory used. These copies have the correct width and are private,
unlike d, dmp1, and dmq1 which are sadly exposed. Fix private key
operations to use them.
- Move the frozen bit out of rsa->flags, as that too was historically
accessible without locking.
(Serialization still uses the original BIGNUMs, but the RSAPrivateKey
serialization format already inherently leaks the magnitude, so this
doesn't matter.)
Change-Id: Ia3a9b0629f8efef23abb30bfed110d247d1db42f
Reviewed-on: https://boringssl-review.googlesource.com/25824
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
2018-02-09 20:52:58 +00:00
|
|
|
BN_free(rsa->d_fixed);
|
|
|
|
BN_free(rsa->dmp1_fixed);
|
|
|
|
BN_free(rsa->dmq1_fixed);
|
Make the rest of RSA CRT constant-time.
Alas, the existence of RSA keys with q > p is obnoxious, but we can
canonicalize it away. To my knowledge, the remaining leaks in RSA are:
- Key generation. This is kind of hopelessly non-constant-time but
perhaps deserves a more careful ponder. Though hopefully it does not
come in at a measurable point for practical purposes.
- Private key serialization. RSAPrivateKey inherently leaks the
magnitudes of d, dmp1, dmq1, and iqmp. This is unavoidable but
hopefully does not come in at a measurable point for practical
purposes.
- If p and q have different word widths, we currently fall back to the
variable-time BN_mod rather than Montgomery reduction at the start of
CRT. I can think of ways to apply Montgomery reduction, but it's
probably better to deny CRT to such keys, if not reject them outright.
- bn_mul_fixed and bn_sqr_fixed which affect the Montgomery
multiplication bn_mul_mont-less configurations, as well as the final
CRT multiplication. We should fix this.
Bug: 233
Change-Id: I8c2ecf8f8ec104e9f26299b66ac8cbb0cad04616
Reviewed-on: https://boringssl-review.googlesource.com/25263
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
2018-01-24 22:22:17 +00:00
|
|
|
BN_free(rsa->inv_small_mod_large_mont);
|
2014-06-20 20:00:00 +01:00
|
|
|
for (u = 0; u < rsa->num_blindings; u++) {
|
|
|
|
BN_BLINDING_free(rsa->blindings[u]);
|
|
|
|
}
|
2015-04-22 21:09:09 +01:00
|
|
|
OPENSSL_free(rsa->blindings);
|
|
|
|
OPENSSL_free(rsa->blindings_inuse);
|
2015-04-13 19:04:14 +01:00
|
|
|
CRYPTO_MUTEX_cleanup(&rsa->lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
OPENSSL_free(rsa);
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_up_ref(RSA *rsa) {
|
2015-05-15 20:49:30 +01:00
|
|
|
CRYPTO_refcount_inc(&rsa->references);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-11-22 01:33:36 +00:00
|
|
|
unsigned RSA_bits(const RSA *rsa) { return BN_num_bits(rsa->n); }
|
|
|
|
|
2016-08-09 16:04:24 +01:00
|
|
|
void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e,
|
|
|
|
const BIGNUM **out_d) {
|
|
|
|
if (out_n != NULL) {
|
|
|
|
*out_n = rsa->n;
|
|
|
|
}
|
|
|
|
if (out_e != NULL) {
|
|
|
|
*out_e = rsa->e;
|
|
|
|
}
|
|
|
|
if (out_d != NULL) {
|
|
|
|
*out_d = rsa->d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
|
|
|
|
const BIGNUM **out_q) {
|
|
|
|
if (out_p != NULL) {
|
|
|
|
*out_p = rsa->p;
|
|
|
|
}
|
|
|
|
if (out_q != NULL) {
|
|
|
|
*out_q = rsa->q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
|
|
|
|
const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) {
|
|
|
|
if (out_dmp1 != NULL) {
|
|
|
|
*out_dmp1 = rsa->dmp1;
|
|
|
|
}
|
|
|
|
if (out_dmq1 != NULL) {
|
|
|
|
*out_dmq1 = rsa->dmq1;
|
|
|
|
}
|
|
|
|
if (out_iqmp != NULL) {
|
|
|
|
*out_iqmp = rsa->iqmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:48:19 +01:00
|
|
|
int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
|
|
|
|
if ((rsa->n == NULL && n == NULL) ||
|
|
|
|
(rsa->e == NULL && e == NULL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n != NULL) {
|
|
|
|
BN_free(rsa->n);
|
|
|
|
rsa->n = n;
|
|
|
|
}
|
|
|
|
if (e != NULL) {
|
|
|
|
BN_free(rsa->e);
|
|
|
|
rsa->e = e;
|
|
|
|
}
|
|
|
|
if (d != NULL) {
|
|
|
|
BN_free(rsa->d);
|
|
|
|
rsa->d = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) {
|
|
|
|
if ((rsa->p == NULL && p == NULL) ||
|
|
|
|
(rsa->q == NULL && q == NULL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != NULL) {
|
|
|
|
BN_free(rsa->p);
|
|
|
|
rsa->p = p;
|
|
|
|
}
|
|
|
|
if (q != NULL) {
|
|
|
|
BN_free(rsa->q);
|
|
|
|
rsa->q = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
|
|
|
|
if ((rsa->dmp1 == NULL && dmp1 == NULL) ||
|
|
|
|
(rsa->dmq1 == NULL && dmq1 == NULL) ||
|
|
|
|
(rsa->iqmp == NULL && iqmp == NULL)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dmp1 != NULL) {
|
|
|
|
BN_free(rsa->dmp1);
|
|
|
|
rsa->dmp1 = dmp1;
|
|
|
|
}
|
|
|
|
if (dmq1 != NULL) {
|
|
|
|
BN_free(rsa->dmq1);
|
|
|
|
rsa->dmq1 = dmq1;
|
|
|
|
}
|
|
|
|
if (iqmp != NULL) {
|
|
|
|
BN_free(rsa->iqmp);
|
|
|
|
rsa->iqmp = iqmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
2014-06-20 20:00:00 +01:00
|
|
|
int padding) {
|
|
|
|
size_t out_len;
|
|
|
|
|
|
|
|
if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
if (out_len > INT_MAX) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_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) {
|
|
|
|
if (rsa->meth->sign_raw) {
|
|
|
|
return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
2014-06-20 20:00:00 +01:00
|
|
|
int padding) {
|
|
|
|
size_t out_len;
|
|
|
|
|
|
|
|
if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
if (out_len > INT_MAX) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_decrypt(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->meth->decrypt) {
|
|
|
|
return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-09-19 18:35:39 +01:00
|
|
|
int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
2014-06-20 20:00:00 +01:00
|
|
|
int padding) {
|
|
|
|
size_t out_len;
|
|
|
|
|
|
|
|
if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-19 18:35:39 +01:00
|
|
|
if (out_len > INT_MAX) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
|
2014-06-20 20:00:00 +01:00
|
|
|
int padding) {
|
|
|
|
size_t out_len;
|
|
|
|
|
|
|
|
if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-19 21:38:36 +01:00
|
|
|
if (out_len > INT_MAX) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned RSA_size(const RSA *rsa) {
|
2014-07-11 19:14:08 +01:00
|
|
|
if (rsa->meth->size) {
|
|
|
|
return rsa->meth->size(rsa);
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
return rsa_default_size(rsa);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-07-18 23:39:42 +01:00
|
|
|
int RSA_is_opaque(const RSA *rsa) {
|
|
|
|
return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
|
|
|
|
}
|
|
|
|
|
2015-12-05 04:14:35 +00:00
|
|
|
int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
|
2017-05-14 22:10:18 +01:00
|
|
|
CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
|
2015-04-15 22:29:53 +01:00
|
|
|
int index;
|
2017-05-03 19:50:51 +01:00
|
|
|
if (!CRYPTO_get_ex_new_index(g_rsa_ex_data_class_bss_get(), &index, argl,
|
2017-05-14 22:10:18 +01:00
|
|
|
argp, free_func)) {
|
2015-04-15 22:29:53 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return index;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 00:09:42 +01:00
|
|
|
int RSA_set_ex_data(RSA *rsa, int idx, void *arg) {
|
|
|
|
return CRYPTO_set_ex_data(&rsa->ex_data, idx, arg);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-01 00:09:42 +01:00
|
|
|
void *RSA_get_ex_data(const RSA *rsa, int idx) {
|
|
|
|
return CRYPTO_get_ex_data(&rsa->ex_data, idx);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
|
|
|
|
// the length of an MD5 and SHA1 hash.
|
2014-06-20 20:00:00 +01:00
|
|
|
static const unsigned SSL_SIG_LENGTH = 36;
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
|
|
|
|
// to be signed with PKCS#1.
|
2014-06-20 20:00:00 +01:00
|
|
|
struct pkcs1_sig_prefix {
|
2017-08-18 19:06:02 +01:00
|
|
|
// nid identifies the hash function.
|
2014-06-20 20:00:00 +01:00
|
|
|
int nid;
|
2017-08-18 19:06:02 +01:00
|
|
|
// hash_len is the expected length of the hash function.
|
2017-05-02 19:15:21 +01:00
|
|
|
uint8_t hash_len;
|
2017-08-18 19:06:02 +01:00
|
|
|
// len is the number of bytes of |bytes| which are valid.
|
2014-06-20 20:00:00 +01:00
|
|
|
uint8_t len;
|
2017-08-18 19:06:02 +01:00
|
|
|
// bytes contains the DER bytes.
|
2014-06-20 20:00:00 +01:00
|
|
|
uint8_t bytes[19];
|
|
|
|
};
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
|
|
|
|
// different hash functions.
|
2014-06-20 20:00:00 +01:00
|
|
|
static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
|
|
|
|
{
|
|
|
|
NID_md5,
|
2017-05-02 19:15:21 +01:00
|
|
|
MD5_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
18,
|
|
|
|
{0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
|
|
|
|
0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NID_sha1,
|
2017-05-02 19:15:21 +01:00
|
|
|
SHA_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
15,
|
|
|
|
{0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
|
|
|
|
0x00, 0x04, 0x14},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NID_sha224,
|
2017-05-02 19:15:21 +01:00
|
|
|
SHA224_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
19,
|
|
|
|
{0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
|
|
|
0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NID_sha256,
|
2017-05-02 19:15:21 +01:00
|
|
|
SHA256_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
19,
|
|
|
|
{0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
|
|
|
0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NID_sha384,
|
2017-05-02 19:15:21 +01:00
|
|
|
SHA384_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
19,
|
|
|
|
{0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
|
|
|
0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NID_sha512,
|
2017-05-02 19:15:21 +01:00
|
|
|
SHA512_DIGEST_LENGTH,
|
2014-06-20 20:00:00 +01:00
|
|
|
19,
|
|
|
|
{0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
|
|
|
|
0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
|
|
|
|
},
|
|
|
|
{
|
2017-05-02 19:15:21 +01:00
|
|
|
NID_undef, 0, 0, {0},
|
2014-06-20 20:00:00 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-05-28 21:53:31 +01:00
|
|
|
int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
|
|
|
|
int *is_alloced, int hash_nid, const uint8_t *msg,
|
|
|
|
size_t msg_len) {
|
2014-06-20 20:00:00 +01:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (hash_nid == NID_md5_sha1) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Special case: SSL signature, just check the length.
|
2014-06-20 20:00:00 +01:00
|
|
|
if (msg_len != SSL_SIG_LENGTH) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_msg = (uint8_t*) msg;
|
|
|
|
*out_msg_len = SSL_SIG_LENGTH;
|
|
|
|
*is_alloced = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
|
|
|
|
const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
|
2015-01-29 23:03:18 +00:00
|
|
|
if (sig_prefix->nid != hash_nid) {
|
|
|
|
continue;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-05-02 19:15:21 +01:00
|
|
|
if (msg_len != sig_prefix->hash_len) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-29 23:03:18 +00:00
|
|
|
const uint8_t* prefix = sig_prefix->bytes;
|
|
|
|
unsigned prefix_len = sig_prefix->len;
|
|
|
|
unsigned signed_msg_len;
|
|
|
|
uint8_t *signed_msg;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-29 23:03:18 +00:00
|
|
|
signed_msg_len = prefix_len + msg_len;
|
|
|
|
if (signed_msg_len < prefix_len) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
|
2015-01-29 23:03:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-29 23:03:18 +00:00
|
|
|
signed_msg = OPENSSL_malloc(signed_msg_len);
|
|
|
|
if (!signed_msg) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2015-01-29 23:03:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcpy(signed_msg, prefix, prefix_len);
|
|
|
|
OPENSSL_memcpy(signed_msg + prefix_len, msg, msg_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-29 23:03:18 +00:00
|
|
|
*out_msg = signed_msg;
|
|
|
|
*out_msg_len = signed_msg_len;
|
|
|
|
*is_alloced = 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-29 23:03:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
|
2015-01-29 23:03:18 +00:00
|
|
|
return 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
|
|
|
|
unsigned *out_len, RSA *rsa) {
|
|
|
|
const unsigned rsa_size = RSA_size(rsa);
|
|
|
|
int ret = 0;
|
2017-05-02 22:21:15 +01:00
|
|
|
uint8_t *signed_msg = NULL;
|
|
|
|
size_t signed_msg_len = 0;
|
2014-06-20 20:00:00 +01:00
|
|
|
int signed_msg_is_alloced = 0;
|
|
|
|
size_t size_t_out_len;
|
|
|
|
|
|
|
|
if (rsa->meth->sign) {
|
|
|
|
return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:53:31 +01:00
|
|
|
if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
|
2017-04-11 17:54:06 +01:00
|
|
|
&signed_msg_is_alloced, hash_nid, in, in_len) ||
|
|
|
|
!RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
|
|
|
|
signed_msg_len, RSA_PKCS1_PADDING)) {
|
|
|
|
goto err;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-04-11 17:54:06 +01:00
|
|
|
*out_len = size_t_out_len;
|
|
|
|
ret = 1;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-04-11 17:54:06 +01:00
|
|
|
err:
|
2014-06-20 20:00:00 +01:00
|
|
|
if (signed_msg_is_alloced) {
|
|
|
|
OPENSSL_free(signed_msg);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-05-02 19:34:09 +01:00
|
|
|
int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
|
|
|
|
const uint8_t *in, size_t in_len, const EVP_MD *md,
|
|
|
|
const EVP_MD *mgf1_md, int salt_len) {
|
|
|
|
if (in_len != EVP_MD_size(md)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t padded_len = RSA_size(rsa);
|
|
|
|
uint8_t *padded = OPENSSL_malloc(padded_len);
|
|
|
|
if (padded == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret =
|
|
|
|
RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, in, md, mgf1_md, salt_len) &&
|
|
|
|
RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len,
|
|
|
|
RSA_NO_PADDING);
|
|
|
|
OPENSSL_free(padded);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
|
|
|
|
const uint8_t *sig, size_t sig_len, RSA *rsa) {
|
2016-03-04 18:54:07 +00:00
|
|
|
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 size_t rsa_size = RSA_size(rsa);
|
|
|
|
uint8_t *buf = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
uint8_t *signed_msg = NULL;
|
2017-05-02 22:21:15 +01:00
|
|
|
size_t signed_msg_len = 0, len;
|
2014-06-20 20:00:00 +01:00
|
|
|
int signed_msg_is_alloced = 0;
|
|
|
|
|
|
|
|
if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
|
2014-06-20 20:00:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = OPENSSL_malloc(rsa_size);
|
|
|
|
if (!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
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
|
|
|
|
RSA_PKCS1_PADDING)) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:53:31 +01:00
|
|
|
if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
|
|
|
|
&signed_msg_is_alloced, hash_nid, msg, msg_len)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Check that no other information follows the hash value (FIPS 186-4 Section
|
|
|
|
// 5.5) and it matches the expected hash.
|
2016-12-13 06:07:13 +00:00
|
|
|
if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
|
2014-06-20 20:00:00 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
out:
|
2015-04-22 21:09:09 +01:00
|
|
|
OPENSSL_free(buf);
|
2014-06-20 20:00:00 +01:00
|
|
|
if (signed_msg_is_alloced) {
|
|
|
|
OPENSSL_free(signed_msg);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2017-05-02 19:34:09 +01:00
|
|
|
int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *msg, size_t msg_len,
|
|
|
|
const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len,
|
|
|
|
const uint8_t *sig, size_t sig_len) {
|
|
|
|
if (msg_len != EVP_MD_size(md)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t em_len = RSA_size(rsa);
|
|
|
|
uint8_t *em = OPENSSL_malloc(em_len);
|
|
|
|
if (em == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (em_len != RSA_size(rsa)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = RSA_verify_PKCS1_PSS_mgf1(rsa, msg, md, mgf1_md, em, salt_len);
|
|
|
|
|
|
|
|
err:
|
|
|
|
OPENSSL_free(em);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-25 20:03:51 +01:00
|
|
|
int RSA_check_key(const RSA *key) {
|
2016-07-26 03:01:10 +01:00
|
|
|
BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
|
2014-07-25 20:03:51 +01:00
|
|
|
BN_CTX *ctx;
|
|
|
|
int ok = 0, has_crt_values;
|
|
|
|
|
|
|
|
if (RSA_is_opaque(key)) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Opaque keys can't be checked.
|
2014-07-25 20:03:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((key->p != NULL) != (key->q != NULL)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
|
2014-07-25 20:03:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key->n || !key->e) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
|
2014-07-25 20:03:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key->d || !key->p) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// For a public key, or without p and q, there's nothing that can be
|
|
|
|
// checked.
|
2014-07-25 20:03:51 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2014-07-25 20:03:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_init(&n);
|
|
|
|
BN_init(&pm1);
|
|
|
|
BN_init(&qm1);
|
|
|
|
BN_init(&lcm);
|
|
|
|
BN_init(&gcd);
|
|
|
|
BN_init(&de);
|
|
|
|
BN_init(&dmp1);
|
|
|
|
BN_init(&dmq1);
|
2016-07-26 03:01:10 +01:00
|
|
|
BN_init(&iqmp_times_q);
|
2014-07-25 20:03:51 +01:00
|
|
|
|
2015-05-26 19:36:46 +01:00
|
|
|
if (!BN_mul(&n, key->p, key->q, ctx) ||
|
2017-08-18 19:06:02 +01:00
|
|
|
// lcm = lcm(p, q)
|
2014-07-25 20:03:51 +01:00
|
|
|
!BN_sub(&pm1, key->p, BN_value_one()) ||
|
|
|
|
!BN_sub(&qm1, key->q, BN_value_one()) ||
|
|
|
|
!BN_mul(&lcm, &pm1, &qm1, ctx) ||
|
2015-05-26 19:36:46 +01:00
|
|
|
!BN_gcd(&gcd, &pm1, &qm1, ctx)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
|
2015-05-26 19:36:46 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
|
2014-07-25 20:03:51 +01:00
|
|
|
!BN_gcd(&gcd, &pm1, &qm1, ctx) ||
|
2017-08-18 19:06:02 +01:00
|
|
|
// de = d*e mod lcm(p, q).
|
2014-07-25 20:03:51 +01:00
|
|
|
!BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_cmp(&n, key->n) != 0) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!BN_is_one(&de)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
has_crt_values = key->dmp1 != NULL;
|
|
|
|
if (has_crt_values != (key->dmq1 != NULL) ||
|
|
|
|
has_crt_values != (key->iqmp != NULL)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-04-12 23:28:48 +01:00
|
|
|
if (has_crt_values) {
|
2017-08-18 19:06:02 +01:00
|
|
|
if (// dmp1 = d mod (p-1)
|
2014-07-25 20:03:51 +01:00
|
|
|
!BN_mod(&dmp1, key->d, &pm1, ctx) ||
|
2017-08-18 19:06:02 +01:00
|
|
|
// dmq1 = d mod (q-1)
|
2014-07-25 20:03:51 +01:00
|
|
|
!BN_mod(&dmq1, key->d, &qm1, ctx) ||
|
2017-08-18 19:06:02 +01:00
|
|
|
// iqmp = q^-1 mod p
|
2016-07-26 03:01:10 +01:00
|
|
|
!BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_cmp(&dmp1, key->dmp1) != 0 ||
|
|
|
|
BN_cmp(&dmq1, key->dmq1) != 0 ||
|
2016-07-26 03:01:10 +01:00
|
|
|
BN_cmp(key->iqmp, key->p) >= 0 ||
|
|
|
|
!BN_is_one(&iqmp_times_q)) {
|
2015-06-29 05:28:17 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
|
2014-07-25 20:03:51 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
BN_free(&n);
|
|
|
|
BN_free(&pm1);
|
|
|
|
BN_free(&qm1);
|
|
|
|
BN_free(&lcm);
|
|
|
|
BN_free(&gcd);
|
|
|
|
BN_free(&de);
|
|
|
|
BN_free(&dmp1);
|
|
|
|
BN_free(&dmq1);
|
2016-07-26 03:01:10 +01:00
|
|
|
BN_free(&iqmp_times_q);
|
2014-07-25 20:03:51 +01:00
|
|
|
BN_CTX_free(ctx);
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:46:03 +01:00
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// This is the product of the 132 smallest odd primes, from 3 to 751.
|
2017-04-11 17:46:03 +01:00
|
|
|
static const BN_ULONG kSmallFactorsLimbs[] = {
|
|
|
|
TOBN(0xc4309333, 0x3ef4e3e1), TOBN(0x71161eb6, 0xcd2d655f),
|
|
|
|
TOBN(0x95e2238c, 0x0bf94862), TOBN(0x3eb233d3, 0x24f7912b),
|
|
|
|
TOBN(0x6b55514b, 0xbf26c483), TOBN(0x0a84d817, 0x5a144871),
|
|
|
|
TOBN(0x77d12fee, 0x9b82210a), TOBN(0xdb5b93c2, 0x97f050b3),
|
|
|
|
TOBN(0x4acad6b9, 0x4d6c026b), TOBN(0xeb7751f3, 0x54aec893),
|
|
|
|
TOBN(0xdba53368, 0x36bc85c4), TOBN(0xd85a1b28, 0x7f5ec78e),
|
|
|
|
TOBN(0x2eb072d8, 0x6b322244), TOBN(0xbba51112, 0x5e2b3aea),
|
|
|
|
TOBN(0x36ed1a6c, 0x0e2486bf), TOBN(0x5f270460, 0xec0c5727),
|
|
|
|
0x000017b1
|
|
|
|
};
|
2017-05-03 19:50:51 +01:00
|
|
|
|
|
|
|
DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) {
|
|
|
|
out->d = (BN_ULONG *) kSmallFactorsLimbs;
|
2018-01-15 10:23:24 +00:00
|
|
|
out->width = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs);
|
|
|
|
out->dmax = out->width;
|
2017-05-03 19:50:51 +01:00
|
|
|
out->neg = 0;
|
|
|
|
out->flags = BN_FLG_STATIC_DATA;
|
|
|
|
}
|
2017-04-11 17:46:03 +01:00
|
|
|
|
2017-04-06 19:55:18 +01:00
|
|
|
int RSA_check_fips(RSA *key) {
|
2017-04-11 17:46:03 +01:00
|
|
|
if (RSA_is_opaque(key)) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// Opaque keys can't be checked.
|
2017-04-11 17:46:03 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RSA_check_key(key)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_CTX *ctx = BN_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIGNUM small_gcd;
|
|
|
|
BN_init(&small_gcd);
|
|
|
|
|
|
|
|
int ret = 1;
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// Perform partial public key validation of RSA keys (SP 800-89 5.3.3).
|
2017-04-20 15:45:25 +01:00
|
|
|
enum bn_primality_result_t primality_result;
|
2017-04-20 20:36:18 +01:00
|
|
|
if (BN_num_bits(key->e) <= 16 ||
|
2017-04-11 17:46:03 +01:00
|
|
|
BN_num_bits(key->e) > 256 ||
|
|
|
|
!BN_is_odd(key->n) ||
|
|
|
|
!BN_is_odd(key->e) ||
|
2017-05-03 19:50:51 +01:00
|
|
|
!BN_gcd(&small_gcd, key->n, g_small_factors(), ctx) ||
|
2017-04-20 15:45:25 +01:00
|
|
|
!BN_is_one(&small_gcd) ||
|
|
|
|
!BN_enhanced_miller_rabin_primality_test(&primality_result, key->n,
|
|
|
|
BN_prime_checks, ctx, NULL) ||
|
|
|
|
primality_result != bn_non_prime_power_composite) {
|
2017-04-11 17:46:03 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BN_free(&small_gcd);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
|
2017-04-13 18:14:12 +01:00
|
|
|
if (!ret || key->d == NULL || key->p == NULL) {
|
2017-08-18 19:06:02 +01:00
|
|
|
// On a failure or on only a public key, there's nothing else can be
|
|
|
|
// checked.
|
2017-04-06 19:55:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-18 19:06:02 +01:00
|
|
|
// FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG,
|
|
|
|
// section 9.9, it is not known whether |rsa| will be used for signing or
|
|
|
|
// encryption, so either pair-wise consistency self-test is acceptable. We
|
|
|
|
// perform a signing test.
|
2017-04-06 19:55:18 +01:00
|
|
|
uint8_t data[32] = {0};
|
|
|
|
unsigned sig_len = RSA_size(key);
|
|
|
|
uint8_t *sig = OPENSSL_malloc(sig_len);
|
|
|
|
if (sig == NULL) {
|
2017-04-13 18:14:12 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
2017-04-06 19:55:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:34:45 +01:00
|
|
|
if (!RSA_sign(NID_sha256, data, sizeof(data), sig, &sig_len, key)) {
|
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#if defined(BORINGSSL_FIPS_BREAK_RSA_PWCT)
|
|
|
|
data[0] = ~data[0];
|
|
|
|
#endif
|
|
|
|
if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) {
|
2017-04-13 18:14:12 +01:00
|
|
|
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
2017-04-06 19:55:18 +01:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-12 23:34:45 +01:00
|
|
|
cleanup:
|
2017-04-06 19:55:18 +01:00
|
|
|
OPENSSL_free(sig);
|
|
|
|
|
2017-04-11 17:46:03 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:29:45 +01:00
|
|
|
int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
|
|
|
|
size_t len) {
|
|
|
|
if (rsa->meth->private_transform) {
|
|
|
|
return rsa->meth->private_transform(rsa, out, in, len);
|
|
|
|
}
|
|
|
|
|
2015-10-29 17:19:12 +00:00
|
|
|
return rsa_default_private_transform(rsa, out, in, len);
|
2014-08-18 21:29:45 +01:00
|
|
|
}
|
2015-04-13 22:34:17 +01:00
|
|
|
|
2017-12-18 21:34:49 +00:00
|
|
|
int RSA_flags(const RSA *rsa) { return rsa->flags; }
|
|
|
|
|
2015-04-13 22:34:17 +01:00
|
|
|
int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
|
|
|
|
return 1;
|
|
|
|
}
|