450916
diff -up openssl-1.0.2k/crypto/dsa/dsa_ossl.c.dsa-signing openssl-1.0.2k/crypto/dsa/dsa_ossl.c
450916
--- openssl-1.0.2k/crypto/dsa/dsa_ossl.c.dsa-signing	2019-02-08 10:53:17.825805336 +0100
450916
+++ openssl-1.0.2k/crypto/dsa/dsa_ossl.c	2019-04-04 16:05:53.155386419 +0200
450916
@@ -76,6 +76,8 @@ static int dsa_do_verify(const unsigned
450916
                          DSA_SIG *sig, DSA *dsa);
450916
 static int dsa_init(DSA *dsa);
450916
 static int dsa_finish(DSA *dsa);
450916
+static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
450916
+                                      BN_CTX *ctx);
450916
 
450916
 static DSA_METHOD openssl_dsa_meth = {
450916
     "OpenSSL DSA method",
450916
@@ -275,7 +277,9 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
 {
450916
     BN_CTX *ctx;
450916
     BIGNUM k, kq, *K, *kinv = NULL, *r = NULL;
450916
+    BIGNUM l, m;
450916
     int ret = 0;
450916
+    int q_bits;
450916
 
450916
     if (!dsa->p || !dsa->q || !dsa->g) {
450916
         DSAerr(DSA_F_DSA_SIGN_SETUP, DSA_R_MISSING_PARAMETERS);
450916
@@ -284,6 +288,8 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
 
450916
     BN_init(&k);
450916
     BN_init(&kq;;
450916
+    BN_init(&l);
450916
+    BN_init(&m);
450916
 
450916
     if (ctx_in == NULL) {
450916
         if ((ctx = BN_CTX_new()) == NULL)
450916
@@ -294,6 +300,13 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
     if ((r = BN_new()) == NULL)
450916
         goto err;
450916
 
450916
+    /* Preallocate space */
450916
+    q_bits = BN_num_bits(dsa->q) + sizeof(dsa->q->d[0]) * 16;
450916
+    if (!BN_set_bit(&k, q_bits)
450916
+        || !BN_set_bit(&l, q_bits)
450916
+        || !BN_set_bit(&m, q_bits))
450916
+        goto err;
450916
+
450916
     /* Get random k */
450916
     do
450916
         if (!BN_rand_range(&k, dsa->q))
450916
@@ -302,9 +315,9 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
 
450916
     if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {
450916
         BN_set_flags(&k, BN_FLG_CONSTTIME);
450916
+        BN_set_flags(&l, BN_FLG_CONSTTIME);
450916
     }
450916
 
450916
-
450916
     if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
450916
         if (!BN_MONT_CTX_set_locked(&dsa->method_mont_p,
450916
                                     CRYPTO_LOCK_DSA, dsa->p, ctx))
450916
@@ -314,24 +327,23 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
     /* Compute r = (g^k mod p) mod q */
450916
 
450916
     if ((dsa->flags & DSA_FLAG_NO_EXP_CONSTTIME) == 0) {
450916
-        if (!BN_copy(&kq, &k))
450916
-            goto err;
450916
-
450916
-        BN_set_flags(&kq, BN_FLG_CONSTTIME);
450916
-
450916
         /*
450916
          * We do not want timing information to leak the length of k, so we
450916
-         * compute g^k using an equivalent exponent of fixed length. (This
450916
-         * is a kludge that we need because the BN_mod_exp_mont() does not
450916
-         * let us specify the desired timing behaviour.)
450916
+         * compute G^k using an equivalent scalar of fixed bit-length.
450916
+         *
450916
+         * We unconditionally perform both of these additions to prevent a
450916
+         * small timing information leakage.  We then choose the sum that is
450916
+         * one bit longer than the modulus.
450916
+         *
450916
+         * TODO: revisit the BN_copy aiming for a memory access agnostic
450916
+         * conditional copy.
450916
          */
450916
-
450916
-        if (!BN_add(&kq, &kq, dsa->q))
450916
+        if (!BN_add(&l, &k, dsa->q)
450916
+            || !BN_add(&m, &l, dsa->q)
450916
+            || !BN_copy(&kq, BN_num_bits(&l) > q_bits ? &l : &m))
450916
             goto err;
450916
-        if (BN_num_bits(&kq) <= BN_num_bits(dsa->q)) {
450916
-            if (!BN_add(&kq, &kq, dsa->q))
450916
-                goto err;
450916
-        }
450916
+
450916
+        BN_set_flags(&kq, BN_FLG_CONSTTIME);
450916
 
450916
         K = &kq;
450916
     } else {
450916
@@ -343,8 +355,8 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
     if (!BN_mod(r, r, dsa->q, ctx))
450916
         goto err;
450916
 
450916
-    /* Compute  part of 's = inv(k) (m + xr) mod q' */
450916
-    if ((kinv = BN_mod_inverse(NULL, &k, dsa->q, ctx)) == NULL)
450916
+    /* Compute part of 's = inv(k) (m + xr) mod q' */
450916
+    if ((kinv = dsa_mod_inverse_fermat(&k, dsa->q, ctx)) == NULL)
450916
         goto err;
450916
 
450916
     if (*kinvp != NULL)
450916
@@ -365,7 +377,9 @@ static int dsa_sign_setup(DSA *dsa, BN_C
450916
         BN_CTX_free(ctx);
450916
     BN_clear_free(&k);
450916
     BN_clear_free(&kq;;
450916
-    return (ret);
450916
+    BN_clear_free(&l);
450916
+    BN_clear_free(&m);
450916
+    return ret;
450916
 }
450916
 
450916
 static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
450916
@@ -491,3 +505,31 @@ static int dsa_finish(DSA *dsa)
450916
         BN_MONT_CTX_free(dsa->method_mont_p);
450916
     return (1);
450916
 }
450916
+
450916
+/*
450916
+ * Compute the inverse of k modulo q.
450916
+ * Since q is prime, Fermat's Little Theorem applies, which reduces this to
450916
+ * mod-exp operation.  Both the exponent and modulus are public information
450916
+ * so a mod-exp that doesn't leak the base is sufficient.  A newly allocated
450916
+ * BIGNUM is returned which the caller must free.
450916
+ */
450916
+static BIGNUM *dsa_mod_inverse_fermat(const BIGNUM *k, const BIGNUM *q,
450916
+                                      BN_CTX *ctx)
450916
+{
450916
+    BIGNUM *res = NULL;
450916
+    BIGNUM *r, e;
450916
+
450916
+    if ((r = BN_new()) == NULL)
450916
+        return NULL;
450916
+
450916
+    BN_init(&e);
450916
+
450916
+    if (BN_set_word(r, 2)
450916
+            && BN_sub(&e, q, r)
450916
+            && BN_mod_exp_mont(r, k, &e, q, ctx, NULL))
450916
+        res = r;
450916
+    else
450916
+        BN_free(r);
450916
+    BN_free(&e);
450916
+    return res;
450916
+}