Blame SOURCES/0102-CVE-2022-4304-RSA-time-oracle.patch

1ac26c
From 8e257b86e5812c6e1cfa9e8e5f5660ac7bed899d Mon Sep 17 00:00:00 2001
1ac26c
From: Dmitry Belyavskiy <beldmit@gmail.com>
1ac26c
Date: Fri, 20 Jan 2023 15:03:40 +0000
1ac26c
Subject: [PATCH 03/18] Fix Timing Oracle in RSA decryption
1ac26c
1ac26c
A timing based side channel exists in the OpenSSL RSA Decryption
1ac26c
implementation which could be sufficient to recover a plaintext across
1ac26c
a network in a Bleichenbacher style attack. To achieve a successful
1ac26c
decryption an attacker would have to be able to send a very large number
1ac26c
of trial messages for decryption. The vulnerability affects all RSA
1ac26c
padding modes: PKCS#1 v1.5, RSA-OEAP and RSASVE.
1ac26c
1ac26c
Patch written by Dmitry Belyavsky and Hubert Kario
1ac26c
1ac26c
CVE-2022-4304
1ac26c
1ac26c
Reviewed-by: Matt Caswell <matt@openssl.org>
1ac26c
Reviewed-by: Tomas Mraz <tomas@openssl.org>
1ac26c
---
1ac26c
 crypto/bn/bn_blind.c    |  14 -
1ac26c
 crypto/bn/bn_local.h    |  14 +
1ac26c
 crypto/bn/build.info    |   2 +-
1ac26c
 crypto/bn/rsa_sup_mul.c | 604 ++++++++++++++++++++++++++++++++++++++++
1ac26c
 crypto/rsa/rsa_ossl.c   |  19 +-
1ac26c
 include/crypto/bn.h     |   6 +
1ac26c
 6 files changed, 638 insertions(+), 21 deletions(-)
1ac26c
 create mode 100644 crypto/bn/rsa_sup_mul.c
1ac26c
1ac26c
diff --git a/crypto/bn/bn_blind.c b/crypto/bn/bn_blind.c
1ac26c
index 72457b34cf..6061ebb4c0 100644
1ac26c
--- a/crypto/bn/bn_blind.c
1ac26c
+++ b/crypto/bn/bn_blind.c
1ac26c
@@ -13,20 +13,6 @@
1ac26c
 
1ac26c
 #define BN_BLINDING_COUNTER     32
1ac26c
 
1ac26c
-struct bn_blinding_st {
1ac26c
-    BIGNUM *A;
1ac26c
-    BIGNUM *Ai;
1ac26c
-    BIGNUM *e;
1ac26c
-    BIGNUM *mod;                /* just a reference */
1ac26c
-    CRYPTO_THREAD_ID tid;
1ac26c
-    int counter;
1ac26c
-    unsigned long flags;
1ac26c
-    BN_MONT_CTX *m_ctx;
1ac26c
-    int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1ac26c
-                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
1ac26c
-    CRYPTO_RWLOCK *lock;
1ac26c
-};
1ac26c
-
1ac26c
 BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
1ac26c
 {
1ac26c
     BN_BLINDING *ret = NULL;
1ac26c
diff --git a/crypto/bn/bn_local.h b/crypto/bn/bn_local.h
1ac26c
index c9a7ecf298..8c428f919d 100644
1ac26c
--- a/crypto/bn/bn_local.h
1ac26c
+++ b/crypto/bn/bn_local.h
1ac26c
@@ -290,6 +290,20 @@ struct bn_gencb_st {
1ac26c
     } cb;
1ac26c
 };
1ac26c
 
1ac26c
+struct bn_blinding_st {
1ac26c
+    BIGNUM *A;
1ac26c
+    BIGNUM *Ai;
1ac26c
+    BIGNUM *e;
1ac26c
+    BIGNUM *mod;                /* just a reference */
1ac26c
+    CRYPTO_THREAD_ID tid;
1ac26c
+    int counter;
1ac26c
+    unsigned long flags;
1ac26c
+    BN_MONT_CTX *m_ctx;
1ac26c
+    int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1ac26c
+                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
1ac26c
+    CRYPTO_RWLOCK *lock;
1ac26c
+};
1ac26c
+
1ac26c
 /*-
1ac26c
  * BN_window_bits_for_exponent_size -- macro for sliding window mod_exp functions
1ac26c
  *
1ac26c
diff --git a/crypto/bn/build.info b/crypto/bn/build.info
1ac26c
index c4ba51b265..f4ff619239 100644
1ac26c
--- a/crypto/bn/build.info
1ac26c
+++ b/crypto/bn/build.info
1ac26c
@@ -105,7 +105,7 @@ $COMMON=bn_add.c bn_div.c bn_exp.c bn_lib.c bn_ctx.c bn_mul.c \
1ac26c
         bn_mod.c bn_conv.c bn_rand.c bn_shift.c bn_word.c bn_blind.c \
1ac26c
         bn_kron.c bn_sqrt.c bn_gcd.c bn_prime.c bn_sqr.c \
1ac26c
         bn_recp.c bn_mont.c bn_mpi.c bn_exp2.c bn_gf2m.c bn_nist.c \
1ac26c
-        bn_intern.c bn_dh.c bn_rsa_fips186_4.c bn_const.c
1ac26c
+        bn_intern.c bn_dh.c bn_rsa_fips186_4.c bn_const.c rsa_sup_mul.c
1ac26c
 SOURCE[../../libcrypto]=$COMMON $BNASM bn_print.c bn_err.c bn_srp.c
1ac26c
 DEFINE[../../libcrypto]=$BNDEF
1ac26c
 IF[{- !$disabled{'deprecated-0.9.8'} -}]
1ac26c
diff --git a/crypto/bn/rsa_sup_mul.c b/crypto/bn/rsa_sup_mul.c
1ac26c
new file mode 100644
1ac26c
index 0000000000..0e0d02e194
1ac26c
--- /dev/null
1ac26c
+++ b/crypto/bn/rsa_sup_mul.c
1ac26c
@@ -0,0 +1,604 @@
1ac26c
+#include <openssl/e_os2.h>
1ac26c
+#include <stddef.h>
1ac26c
+#include <sys/types.h>
1ac26c
+#include <string.h>
1ac26c
+#include <openssl/bn.h>
1ac26c
+#include <openssl/err.h>
1ac26c
+#include <openssl/rsaerr.h>
1ac26c
+#include "internal/endian.h"
1ac26c
+#include "internal/numbers.h"
1ac26c
+#include "internal/constant_time.h"
1ac26c
+#include "bn_local.h"
1ac26c
+
1ac26c
+# if BN_BYTES == 8
1ac26c
+typedef uint64_t limb_t;
1ac26c
+#  if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16
1ac26c
+typedef uint128_t limb2_t;
1ac26c
+#   define HAVE_LIMB2_T
1ac26c
+#  endif
1ac26c
+#  define LIMB_BIT_SIZE 64
1ac26c
+#  define LIMB_BYTE_SIZE 8
1ac26c
+# elif BN_BYTES == 4
1ac26c
+typedef uint32_t limb_t;
1ac26c
+typedef uint64_t limb2_t;
1ac26c
+#  define LIMB_BIT_SIZE 32
1ac26c
+#  define LIMB_BYTE_SIZE 4
1ac26c
+#  define HAVE_LIMB2_T
1ac26c
+# else
1ac26c
+#  error "Not supported"
1ac26c
+# endif
1ac26c
+
1ac26c
+/*
1ac26c
+ * For multiplication we're using schoolbook multiplication,
1ac26c
+ * so if we have two numbers, each with 6 "digits" (words)
1ac26c
+ * the multiplication is calculated as follows:
1ac26c
+ *                        A B C D E F
1ac26c
+ *                     x  I J K L M N
1ac26c
+ *                     --------------
1ac26c
+ *                                N*F
1ac26c
+ *                              N*E
1ac26c
+ *                            N*D
1ac26c
+ *                          N*C
1ac26c
+ *                        N*B
1ac26c
+ *                      N*A
1ac26c
+ *                              M*F
1ac26c
+ *                            M*E
1ac26c
+ *                          M*D
1ac26c
+ *                        M*C
1ac26c
+ *                      M*B
1ac26c
+ *                    M*A
1ac26c
+ *                            L*F
1ac26c
+ *                          L*E
1ac26c
+ *                        L*D
1ac26c
+ *                      L*C
1ac26c
+ *                    L*B
1ac26c
+ *                  L*A
1ac26c
+ *                          K*F
1ac26c
+ *                        K*E
1ac26c
+ *                      K*D
1ac26c
+ *                    K*C
1ac26c
+ *                  K*B
1ac26c
+ *                K*A
1ac26c
+ *                        J*F
1ac26c
+ *                      J*E
1ac26c
+ *                    J*D
1ac26c
+ *                  J*C
1ac26c
+ *                J*B
1ac26c
+ *              J*A
1ac26c
+ *                      I*F
1ac26c
+ *                    I*E
1ac26c
+ *                  I*D
1ac26c
+ *                I*C
1ac26c
+ *              I*B
1ac26c
+ *         +  I*A
1ac26c
+ *         ==========================
1ac26c
+ *                        N*B N*D N*F
1ac26c
+ *                    + N*A N*C N*E
1ac26c
+ *                    + M*B M*D M*F
1ac26c
+ *                  + M*A M*C M*E
1ac26c
+ *                  + L*B L*D L*F
1ac26c
+ *                + L*A L*C L*E
1ac26c
+ *                + K*B K*D K*F
1ac26c
+ *              + K*A K*C K*E
1ac26c
+ *              + J*B J*D J*F
1ac26c
+ *            + J*A J*C J*E
1ac26c
+ *            + I*B I*D I*F
1ac26c
+ *          + I*A I*C I*E
1ac26c
+ *
1ac26c
+ *                1+1 1+3 1+5
1ac26c
+ *              1+0 1+2 1+4
1ac26c
+ *              0+1 0+3 0+5
1ac26c
+ *            0+0 0+2 0+4
1ac26c
+ *
1ac26c
+ *            0 1 2 3 4 5 6
1ac26c
+ * which requires n^2 multiplications and 2n full length additions
1ac26c
+ * as we can keep every other result of limb multiplication in two separate
1ac26c
+ * limbs
1ac26c
+ */
1ac26c
+
1ac26c
+#if defined HAVE_LIMB2_T
1ac26c
+static ossl_inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b)
1ac26c
+{
1ac26c
+    limb2_t t;
1ac26c
+    /*
1ac26c
+     * this is idiomatic code to tell compiler to use the native mul
1ac26c
+     * those three lines will actually compile to single instruction
1ac26c
+     */
1ac26c
+
1ac26c
+    t = (limb2_t)a * b;
1ac26c
+    *hi = t >> LIMB_BIT_SIZE;
1ac26c
+    *lo = (limb_t)t;
1ac26c
+}
1ac26c
+#elif (BN_BYTES == 8) && (defined _MSC_VER)
1ac26c
+/* https://learn.microsoft.com/en-us/cpp/intrinsics/umul128?view=msvc-170 */
1ac26c
+#pragma intrinsic(_umul128)
1ac26c
+static ossl_inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b)
1ac26c
+{
1ac26c
+    *lo = _umul128(a, b, hi);
1ac26c
+}
1ac26c
+#else
1ac26c
+/*
1ac26c
+ * if the compiler doesn't have either a 128bit data type nor a "return
1ac26c
+ * high 64 bits of multiplication"
1ac26c
+ */
1ac26c
+static ossl_inline void _mul_limb(limb_t *hi, limb_t *lo, limb_t a, limb_t b)
1ac26c
+{
1ac26c
+    limb_t a_low = (limb_t)(uint32_t)a;
1ac26c
+    limb_t a_hi = a >> 32;
1ac26c
+    limb_t b_low = (limb_t)(uint32_t)b;
1ac26c
+    limb_t b_hi = b >> 32;
1ac26c
+
1ac26c
+    limb_t p0 = a_low * b_low;
1ac26c
+    limb_t p1 = a_low * b_hi;
1ac26c
+    limb_t p2 = a_hi * b_low;
1ac26c
+    limb_t p3 = a_hi * b_hi;
1ac26c
+
1ac26c
+    uint32_t cy = (uint32_t)(((p0 >> 32) + (uint32_t)p1 + (uint32_t)p2) >> 32);
1ac26c
+
1ac26c
+    *lo = p0 + (p1 << 32) + (p2 << 32);
1ac26c
+    *hi = p3 + (p1 >> 32) + (p2 >> 32) + cy;
1ac26c
+}
1ac26c
+#endif
1ac26c
+
1ac26c
+/* add two limbs with carry in, return carry out */
1ac26c
+static ossl_inline limb_t _add_limb(limb_t *ret, limb_t a, limb_t b, limb_t carry)
1ac26c
+{
1ac26c
+    limb_t carry1, carry2, t;
1ac26c
+    /*
1ac26c
+     * `c = a + b; if (c < a)` is idiomatic code that makes compilers
1ac26c
+     * use add with carry on assembly level
1ac26c
+     */
1ac26c
+
1ac26c
+    *ret = a + carry;
1ac26c
+    if (*ret < a)
1ac26c
+        carry1 = 1;
1ac26c
+    else
1ac26c
+        carry1 = 0;
1ac26c
+
1ac26c
+    t = *ret;
1ac26c
+    *ret = t + b;
1ac26c
+    if (*ret < t)
1ac26c
+        carry2 = 1;
1ac26c
+    else
1ac26c
+        carry2 = 0;
1ac26c
+
1ac26c
+    return carry1 + carry2;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * add two numbers of the same size, return overflow
1ac26c
+ *
1ac26c
+ * add a to b, place result in ret; all arrays need to be n limbs long
1ac26c
+ * return overflow from addition (0 or 1)
1ac26c
+ */
1ac26c
+static ossl_inline limb_t add(limb_t *ret, limb_t *a, limb_t *b, size_t n)
1ac26c
+{
1ac26c
+    limb_t c = 0;
1ac26c
+    ossl_ssize_t i;
1ac26c
+
1ac26c
+    for(i = n - 1; i > -1; i--)
1ac26c
+        c = _add_limb(&ret[i], a[i], b[i], c);
1ac26c
+
1ac26c
+    return c;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * return number of limbs necessary for temporary values
1ac26c
+ * when multiplying numbers n limbs large
1ac26c
+ */
1ac26c
+static ossl_inline size_t mul_limb_numb(size_t n)
1ac26c
+{
1ac26c
+    return  2 * n * 2;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * multiply two numbers of the same size
1ac26c
+ *
1ac26c
+ * multiply a by b, place result in ret; a and b need to be n limbs long
1ac26c
+ * ret needs to be 2*n limbs long, tmp needs to be mul_limb_numb(n) limbs
1ac26c
+ * long
1ac26c
+ */
1ac26c
+static void limb_mul(limb_t *ret, limb_t *a, limb_t *b, size_t n, limb_t *tmp)
1ac26c
+{
1ac26c
+    limb_t *r_odd, *r_even;
1ac26c
+    size_t i, j, k;
1ac26c
+
1ac26c
+    r_odd = tmp;
1ac26c
+    r_even = &tmp[2 * n];
1ac26c
+
1ac26c
+    memset(ret, 0, 2 * n * sizeof(limb_t));
1ac26c
+
1ac26c
+    for (i = 0; i < n; i++) {
1ac26c
+        for (k = 0; k < i + n + 1; k++) {
1ac26c
+            r_even[k] = 0;
1ac26c
+            r_odd[k] = 0;
1ac26c
+        }
1ac26c
+        for (j = 0; j < n; j++) {
1ac26c
+            /*
1ac26c
+             * place results from even and odd limbs in separate arrays so that
1ac26c
+             * we don't have to calculate overflow every time we get individual
1ac26c
+             * limb multiplication result
1ac26c
+             */
1ac26c
+            if (j % 2 == 0)
1ac26c
+                _mul_limb(&r_even[i + j], &r_even[i + j + 1], a[i], b[j]);
1ac26c
+            else
1ac26c
+                _mul_limb(&r_odd[i + j], &r_odd[i + j + 1], a[i], b[j]);
1ac26c
+        }
1ac26c
+        /*
1ac26c
+         * skip the least significant limbs when adding multiples of
1ac26c
+         * more significant limbs (they're zero anyway)
1ac26c
+         */
1ac26c
+        add(ret, ret, r_even, n + i + 1);
1ac26c
+        add(ret, ret, r_odd, n + i + 1);
1ac26c
+    }
1ac26c
+}
1ac26c
+
1ac26c
+/* modifies the value in place by performing a right shift by one bit */
1ac26c
+static ossl_inline void rshift1(limb_t *val, size_t n)
1ac26c
+{
1ac26c
+    limb_t shift_in = 0, shift_out = 0;
1ac26c
+    size_t i;
1ac26c
+
1ac26c
+    for (i = 0; i < n; i++) {
1ac26c
+        shift_out = val[i] & 1;
1ac26c
+        val[i] = shift_in << (LIMB_BIT_SIZE - 1) | (val[i] >> 1);
1ac26c
+        shift_in = shift_out;
1ac26c
+    }
1ac26c
+}
1ac26c
+
1ac26c
+/* extend the LSB of flag to all bits of limb */
1ac26c
+static ossl_inline limb_t mk_mask(limb_t flag)
1ac26c
+{
1ac26c
+    flag |= flag << 1;
1ac26c
+    flag |= flag << 2;
1ac26c
+    flag |= flag << 4;
1ac26c
+    flag |= flag << 8;
1ac26c
+    flag |= flag << 16;
1ac26c
+#if (LIMB_BYTE_SIZE == 8)
1ac26c
+    flag |= flag << 32;
1ac26c
+#endif
1ac26c
+    return flag;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * copy from either a or b to ret based on flag
1ac26c
+ * when flag == 0, then copies from b
1ac26c
+ * when flag == 1, then copies from a
1ac26c
+ */
1ac26c
+static ossl_inline void cselect(limb_t flag, limb_t *ret, limb_t *a, limb_t *b, size_t n)
1ac26c
+{
1ac26c
+    /*
1ac26c
+     * would be more efficient with non volatile mask, but then gcc
1ac26c
+     * generates code with jumps
1ac26c
+     */
1ac26c
+    volatile limb_t mask;
1ac26c
+    size_t i;
1ac26c
+
1ac26c
+    mask = mk_mask(flag);
1ac26c
+    for (i = 0; i < n; i++) {
1ac26c
+#if (LIMB_BYTE_SIZE == 8)
1ac26c
+        ret[i] = constant_time_select_64(mask, a[i], b[i]);
1ac26c
+#else
1ac26c
+        ret[i] = constant_time_select_32(mask, a[i], b[i]);
1ac26c
+#endif
1ac26c
+    }
1ac26c
+}
1ac26c
+
1ac26c
+static limb_t _sub_limb(limb_t *ret, limb_t a, limb_t b, limb_t borrow)
1ac26c
+{
1ac26c
+    limb_t borrow1, borrow2, t;
1ac26c
+    /*
1ac26c
+     * while it doesn't look constant-time, this is idiomatic code
1ac26c
+     * to tell compilers to use the carry bit from subtraction
1ac26c
+     */
1ac26c
+
1ac26c
+    *ret = a - borrow;
1ac26c
+    if (*ret > a)
1ac26c
+        borrow1 = 1;
1ac26c
+    else
1ac26c
+        borrow1 = 0;
1ac26c
+
1ac26c
+    t = *ret;
1ac26c
+    *ret = t - b;
1ac26c
+    if (*ret > t)
1ac26c
+        borrow2 = 1;
1ac26c
+    else
1ac26c
+        borrow2 = 0;
1ac26c
+
1ac26c
+    return borrow1 + borrow2;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * place the result of a - b into ret, return the borrow bit.
1ac26c
+ * All arrays need to be n limbs long
1ac26c
+ */
1ac26c
+static limb_t sub(limb_t *ret, limb_t *a, limb_t *b, size_t n)
1ac26c
+{
1ac26c
+    limb_t borrow = 0;
1ac26c
+    ossl_ssize_t i;
1ac26c
+
1ac26c
+    for (i = n - 1; i > -1; i--)
1ac26c
+        borrow = _sub_limb(&ret[i], a[i], b[i], borrow);
1ac26c
+
1ac26c
+    return borrow;
1ac26c
+}
1ac26c
+
1ac26c
+/* return the number of limbs necessary to allocate for the mod() tmp operand */
1ac26c
+static ossl_inline size_t mod_limb_numb(size_t anum, size_t modnum)
1ac26c
+{
1ac26c
+    return (anum + modnum) * 3;
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * calculate a % mod, place the result in ret
1ac26c
+ * size of a is defined by anum, size of ret and mod is modnum,
1ac26c
+ * size of tmp is returned by mod_limb_numb()
1ac26c
+ */
1ac26c
+static void mod(limb_t *ret, limb_t *a, size_t anum, limb_t *mod,
1ac26c
+               size_t modnum, limb_t *tmp)
1ac26c
+{
1ac26c
+    limb_t *atmp, *modtmp, *rettmp;
1ac26c
+    limb_t res;
1ac26c
+    size_t i;
1ac26c
+
1ac26c
+    memset(tmp, 0, mod_limb_numb(anum, modnum) * LIMB_BYTE_SIZE);
1ac26c
+
1ac26c
+    atmp = tmp;
1ac26c
+    modtmp = &tmp[anum + modnum];
1ac26c
+    rettmp = &tmp[(anum + modnum) * 2];
1ac26c
+
1ac26c
+    for (i = modnum; i 
1ac26c
+        atmp[i] = a[i-modnum];
1ac26c
+
1ac26c
+    for (i = 0; i < modnum; i++)
1ac26c
+        modtmp[i] = mod[i];
1ac26c
+
1ac26c
+    for (i = 0; i < anum * LIMB_BIT_SIZE; i++) {
1ac26c
+        rshift1(modtmp, anum + modnum);
1ac26c
+        res = sub(rettmp, atmp, modtmp, anum+modnum);
1ac26c
+        cselect(res, atmp, atmp, rettmp, anum+modnum);
1ac26c
+    }
1ac26c
+
1ac26c
+    memcpy(ret, &atmp[anum], sizeof(limb_t) * modnum);
1ac26c
+}
1ac26c
+
1ac26c
+/* necessary size of tmp for a _mul_add_limb() call with provided anum */
1ac26c
+static ossl_inline size_t _mul_add_limb_numb(size_t anum)
1ac26c
+{
1ac26c
+    return 2 * (anum + 1);
1ac26c
+}
1ac26c
+
1ac26c
+/* multiply a by m, add to ret, return carry */
1ac26c
+static limb_t _mul_add_limb(limb_t *ret, limb_t *a, size_t anum,
1ac26c
+                           limb_t m, limb_t *tmp)
1ac26c
+{
1ac26c
+    limb_t carry = 0;
1ac26c
+    limb_t *r_odd, *r_even;
1ac26c
+    size_t i;
1ac26c
+
1ac26c
+    memset(tmp, 0, sizeof(limb_t) * (anum + 1) * 2);
1ac26c
+
1ac26c
+    r_odd = tmp;
1ac26c
+    r_even = &tmp[anum + 1];
1ac26c
+
1ac26c
+    for (i = 0; i < anum; i++) {
1ac26c
+        /*
1ac26c
+         * place the results from even and odd limbs in separate arrays
1ac26c
+         * so that we have to worry about carry just once
1ac26c
+         */
1ac26c
+        if (i % 2 == 0)
1ac26c
+            _mul_limb(&r_even[i], &r_even[i + 1], a[i], m);
1ac26c
+        else
1ac26c
+            _mul_limb(&r_odd[i], &r_odd[i + 1], a[i], m);
1ac26c
+    }
1ac26c
+    /* assert: add() carry here will be equal zero */
1ac26c
+    add(r_even, r_even, r_odd, anum + 1);
1ac26c
+    /*
1ac26c
+     * while here it will not overflow as the max value from multiplication
1ac26c
+     * is -2 while max overflow from addition is 1, so the max value of
1ac26c
+     * carry is -1 (i.e. max int)
1ac26c
+     */
1ac26c
+    carry = add(ret, ret, &r_even[1], anum) + r_even[0];
1ac26c
+
1ac26c
+    return carry;
1ac26c
+}
1ac26c
+
1ac26c
+static ossl_inline size_t mod_montgomery_limb_numb(size_t modnum)
1ac26c
+{
1ac26c
+    return modnum * 2 + _mul_add_limb_numb(modnum);
1ac26c
+}
1ac26c
+
1ac26c
+/*
1ac26c
+ * calculate a % mod, place result in ret
1ac26c
+ * assumes that a is in Montgomery form with the R (Montgomery modulus) being
1ac26c
+ * smallest power of two big enough to fit mod and that's also a power
1ac26c
+ * of the count of number of bits in limb_t (B).
1ac26c
+ * For calculation, we also need n', such that mod * n' == -1 mod B.
1ac26c
+ * anum must be <= 2 * modnum
1ac26c
+ * ret needs to be modnum words long
1ac26c
+ * tmp needs to be mod_montgomery_limb_numb(modnum) limbs long
1ac26c
+ */
1ac26c
+static void mod_montgomery(limb_t *ret, limb_t *a, size_t anum, limb_t *mod,
1ac26c
+                          size_t modnum, limb_t ni0, limb_t *tmp)
1ac26c
+{
1ac26c
+    limb_t carry, v;
1ac26c
+    limb_t *res, *rp, *tmp2;
1ac26c
+    ossl_ssize_t i;
1ac26c
+
1ac26c
+    res = tmp;
1ac26c
+    /*
1ac26c
+     * for intermediate result we need an integer twice as long as modulus
1ac26c
+     * but keep the input in the least significant limbs
1ac26c
+     */
1ac26c
+    memset(res, 0, sizeof(limb_t) * (modnum * 2));
1ac26c
+    memcpy(&res[modnum * 2 - anum], a, sizeof(limb_t) * anum);
1ac26c
+    rp = &res[modnum];
1ac26c
+    tmp2 = &res[modnum * 2];
1ac26c
+
1ac26c
+    carry = 0;
1ac26c
+
1ac26c
+    /* add multiples of the modulus to the value until R divides it cleanly */
1ac26c
+    for (i = modnum; i > 0; i--, rp--) {
1ac26c
+        v = _mul_add_limb(rp, mod, modnum, rp[modnum-1] * ni0, tmp2);
1ac26c
+        v = v + carry + rp[-1];
1ac26c
+        carry |= (v != rp[-1]);
1ac26c
+        carry &= (v <= rp[-1]);
1ac26c
+        rp[-1] = v;
1ac26c
+    }
1ac26c
+
1ac26c
+    /* perform the final reduction by mod... */
1ac26c
+    carry -= sub(ret, rp, mod, modnum);
1ac26c
+
1ac26c
+    /* ...conditionally */
1ac26c
+    cselect(carry, ret, rp, ret, modnum);
1ac26c
+}
1ac26c
+
1ac26c
+/* allocated buffer should be freed afterwards */
1ac26c
+static void BN_to_limb(const BIGNUM *bn, limb_t *buf, size_t limbs)
1ac26c
+{
1ac26c
+    int i;
1ac26c
+    int real_limbs = (BN_num_bytes(bn) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE;
1ac26c
+    limb_t *ptr = buf + (limbs - real_limbs);
1ac26c
+
1ac26c
+    for (i = 0; i < real_limbs; i++)
1ac26c
+         ptr[i] = bn->d[real_limbs - i - 1];
1ac26c
+}
1ac26c
+
1ac26c
+#if LIMB_BYTE_SIZE == 8
1ac26c
+static ossl_inline uint64_t be64(uint64_t host)
1ac26c
+{
1ac26c
+    uint64_t big = 0;
1ac26c
+    DECLARE_IS_ENDIAN;
1ac26c
+
1ac26c
+    if (!IS_LITTLE_ENDIAN)
1ac26c
+        return host;
1ac26c
+
1ac26c
+    big |= (host & 0xff00000000000000) >> 56;
1ac26c
+    big |= (host & 0x00ff000000000000) >> 40;
1ac26c
+    big |= (host & 0x0000ff0000000000) >> 24;
1ac26c
+    big |= (host & 0x000000ff00000000) >>  8;
1ac26c
+    big |= (host & 0x00000000ff000000) <<  8;
1ac26c
+    big |= (host & 0x0000000000ff0000) << 24;
1ac26c
+    big |= (host & 0x000000000000ff00) << 40;
1ac26c
+    big |= (host & 0x00000000000000ff) << 56;
1ac26c
+    return big;
1ac26c
+}
1ac26c
+
1ac26c
+#else
1ac26c
+/* Not all platforms have htobe32(). */
1ac26c
+static ossl_inline uint32_t be32(uint32_t host)
1ac26c
+{
1ac26c
+    uint32_t big = 0;
1ac26c
+    DECLARE_IS_ENDIAN;
1ac26c
+
1ac26c
+    if (!IS_LITTLE_ENDIAN)
1ac26c
+        return host;
1ac26c
+
1ac26c
+    big |= (host & 0xff000000) >> 24;
1ac26c
+    big |= (host & 0x00ff0000) >> 8;
1ac26c
+    big |= (host & 0x0000ff00) << 8;
1ac26c
+    big |= (host & 0x000000ff) << 24;
1ac26c
+    return big;
1ac26c
+}
1ac26c
+#endif
1ac26c
+
1ac26c
+/*
1ac26c
+ * We assume that intermediate, possible_arg2, blinding, and ctx are used
1ac26c
+ * similar to BN_BLINDING_invert_ex() arguments.
1ac26c
+ * to_mod is RSA modulus.
1ac26c
+ * buf and num is the serialization buffer and its length.
1ac26c
+ *
1ac26c
+ * Here we use classic/Montgomery multiplication and modulo. After the calculation finished
1ac26c
+ * we serialize the new structure instead of BIGNUMs taking endianness into account.
1ac26c
+ */
1ac26c
+int ossl_bn_rsa_do_unblind(const BIGNUM *intermediate,
1ac26c
+                           const BN_BLINDING *blinding,
1ac26c
+                           const BIGNUM *possible_arg2,
1ac26c
+                           const BIGNUM *to_mod, BN_CTX *ctx,
1ac26c
+                           unsigned char *buf, int num)
1ac26c
+{
1ac26c
+    limb_t *l_im = NULL, *l_mul = NULL, *l_mod = NULL;
1ac26c
+    limb_t *l_ret = NULL, *l_tmp = NULL, l_buf;
1ac26c
+    size_t l_im_count = 0, l_mul_count = 0, l_size = 0, l_mod_count = 0;
1ac26c
+    size_t l_tmp_count = 0;
1ac26c
+    int ret = 0;
1ac26c
+    size_t i;
1ac26c
+    unsigned char *tmp;
1ac26c
+    const BIGNUM *arg1 = intermediate;
1ac26c
+    const BIGNUM *arg2 = (possible_arg2 == NULL) ? blinding->Ai : possible_arg2;
1ac26c
+
1ac26c
+    l_im_count  = (BN_num_bytes(arg1)   + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE;
1ac26c
+    l_mul_count = (BN_num_bytes(arg2)   + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE;
1ac26c
+    l_mod_count = (BN_num_bytes(to_mod) + LIMB_BYTE_SIZE - 1) / LIMB_BYTE_SIZE;
1ac26c
+
1ac26c
+    l_size = l_im_count > l_mul_count ? l_im_count : l_mul_count;
1ac26c
+    l_im  = OPENSSL_zalloc(l_size * LIMB_BYTE_SIZE);
1ac26c
+    l_mul = OPENSSL_zalloc(l_size * LIMB_BYTE_SIZE);
1ac26c
+    l_mod = OPENSSL_zalloc(l_mod_count * LIMB_BYTE_SIZE);
1ac26c
+
1ac26c
+    if ((l_im == NULL) || (l_mul == NULL) || (l_mod == NULL))
1ac26c
+        goto err;
1ac26c
+
1ac26c
+    BN_to_limb(arg1,   l_im,  l_size);
1ac26c
+    BN_to_limb(arg2,   l_mul, l_size);
1ac26c
+    BN_to_limb(to_mod, l_mod, l_mod_count);
1ac26c
+
1ac26c
+    l_ret = OPENSSL_malloc(2 * l_size * LIMB_BYTE_SIZE);
1ac26c
+
1ac26c
+    if (blinding->m_ctx != NULL) {
1ac26c
+        l_tmp_count = mul_limb_numb(l_size) > mod_montgomery_limb_numb(l_mod_count) ?
1ac26c
+                      mul_limb_numb(l_size) : mod_montgomery_limb_numb(l_mod_count);
1ac26c
+        l_tmp = OPENSSL_malloc(l_tmp_count * LIMB_BYTE_SIZE);
1ac26c
+    } else {
1ac26c
+        l_tmp_count = mul_limb_numb(l_size) > mod_limb_numb(2 * l_size, l_mod_count) ?
1ac26c
+                      mul_limb_numb(l_size) : mod_limb_numb(2 * l_size, l_mod_count);
1ac26c
+        l_tmp = OPENSSL_malloc(l_tmp_count * LIMB_BYTE_SIZE);
1ac26c
+    }
1ac26c
+
1ac26c
+    if ((l_ret == NULL) || (l_tmp == NULL))
1ac26c
+        goto err;
1ac26c
+
1ac26c
+    if (blinding->m_ctx != NULL) {
1ac26c
+        limb_mul(l_ret, l_im, l_mul, l_size, l_tmp);
1ac26c
+        mod_montgomery(l_ret, l_ret, 2 * l_size, l_mod, l_mod_count,
1ac26c
+                       blinding->m_ctx->n0[0], l_tmp);
1ac26c
+    } else {
1ac26c
+        limb_mul(l_ret, l_im, l_mul, l_size, l_tmp);
1ac26c
+        mod(l_ret, l_ret, 2 * l_size, l_mod, l_mod_count, l_tmp);
1ac26c
+    }
1ac26c
+
1ac26c
+    /* modulus size in bytes can be equal to num but after limbs conversion it becomes bigger */
1ac26c
+    if (num < BN_num_bytes(to_mod)) {
1ac26c
+        ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
1ac26c
+        goto err;
1ac26c
+    }
1ac26c
+
1ac26c
+    memset(buf, 0, num);
1ac26c
+    tmp = buf + num - BN_num_bytes(to_mod);
1ac26c
+    for (i = 0; i < l_mod_count; i++) {
1ac26c
+#if LIMB_BYTE_SIZE == 8
1ac26c
+        l_buf = be64(l_ret[i]);
1ac26c
+#else
1ac26c
+        l_buf = be32(l_ret[i]);
1ac26c
+#endif
1ac26c
+        if (i == 0) {
1ac26c
+            int delta = LIMB_BYTE_SIZE - ((l_mod_count * LIMB_BYTE_SIZE) - num);
1ac26c
+
1ac26c
+            memcpy(tmp, ((char *)&l_buf) + LIMB_BYTE_SIZE - delta, delta);
1ac26c
+            tmp += delta;
1ac26c
+        } else {
1ac26c
+            memcpy(tmp, &l_buf, LIMB_BYTE_SIZE);
1ac26c
+            tmp += LIMB_BYTE_SIZE;
1ac26c
+        }
1ac26c
+    }
1ac26c
+    ret = num;
1ac26c
+
1ac26c
+ err:
1ac26c
+    OPENSSL_free(l_im);
1ac26c
+    OPENSSL_free(l_mul);
1ac26c
+    OPENSSL_free(l_mod);
1ac26c
+    OPENSSL_free(l_tmp);
1ac26c
+    OPENSSL_free(l_ret);
1ac26c
+
1ac26c
+    return ret;
1ac26c
+}
1ac26c
diff --git a/crypto/rsa/rsa_ossl.c b/crypto/rsa/rsa_ossl.c
1ac26c
index 381c659352..7e8b791fba 100644
1ac26c
--- a/crypto/rsa/rsa_ossl.c
1ac26c
+++ b/crypto/rsa/rsa_ossl.c
1ac26c
@@ -469,13 +469,20 @@ static int rsa_ossl_private_decrypt(int flen, const unsigned char *from,
1ac26c
         BN_free(d);
1ac26c
     }
1ac26c
 
1ac26c
-    if (blinding)
1ac26c
-        if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
1ac26c
+    if (blinding) {
1ac26c
+        /*
1ac26c
+         * ossl_bn_rsa_do_unblind() combines blinding inversion and
1ac26c
+         * 0-padded BN BE serialization
1ac26c
+         */
1ac26c
+        j = ossl_bn_rsa_do_unblind(ret, blinding, unblind, rsa->n, ctx,
1ac26c
+                                   buf, num);
1ac26c
+        if (j == 0)
1ac26c
             goto err;
1ac26c
-
1ac26c
-    j = BN_bn2binpad(ret, buf, num);
1ac26c
-    if (j < 0)
1ac26c
-        goto err;
1ac26c
+    } else {
1ac26c
+        j = BN_bn2binpad(ret, buf, num);
1ac26c
+        if (j < 0)
1ac26c
+            goto err;
1ac26c
+    }
1ac26c
 
1ac26c
     switch (padding) {
1ac26c
     case RSA_PKCS1_PADDING:
1ac26c
diff --git a/include/crypto/bn.h b/include/crypto/bn.h
1ac26c
index cf69bea848..cd45654210 100644
1ac26c
--- a/include/crypto/bn.h
1ac26c
+++ b/include/crypto/bn.h
1ac26c
@@ -114,4 +114,10 @@ OSSL_LIB_CTX *ossl_bn_get_libctx(BN_CTX *ctx);
1ac26c
 
1ac26c
 extern const BIGNUM ossl_bn_inv_sqrt_2;
1ac26c
 
1ac26c
+int ossl_bn_rsa_do_unblind(const BIGNUM *intermediate,
1ac26c
+                           const BN_BLINDING *blinding,
1ac26c
+                           const BIGNUM *possible_arg2,
1ac26c
+                           const BIGNUM *to_mod, BN_CTX *ctx,
1ac26c
+                           unsigned char *buf, int num);
1ac26c
+
1ac26c
 #endif
1ac26c
-- 
1ac26c
2.39.1
1ac26c