From cf8a68efe95fbdbaf9fb2b5d421639c6f404ea32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Tue, 14 Nov 2023 20:40:29 +0100
Subject: [PATCH] Fixes for ecdsa-sign-test in noasm and mini-gmp builds.

---
 ChangeLog                   | 8 ++++++--
 ecc-pm1-redc.c              | 2 +-
 ecc-pp1-redc.c              | 2 +-
 ecc-secp192r1.c             | 6 +++---
 ecc-secp384r1.c             | 6 +++---
 testsuite/ecdsa-sign-test.c | 2 +-
 6 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 27655667..e65ddf78 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,10 +10,14 @@
 	* ecc-mod-arith.c: Convert most asserts to assert_maybe.
 	* ecc-mod-inv.c (ecc_mod_inv): Likewise.
 	* ecc-mod.c (ecc_mod): Likewise.
+	* ecc-pm1-redc.c (ecc_pm1_redc): Likewise.
+	* ecc-pp1-redc.c (ecc_pp1_redc): Likewise.
+	* ecc-secp192r1.c (ecc_secp192r1_modp): Likewise.
+	* ecc-secp384r1.c (ecc_secp384r1_modp): Likewise.
 	* testsuite/ecdsa-sign-test.c (test_ecdsa): Add calls to
 	mark_bytes_undefined and mark_bytes_defined.
-	(test_main): Skip test if both side channel tests and extra
-	asserts are enabled.
+	(test_main): Skip side-channel tests in builds with mini-gmp or
+	extra asserts enabled.
 	* testsuite/sc-ecdsa-sign-test: New testcase.
 	* testsuite/Makefile.in (TS_SC): Add sc-ecdsa-sign-test.
 
diff --git a/ecc-pm1-redc.c b/ecc-pm1-redc.c
index cc95c6e4..0f728a37 100644
--- a/ecc-pm1-redc.c
+++ b/ecc-pm1-redc.c
@@ -54,7 +54,7 @@ ecc_pm1_redc (const struct ecc_modulo *m, mp_limb_t *rp, mp_limb_t *xp)
 			  m->redc_mpm1, m->size - k, xp[i]);
   hi = mpn_sub_n (xp, xp + m->size, xp, m->size);
   cy = mpn_cnd_add_n (hi, rp, xp, m->m, m->size);
-  assert (cy == hi);
+  assert_maybe (cy == hi);
 
   if (shift > 0)
     {
diff --git a/ecc-pp1-redc.c b/ecc-pp1-redc.c
index b088c4c5..c4722041 100644
--- a/ecc-pp1-redc.c
+++ b/ecc-pp1-redc.c
@@ -64,6 +64,6 @@ ecc_pp1_redc (const struct ecc_modulo *m, mp_limb_t *rp, mp_limb_t *xp)
   else
     {
       cy = mpn_cnd_sub_n (hi, rp, rp, m->m, m->size);
-      assert (cy == hi);      
+      assert_maybe (cy == hi);
     }
 }
diff --git a/ecc-secp192r1.c b/ecc-secp192r1.c
index 4a07bca3..6097622b 100644
--- a/ecc-secp192r1.c
+++ b/ecc-secp192r1.c
@@ -90,7 +90,7 @@ ecc_secp192r1_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp, mp_limb_t
   cy = mpn_add_n (xp + 1, xp + 1, xp + 4, 2);
   cy = sec_add_1 (xp + 3, xp + 3, 1, cy);
   cy += mpn_add_n (xp + 2, xp + 2, xp + 4, 2);
-  assert (cy <= 2);
+  assert_maybe (cy <= 2);
 
   xp[4] = cy;
 
@@ -99,9 +99,9 @@ ecc_secp192r1_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp, mp_limb_t
   cy = sec_add_1 (xp + 2, xp + 2, 1, cy);
   cy += mpn_add_n (xp + 1, xp + 1, xp + 3, 2);
 
-  assert (cy <= 1);
+  assert_maybe (cy <= 1);
   cy = mpn_cnd_add_n (cy, rp, xp, ecc_Bmodp, 3);
-  assert (cy == 0);  
+  assert_maybe (cy == 0);
 }
   
 #else
diff --git a/ecc-secp384r1.c b/ecc-secp384r1.c
index abac5e6d..bf5f402f 100644
--- a/ecc-secp384r1.c
+++ b/ecc-secp384r1.c
@@ -122,7 +122,7 @@ ecc_secp384r1_modp (const struct ecc_modulo *p, mp_limb_t *rp, mp_limb_t *xp)
   cy += mpn_add_n (xp + 2, xp + 2, tp, 6);
   cy += mpn_add_n (xp + 4, xp + 4, xp + 8, 4);
 
-  assert (cy <= 2);
+  assert_maybe (cy <= 2);
   xp[8] = cy;
 
   /* Reduce from 9 to 6 limbs */
@@ -137,10 +137,10 @@ ecc_secp384r1_modp (const struct ecc_modulo *p, mp_limb_t *rp, mp_limb_t *xp)
   cy += mpn_add_n (xp + 2, xp + 2, xp + 6, 3);
 
   cy = sec_add_1 (xp + 5, xp + 5, 1, cy);
-  assert (cy <= 1);
+  assert_maybe (cy <= 1);
 
   cy = mpn_cnd_add_n (cy, xp, xp, p->B, ECC_LIMB_SIZE);
-  assert (cy == 0);
+  assert_maybe (cy == 0);
   mpn_copyi (rp, xp, ECC_LIMB_SIZE);
 }
 #else
diff --git a/testsuite/ecdsa-sign-test.c b/testsuite/ecdsa-sign-test.c
index a3c43568..46fc2738 100644
--- a/testsuite/ecdsa-sign-test.c
+++ b/testsuite/ecdsa-sign-test.c
@@ -70,7 +70,7 @@ test_ecdsa (const struct ecc_curve *ecc,
 void
 test_main (void)
 {
-#if WITH_EXTRA_ASSERTS
+#if NETTLE_USE_MINI_GMP || WITH_EXTRA_ASSERTS
   if (test_side_channel)
     SKIP();
 #endif
-- 
GitLab