/* Copyright (C) 1995-1998 Eric Young ([email protected]) * All rights reserved. * * This package is an SSL implementation written * by Eric Young ([email protected]). * 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 ([email protected]). * * 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 ([email protected])" * 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 ([email protected])" * * 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 <assert.h> #include <limits.h> #include <string.h> #include <openssl/bn.h> #include <openssl/err.h> #include <openssl/mem.h> #include <openssl/thread.h> #include "../../bcm_support.h" #include "../../internal.h" #include "../bn/internal.h" #include "../delocate.h" #include "../service_indicator/internal.h" #include "internal.h" int rsa_check_public_key(const RSA *rsa) { … } static int ensure_fixed_copy(BIGNUM **out, const BIGNUM *in, int width) { … } // freeze_private_key finishes initializing |rsa|'s private key components. // After this function has returned, |rsa| may not be changed. This is needed // because |RSA| is a public struct and, additionally, OpenSSL 1.1.0 opaquified // it wrong (see https://github.com/openssl/openssl/issues/5158). static int freeze_private_key(RSA *rsa, BN_CTX *ctx) { … } void rsa_invalidate_key(RSA *rsa) { … } size_t rsa_default_size(const RSA *rsa) { … } // 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. #if defined(OPENSSL_TSAN) // Smaller under TSAN so that the edge case can be hit with fewer threads. #define MAX_BLINDINGS_PER_RSA … #else #define MAX_BLINDINGS_PER_RSA … #endif // 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, size_t *index_used, BN_CTX *ctx) { … } // 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, size_t blinding_index) { … } // signing 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) { … } static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx); int rsa_verify_raw_no_self_test(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_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) { … } int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) { … } // mod_montgomery sets |r| to |I| mod |p|. |I| must already be fully reduced // modulo |p| times |q|. It returns one on success and zero on error. static int mod_montgomery(BIGNUM *r, const BIGNUM *I, const BIGNUM *p, const BN_MONT_CTX *mont_p, const BIGNUM *q, BN_CTX *ctx) { … } static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) { … } static int ensure_bignum(BIGNUM **out) { … } // kBoringSSLRSASqrtTwo is the BIGNUM representation of ⌊2²⁰⁴⁷×√2⌋. This is // chosen to give enough precision for 4096-bit RSA, the largest key size FIPS // specifies. Key sizes beyond this will round up. // // To calculate, use the following Haskell code: // // import Text.Printf (printf) // import Data.List (intercalate) // // pow2 = 4095 // target = 2^pow2 // // f x = x*x - (toRational target) // // fprime x = 2*x // // newtonIteration x = x - (f x) / (fprime x) // // converge x = // let n = floor x in // if n*n - target < 0 && (n+1)*(n+1) - target > 0 // then n // else converge (newtonIteration x) // // divrem bits x = (x `div` (2^bits), x `rem` (2^bits)) // // bnWords :: Integer -> [Integer] // bnWords x = // if x == 0 // then [] // else let (high, low) = divrem 64 x in low : bnWords high // // showWord x = let (high, low) = divrem 32 x in printf "TOBN(0x%08x, 0x%08x)" high low // // output :: String // output = intercalate ", " $ map showWord $ bnWords $ converge (2 ^ (pow2 `div` 2)) // // To verify this number, check that n² < 2⁴⁰⁹⁵ < (n+1)², where n is value // represented here. Note the components are listed in little-endian order. Here // is some sample Python code to check: // // >>> TOBN = lambda a, b: a << 32 | b // >>> l = [ <paste the contents of kSqrtTwo> ] // >>> n = sum(a * 2**(64*i) for i, a in enumerate(l)) // >>> n**2 < 2**4095 < (n+1)**2 // True const BN_ULONG kBoringSSLRSASqrtTwo[] = …; const size_t kBoringSSLRSASqrtTwoLen = …; // generate_prime sets |out| to a prime with length |bits| such that |out|-1 is // relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to // |p|. |sqrt2| must be ⌊2^(bits-1)×√2⌋ (or a slightly overestimate for large // sizes), and |pow2_bits_100| must be 2^(bits-100). // // This function fails with probability around 2^-21. static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e, const BIGNUM *p, const BIGNUM *sqrt2, const BIGNUM *pow2_bits_100, BN_CTX *ctx, BN_GENCB *cb) { … } // rsa_generate_key_impl generates an RSA key using a generalized version of // FIPS 186-4 appendix B.3. |RSA_generate_key_fips| performs additional checks // for FIPS-compliant key generation. // // This function returns one on success and zero on failure. It has a failure // probability of about 2^-20. static int rsa_generate_key_impl(RSA *rsa, int bits, const BIGNUM *e_value, BN_GENCB *cb) { … } static void replace_bignum(BIGNUM **out, BIGNUM **in) { … } static void replace_bn_mont_ctx(BN_MONT_CTX **out, BN_MONT_CTX **in) { … } static int RSA_generate_key_ex_maybe_fips(RSA *rsa, int bits, const BIGNUM *e_value, BN_GENCB *cb, int check_fips) { … } int RSA_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e_value, BN_GENCB *cb) { … } int RSA_generate_key_fips(RSA *rsa, int bits, BN_GENCB *cb) { … } DEFINE_METHOD_FUNCTION(RSA_METHOD, RSA_default_method) { … }