From 949ff36623eafc3523a9f91784992965018ffb05 Mon Sep 17 00:00:00 2001
From: Matt Caswell <matt@openssl.org>
Date: Fri, 25 May 2018 12:10:13 +0100
Subject: [PATCH] Add blinding to an ECDSA signature

Keegan Ryan (NCC Group) has demonstrated a side channel attack on an
ECDSA signature operation. During signing the signer calculates:

s:= k^-1 * (m + r * priv_key) mod order

The addition operation above provides a sufficient signal for a
flush+reload attack to derive the private key given sufficient signature
operations.

As a mitigation (based on a suggestion from Keegan) we add blinding to
the operation so that:

s := k^-1 * blind^-1 (blind * m + blind * r * priv_key) mod order

Since this attack is a localhost side channel only no CVE is assigned.

Reviewed-by: Rich Salz <rsalz@openssl.org>
---
 CHANGES                  |  4 ++
 crypto/ecdsa/ecdsatest.c |  9 ++++-
 crypto/ecdsa/ecs_ossl.c  | 82 ++++++++++++++++++++++++++++++++--------
 3 files changed, 79 insertions(+), 16 deletions(-)

Index: openssl-0.9.8j/crypto/ecdsa/ecdsatest.c
===================================================================
--- openssl-0.9.8j.orig/crypto/ecdsa/ecdsatest.c	2018-08-13 12:22:52.525216069 +0200
+++ openssl-0.9.8j/crypto/ecdsa/ecdsatest.c	2018-08-13 12:24:05.001676253 +0200
@@ -137,7 +137,7 @@ int restore_rand(void)
 		return 1;
 	}
 
-static int fbytes_counter = 0;
+static int fbytes_counter = 0, use_fake = 0;
 static const char *numbers[8] = {
 	"651056770906015076056810763456358567190100156695615665659",
 	"6140507067065001063065065565667405560006161556565665656654",
@@ -157,6 +157,11 @@ int fbytes(unsigned char *buf, int num)
 	int	ret;
 	BIGNUM	*tmp = NULL;
 
+    if (use_fake == 0)
+        return old_rand->bytes(buf, num);
+
+    use_fake = 0;
+
 	if (fbytes_counter >= 8)
 		return 0;
 	tmp = BN_new();
@@ -200,11 +205,13 @@ int x9_62_test_internal(BIO *out, int ni
 	/* create the key */
 	if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
 		goto x962_int_err;
+    use_fake = 1;
 	if (!EC_KEY_generate_key(key))
 		goto x962_int_err;
 	BIO_printf(out, ".");
 	(void)BIO_flush(out);
 	/* create the signature */
+    use_fake = 1;
 	signature = ECDSA_do_sign(digest, 20, key);
 	if (signature == NULL)
 		goto x962_int_err;
Index: openssl-0.9.8j/crypto/ecdsa/ecs_ossl.c
===================================================================
--- openssl-0.9.8j.orig/crypto/ecdsa/ecs_ossl.c	2018-08-13 12:23:01.033270090 +0200
+++ openssl-0.9.8j/crypto/ecdsa/ecs_ossl.c	2018-08-13 12:40:21.767908367 +0200
@@ -225,6 +225,7 @@ static ECDSA_SIG *ecdsa_do_sign(const un
 {
 	int     ok = 0;
 	BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
+    BIGNUM *blind = NULL, *blindm = NULL;
 	const BIGNUM *ckinv;
 	BN_CTX     *ctx = NULL;
 	const EC_GROUP   *group;
@@ -250,9 +251,19 @@ static ECDSA_SIG *ecdsa_do_sign(const un
 	}
 	s = ret->s;
 
-	if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
-		(tmp = BN_new()) == NULL || (m = BN_new()) == NULL)
-	{
+    ctx = BN_CTX_new();
+    if (ctx == NULL) {
+        ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
+        goto err;
+    }
+
+    BN_CTX_start(ctx);
+    order = BN_CTX_get(ctx);
+    tmp = BN_CTX_get(ctx);
+    m = BN_CTX_get(ctx);
+    blind = BN_CTX_get(ctx);
+    blindm = BN_CTX_get(ctx);
+    if (blindm == NULL) {
 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
 		goto err;
 	}
@@ -303,18 +314,59 @@ static ECDSA_SIG *ecdsa_do_sign(const un
 			}
 		}
 
-		if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
-		{
+        /*
+         * The normal signature calculation is:
+         *
+         *   s := k^-1 * (m + r * priv_key) mod order
+         *
+         * We will blind this to protect against side channel attacks
+         *
+         *   s := blind^-1 * k^-1 * (blind * m + blind * r * priv_key) mod order
+         */
+
+        /* Generate a blinding value */
+        do {
+            if (!BN_rand(blind, BN_num_bits(order) - 1, -1, 0))
+                goto err;
+        } while (BN_is_zero(blind));
+        BN_set_flags(blind, BN_FLG_CONSTTIME);
+        BN_set_flags(blindm, BN_FLG_CONSTTIME);
+        BN_set_flags(tmp, BN_FLG_CONSTTIME);
+
+        /* tmp := blind * priv_key * r mod order */
+        if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
+            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
+            goto err;
+        }
+        if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
+            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
+            goto err;
+        }
+
+        /* blindm := blind * m mod order */
+        if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
+            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
+            goto err;
+        }
+
+        /* s : = (blind * priv_key * r) + (blind * m) mod order */
+        if (!BN_mod_add_quick(s, tmp, blindm, order)) {
+            ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
+            goto err;
+        }
+
+        /* s := s * k^-1 mod order */
+        if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
 			goto err;
 		}
-		if (!BN_mod_add_quick(s, tmp, m, order))
-		{
+        /* s:= s * blind^-1 mod order */
+        if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
 			goto err;
 		}
-		if (!BN_mod_mul(s, s, ckinv, order, ctx))
-		{
+        /* s := s * k^-1 mod order */
+        if (!BN_mod_mul(s, s, blind, order, ctx)) {
 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
 			goto err;
 		}
@@ -341,15 +393,11 @@ err:
 		ECDSA_SIG_free(ret);
 		ret = NULL;
 	}
-	if (ctx)
+    if (ctx != NULL) {
+        BN_CTX_end(ctx);
 		BN_CTX_free(ctx);
-	if (m)
-		BN_clear_free(m);
-	if (tmp)
-		BN_clear_free(tmp);
-	if (order)
-		BN_free(order);
-	if (kinv)
+    }
+    if (kinv != NULL)
 		BN_clear_free(kinv);
 	return ret;
 }
