Blame SOURCES/openssl-1.0.2k-fix-one-and-done.patch

450916
diff -up openssl-1.0.2k/crypto/bn/bn_exp.c.one-and-done openssl-1.0.2k/crypto/bn/bn_exp.c
450916
--- openssl-1.0.2k/crypto/bn/bn_exp.c.one-and-done	2019-04-04 16:46:21.287257363 +0200
450916
+++ openssl-1.0.2k/crypto/bn/bn_exp.c	2019-04-04 16:45:32.875130057 +0200
450916
@@ -579,7 +579,6 @@ int BN_mod_exp_mont(BIGNUM *rr, const BI
450916
     return (ret);
450916
 }
450916
 
450916
-#if defined(SPARC_T4_MONT)
450916
 static BN_ULONG bn_get_bits(const BIGNUM *a, int bitpos)
450916
 {
450916
     BN_ULONG ret = 0;
450916
@@ -598,7 +597,6 @@ static BN_ULONG bn_get_bits(const BIGNUM
450916
 
450916
     return ret & BN_MASK2;
450916
 }
450916
-#endif
450916
 
450916
 /*
450916
  * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
450916
@@ -697,7 +695,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr
450916
                               const BIGNUM *m, BN_CTX *ctx,
450916
                               BN_MONT_CTX *in_mont)
450916
 {
450916
-    int i, bits, ret = 0, window, wvalue;
450916
+    int i, bits, ret = 0, window, wvalue, wmask, window0;
450916
     int top;
450916
     BN_MONT_CTX *mont = NULL;
450916
 
450916
@@ -945,20 +943,27 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr
450916
         top /= 2;
450916
         bn_flip_t4(np, mont->N.d, top);
450916
 
450916
-        bits--;
450916
-        for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)
450916
-            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
450916
+        /*
450916
+         * The exponent may not have a whole number of fixed-size windows.
450916
+         * To simplify the main loop, the initial window has between 1 and
450916
+         * full-window-size bits such that what remains is always a whole
450916
+         * number of windows
450916
+         */
450916
+        window0 = (bits - 1) % 5 + 1;
450916
+        wmask = (1 << window0) - 1;
450916
+        bits -= window0;
450916
+        wvalue = bn_get_bits(p, bits) & wmask;
450916
         bn_gather5_t4(tmp.d, top, powerbuf, wvalue);
450916
 
450916
         /*
450916
          * Scan the exponent one window at a time starting from the most
450916
          * significant bits.
450916
          */
450916
-        while (bits >= 0) {
450916
+        while (bits > 0) {
450916
             if (bits < stride)
450916
-                stride = bits + 1;
450916
+                stride = bits;
450916
             bits -= stride;
450916
-            wvalue = bn_get_bits(p, bits + 1);
450916
+            wvalue = bn_get_bits(p, bits);
450916
 
450916
             if ((*pwr5_worker) (tmp.d, np, n0, powerbuf, wvalue, stride))
450916
                 continue;
450916
@@ -1066,32 +1071,36 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr
450916
             bn_scatter5(tmp.d, top, powerbuf, i);
450916
         }
450916
 # endif
450916
-        bits--;
450916
-        for (wvalue = 0, i = bits % 5; i >= 0; i--, bits--)
450916
-            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
450916
+        /*
450916
+         * The exponent may not have a whole number of fixed-size windows.
450916
+         * To simplify the main loop, the initial window has between 1 and
450916
+         * full-window-size bits such that what remains is always a whole
450916
+         * number of windows
450916
+         */
450916
+        window0 = (bits - 1) % 5 + 1;
450916
+        wmask = (1 << window0) - 1;
450916
+        bits -= window0;
450916
+        wvalue = bn_get_bits(p, bits) & wmask;
450916
         bn_gather5(tmp.d, top, powerbuf, wvalue);
450916
 
450916
         /*
450916
          * Scan the exponent one window at a time starting from the most
450916
          * significant bits.
450916
          */
450916
-        if (top & 7)
450916
-            while (bits >= 0) {
450916
-                for (wvalue = 0, i = 0; i < 5; i++, bits--)
450916
-                    wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
450916
-
450916
+        if (top & 7) {
450916
+            while (bits > 0) {
450916
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
450916
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
450916
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
450916
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
450916
                 bn_mul_mont(tmp.d, tmp.d, tmp.d, np, n0, top);
450916
                 bn_mul_mont_gather5(tmp.d, tmp.d, powerbuf, np, n0, top,
450916
-                                    wvalue);
450916
+                                    bn_get_bits5(p->d, bits -= 5));
450916
+            }
450916
         } else {
450916
-            while (bits >= 0) {
450916
-                wvalue = bn_get_bits5(p->d, bits - 4);
450916
-                bits -= 5;
450916
-                bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top, wvalue);
450916
+            while (bits > 0) {
450916
+                bn_power5(tmp.d, tmp.d, powerbuf, np, n0, top,
450916
+                          bn_get_bits5(p->d, bits -= 5));
450916
             }
450916
         }
450916
 
450916
@@ -1133,28 +1142,45 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr
450916
             }
450916
         }
450916
 
450916
-        bits--;
450916
-        for (wvalue = 0, i = bits % window; i >= 0; i--, bits--)
450916
-            wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
450916
+        /*
450916
+         * The exponent may not have a whole number of fixed-size windows.
450916
+         * To simplify the main loop, the initial window has between 1 and
450916
+         * full-window-size bits such that what remains is always a whole
450916
+         * number of windows
450916
+         */
450916
+        window0 = (bits - 1) % window + 1;
450916
+        wmask = (1 << window0) - 1;
450916
+        bits -= window0;
450916
+        wvalue = bn_get_bits(p, bits) & wmask;
450916
         if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&tmp, top, powerbuf, wvalue,
450916
                                             window))
450916
             goto err;
450916
 
450916
+        wmask = (1 << window) - 1;
450916
         /*
450916
          * Scan the exponent one window at a time starting from the most
450916
          * significant bits.
450916
          */
450916
-        while (bits >= 0) {
450916
-            wvalue = 0;         /* The 'value' of the window */
450916
+        while (bits > 0) {
450916
 
450916
-            /* Scan the window, squaring the result as we go */
450916
-            for (i = 0; i < window; i++, bits--) {
450916
+            /* Square the result window-size times */
450916
+            for (i = 0; i < window; i++)
450916
                 if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))
450916
                     goto err;
450916
-                wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
450916
-            }
450916
 
450916
             /*
450916
+             * Get a window's worth of bits from the exponent
450916
+             * This avoids calling BN_is_bit_set for each bit, which
450916
+             * is not only slower but also makes each bit vulnerable to
450916
+             * EM (and likely other) side-channel attacks like One&Done
450916
+             * (for details see "One&Done: A Single-Decryption EM-Based
450916
+             *  Attack on OpenSSL's Constant-Time Blinded RSA" by M. Alam,
450916
+             *  H. Khan, M. Dey, N. Sinha, R. Callan, A. Zajic, and
450916
+             *  M. Prvulovic, in USENIX Security'18)
450916
+             */
450916
+            bits -= window;
450916
+            wvalue = bn_get_bits(p, bits) & wmask;
450916
+            /*
450916
              * Fetch the appropriate pre-computed value from the pre-buf
450916
              */
450916
             if (!MOD_EXP_CTIME_COPY_FROM_PREBUF(&am, top, powerbuf, wvalue,