Blame SOURCES/openssl-1.0.2k-cve-2018-0495.patch

7779df
diff --git a/crypto/Makefile b/crypto/Makefile
7779df
index 8bc5850..3a2bc51 100644
7779df
--- a/crypto/Makefile
7779df
+++ b/crypto/Makefile
7779df
@@ -45,7 +45,7 @@ SRC= $(LIBSRC)
7779df
 EXHEADER= crypto.h opensslv.h opensslconf.h ebcdic.h symhacks.h \
7779df
 	ossl_typ.h
7779df
 HEADER=	cryptlib.h buildinf.h md32_common.h o_time.h o_str.h o_dir.h \
7779df
-	constant_time_locl.h $(EXHEADER)
7779df
+	constant_time_locl.h bn_int.h $(EXHEADER)
7779df
 
7779df
 ALL=    $(GENERAL) $(SRC) $(HEADER)
7779df
 
7779df
diff --git a/crypto/bn/bn.h b/crypto/bn/bn.h
7779df
index b37f6ec..0e6e30f 100644
7779df
--- a/crypto/bn/bn.h
7779df
+++ b/crypto/bn/bn.h
7779df
@@ -702,6 +702,16 @@ BIGNUM *bn_dup_expand(const BIGNUM *a, int words); /* unused */
7779df
 /* We only need assert() when debugging */
7779df
 #  include <assert.h>
7779df
 
7779df
+/*
7779df
+ * The new BN_FLG_FIXED_TOP flag marks vectors that were not treated with
7779df
+ * bn_correct_top, in other words such vectors are permitted to have zeros
7779df
+ * in most significant limbs. Such vectors are used internally to achieve
7779df
+ * execution time invariance for critical operations with private keys.
7779df
+ * It's BN_DEBUG-only flag, because user application is not supposed to
7779df
+ * observe it anyway. Moreover, optimizing compiler would actually remove
7779df
+ * all operations manipulating the bit in question in non-BN_DEBUG build.
7779df
+ */
7779df
+#  define BN_FLG_FIXED_TOP 0x10000
7779df
 #  ifdef BN_DEBUG_RAND
7779df
 /* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */
7779df
 #   ifndef RAND_pseudo_bytes
7779df
@@ -734,8 +744,10 @@ int RAND_pseudo_bytes(unsigned char *buf, int num);
7779df
         do { \
7779df
                 const BIGNUM *_bnum2 = (a); \
7779df
                 if (_bnum2 != NULL) { \
7779df
-                        assert((_bnum2->top == 0) || \
7779df
-                                (_bnum2->d[_bnum2->top - 1] != 0)); \
7779df
+                        int _top = _bnum2->top; \
7779df
+                        assert((_top == 0) || \
7779df
+                               (_bnum2->flags & BN_FLG_FIXED_TOP) || \
7779df
+                               (_bnum2->d[_top - 1] != 0)); \
7779df
                         bn_pollute(_bnum2); \
7779df
                 } \
7779df
         } while(0)
7779df
@@ -753,6 +765,7 @@ int RAND_pseudo_bytes(unsigned char *buf, int num);
7779df
 
7779df
 # else                          /* !BN_DEBUG */
7779df
 
7779df
+#  define BN_FLG_FIXED_TOP 0
7779df
 #  define bn_pollute(a)
7779df
 #  define bn_check_top(a)
7779df
 #  define bn_fix_top(a)           bn_correct_top(a)
7779df
diff --git a/crypto/bn/bn_div.c b/crypto/bn/bn_div.c
7779df
index bc37671..460d8b7 100644
7779df
--- a/crypto/bn/bn_div.c
7779df
+++ b/crypto/bn/bn_div.c
7779df
@@ -290,6 +290,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
7779df
     wnum.neg = 0;
7779df
     wnum.d = &(snum->d[loop]);
7779df
     wnum.top = div_n;
7779df
+    wnum.flags = BN_FLG_STATIC_DATA;
7779df
     /*
7779df
      * only needed when BN_ucmp messes up the values between top and max
7779df
      */
7779df
diff --git a/crypto/bn/bn_exp.c b/crypto/bn/bn_exp.c
7779df
index 195a786..816a198 100644
7779df
--- a/crypto/bn/bn_exp.c
7779df
+++ b/crypto/bn/bn_exp.c
7779df
@@ -466,17 +466,17 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
         ret = 1;
7779df
         goto err;
7779df
     }
7779df
-    if (!BN_to_montgomery(val[0], aa, mont, ctx))
7779df
+    if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))
7779df
         goto err;               /* 1 */
7779df
 
7779df
     window = BN_window_bits_for_exponent_size(bits);
7779df
     if (window > 1) {
7779df
-        if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))
7779df
+        if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))
7779df
             goto err;           /* 2 */
7779df
         j = 1 << (window - 1);
7779df
         for (i = 1; i < j; i++) {
7779df
             if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
7779df
-                !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))
7779df
+                !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))
7779df
                 goto err;
7779df
         }
7779df
     }
7779df
@@ -498,19 +498,15 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
         for (i = 1; i < j; i++)
7779df
             r->d[i] = (~m->d[i]) & BN_MASK2;
7779df
         r->top = j;
7779df
-        /*
7779df
-         * Upper words will be zero if the corresponding words of 'm' were
7779df
-         * 0xfff[...], so decrement r->top accordingly.
7779df
-         */
7779df
-        bn_correct_top(r);
7779df
+        r->flags |= BN_FLG_FIXED_TOP;
7779df
     } else
7779df
 #endif
7779df
-    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
7779df
+    if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))
7779df
         goto err;
7779df
     for (;;) {
7779df
         if (BN_is_bit_set(p, wstart) == 0) {
7779df
             if (!start) {
7779df
-                if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
7779df
+                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
7779df
                     goto err;
7779df
             }
7779df
             if (wstart == 0)
7779df
@@ -541,12 +537,12 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
         /* add the 'bytes above' */
7779df
         if (!start)
7779df
             for (i = 0; i < j; i++) {
7779df
-                if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
7779df
+                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))
7779df
                     goto err;
7779df
             }
7779df
 
7779df
         /* wvalue will be an odd number < 2^window */
7779df
-        if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))
7779df
+        if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))
7779df
             goto err;
7779df
 
7779df
         /* move the 'window' down further */
7779df
@@ -556,6 +552,11 @@ int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
         if (wstart < 0)
7779df
             break;
7779df
     }
7779df
+    /*
7779df
+     * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
7779df
+     * removes padding [if any] and makes return value suitable for public
7779df
+     * API consumer.
7779df
+     */
7779df
 #if defined(SPARC_T4_MONT)
7779df
     if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
7779df
         j = mont->N.top;        /* borrow j */
7779df
@@ -674,7 +675,7 @@ static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,
7779df
     }
7779df
 
7779df
     b->top = top;
7779df
-    bn_correct_top(b);
7779df
+    b->flags |= BN_FLG_FIXED_TOP;
7779df
     return 1;
7779df
 }
7779df
 
7779df
@@ -841,16 +842,16 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
         tmp.top = top;
7779df
     } else
7779df
 #endif
7779df
-    if (!BN_to_montgomery(&tmp, BN_value_one(), mont, ctx))
7779df
+    if (!bn_to_mont_fixed_top(&tmp, BN_value_one(), mont, ctx))
7779df
         goto err;
7779df
 
7779df
     /* prepare a^1 in Montgomery domain */
7779df
     if (a->neg || BN_ucmp(a, m) >= 0) {
7779df
         if (!BN_mod(&am, a, m, ctx))
7779df
             goto err;
7779df
-        if (!BN_to_montgomery(&am, &am, mont, ctx))
7779df
+        if (!bn_to_mont_fixed_top(&am, &am, mont, ctx))
7779df
             goto err;
7779df
-    } else if (!BN_to_montgomery(&am, a, mont, ctx))
7779df
+    } else if (!bn_to_mont_fixed_top(&am, a, mont, ctx))
7779df
         goto err;
7779df
 
7779df
 #if defined(SPARC_T4_MONT)
7779df
@@ -1117,14 +1118,14 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
          * performance advantage of sqr over mul).
7779df
          */
7779df
         if (window > 1) {
7779df
-            if (!BN_mod_mul_montgomery(&tmp, &am, &am, mont, ctx))
7779df
+            if (!bn_mul_mont_fixed_top(&tmp, &am, &am, mont, ctx))
7779df
                 goto err;
7779df
             if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, 2,
7779df
                                               window))
7779df
                 goto err;
7779df
             for (i = 3; i < numPowers; i++) {
7779df
                 /* Calculate a^i = a^(i-1) * a */
7779df
-                if (!BN_mod_mul_montgomery(&tmp, &am, &tmp, mont, ctx))
7779df
+                if (!bn_mul_mont_fixed_top(&tmp, &am, &tmp, mont, ctx))
7779df
                     goto err;
7779df
                 if (!MOD_EXP_CTIME_COPY_TO_PREBUF(&tmp, top, powerbuf, i,
7779df
                                                   window))
7779df
@@ -1148,7 +1149,7 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
 
7779df
             /* Scan the window, squaring the result as we go */
7779df
             for (i = 0; i < window; i++, bits--) {
7779df
-                if (!BN_mod_mul_montgomery(&tmp, &tmp, &tmp, mont, ctx))
7779df
+                if (!bn_mul_mont_fixed_top(&tmp, &tmp, &tmp, mont, ctx))
7779df
                     goto err;
7779df
                 wvalue = (wvalue << 1) + BN_is_bit_set(p, bits);
7779df
             }
7779df
@@ -1161,12 +1162,16 @@ int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
7779df
                 goto err;
7779df
 
7779df
             /* Multiply the result into the intermediate result */
7779df
-            if (!BN_mod_mul_montgomery(&tmp, &tmp, &am, mont, ctx))
7779df
+            if (!bn_mul_mont_fixed_top(&tmp, &tmp, &am, mont, ctx))
7779df
                 goto err;
7779df
         }
7779df
     }
7779df
 
7779df
-    /* Convert the final result from montgomery to standard format */
7779df
+    /*
7779df
+     * Done with zero-padded intermediate BIGNUMs. Final BN_from_montgomery
7779df
+     * removes padding [if any] and makes return value suitable for public
7779df
+     * API consumer.
7779df
+     */
7779df
 #if defined(SPARC_T4_MONT)
7779df
     if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {
7779df
         am.d[0] = 1;            /* borrow am */
7779df
diff --git a/crypto/bn/bn_lcl.h b/crypto/bn/bn_lcl.h
7779df
index 00f4f09..1aa7fe8 100644
7779df
--- a/crypto/bn/bn_lcl.h
7779df
+++ b/crypto/bn/bn_lcl.h
7779df
@@ -113,6 +113,7 @@
7779df
 # define HEADER_BN_LCL_H
7779df
 
7779df
 # include <openssl/bn.h>
7779df
+# include "bn_int.h"
7779df
 
7779df
 #ifdef  __cplusplus
7779df
 extern "C" {
7779df
diff --git a/crypto/bn/bn_lib.c b/crypto/bn/bn_lib.c
7779df
index 10b78f5..e42e3fb 100644
7779df
--- a/crypto/bn/bn_lib.c
7779df
+++ b/crypto/bn/bn_lib.c
7779df
@@ -290,8 +290,6 @@ static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
7779df
     const BN_ULONG *B;
7779df
     int i;
7779df
 
7779df
-    bn_check_top(b);
7779df
-
7779df
     if (words > (INT_MAX / (4 * BN_BITS2))) {
7779df
         BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
7779df
         return NULL;
7779df
@@ -425,8 +423,6 @@ BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
7779df
 
7779df
 BIGNUM *bn_expand2(BIGNUM *b, int words)
7779df
 {
7779df
-    bn_check_top(b);
7779df
-
7779df
     if (words > b->dmax) {
7779df
         BN_ULONG *a = bn_expand_internal(b, words);
7779df
         if (!a)
7779df
@@ -460,7 +456,6 @@ BIGNUM *bn_expand2(BIGNUM *b, int words)
7779df
         assert(A == &(b->d[b->dmax]));
7779df
     }
7779df
 #endif
7779df
-    bn_check_top(b);
7779df
     return b;
7779df
 }
7779df
 
7779df
@@ -572,6 +567,7 @@ void BN_clear(BIGNUM *a)
7779df
         OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
7779df
     a->top = 0;
7779df
     a->neg = 0;
7779df
+    a->flags &= ~BN_FLG_FIXED_TOP;
7779df
 }
7779df
 
7779df
 BN_ULONG BN_get_word(const BIGNUM *a)
7779df
@@ -592,6 +588,7 @@ int BN_set_word(BIGNUM *a, BN_ULONG w)
7779df
     a->neg = 0;
7779df
     a->d[0] = w;
7779df
     a->top = (w ? 1 : 0);
7779df
+    a->flags &= ~BN_FLG_FIXED_TOP;
7779df
     bn_check_top(a);
7779df
     return (1);
7779df
 }
7779df
@@ -738,6 +735,7 @@ int BN_set_bit(BIGNUM *a, int n)
7779df
         for (k = a->top; k < i + 1; k++)
7779df
             a->d[k] = 0;
7779df
         a->top = i + 1;
7779df
+        a->flags &= ~BN_FLG_FIXED_TOP;
7779df
     }
7779df
 
7779df
     a->d[i] |= (((BN_ULONG)1) << j);
7779df
diff --git a/crypto/bn/bn_mod.c b/crypto/bn/bn_mod.c
7779df
index ffbce89..23ddd48 100644
7779df
--- a/crypto/bn/bn_mod.c
7779df
+++ b/crypto/bn/bn_mod.c
7779df
@@ -149,16 +149,71 @@ int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
7779df
 
7779df
 /*
7779df
  * BN_mod_add variant that may be used if both a and b are non-negative and
7779df
- * less than m
7779df
+ * less than m. The original algorithm was
7779df
+ *
7779df
+ *    if (!BN_uadd(r, a, b))
7779df
+ *       return 0;
7779df
+ *    if (BN_ucmp(r, m) >= 0)
7779df
+ *       return BN_usub(r, r, m);
7779df
+ *
7779df
+ * which is replaced with addition, subtracting modulus, and conditional
7779df
+ * move depending on whether or not subtraction borrowed.
7779df
  */
7779df
+int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
+                         const BIGNUM *m)
7779df
+{
7779df
+    size_t i, ai, bi, mtop = m->top;
7779df
+    BN_ULONG storage[1024 / BN_BITS2];
7779df
+    BN_ULONG carry, temp, mask, *rp, *tp = storage;
7779df
+    const BN_ULONG *ap, *bp;
7779df
+
7779df
+    if (bn_wexpand(r, m->top) == NULL)
7779df
+        return 0;
7779df
+
7779df
+    if (mtop > sizeof(storage) / sizeof(storage[0])
7779df
+        && (tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG))) == NULL)
7779df
+	return 0;
7779df
+
7779df
+    ap = a->d != NULL ? a->d : tp;
7779df
+    bp = b->d != NULL ? b->d : tp;
7779df
+
7779df
+    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
7779df
+        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
7779df
+        temp = ((ap[ai] & mask) + carry) & BN_MASK2;
7779df
+        carry = (temp < carry);
7779df
+
7779df
+        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
7779df
+        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
7779df
+        carry += (tp[i] < temp);
7779df
+
7779df
+        i++;
7779df
+        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
7779df
+        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
7779df
+    }
7779df
+    rp = r->d;
7779df
+    carry -= bn_sub_words(rp, tp, m->d, mtop);
7779df
+    for (i = 0; i < mtop; i++) {
7779df
+        rp[i] = (carry & tp[i]) | (~carry & rp[i]);
7779df
+        ((volatile BN_ULONG *)tp)[i] = 0;
7779df
+    }
7779df
+    r->top = mtop;
7779df
+    r->neg = 0;
7779df
+
7779df
+    if (tp != storage)
7779df
+        OPENSSL_free(tp);
7779df
+
7779df
+    return 1;
7779df
+}
7779df
+
7779df
 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
                      const BIGNUM *m)
7779df
 {
7779df
-    if (!BN_uadd(r, a, b))
7779df
-        return 0;
7779df
-    if (BN_ucmp(r, m) >= 0)
7779df
-        return BN_usub(r, r, m);
7779df
-    return 1;
7779df
+    int ret = bn_mod_add_fixed_top(r, a, b, m);
7779df
+
7779df
+    if (ret)
7779df
+        bn_correct_top(r);
7779df
+
7779df
+    return ret;
7779df
 }
7779df
 
7779df
 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
7779df
diff --git a/crypto/bn/bn_mont.c b/crypto/bn/bn_mont.c
7779df
index be95bd5..d41434a 100644
7779df
--- a/crypto/bn/bn_mont.c
7779df
+++ b/crypto/bn/bn_mont.c
7779df
@@ -56,7 +56,7 @@
7779df
  * [including the GNU Public Licence.]
7779df
  */
7779df
 /* ====================================================================
7779df
- * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
7779df
+ * Copyright (c) 1998-2018 The OpenSSL Project.  All rights reserved.
7779df
  *
7779df
  * Redistribution and use in source and binary forms, with or without
7779df
  * modification, are permitted provided that the following conditions
7779df
@@ -123,11 +123,22 @@
7779df
 #define MONT_WORD               /* use the faster word-based algorithm */
7779df
 
7779df
 #ifdef MONT_WORD
7779df
-static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont);
7779df
+static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont);
7779df
 #endif
7779df
 
7779df
 int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
                           BN_MONT_CTX *mont, BN_CTX *ctx)
7779df
+{
7779df
+    int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);
7779df
+
7779df
+    bn_correct_top(r);
7779df
+    bn_check_top(r);
7779df
+
7779df
+    return ret;
7779df
+}
7779df
+
7779df
+int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
+                          BN_MONT_CTX *mont, BN_CTX *ctx)
7779df
 {
7779df
     BIGNUM *tmp;
7779df
     int ret = 0;
7779df
@@ -140,8 +151,8 @@ int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
         if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
7779df
             r->neg = a->neg ^ b->neg;
7779df
             r->top = num;
7779df
-            bn_correct_top(r);
7779df
-            return (1);
7779df
+            r->flags |= BN_FLG_FIXED_TOP;
7779df
+            return 1;
7779df
         }
7779df
     }
7779df
 #endif
7779df
@@ -161,13 +172,12 @@ int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
     }
7779df
     /* reduce from aRR to aR */
7779df
 #ifdef MONT_WORD
7779df
-    if (!BN_from_montgomery_word(r, tmp, mont))
7779df
+    if (!bn_from_montgomery_word(r, tmp, mont))
7779df
         goto err;
7779df
 #else
7779df
     if (!BN_from_montgomery(r, tmp, mont, ctx))
7779df
         goto err;
7779df
 #endif
7779df
-    bn_check_top(r);
7779df
     ret = 1;
7779df
  err:
7779df
     BN_CTX_end(ctx);
7779df
@@ -175,7 +185,7 @@ int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
 }
7779df
 
7779df
 #ifdef MONT_WORD
7779df
-static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
7779df
+static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
7779df
 {
7779df
     BIGNUM *n;
7779df
     BN_ULONG *ap, *np, *rp, n0, v, carry;
7779df
@@ -205,28 +215,16 @@ static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
7779df
 # endif
7779df
 
7779df
     r->top = max;
7779df
+    r->flags |= BN_FLG_FIXED_TOP;
7779df
     n0 = mont->n0[0];
7779df
 
7779df
-# ifdef BN_COUNT
7779df
-    fprintf(stderr, "word BN_from_montgomery_word %d * %d\n", nl, nl);
7779df
-# endif
7779df
+    /*
7779df
+     * Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
7779df
+     * input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
7779df
+     * includes |carry| which is stored separately.
7779df
+     */
7779df
     for (carry = 0, i = 0; i < nl; i++, rp++) {
7779df
-# ifdef __TANDEM
7779df
-        {
7779df
-            long long t1;
7779df
-            long long t2;
7779df
-            long long t3;
7779df
-            t1 = rp[0] * (n0 & 0177777);
7779df
-            t2 = 037777600000l;
7779df
-            t2 = n0 & t2;
7779df
-            t3 = rp[0] & 0177777;
7779df
-            t2 = (t3 * t2) & BN_MASK2;
7779df
-            t1 = t1 + t2;
7779df
-            v = bn_mul_add_words(rp, np, nl, (BN_ULONG)t1);
7779df
-        }
7779df
-# else
7779df
         v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
7779df
-# endif
7779df
         v = (v + carry + rp[nl]) & BN_MASK2;
7779df
         carry |= (v != rp[nl]);
7779df
         carry &= (v <= rp[nl]);
7779df
@@ -236,52 +234,27 @@ static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
7779df
     if (bn_wexpand(ret, nl) == NULL)
7779df
         return (0);
7779df
     ret->top = nl;
7779df
+    ret->flags |= BN_FLG_FIXED_TOP;
7779df
     ret->neg = r->neg;
7779df
 
7779df
     rp = ret->d;
7779df
+
7779df
+    /*
7779df
+     * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|
7779df
+     * includes |carry| which is stored separately.
7779df
+     */
7779df
     ap = &(r->d[nl]);
7779df
 
7779df
-# define BRANCH_FREE 1
7779df
-# if BRANCH_FREE
7779df
-    {
7779df
-        BN_ULONG *nrp;
7779df
-        size_t m;
7779df
-
7779df
-        v = bn_sub_words(rp, ap, np, nl) - carry;
7779df
-        /*
7779df
-         * if subtraction result is real, then trick unconditional memcpy
7779df
-         * below to perform in-place "refresh" instead of actual copy.
7779df
-         */
7779df
-        m = (0 - (size_t)v);
7779df
-        nrp =
7779df
-            (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
7779df
-
7779df
-        for (i = 0, nl -= 4; i < nl; i += 4) {
7779df
-            BN_ULONG t1, t2, t3, t4;
7779df
-
7779df
-            t1 = nrp[i + 0];
7779df
-            t2 = nrp[i + 1];
7779df
-            t3 = nrp[i + 2];
7779df
-            ap[i + 0] = 0;
7779df
-            t4 = nrp[i + 3];
7779df
-            ap[i + 1] = 0;
7779df
-            rp[i + 0] = t1;
7779df
-            ap[i + 2] = 0;
7779df
-            rp[i + 1] = t2;
7779df
-            ap[i + 3] = 0;
7779df
-            rp[i + 2] = t3;
7779df
-            rp[i + 3] = t4;
7779df
-        }
7779df
-        for (nl += 4; i < nl; i++)
7779df
-            rp[i] = nrp[i], ap[i] = 0;
7779df
+    carry -= bn_sub_words(rp, ap, np, nl);
7779df
+    /*
7779df
+     * |carry| is -1 if |ap| - |np| underflowed or zero if it did not. Note
7779df
+     * |carry| cannot be 1. That would imply the subtraction did not fit in
7779df
+     * |nl| words, and we know at most one subtraction is needed.
7779df
+     */
7779df
+    for (i = 0; i < nl; i++) {
7779df
+        rp[i] = (carry & ap[i]) | (~carry & rp[i]);
7779df
+        ap[i] = 0;
7779df
     }
7779df
-# else
7779df
-    if (bn_sub_words(rp, ap, np, nl) - carry)
7779df
-        memcpy(rp, ap, nl * sizeof(BN_ULONG));
7779df
-# endif
7779df
-    bn_correct_top(r);
7779df
-    bn_correct_top(ret);
7779df
-    bn_check_top(ret);
7779df
 
7779df
     return (1);
7779df
 }
7779df
@@ -295,8 +268,11 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
7779df
     BIGNUM *t;
7779df
 
7779df
     BN_CTX_start(ctx);
7779df
-    if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))
7779df
-        retn = BN_from_montgomery_word(ret, t, mont);
7779df
+    if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) {
7779df
+        retn = bn_from_montgomery_word(ret, t, mont);
7779df
+        bn_correct_top(ret);
7779df
+        bn_check_top(ret);
7779df
+    }
7779df
     BN_CTX_end(ctx);
7779df
 #else                           /* !MONT_WORD */
7779df
     BIGNUM *t1, *t2;
7779df
@@ -334,6 +310,12 @@ int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
7779df
     return (retn);
7779df
 }
7779df
 
7779df
+int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
7779df
+                         BN_CTX *ctx)
7779df
+{
7779df
+    return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);
7779df
+}
7779df
+
7779df
 BN_MONT_CTX *BN_MONT_CTX_new(void)
7779df
 {
7779df
     BN_MONT_CTX *ret;
7779df
@@ -370,7 +352,7 @@ void BN_MONT_CTX_free(BN_MONT_CTX *mont)
7779df
 
7779df
 int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
7779df
 {
7779df
-    int ret = 0;
7779df
+    int i, ret = 0;
7779df
     BIGNUM *Ri, *R;
7779df
 
7779df
     if (BN_is_zero(mod))
7779df
@@ -382,6 +364,8 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
7779df
     R = &(mont->RR);            /* grab RR as a temp */
7779df
     if (!BN_copy(&(mont->N), mod))
7779df
         goto err;               /* Set N */
7779df
+    if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
7779df
+        BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);
7779df
     mont->N.neg = 0;
7779df
 
7779df
 #ifdef MONT_WORD
7779df
@@ -394,6 +378,9 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
7779df
         tmod.dmax = 2;
7779df
         tmod.neg = 0;
7779df
 
7779df
+        if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
7779df
+            BN_set_flags(&tmod, BN_FLG_CONSTTIME);
7779df
+
7779df
         mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
7779df
 
7779df
 # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
7779df
@@ -496,6 +483,11 @@ int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
7779df
     if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
7779df
         goto err;
7779df
 
7779df
+    for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)
7779df
+        mont->RR.d[i] = 0;
7779df
+    mont->RR.top = ret;
7779df
+    mont->RR.flags |= BN_FLG_FIXED_TOP;
7779df
+
7779df
     ret = 1;
7779df
  err:
7779df
     BN_CTX_end(ctx);
7779df
diff --git a/crypto/bn/bn_sqr.c b/crypto/bn/bn_sqr.c
7779df
index 256d26e..5e69297 100644
7779df
--- a/crypto/bn/bn_sqr.c
7779df
+++ b/crypto/bn/bn_sqr.c
7779df
@@ -135,14 +135,8 @@ int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
7779df
     }
7779df
 
7779df
     rr->neg = 0;
7779df
-    /*
7779df
-     * If the most-significant half of the top word of 'a' is zero, then the
7779df
-     * square of 'a' will max-1 words.
7779df
-     */
7779df
-    if (a->d[al - 1] == (a->d[al - 1] & BN_MASK2l))
7779df
-        rr->top = max - 1;
7779df
-    else
7779df
-        rr->top = max;
7779df
+    rr->top = max;
7779df
+    bn_correct_top(rr);
7779df
     if (r != rr && BN_copy(r, rr) == NULL)
7779df
         goto err;
7779df
 
7779df
diff -up openssl-1.0.2k/crypto/bn_int.h.rohnp-fix openssl-1.0.2k/crypto/bn_int.h
7779df
--- openssl-1.0.2k/crypto/bn_int.h.rohnp-fix   2018-08-14 10:57:21.597518822 +0200
7779df
+++ openssl-1.0.2k/crypto/bn_int.h     2018-08-14 10:57:21.599518871 +0200
7779df
@@ -0,0 +1,13 @@
7779df
+/*
7779df
+ * Some BIGNUM functions assume most significant limb to be non-zero, which
7779df
+ * is customarily arranged by bn_correct_top. Output from below functions
7779df
+ * is not processed with bn_correct_top, and for this reason it may not be
7779df
+ * returned out of public API. It may only be passed internally into other
7779df
+ * functions known to support non-minimal or zero-padded BIGNUMs.
7779df
+ */
7779df
+int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
+                          BN_MONT_CTX *mont, BN_CTX *ctx);
7779df
+int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
7779df
+                         BN_CTX *ctx);
7779df
+int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
7779df
+                         const BIGNUM *m);
7779df
7779df
diff --git a/crypto/dsa/dsa_ossl.c b/crypto/dsa/dsa_ossl.c
7779df
index b5f67bd..894dff8 100644
7779df
--- a/crypto/dsa/dsa_ossl.c
7779df
+++ b/crypto/dsa/dsa_ossl.c
7779df
@@ -136,8 +136,7 @@ const DSA_METHOD *DSA_OpenSSL(void)
7779df
 static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
7779df
 {
7779df
     BIGNUM *kinv = NULL, *r = NULL, *s = NULL;
7779df
-    BIGNUM m;
7779df
-    BIGNUM xr;
7779df
+    BIGNUM *m, *blind, *blindm, *tmp;
7779df
     BN_CTX *ctx = NULL;
7779df
     int reason = ERR_R_BN_LIB;
7779df
     DSA_SIG *ret = NULL;
7779df
@@ -156,9 +155,6 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
7779df
     }
7779df
 #endif
7779df
 
7779df
-    BN_init(&m);
7779df
-    BN_init(&xr);
7779df
-
7779df
     if (!dsa->p || !dsa->q || !dsa->g) {
7779df
         reason = DSA_R_MISSING_PARAMETERS;
7779df
         goto err;
7779df
@@ -170,6 +166,14 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
7779df
     ctx = BN_CTX_new();
7779df
     if (ctx == NULL)
7779df
         goto err;
7779df
+    BN_CTX_start(ctx);
7779df
+    m = BN_CTX_get(ctx);
7779df
+    blind = BN_CTX_get(ctx);
7779df
+    blindm = BN_CTX_get(ctx);
7779df
+    tmp = BN_CTX_get(ctx);
7779df
+    if (tmp == NULL)
7779df
+        goto err;
7779df
+
7779df
  redo:
7779df
     if ((dsa->kinv == NULL) || (dsa->r == NULL)) {
7779df
         if (!DSA_sign_setup(dsa, ctx, &kinv, &r))
7779df
@@ -189,20 +193,52 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
7779df
          * 4.2
7779df
          */
7779df
         dlen = BN_num_bytes(dsa->q);
7779df
-    if (BN_bin2bn(dgst, dlen, &m) == NULL)
7779df
+    if (BN_bin2bn(dgst, dlen, m) == NULL)
7779df
         goto err;
7779df
 
7779df
-    /* Compute  s = inv(k) (m + xr) mod q */
7779df
-    if (!BN_mod_mul(&xr, dsa->priv_key, r, dsa->q, ctx))
7779df
-        goto err;               /* s = xr */
7779df
-    if (!BN_add(s, &xr, &m))
7779df
-        goto err;               /* s = m + xr */
7779df
-    if (BN_cmp(s, dsa->q) > 0)
7779df
-        if (!BN_sub(s, s, dsa->q))
7779df
+    /*
7779df
+     * The normal signature calculation is:
7779df
+     *
7779df
+     *   s := k^-1 * (m + r * priv_key) mod q
7779df
+     *
7779df
+     * We will blind this to protect against side channel attacks
7779df
+     *
7779df
+     *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod q
7779df
+     */
7779df
+
7779df
+    /* Generate a blinding value */
7779df
+    do {
7779df
+        if (!BN_rand(blind, BN_num_bits(dsa->q) - 1, -1, 0))
7779df
             goto err;
7779df
+    } while (BN_is_zero(blind));
7779df
+    BN_set_flags(blind, BN_FLG_CONSTTIME);
7779df
+    BN_set_flags(blindm, BN_FLG_CONSTTIME);
7779df
+    BN_set_flags(tmp, BN_FLG_CONSTTIME);
7779df
+
7779df
+    /* tmp := blind * priv_key * r mod q */
7779df
+    if (!BN_mod_mul(tmp, blind, dsa->priv_key, dsa->q, ctx))
7779df
+        goto err;
7779df
+    if (!BN_mod_mul(tmp, tmp, r, dsa->q, ctx))
7779df
+        goto err;
7779df
+
7779df
+    /* blindm := blind * m mod q */
7779df
+    if (!BN_mod_mul(blindm, blind, m, dsa->q, ctx))
7779df
+        goto err;
7779df
+
7779df
+    /* s : = (blind * priv_key * r) + (blind * m) mod q */
7779df
+    if (!BN_mod_add_quick(s, tmp, blindm, dsa->q))
7779df
+        goto err;
7779df
+
7779df
+    /* s := s * k^-1 mod q */
7779df
     if (!BN_mod_mul(s, s, kinv, dsa->q, ctx))
7779df
         goto err;
7779df
 
7779df
+    /* s:= s * blind^-1 mod q */
7779df
+    if (BN_mod_inverse(blind, blind, dsa->q, ctx) == NULL)
7779df
+        goto err;
7779df
+    if (!BN_mod_mul(s, s, blind, dsa->q, ctx))
7779df
+        goto err;
7779df
+
7779df
     /*
7779df
      * Redo if r or s is zero as required by FIPS 186-3: this is very
7779df
      * unlikely.
7779df
@@ -226,13 +262,12 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
7779df
         BN_free(r);
7779df
         BN_free(s);
7779df
     }
7779df
-    if (ctx != NULL)
7779df
+    if (ctx != NULL) {
7779df
+        BN_CTX_end(ctx);
7779df
         BN_CTX_free(ctx);
7779df
-    BN_clear_free(&m);
7779df
-    BN_clear_free(&xr);
7779df
-    if (kinv != NULL)           /* dsa->kinv is NULL now if we used it */
7779df
-        BN_clear_free(kinv);
7779df
-    return (ret);
7779df
+    }
7779df
+    BN_clear_free(kinv);
7779df
+    return ret;
7779df
 }
7779df
 
7779df
 static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, BIGNUM **kinvp,
7779df
diff --git a/crypto/ecdsa/ecs_ossl.c b/crypto/ecdsa/ecs_ossl.c
7779df
index dd76960..2b8f169 100644
7779df
--- a/crypto/ecdsa/ecs_ossl.c
7779df
+++ b/crypto/ecdsa/ecs_ossl.c
7779df
@@ -60,6 +60,7 @@
7779df
 #include <openssl/err.h>
7779df
 #include <openssl/obj_mac.h>
7779df
 #include <openssl/bn.h>
7779df
+#include "bn_int.h"
7779df
 
7779df
 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
7779df
                                 const BIGNUM *, const BIGNUM *,
7779df
@@ -95,6 +96,7 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
7779df
     EC_POINT *tmp_point = NULL;
7779df
     const EC_GROUP *group;
7779df
     int ret = 0;
7779df
+    int order_bits;
7779df
 
7779df
     if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
7779df
         ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
7779df
@@ -126,6 +128,13 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
7779df
         goto err;
7779df
     }
7779df
 
7779df
+    /* Preallocate space */
7779df
+    order_bits = BN_num_bits(order);
7779df
+    if (!BN_set_bit(k, order_bits)
7779df
+        || !BN_set_bit(r, order_bits)
7779df
+        || !BN_set_bit(X, order_bits))
7779df
+        goto err;
7779df
+
7779df
     do {
7779df
         /* get random k */
7779df
         do
7779df
@@ -139,13 +148,19 @@ static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
7779df
         /*
7779df
          * We do not want timing information to leak the length of k, so we
7779df
          * compute G*k using an equivalent scalar of fixed bit-length.
7779df
+         *
7779df
+         * We unconditionally perform both of these additions to prevent a
7779df
+         * small timing information leakage.  We then choose the sum that is
7779df
+         * one bit longer than the order.  This guarantees the code
7779df
+         * path used in the constant time implementations elsewhere.
7779df
+         *
7779df
+         * TODO: revisit the BN_copy aiming for a memory access agnostic
7779df
+         * conditional copy.
7779df
          */
7779df
-
7779df
-        if (!BN_add(k, k, order))
7779df
+        if (!BN_add(r, k, order)
7779df
+            || !BN_add(X, r, order)
7779df
+            || !BN_copy(k, BN_num_bits(r) > order_bits ? r : X))
7779df
             goto err;
7779df
-        if (BN_num_bits(k) <= BN_num_bits(order))
7779df
-            if (!BN_add(k, k, order))
7779df
-                goto err;
7779df
 
7779df
         /* compute r the x-coordinate of generator * k */
7779df
         if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
7779df
@@ -237,13 +252,14 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
7779df
                                 EC_KEY *eckey)
7779df
 {
7779df
     int ok = 0, i;
7779df
-    BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
7779df
+    BIGNUM *kinv = NULL, *s, *m = NULL, *order = NULL;
7779df
     const BIGNUM *ckinv;
7779df
     BN_CTX *ctx = NULL;
7779df
     const EC_GROUP *group;
7779df
     ECDSA_SIG *ret;
7779df
     ECDSA_DATA *ecdsa;
7779df
     const BIGNUM *priv_key;
7779df
+    BN_MONT_CTX *mont_data;
7779df
 
7779df
     ecdsa = ecdsa_check(eckey);
7779df
     group = EC_KEY_get0_group(eckey);
7779df
@@ -262,7 +278,7 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
7779df
     s = ret->s;
7779df
 
7779df
     if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
7779df
-        (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
7779df
+        (m = BN_new()) == NULL) {
7779df
         ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
7779df
         goto err;
7779df
     }
7779df
@@ -271,6 +287,8 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
7779df
         ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
7779df
         goto err;
7779df
     }
7779df
+    mont_data = EC_GROUP_get_mont_data(group);
7779df
+
7779df
     i = BN_num_bits(order);
7779df
     /*
7779df
      * Need to truncate digest if it is too long: first truncate whole bytes.
7779df
@@ -301,21 +319,33 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
7779df
             }
7779df
         }
7779df
 
7779df
-        if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
7779df
-            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
7779df
+        /*
7779df
+         * With only one multiplicant being in Montgomery domain
7779df
+         * multiplication yields real result without post-conversion.
7779df
+         * Also note that all operations but last are performed with
7779df
+         * zero-padded vectors. Last operation, BN_mod_mul_montgomery
7779df
+         * below, returns user-visible value with removed zero padding.
7779df
+         */
7779df
+        if (!bn_to_mont_fixed_top(s, ret->r, mont_data, ctx)
7779df
+            || !bn_mul_mont_fixed_top(s, s, priv_key, mont_data, ctx)) {
7779df
             goto err;
7779df
         }
7779df
-        if (!BN_mod_add_quick(s, tmp, m, order)) {
7779df
+        if (!bn_mod_add_fixed_top(s, s, m, order)) {
7779df
             ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
7779df
             goto err;
7779df
         }
7779df
-        if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
7779df
+        /*
7779df
+         * |s| can still be larger than modulus, because |m| can be. In
7779df
+         * such case we count on Montgomery reduction to tie it up.
7779df
+         */
7779df
+        if (!bn_to_mont_fixed_top(s, s, mont_data, ctx)
7779df
+            || !BN_mod_mul_montgomery(s, s, ckinv, mont_data, ctx)) {
7779df
             ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
7779df
             goto err;
7779df
         }
7779df
         if (BN_is_zero(s)) {
7779df
             /*
7779df
-             * if kinv and r have been supplied by the caller don't to
7779df
+             * if kinv and r have been supplied by the caller don't
7779df
              * generate new kinv and r values
7779df
              */
7779df
             if (in_kinv != NULL && in_r != NULL) {
7779df
@@ -339,8 +369,6 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
7779df
         BN_CTX_free(ctx);
7779df
     if (m)
7779df
         BN_clear_free(m);
7779df
-    if (tmp)
7779df
-        BN_clear_free(tmp);
7779df
     if (order)
7779df
         BN_free(order);
7779df
     if (kinv)