Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
SUSE:SLE-12-SP2:GA
compat-openssl098.29129
openssl-CVE-2022-4304.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File openssl-CVE-2022-4304.patch of Package compat-openssl098.29129
commit 0ee66695c4bcb209919d3c7ee88ce46440b29f46 Author: Matt Caswell <matt@openssl.org> Date: Fri Jan 20 15:26:54 2023 +0000 Fix Timing Oracle in RSA decryption A timing based side channel exists in the OpenSSL RSA Decryption implementation which could be sufficient to recover a ciphertext across a network in a Bleichenbacher style attack. To achieve a successful decryption an attacker would have to be able to send a very large number of trial messages for decryption. The vulnerability affects all RSA padding modes: PKCS#1 v1.5, RSA-OEAP and RSASVE. Patch written by Dmitry Belyavsky and Hubert Kario CVE-2022-4304 --- crypto/bn/bn_blind.c | 16 - crypto/bn/bn_lcl.h | 15 + crypto/constant_time_locl.h | 24 + crypto/err/err.h | 1 crypto/err/err_str.c | 1 crypto/rsa/Makefile | 6 crypto/rsa/rsa.h | 1 crypto/rsa/rsa_eay.c | 16 - crypto/rsa/rsa_err.c | 1 crypto/rsa/rsa_sup_mul.c | 616 ++++++++++++++++++++++++++++++++++++++++++++ crypto/rsa/rsa_sup_mul.h | 6 11 files changed, 682 insertions(+), 21 deletions(-) --- a/crypto/bn/bn_blind.c +++ b/crypto/bn/bn_blind.c @@ -115,22 +115,6 @@ #define BN_BLINDING_COUNTER 32 -struct bn_blinding_st - { - BIGNUM *A; - BIGNUM *Ai; - BIGNUM *e; - BIGNUM *mod; /* just a reference */ - unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b; - * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */ - unsigned int counter; - unsigned long flags; - BN_MONT_CTX *m_ctx; - int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, - const BIGNUM *m, BN_CTX *ctx, - BN_MONT_CTX *m_ctx); - }; - BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, /* const */ BIGNUM *mod) { BN_BLINDING *ret=NULL; --- a/crypto/bn/bn_lcl.h +++ b/crypto/bn/bn_lcl.h @@ -118,6 +118,21 @@ extern "C" { #endif +struct bn_blinding_st + { + BIGNUM *A; + BIGNUM *Ai; + BIGNUM *e; + BIGNUM *mod; /* just a reference */ + unsigned long thread_id; /* added in OpenSSL 0.9.6j and 0.9.7b; + * used only by crypto/rsa/rsa_eay.c, rsa_lib.c */ + unsigned int counter; + unsigned long flags; + BN_MONT_CTX *m_ctx; + int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, + const BIGNUM *m, BN_CTX *ctx, + BN_MONT_CTX *m_ctx); + }; /* * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions --- a/crypto/constant_time_locl.h +++ b/crypto/constant_time_locl.h @@ -49,6 +49,12 @@ # include "e_os.h" /* For 'inline' */ +# ifndef OPENSSL_SYS_VMS +# include <stdint.h> +# else +# include <inttypes.h> +# endif + #ifdef __cplusplus extern "C" { #endif @@ -127,6 +133,12 @@ static inline unsigned int constant_time static inline unsigned char constant_time_select_8(unsigned char mask, unsigned char a, unsigned char b); +/* Convenience method for uint32_t. */ +static inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a, + uint32_t b); +/* Convenience method for uint64_t. */ +static inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a, + uint64_t b); /* Convenience method for signed integers. */ static inline int constant_time_select_int(unsigned int mask, int a, int b); @@ -199,11 +211,23 @@ static inline unsigned char constant_tim return (unsigned char)(constant_time_select(mask, a, b)); } +static inline uint32_t constant_time_select_32(uint32_t mask, uint32_t a, + uint32_t b) +{ + return (mask & a) | (~mask & b); +} + static inline int constant_time_select_int(unsigned int mask, int a, int b) { return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b))); } +static inline uint64_t constant_time_select_64(uint64_t mask, uint64_t a, + uint64_t b) +{ + return (mask & a) | (~mask & b); +} + /* * Expected usage pattern is to unconditionally set error and then * wipe it if there was no actual error. |clear| is 1 or 0. --- a/crypto/err/err.h +++ b/crypto/err/err.h @@ -247,6 +247,7 @@ typedef struct err_state_st #define ERR_R_PASSED_NULL_PARAMETER (3|ERR_R_FATAL) #define ERR_R_INTERNAL_ERROR (4|ERR_R_FATAL) #define ERR_R_DISABLED (5|ERR_R_FATAL) +#define ERR_R_PASSED_INVALID_ARGUMENT (7) /* 99 is the maximum possible ERR_R_... code, higher values * are reserved for the individual libraries */ --- a/crypto/err/err_str.c +++ b/crypto/err/err_str.c @@ -209,6 +209,7 @@ static ERR_STRING_DATA ERR_str_reasons[] {ERR_R_PASSED_NULL_PARAMETER ,"passed a null parameter"}, {ERR_R_INTERNAL_ERROR ,"internal error"}, {ERR_R_DISABLED ,"called a function that was disabled at compile-time"}, +{ERR_R_PASSED_INVALID_ARGUMENT ,"passed invalid argument"}, {0,NULL}, }; --- a/crypto/rsa/Makefile +++ b/crypto/rsa/Makefile @@ -19,10 +19,12 @@ APPS= LIB=$(TOP)/libcrypto.a LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \ rsa_pk1.c rsa_ssl.c rsa_none.c rsa_oaep.c rsa_chk.c rsa_null.c \ - rsa_pss.c rsa_x931.c rsa_x931g.c rsa_asn1.c rsa_depr.c rsa_eng.c + rsa_pss.c rsa_x931.c rsa_x931g.c rsa_asn1.c rsa_depr.c rsa_eng.c \ + rsa_sup_mul.c LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o rsa_err.o \ rsa_pk1.o rsa_ssl.o rsa_none.o rsa_oaep.o rsa_chk.o rsa_null.o \ - rsa_pss.o rsa_x931.o rsa_x931g.o rsa_asn1.o rsa_depr.o rsa_eng.o + rsa_pss.o rsa_x931.o rsa_x931g.o rsa_asn1.o rsa_depr.o rsa_eng.o \ + rsa_sup_mul.o SRC= $(LIBSRC) --- a/crypto/rsa/rsa.h +++ b/crypto/rsa/rsa.h @@ -429,6 +429,7 @@ void ERR_load_RSA_strings(void); /* Error codes for the RSA functions. */ /* Function codes. */ +#define RSA_F_DO_UNBLIND 168 #define RSA_F_FIPS_RSA_SIGN 140 #define RSA_F_FIPS_RSA_VERIFY 141 #define RSA_F_MEMORY_LOCK 100 --- a/crypto/rsa/rsa_eay.c +++ b/crypto/rsa/rsa_eay.c @@ -114,6 +114,7 @@ #include <openssl/bn.h> #include <openssl/rsa.h> #include <openssl/rand.h> +#include "rsa_sup_mul.h" #include "constant_time_locl.h" #if !defined(RSA_NULL) && !defined(OPENSSL_FIPS) @@ -555,11 +556,20 @@ static int RSA_eay_private_decrypt(int f goto err; } - if (blinding) - if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx)) + if (blinding) { + /* + * do_unblind combines blinding inversion and + * 0-padded BN BE serialization + */ + j = do_unblind(ret, blinding, unblind, rsa->n, ctx, buf, num); + if (j == 0) goto err; - j = bn_bn2binpad(ret, buf, num); + } else { + j = bn_bn2binpad(ret, buf, num); + if (j < 0) + goto err; + } switch (padding) { --- a/crypto/rsa/rsa_err.c +++ b/crypto/rsa/rsa_err.c @@ -70,6 +70,7 @@ static ERR_STRING_DATA RSA_str_functs[]= { +{ERR_FUNC(RSA_F_DO_UNBLIND), "do_unblind"}, {ERR_FUNC(RSA_F_FIPS_RSA_SIGN), "FIPS_RSA_SIGN"}, {ERR_FUNC(RSA_F_FIPS_RSA_VERIFY), "FIPS_RSA_VERIFY"}, {ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"}, --- /dev/null +++ b/crypto/rsa/rsa_sup_mul.c @@ -0,0 +1,616 @@ +#include <openssl/e_os2.h> +#include <stddef.h> +#include <sys/types.h> +#include <string.h> +#include "openssl/bn.h" +#include "rsa_sup_mul.h" +#include "crypto/bn/bn_lcl.h" +#include "crypto/constant_time_locl.h" +#include <openssl/err.h> +#include <openssl/crypto.h> +#include <openssl/rsa.h> + +# if BN_BYTES == 8 +typedef uint64_t limb_t; +# if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16 +/* nonstandard; implemented by gcc on 64-bit platforms */ +typedef __uint128_t limb2_t; +# define HAVE_LIMB2_T +# endif +# define LIMB_BIT_SIZE 64 +# define LIMB_BYTE_SIZE 8 +# elif BN_BYTES == 4 +typedef uint32_t limb_t; +typedef uint64_t limb2_t; +# define LIMB_BIT_SIZE 32 +# define LIMB_BYTE_SIZE 4 +# define HAVE_LIMB2_T +# else +# error "Not supported" +# endif + +/* + * For multiplication we're using schoolbook multiplication, + * so if we have two numbers, each with 6 "digits" (words) + * the multiplication is calculated as follows: + * A B C D E F + * x I J K L M N + * -------------- + * N*F + * N*E + * N*D + * N*C + * N*B + * N*A + * M*F + * M*E + * M*D + * M*C + * M*B + * M*A + * L*F + * L*E + * L*D + * L*C + * L*B + * L*A + * K*F + * K*E + * K*D + * K*C + * K*B + * K*A + * J*F + * J*E + * J*D + * J*C + * J*B + * J*A + * I*F + * I*E + * I*D + * I*C + * I*B + * + I*A + * ========================== + * N*B N*D N*F + * + N*A N*C N*E + * + M*B M*D M*F + * + M*A M*C M*E + * + L*B L*D L*F + * + L*A L*C L*E + * + K*B K*D K*F + * + K*A K*C K*E + * + J*B J*D J*F + * + J*A J*C J*E + * + I*B I*D I*F + * + I*A I*C I*E + * + * 1+1 1+3 1+5 + * 1+0 1+2 1+4 + * 0+1 0+3 0+5 + * 0+0 0+2 0+4 + * + * 0 1 2 3 4 5 6 + * which requires n^2 multiplications and 2n full length additions + * as we can keep every other result of limb multiplication in two separate + * limbs + */ + +#if defined HAVE_LIMB2_T +static inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b) +{ + limb2_t t; + /* + * this is idiomatic code to tell compiler to use the native mul + * those three lines will actually compile to single instruction + */ + + t = (limb2_t)a * b; + *hi = t >> LIMB_BIT_SIZE; + *lo = t & -1UL; +} +#elif (BN_BYTES == 8) && (defined _MSC_VER) +/* https://learn.microsoft.com/en-us/cpp/intrinsics/mul128?view=msvc-170 */ +#pragma intrinsic(_mul128) +static inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b) +{ + *lo = _mul128(a, b, hi); +} +#else +/* + * if the compiler doesn't have either a 128bit data type nor a "return + * high 64 bits of multiplication" + */ +static inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b) +{ + limb_t a_low = (limb_t)(uint32_t)a; + limb_t a_hi = a >> 32; + limb_t b_low = (limb_t)(uint32_t)b; + limb_t b_hi = b >> 32; + + limb_t p0 = a_low * b_low; + limb_t p1 = a_low * b_hi; + limb_t p2 = a_hi * b_low; + limb_t p3 = a_hi * b_hi; + + uint32_t cy = (uint32_t)(((p0 >> 32) + (uint32_t)p1 + (uint32_t)p2) >> 32); + + *lo = p0 + (p1 << 32) + (p2 << 32); + *hi = p3 + (p1 >> 32) + (p2 >> 32) + cy; +} +#endif + +/* add two limbs with carry in, return carry out */ +static inline limb_t _add_limb(limb_t *ret, limb_t a, limb_t b, limb_t carry) +{ + limb_t carry1, carry2, t; + /* + * `c = a + b; if (c < a)` is idiomatic code that makes compilers + * use add with carry on assembly level + */ + + *ret = a + carry; + if (*ret < a) + carry1 = 1; + else + carry1 = 0; + + t = *ret; + *ret = t + b; + if (*ret < t) + carry2 = 1; + else + carry2 = 0; + + return carry1 + carry2; +} + +/* + * add two numbers of the same size, return overflow + * + * add a to b, place result in ret; all arrays need to be n limbs long + * return overflow from addition (0 or 1) + */ +static inline limb_t add(limb_t *ret, limb_t *a, limb_t *b, size_t n) +{ + limb_t c = 0; + ssize_t i; + + for(i = n - 1; i > -1; i--) + c = _add_limb(&ret[i], a[i], b[i], c); + + return c; +} + +/* + * return number of limbs necessary for temporary values + * when multiplying numbers n limbs large + */ +static inline size_t mul_limb_numb(size_t n) +{ + return 2 * n * 2; +} + +/* + * multiply two numbers of the same size + * + * multiply a by b, place result in ret; a and b need to be n limbs long + * ret needs to be 2*n limbs long, tmp needs to be mul_limb_numb(n) limbs + * long + */ +static void limb_mul(limb_t *ret, limb_t *a, limb_t *b, size_t n, limb_t *tmp) +{ + limb_t *r_odd, *r_even; + size_t i, j, k; + + r_odd = tmp; + r_even = &tmp[2 * n]; + + memset(ret, 0, 2 * n * sizeof(limb_t)); + + for (i = 0; i < n; i++) { + for (k = 0; k < i + n + 1; k++) { + r_even[k] = 0; + r_odd[k] = 0; + } + for (j = 0; j < n; j++) { + /* + * place results from even and odd limbs in separate arrays so that + * we don't have to calculate overflow every time we get individual + * limb multiplication result + */ + if (j % 2 == 0) + _mul_limb(&r_even[i + j], &r_even[i + j + 1], a[i], b[j]); + else + _mul_limb(&r_odd[i + j], &r_odd[i + j + 1], a[i], b[j]); + } + /* + * skip the least significant limbs when adding multiples of + * more significant limbs (they're zero anyway) + */ + add(ret, ret, r_even, n + i + 1); + add(ret, ret, r_odd, n + i + 1); + } +} + +/* modifies the value in place by performing a right shift by one bit */ +static inline void rshift1(limb_t *val, size_t n) +{ + limb_t shift_in = 0, shift_out = 0; + size_t i; + + for (i = 0; i < n; i++) { + shift_out = val[i] & 1; + val[i] = shift_in << (LIMB_BIT_SIZE - 1) | (val[i] >> 1); + shift_in = shift_out; + } +} + +/* extend the LSB of flag to all bits of limb */ +static inline limb_t mk_mask(limb_t flag) +{ + flag |= flag << 1; + flag |= flag << 2; + flag |= flag << 4; + flag |= flag << 8; + flag |= flag << 16; +#if (LIMB_BYTE_SIZE == 8) + flag |= flag << 32; +#endif + return flag; +} + +/* + * copy from either a or b to ret based on flag + * when flag == 0, then copies from b + * when flag == 1, then copies from a + */ +static inline void cselect(limb_t flag, limb_t *ret, limb_t *a, limb_t *b, size_t n) +{ + /* + * would be more efficient with non volatile mask, but then gcc + * generates code with jumps + */ + volatile limb_t mask; + size_t i; + + mask = mk_mask(flag); + for (i = 0; i < n; i++) { +#if (LIMB_BYTE_SIZE == 8) + ret[i] = constant_time_select_64(mask, a[i], b[i]); +#else + ret[i] = constant_time_select_32(mask, a[i], b[i]); +#endif + } +} + +static limb_t _sub_limb(limb_t *ret, limb_t a, limb_t b, limb_t borrow) +{ + limb_t borrow1, borrow2, t; + /* + * while it doesn't look constant-time, this is idiomatic code + * to tell compilers to use the carry bit from subtraction + */ + + *ret = a - borrow; + if (*ret > a) + borrow1 = 1; + else + borrow1 = 0; + + t = *ret; + *ret = t - b; + if (*ret > t) + borrow2 = 1; + else + borrow2 = 0; + + return borrow1 + borrow2; +} + +/* + * place the result of a - b into ret, return the borrow bit. + * All arrays need to be n limbs long + */ +static limb_t sub(limb_t *ret, limb_t *a, limb_t *b, size_t n) +{ + limb_t borrow = 0; + ssize_t i; + + for (i = n - 1; i > -1; i--) + borrow = _sub_limb(&ret[i], a[i], b[i], borrow); + + return borrow; +} + +/* return the number of limbs necessary to allocate for the mod() tmp operand */ +static inline size_t mod_limb_numb(size_t anum, size_t modnum) +{ + return (anum + modnum) * 3; +} + +/* + * calculate a % mod, place the result in ret + * size of a is defined by anum, size of ret and mod is modnum, + * size of tmp is returned by mod_limb_numb() + */ +static void mod(limb_t *ret, limb_t *a, size_t anum, limb_t *mod, + size_t modnum, limb_t *tmp) +{ + limb_t *atmp, *modtmp, *rettmp; + limb_t res; + size_t i; + + memset(tmp, 0, mod_limb_numb(anum, modnum) * LIMB_BYTE_SIZE); + + atmp = tmp; + modtmp = &tmp[anum + modnum]; + rettmp = &tmp[(anum + modnum) * 2]; + + for (i = modnum; i <modnum + anum; i++) + atmp[i] = a[i-modnum]; + + for (i = 0; i < modnum; i++) + modtmp[i] = mod[i]; + + for (i = 0; i < anum * LIMB_BIT_SIZE; i++) { + rshift1(modtmp, anum + modnum); + res = sub(rettmp, atmp, modtmp, anum+modnum); + cselect(res, atmp, atmp, rettmp, anum+modnum); + } + + memcpy(ret, &atmp[anum], sizeof(limb_t) * modnum); +} + +/* necessary size of tmp for a _mul_add_limb() call with provided anum */ +static inline size_t _mul_add_limb_numb(size_t anum) +{ + return 2 * (anum + 1); +} + +/* multiply a by m, add to ret, return carry */ +static limb_t _mul_add_limb(limb_t *ret, limb_t *a, size_t anum, + limb_t m, limb_t *tmp) +{ + limb_t carry = 0; + limb_t *r_odd, *r_even; + size_t i; + + memset(tmp, 0, sizeof(limb_t) * (anum + 1) * 2); + + r_odd = tmp; + r_even = &tmp[anum + 1]; + + for (i = 0; i < anum; i++) { + /* + * place the results from even and odd limbs in separate arrays + * so that we have to worry about carry just once + */ + if (i % 2 == 0) + _mul_limb(&r_even[i], &r_even[i + 1], a[i], m); + else + _mul_limb(&r_odd[i], &r_odd[i + 1], a[i], m); + } + /* assert: add() carry here will be equal zero */ + add(r_even, r_even, r_odd, anum + 1); + /* + * while here it will not overflow as the max value from multiplication + * is -2 while max overflow from addition is 1, so the max value of + * carry is -1 (i.e. max int) + */ + carry = add(ret, ret, &r_even[1], anum) + r_even[0]; + + return carry; +} + +static inline size_t mod_montgomery_limb_numb(size_t modnum) +{ + return modnum * 2 + _mul_add_limb_numb(modnum); +} + +/* + * calculate a % mod, place result in ret + * assumes that a is in mongomery form with the R (Mongomery modulus) being + * smallest power of two big enough to fit mod and that's also a power + * of the count of number of bits in limb_t (B). + * For calculation, we also need n', such that mod * n' == -1 mod B. + * anum must be <= 2 * modnum + * ret needs to be modnum words long + * tmp needs to be mod_montgomery_limb_numb(modnum) limbs long + */ +static void mod_montgomery(limb_t *ret, limb_t *a, size_t anum, limb_t *mod, + size_t modnum, limb_t ni0, limb_t *tmp) +{ + limb_t carry, v; + limb_t *res, *rp, *tmp2; + ssize_t i; + + res = tmp; + /* + * for intermediate result we need an integer twice as long as modulus + * but keep the input in the least significant limbs + */ + memset(res, 0, sizeof(limb_t) * (modnum * 2)); + memcpy(&res[modnum * 2 - anum], a, sizeof(limb_t) * anum); + rp = &res[modnum]; + tmp2 = &res[modnum * 2]; + + carry = 0; + + /* add multiples of the modulus to the value until R divides it cleanly */ + for (i = modnum; i > 0; i--, rp--) { + v = _mul_add_limb(rp, mod, modnum, (rp[modnum-1] * ni0) & -1UL, tmp2); + v = (v + carry + rp[-1]) & -1UL; + carry |= (v != rp[-1]); + carry &= (v <= rp[-1]); + rp[-1] = v; + } + + /* perform the final reduction by mod... */ + carry -= sub(ret, rp, mod, modnum); + + /* ...conditionally */ + cselect(carry, ret, rp, ret, modnum); +} + +/* allocated buffer should be freed afterwards */ +static void BN_to_limb(const BIGNUM *bn, limb_t *buf, size_t limbs) +{ + int i; + int real_limbs = (BN_num_bytes(bn) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE; + limb_t *ptr = buf + (limbs - real_limbs); + + for (i = 0; i < real_limbs; i++) + ptr[i] = bn->d[real_limbs - i - 1]; +} + +#if LIMB_BYTE_SIZE == 8 +static inline uint64_t be64(uint64_t host) +{ + const union { + long one; + char little; + } is_endian = { 1 }; + + if (is_endian.little) { + uint64_t big = 0; + + big |= (host & 0xff00000000000000) >> 56; + big |= (host & 0x00ff000000000000) >> 40; + big |= (host & 0x0000ff0000000000) >> 24; + big |= (host & 0x000000ff00000000) >> 8; + big |= (host & 0x00000000ff000000) << 8; + big |= (host & 0x0000000000ff0000) << 24; + big |= (host & 0x000000000000ff00) << 40; + big |= (host & 0x00000000000000ff) << 56; + return big; + } else { + return host; + } +} + +#else +/* Not all platforms have htobe32(). */ +static inline uint32_t be32(uint32_t host) +{ + const union { + long one; + char little; + } is_endian = { 1 }; + + if (is_endian.little) { + uint32_t big = 0; + + big |= (host & 0xff000000) >> 24; + big |= (host & 0x00ff0000) >> 8; + big |= (host & 0x0000ff00) << 8; + big |= (host & 0x000000ff) << 24; + return big; + } else { + return host; + } +} +#endif + +/* + * We assume that intermediate, possible_arg2, blinding, and ctx are used + * similar to BN_BLINDING_invert_ex() arguments. + * to_mod is RSA module + * buf and num is the serialization buffer and its length. + * + * Here we use classic/Mongomery multiplication and modulo. After the calculation finished + * we serialize the new structure instead of BIGNUMs taking endianness into account. + */ +int do_unblind(const BIGNUM *intermediate, const BN_BLINDING *blinding, const BIGNUM *possible_arg2, + const BIGNUM *to_mod, BN_CTX *ctx, unsigned char *buf, int num) +{ + limb_t *l_im = NULL, *l_mul = NULL, *l_mod = NULL; + limb_t *l_ret = NULL, *l_tmp = NULL, l_buf; + size_t l_im_count = 0, l_mul_count = 0, l_size = 0, l_mod_count = 0; + size_t l_tmp_count = 0; + int ret = 0; + size_t i; + unsigned char *tmp; + const BIGNUM *arg1 = intermediate; + const BIGNUM *arg2 = (possible_arg2 == NULL) ? blinding->Ai : possible_arg2; + + l_im_count = (BN_num_bytes(arg1) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE; + l_mul_count = (BN_num_bytes(arg2) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE; + l_mod_count = (BN_num_bytes(to_mod) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE; + + l_size = l_im_count > l_mul_count ? l_im_count : l_mul_count; + l_im = OPENSSL_malloc(l_size * LIMB_BYTE_SIZE); + l_mul = OPENSSL_malloc(l_size * LIMB_BYTE_SIZE); + l_mod = OPENSSL_malloc(l_mod_count * LIMB_BYTE_SIZE); + + if ((l_im == NULL) || (l_mul == NULL) || (l_mod == NULL)) + goto err; + + memset(l_im, 0, l_size * LIMB_BYTE_SIZE); + memset(l_mul, 0, l_size * LIMB_BYTE_SIZE); + memset(l_mod, 0, l_mod_count * LIMB_BYTE_SIZE); + + BN_to_limb(arg1, l_im, l_size); + BN_to_limb(arg2, l_mul, l_size); + BN_to_limb(to_mod, l_mod, l_mod_count); + + l_ret = OPENSSL_malloc(2 * l_size * LIMB_BYTE_SIZE); + + if (blinding->m_ctx != NULL) { + l_tmp_count = mul_limb_numb(l_size) > mod_montgomery_limb_numb(l_mod_count) ? + mul_limb_numb(l_size) : mod_montgomery_limb_numb(l_mod_count); + l_tmp = OPENSSL_malloc(l_tmp_count * LIMB_BYTE_SIZE); + } else { + l_tmp_count = mul_limb_numb(l_size) > mod_limb_numb(2 * l_size, l_mod_count) ? + mul_limb_numb(l_size) : mod_limb_numb(2 * l_size, l_mod_count); + l_tmp = OPENSSL_malloc(l_tmp_count * LIMB_BYTE_SIZE); + } + + if ((l_ret == NULL) || (l_tmp == NULL)) + goto err; + + if (blinding->m_ctx != NULL) { + limb_mul(l_ret, l_im, l_mul, l_size, l_tmp); + mod_montgomery(l_ret, l_ret, 2 * l_size, l_mod, l_mod_count, + blinding->m_ctx->n0, l_tmp); + } else { + limb_mul(l_ret, l_im, l_mul, l_size, l_tmp); + mod(l_ret, l_ret, 2 * l_size, l_mod, l_mod_count, l_tmp); + } + + /* module size in bytes can be equal to num but after limbs conversion it becomes bigger */ + if (num < BN_num_bytes(to_mod)) { + RSAerr(RSA_F_DO_UNBLIND, ERR_R_PASSED_INVALID_ARGUMENT); + goto err; + } + + memset(buf, 0, num); + tmp = buf + num - BN_num_bytes(to_mod); + for (i = 0; i < l_mod_count; i++) { +#if LIMB_BYTE_SIZE == 8 + l_buf = be64(l_ret[i]); +#else + l_buf = be32(l_ret[i]); +#endif + if (i == 0) { + int delta = LIMB_BYTE_SIZE - ((l_mod_count * LIMB_BYTE_SIZE) - num); + + memcpy(tmp, ((char *)&l_buf) + LIMB_BYTE_SIZE - delta, delta); + tmp += delta; + } else { + memcpy(tmp, &l_buf, LIMB_BYTE_SIZE); + tmp += LIMB_BYTE_SIZE; + } + } + ret = num; + + err: + OPENSSL_free(l_im); + OPENSSL_free(l_mul); + OPENSSL_free(l_mod); + OPENSSL_free(l_tmp); + OPENSSL_free(l_ret); + + return ret; +} --- /dev/null +++ b/crypto/rsa/rsa_sup_mul.h @@ -0,0 +1,6 @@ +#ifndef OSSL_CRYPTO_RSA_SUP_MUL_H +#define OSSL_CRYPTO_RSA_SUP_MUL_H + +int do_unblind(const BIGNUM *intermediate, const BN_BLINDING *blinding, const BIGNUM *possible_arg2, + const BIGNUM *to_mod, BN_CTX *ctx, unsigned char *buf, int num); +#endif /* OSSL_CRYPTO_RSA_SUP_MUL_H */
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor