You've already forked linux-packaging-mono
Imported Upstream version 4.8.0.309
Former-commit-id: 5f9c6ae75f295e057a7d2971f3a6df4656fa8850
This commit is contained in:
parent
ee1447783b
commit
94b2861243
26
external/boringssl/crypto/rsa/CMakeLists.txt
vendored
Normal file
26
external/boringssl/crypto/rsa/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
include_directories(../../include)
|
||||
|
||||
add_library(
|
||||
rsa
|
||||
|
||||
OBJECT
|
||||
|
||||
rsa.c
|
||||
rsa_impl.c
|
||||
blinding.c
|
||||
padding.c
|
||||
rsa_asn1.c
|
||||
)
|
||||
|
||||
if(ENABLE_TESTS)
|
||||
add_executable(
|
||||
rsa_test
|
||||
|
||||
rsa_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
)
|
||||
|
||||
target_link_libraries(rsa_test crypto)
|
||||
add_dependencies(all_tests rsa_test)
|
||||
endif()
|
||||
267
external/boringssl/crypto/rsa/blinding.c
vendored
Normal file
267
external/boringssl/crypto/rsa/blinding.c
vendored
Normal file
@@ -0,0 +1,267 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
* 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 above 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 acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* openssl-core@openssl.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com).
|
||||
*
|
||||
* 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>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/mem.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
#define BN_BLINDING_COUNTER 32
|
||||
|
||||
struct bn_blinding_st {
|
||||
BIGNUM *A; /* The base blinding factor, Montgomery-encoded. */
|
||||
BIGNUM *Ai; /* The inverse of the blinding factor, Montgomery-encoded. */
|
||||
unsigned counter;
|
||||
};
|
||||
|
||||
static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e,
|
||||
const BN_MONT_CTX *mont, BN_CTX *ctx);
|
||||
|
||||
BN_BLINDING *BN_BLINDING_new(void) {
|
||||
BN_BLINDING *ret = OPENSSL_malloc(sizeof(BN_BLINDING));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
memset(ret, 0, sizeof(BN_BLINDING));
|
||||
|
||||
ret->A = BN_new();
|
||||
if (ret->A == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret->Ai = BN_new();
|
||||
if (ret->Ai == NULL) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* The blinding values need to be created before this blinding can be used. */
|
||||
ret->counter = BN_BLINDING_COUNTER - 1;
|
||||
|
||||
return ret;
|
||||
|
||||
err:
|
||||
BN_BLINDING_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void BN_BLINDING_free(BN_BLINDING *r) {
|
||||
if (r == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
BN_free(r->A);
|
||||
BN_free(r->Ai);
|
||||
OPENSSL_free(r);
|
||||
}
|
||||
|
||||
static int bn_blinding_update(BN_BLINDING *b, const BIGNUM *e,
|
||||
const BN_MONT_CTX *mont, BN_CTX *ctx) {
|
||||
if (++b->counter == BN_BLINDING_COUNTER) {
|
||||
/* re-create blinding parameters */
|
||||
if (!bn_blinding_create_param(b, e, mont, ctx)) {
|
||||
goto err;
|
||||
}
|
||||
b->counter = 0;
|
||||
} else {
|
||||
if (!BN_mod_mul_montgomery(b->A, b->A, b->A, mont, ctx) ||
|
||||
!BN_mod_mul_montgomery(b->Ai, b->Ai, b->Ai, mont, ctx)) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
/* |A| and |Ai| may be in an inconsistent state so they both need to be
|
||||
* replaced the next time this blinding is used. Note that this is only
|
||||
* sufficient because support for |BN_BLINDING_NO_UPDATE| and
|
||||
* |BN_BLINDING_NO_RECREATE| was previously dropped. */
|
||||
b->counter = BN_BLINDING_COUNTER - 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, const BIGNUM *e,
|
||||
const BN_MONT_CTX *mont, BN_CTX *ctx) {
|
||||
/* |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery|
|
||||
* cancels one Montgomery factor, so the resulting value of |n| is unencoded.
|
||||
*/
|
||||
if (!bn_blinding_update(b, e, mont, ctx) ||
|
||||
!BN_mod_mul_montgomery(n, n, b->A, mont, ctx)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont,
|
||||
BN_CTX *ctx) {
|
||||
/* |n| is not Montgomery-encoded and |b->A| is. |BN_mod_mul_montgomery|
|
||||
* cancels one Montgomery factor, so the resulting value of |n| is unencoded.
|
||||
*/
|
||||
return BN_mod_mul_montgomery(n, n, b->Ai, mont, ctx);
|
||||
}
|
||||
|
||||
static int bn_blinding_create_param(BN_BLINDING *b, const BIGNUM *e,
|
||||
const BN_MONT_CTX *mont, BN_CTX *ctx) {
|
||||
BIGNUM mont_N_consttime;
|
||||
BN_init(&mont_N_consttime);
|
||||
BN_with_flags(&mont_N_consttime, &mont->N, BN_FLG_CONSTTIME);
|
||||
int retry_counter = 32;
|
||||
|
||||
do {
|
||||
if (!BN_rand_range(b->A, &mont->N)) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* |BN_from_montgomery| + |BN_mod_inverse_no_branch| is equivalent to, but
|
||||
* more efficient than, |BN_mod_inverse_no_branch| + |BN_to_montgomery|. */
|
||||
if (!BN_from_montgomery(b->Ai, b->A, mont, ctx)) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int no_inverse;
|
||||
if (BN_mod_inverse_ex(b->Ai, &no_inverse, b->Ai, &mont_N_consttime, ctx) ==
|
||||
NULL) {
|
||||
/* this should almost never happen for good RSA keys */
|
||||
if (no_inverse) {
|
||||
if (retry_counter-- == 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
|
||||
return 0;
|
||||
}
|
||||
ERR_clear_error();
|
||||
} else {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
|
||||
if (!BN_mod_exp_mont(b->A, b->A, e, &mont->N, ctx, mont)) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!BN_to_montgomery(b->A, b->A, mont, ctx)) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
148
external/boringssl/crypto/rsa/internal.h
vendored
Normal file
148
external/boringssl/crypto/rsa/internal.h
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
/* 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.] */
|
||||
|
||||
#ifndef OPENSSL_HEADER_RSA_INTERNAL_H
|
||||
#define OPENSSL_HEADER_RSA_INTERNAL_H
|
||||
|
||||
#include <openssl/base.h>
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Default implementations of RSA operations. */
|
||||
|
||||
extern const RSA_METHOD RSA_default_method;
|
||||
|
||||
size_t rsa_default_size(const RSA *rsa);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
|
||||
size_t len);
|
||||
int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
|
||||
BIGNUM *e_value, BN_GENCB *cb);
|
||||
int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb);
|
||||
|
||||
|
||||
#define RSA_PKCS1_PADDING_SIZE 11
|
||||
|
||||
|
||||
BN_BLINDING *BN_BLINDING_new(void);
|
||||
void BN_BLINDING_free(BN_BLINDING *b);
|
||||
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, const BIGNUM *e,
|
||||
const BN_MONT_CTX *mont_ctx, BN_CTX *ctx);
|
||||
int BN_BLINDING_invert(BIGNUM *n, const BN_BLINDING *b, BN_MONT_CTX *mont_ctx,
|
||||
BN_CTX *ctx);
|
||||
|
||||
|
||||
int RSA_padding_add_PKCS1_type_1(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len);
|
||||
int RSA_padding_check_PKCS1_type_1(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len);
|
||||
int RSA_padding_add_PKCS1_type_2(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len);
|
||||
int RSA_padding_check_PKCS1_type_2(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len);
|
||||
int RSA_padding_add_PKCS1_OAEP_mgf1(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len,
|
||||
const uint8_t *param, unsigned plen,
|
||||
const EVP_MD *md, const EVP_MD *mgf1md);
|
||||
int RSA_padding_check_PKCS1_OAEP_mgf1(uint8_t *to, unsigned to_len,
|
||||
const uint8_t *from, unsigned from_len,
|
||||
const uint8_t *param, unsigned plen,
|
||||
const EVP_MD *md, const EVP_MD *mgf1md);
|
||||
int RSA_padding_add_none(uint8_t *to, unsigned to_len, const uint8_t *from,
|
||||
unsigned from_len);
|
||||
|
||||
/* RSA_private_transform calls either the method-specific |private_transform|
|
||||
* function (if given) or the generic one. See the comment for
|
||||
* |private_transform| in |rsa_meth_st|. */
|
||||
int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
|
||||
size_t len);
|
||||
|
||||
|
||||
/* RSA_additional_prime contains information about the third, forth etc prime
|
||||
* in a multi-prime RSA key. */
|
||||
typedef struct RSA_additional_prime_st {
|
||||
BIGNUM *prime;
|
||||
/* exp is d^{prime-1} mod prime */
|
||||
BIGNUM *exp;
|
||||
/* coeff is such that r×coeff ≡ 1 mod prime. */
|
||||
BIGNUM *coeff;
|
||||
|
||||
/* Values below here are not in the ASN.1 serialisation. */
|
||||
|
||||
/* r is the product of all primes (including p and q) prior to this one. */
|
||||
BIGNUM *r;
|
||||
/* mont is a |BN_MONT_CTX| modulo |prime|. */
|
||||
BN_MONT_CTX *mont;
|
||||
} RSA_additional_prime;
|
||||
|
||||
void RSA_additional_prime_free(RSA_additional_prime *ap);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
} /* extern C */
|
||||
#endif
|
||||
|
||||
#endif /* OPENSSL_HEADER_RSA_INTERNAL_H */
|
||||
706
external/boringssl/crypto/rsa/padding.c
vendored
Normal file
706
external/boringssl/crypto/rsa/padding.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
794
external/boringssl/crypto/rsa/rsa.c
vendored
Normal file
794
external/boringssl/crypto/rsa/rsa.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
445
external/boringssl/crypto/rsa/rsa_asn1.c
vendored
Normal file
445
external/boringssl/crypto/rsa/rsa_asn1.c
vendored
Normal file
@@ -0,0 +1,445 @@
|
||||
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
|
||||
* project 2000.
|
||||
*/
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
* 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 above 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 acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||||
* endorse or promote products derived from this software without
|
||||
* prior written permission. For written permission, please contact
|
||||
* licensing@OpenSSL.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "OpenSSL"
|
||||
* nor may "OpenSSL" appear in their names without prior written
|
||||
* permission of the OpenSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the OpenSSL Project
|
||||
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED 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 OpenSSL PROJECT OR
|
||||
* ITS 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This product includes cryptographic software written by Eric Young
|
||||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||||
* Hudson (tjh@cryptsoft.com). */
|
||||
|
||||
#include <openssl/rsa.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/bytestring.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/mem.h>
|
||||
|
||||
#include "internal.h"
|
||||
#include "../bytestring/internal.h"
|
||||
|
||||
|
||||
static int parse_integer_buggy(CBS *cbs, BIGNUM **out, int buggy) {
|
||||
assert(*out == NULL);
|
||||
*out = BN_new();
|
||||
if (*out == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if (buggy) {
|
||||
return BN_parse_asn1_unsigned_buggy(cbs, *out);
|
||||
}
|
||||
return BN_parse_asn1_unsigned(cbs, *out);
|
||||
}
|
||||
|
||||
static int parse_integer(CBS *cbs, BIGNUM **out) {
|
||||
return parse_integer_buggy(cbs, out, 0 /* not buggy */);
|
||||
}
|
||||
|
||||
static int marshal_integer(CBB *cbb, BIGNUM *bn) {
|
||||
if (bn == NULL) {
|
||||
/* An RSA object may be missing some components. */
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
|
||||
return 0;
|
||||
}
|
||||
return BN_marshal_asn1(cbb, bn);
|
||||
}
|
||||
|
||||
static RSA *parse_public_key(CBS *cbs, int buggy) {
|
||||
RSA *ret = RSA_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
CBS child;
|
||||
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!parse_integer_buggy(&child, &ret->n, buggy) ||
|
||||
!parse_integer(&child, &ret->e) ||
|
||||
CBS_len(&child) != 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!BN_is_odd(ret->e) ||
|
||||
BN_num_bits(ret->e) < 2) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
RSA *RSA_parse_public_key(CBS *cbs) {
|
||||
return parse_public_key(cbs, 0 /* not buggy */);
|
||||
}
|
||||
|
||||
RSA *RSA_parse_public_key_buggy(CBS *cbs) {
|
||||
/* Estonian IDs issued between September 2014 to September 2015 are
|
||||
* broken. See https://crbug.com/532048 and https://crbug.com/534766.
|
||||
*
|
||||
* TODO(davidben): Remove this code and callers in March 2016. */
|
||||
return parse_public_key(cbs, 1 /* buggy */);
|
||||
}
|
||||
|
||||
RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, in_len);
|
||||
RSA *ret = RSA_parse_public_key(&cbs);
|
||||
if (ret == NULL || CBS_len(&cbs) != 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
|
||||
CBB child;
|
||||
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!marshal_integer(&child, rsa->n) ||
|
||||
!marshal_integer(&child, rsa->e) ||
|
||||
!CBB_flush(cbb)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
|
||||
const RSA *rsa) {
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!RSA_marshal_public_key(&cbb, rsa) ||
|
||||
!CBB_finish(&cbb, out_bytes, out_len)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
CBB_cleanup(&cbb);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* kVersionTwoPrime and kVersionMulti are the supported values of the version
|
||||
* field of an RSAPrivateKey structure (RFC 3447). */
|
||||
static const uint64_t kVersionTwoPrime = 0;
|
||||
static const uint64_t kVersionMulti = 1;
|
||||
|
||||
/* rsa_parse_additional_prime parses a DER-encoded OtherPrimeInfo from |cbs| and
|
||||
* advances |cbs|. It returns a newly-allocated |RSA_additional_prime| on
|
||||
* success or NULL on error. The |r| and |mont| fields of the result are set to
|
||||
* NULL. */
|
||||
static RSA_additional_prime *rsa_parse_additional_prime(CBS *cbs) {
|
||||
RSA_additional_prime *ret = OPENSSL_malloc(sizeof(RSA_additional_prime));
|
||||
if (ret == NULL) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
memset(ret, 0, sizeof(RSA_additional_prime));
|
||||
|
||||
CBS child;
|
||||
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!parse_integer(&child, &ret->prime) ||
|
||||
!parse_integer(&child, &ret->exp) ||
|
||||
!parse_integer(&child, &ret->coeff) ||
|
||||
CBS_len(&child) != 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
RSA_additional_prime_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
RSA *RSA_parse_private_key(CBS *cbs) {
|
||||
BN_CTX *ctx = NULL;
|
||||
BIGNUM *product_of_primes_so_far = NULL;
|
||||
RSA *ret = RSA_new();
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CBS child;
|
||||
uint64_t version;
|
||||
if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!CBS_get_asn1_uint64(&child, &version)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (version != kVersionTwoPrime && version != kVersionMulti) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!parse_integer(&child, &ret->n) ||
|
||||
!parse_integer(&child, &ret->e) ||
|
||||
!parse_integer(&child, &ret->d) ||
|
||||
!parse_integer(&child, &ret->p) ||
|
||||
!parse_integer(&child, &ret->q) ||
|
||||
!parse_integer(&child, &ret->dmp1) ||
|
||||
!parse_integer(&child, &ret->dmq1) ||
|
||||
!parse_integer(&child, &ret->iqmp)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (version == kVersionMulti) {
|
||||
/* Although otherPrimeInfos is written as OPTIONAL in RFC 3447, it later
|
||||
* says "[otherPrimeInfos] shall be omitted if version is 0 and shall
|
||||
* contain at least one instance of OtherPrimeInfo if version is 1." The
|
||||
* OPTIONAL is just so both versions share a single definition. */
|
||||
CBS other_prime_infos;
|
||||
if (!CBS_get_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE) ||
|
||||
CBS_len(&other_prime_infos) == 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
goto err;
|
||||
}
|
||||
ret->additional_primes = sk_RSA_additional_prime_new_null();
|
||||
if (ret->additional_primes == NULL) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ctx = BN_CTX_new();
|
||||
product_of_primes_so_far = BN_new();
|
||||
if (ctx == NULL ||
|
||||
product_of_primes_so_far == NULL ||
|
||||
!BN_mul(product_of_primes_so_far, ret->p, ret->q, ctx)) {
|
||||
goto err;
|
||||
}
|
||||
|
||||
while (CBS_len(&other_prime_infos) > 0) {
|
||||
RSA_additional_prime *ap = rsa_parse_additional_prime(&other_prime_infos);
|
||||
if (ap == NULL) {
|
||||
goto err;
|
||||
}
|
||||
if (!sk_RSA_additional_prime_push(ret->additional_primes, ap)) {
|
||||
OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
|
||||
RSA_additional_prime_free(ap);
|
||||
goto err;
|
||||
}
|
||||
ap->r = BN_dup(product_of_primes_so_far);
|
||||
if (ap->r == NULL ||
|
||||
!BN_mul(product_of_primes_so_far, product_of_primes_so_far,
|
||||
ap->prime, ctx)) {
|
||||
goto err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CBS_len(&child) != 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
goto err;
|
||||
}
|
||||
|
||||
BN_CTX_free(ctx);
|
||||
BN_free(product_of_primes_so_far);
|
||||
return ret;
|
||||
|
||||
err:
|
||||
BN_CTX_free(ctx);
|
||||
BN_free(product_of_primes_so_far);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, in, in_len);
|
||||
RSA *ret = RSA_parse_private_key(&cbs);
|
||||
if (ret == NULL || CBS_len(&cbs) != 0) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
|
||||
const int is_multiprime =
|
||||
sk_RSA_additional_prime_num(rsa->additional_primes) > 0;
|
||||
|
||||
CBB child;
|
||||
if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
|
||||
!CBB_add_asn1_uint64(&child,
|
||||
is_multiprime ? kVersionMulti : kVersionTwoPrime) ||
|
||||
!marshal_integer(&child, rsa->n) ||
|
||||
!marshal_integer(&child, rsa->e) ||
|
||||
!marshal_integer(&child, rsa->d) ||
|
||||
!marshal_integer(&child, rsa->p) ||
|
||||
!marshal_integer(&child, rsa->q) ||
|
||||
!marshal_integer(&child, rsa->dmp1) ||
|
||||
!marshal_integer(&child, rsa->dmq1) ||
|
||||
!marshal_integer(&child, rsa->iqmp)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (is_multiprime) {
|
||||
CBB other_prime_infos;
|
||||
if (!CBB_add_asn1(&child, &other_prime_infos, CBS_ASN1_SEQUENCE)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
size_t i;
|
||||
for (i = 0; i < sk_RSA_additional_prime_num(rsa->additional_primes); i++) {
|
||||
RSA_additional_prime *ap =
|
||||
sk_RSA_additional_prime_value(rsa->additional_primes, i);
|
||||
CBB other_prime_info;
|
||||
if (!CBB_add_asn1(&other_prime_infos, &other_prime_info,
|
||||
CBS_ASN1_SEQUENCE) ||
|
||||
!marshal_integer(&other_prime_info, ap->prime) ||
|
||||
!marshal_integer(&other_prime_info, ap->exp) ||
|
||||
!marshal_integer(&other_prime_info, ap->coeff)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!CBB_flush(cbb)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
|
||||
const RSA *rsa) {
|
||||
CBB cbb;
|
||||
CBB_zero(&cbb);
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!RSA_marshal_private_key(&cbb, rsa) ||
|
||||
!CBB_finish(&cbb, out_bytes, out_len)) {
|
||||
OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR);
|
||||
CBB_cleanup(&cbb);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
RSA *ret = RSA_parse_public_key(&cbs);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (out != NULL) {
|
||||
RSA_free(*out);
|
||||
*out = ret;
|
||||
}
|
||||
*inp = CBS_data(&cbs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!RSA_marshal_public_key(&cbb, in)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
CBS cbs;
|
||||
CBS_init(&cbs, *inp, (size_t)len);
|
||||
RSA *ret = RSA_parse_private_key(&cbs);
|
||||
if (ret == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (out != NULL) {
|
||||
RSA_free(*out);
|
||||
*out = ret;
|
||||
}
|
||||
*inp = CBS_data(&cbs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
|
||||
CBB cbb;
|
||||
if (!CBB_init(&cbb, 0) ||
|
||||
!RSA_marshal_private_key(&cbb, in)) {
|
||||
CBB_cleanup(&cbb);
|
||||
return -1;
|
||||
}
|
||||
return CBB_finish_i2d(&cbb, outp);
|
||||
}
|
||||
|
||||
RSA *RSAPublicKey_dup(const RSA *rsa) {
|
||||
uint8_t *der;
|
||||
size_t der_len;
|
||||
if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
|
||||
return NULL;
|
||||
}
|
||||
RSA *ret = RSA_public_key_from_bytes(der, der_len);
|
||||
OPENSSL_free(der);
|
||||
return ret;
|
||||
}
|
||||
|
||||
RSA *RSAPrivateKey_dup(const RSA *rsa) {
|
||||
uint8_t *der;
|
||||
size_t der_len;
|
||||
if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
|
||||
return NULL;
|
||||
}
|
||||
RSA *ret = RSA_private_key_from_bytes(der, der_len);
|
||||
OPENSSL_free(der);
|
||||
return ret;
|
||||
}
|
||||
1140
external/boringssl/crypto/rsa/rsa_impl.c
vendored
Normal file
1140
external/boringssl/crypto/rsa/rsa_impl.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
954
external/boringssl/crypto/rsa/rsa_test.cc
vendored
Normal file
954
external/boringssl/crypto/rsa/rsa_test.cc
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user