Blame SOURCES/openssl-1.1.1-cve-2022-0778.patch

dca3ee
From 3118eb64934499d93db3230748a452351d1d9a65 Mon Sep 17 00:00:00 2001
dca3ee
From: Tomas Mraz <tomas@openssl.org>
dca3ee
Date: Mon, 28 Feb 2022 18:26:21 +0100
dca3ee
Subject: [PATCH] Fix possible infinite loop in BN_mod_sqrt()
dca3ee
dca3ee
The calculation in some cases does not finish for non-prime p.
dca3ee
dca3ee
This fixes CVE-2022-0778.
dca3ee
dca3ee
Based on patch by David Benjamin <davidben@google.com>.
dca3ee
dca3ee
Reviewed-by: Paul Dale <pauli@openssl.org>
dca3ee
Reviewed-by: Matt Caswell <matt@openssl.org>
dca3ee
---
dca3ee
 crypto/bn/bn_sqrt.c | 30 ++++++++++++++++++------------
dca3ee
 1 file changed, 18 insertions(+), 12 deletions(-)
dca3ee
dca3ee
From b5fcb7e133725b8b2eb66f63f5142710ed63a6d1 Mon Sep 17 00:00:00 2001
dca3ee
From: Tomas Mraz <tomas@openssl.org>
dca3ee
Date: Mon, 28 Feb 2022 18:26:30 +0100
dca3ee
Subject: [PATCH] Add documentation of BN_mod_sqrt()
dca3ee
dca3ee
Reviewed-by: Paul Dale <pauli@openssl.org>
dca3ee
Reviewed-by: Matt Caswell <matt@openssl.org>
dca3ee
---
dca3ee
 doc/man3/BN_add.pod | 15 +++++++++++++--
dca3ee
 1 file changed, 13 insertions(+), 2 deletions(-)
dca3ee
dca3ee
From 3ef5c3034e5c545f34d6929568f3f2b10ac4bdf0 Mon Sep 17 00:00:00 2001
dca3ee
From: Tomas Mraz <tomas@openssl.org>
dca3ee
Date: Mon, 28 Feb 2022 18:26:35 +0100
dca3ee
Subject: [PATCH] Add a negative testcase for BN_mod_sqrt
dca3ee
dca3ee
Reviewed-by: Paul Dale <pauli@openssl.org>
dca3ee
Reviewed-by: Matt Caswell <matt@openssl.org>
dca3ee
---
dca3ee
 test/bntest.c                          | 11 ++++++++++-
dca3ee
 test/recipes/10-test_bn_data/bnmod.txt | 12 ++++++++++++
dca3ee
 2 files changed, 22 insertions(+), 1 deletion(-)
dca3ee
dca3ee
diff --git a/crypto/bn/bn_sqrt.c b/crypto/bn/bn_sqrt.c
dca3ee
index 1723d5ded5a8..53b0f559855c 100644
dca3ee
--- a/crypto/bn/bn_sqrt.c
dca3ee
+++ b/crypto/bn/bn_sqrt.c
dca3ee
@@ -14,7 +14,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
dca3ee
 /*
dca3ee
  * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
dca3ee
  * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
dca3ee
- * Theory", algorithm 1.5.1). 'p' must be prime!
dca3ee
+ * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or
dca3ee
+ * an incorrect "result" will be returned.
dca3ee
  */
dca3ee
 {
dca3ee
     BIGNUM *ret = in;
dca3ee
@@ -301,18 +302,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
dca3ee
             goto vrfy;
dca3ee
         }
dca3ee
 
dca3ee
-        /* find smallest  i  such that  b^(2^i) = 1 */
dca3ee
-        i = 1;
dca3ee
-        if (!BN_mod_sqr(t, b, p, ctx))
dca3ee
-            goto end;
dca3ee
-        while (!BN_is_one(t)) {
dca3ee
-            i++;
dca3ee
-            if (i == e) {
dca3ee
-                BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
dca3ee
-                goto end;
dca3ee
+        /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
dca3ee
+        for (i = 1; i < e; i++) {
dca3ee
+            if (i == 1) {
dca3ee
+                if (!BN_mod_sqr(t, b, p, ctx))
dca3ee
+                    goto end;
dca3ee
+
dca3ee
+            } else {
dca3ee
+                if (!BN_mod_mul(t, t, t, p, ctx))
dca3ee
+                    goto end;
dca3ee
             }
dca3ee
-            if (!BN_mod_mul(t, t, t, p, ctx))
dca3ee
-                goto end;
dca3ee
+            if (BN_is_one(t))
dca3ee
+                break;
dca3ee
+        }
dca3ee
+        /* If not found, a is not a square or p is not prime. */
dca3ee
+        if (i >= e) {
dca3ee
+            BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
dca3ee
+            goto end;
dca3ee
         }
dca3ee
 
dca3ee
         /* t := y^2^(e - i - 1) */
dca3ee
diff --git a/doc/man3/BN_add.pod b/doc/man3/BN_add.pod
dca3ee
index dccd4790ede7..1f5e37a4d183 100644
dca3ee
--- a/doc/man3/BN_add.pod
dca3ee
+++ b/doc/man3/BN_add.pod
dca3ee
@@ -3,7 +3,7 @@
dca3ee
 =head1 NAME
dca3ee
 
dca3ee
 BN_add, BN_sub, BN_mul, BN_sqr, BN_div, BN_mod, BN_nnmod, BN_mod_add,
dca3ee
-BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_exp, BN_mod_exp, BN_gcd -
dca3ee
+BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_mod_sqrt, BN_exp, BN_mod_exp, BN_gcd -
dca3ee
 arithmetic operations on BIGNUMs
dca3ee
 
dca3ee
 =head1 SYNOPSIS
dca3ee
@@ -36,6 +36,8 @@ arithmetic operations on BIGNUMs
dca3ee
 
dca3ee
  int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
dca3ee
 
dca3ee
+ BIGNUM *BN_mod_sqrt(BIGNUM *in, BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
dca3ee
+
dca3ee
  int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
dca3ee
 
dca3ee
  int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
dca3ee
@@ -87,6 +89,12 @@ L<BN_mod_mul_reciprocal(3)>.
dca3ee
 BN_mod_sqr() takes the square of I modulo B<m> and places the
dca3ee
 result in I<r>.
dca3ee
 
dca3ee
+BN_mod_sqrt() returns the modular square root of I such that
dca3ee
+C<in^2 = a (mod p)>. The modulus I

must be a

dca3ee
+prime, otherwise an error or an incorrect "result" will be returned.
dca3ee
+The result is stored into I<in> which can be NULL. The result will be
dca3ee
+newly allocated in that case.
dca3ee
+
dca3ee
 BN_exp() raises I to the I

-th power and places the result in I<r>

dca3ee
 (C<r=a^p>). This function is faster than repeated applications of
dca3ee
 BN_mul().
dca3ee
@@ -108,7 +116,10 @@ the arguments.
dca3ee
 
dca3ee
 =head1 RETURN VALUES
dca3ee
 
dca3ee
-For all functions, 1 is returned for success, 0 on error. The return
dca3ee
+The BN_mod_sqrt() returns the result (possibly incorrect if I

is

dca3ee
+not a prime), or NULL.
dca3ee
+
dca3ee
+For all remaining functions, 1 is returned for success, 0 on error. The return
dca3ee
 value should always be checked (e.g., C<if (!BN_add(r,a,b)) goto err;>).
dca3ee
 The error codes can be obtained by L<ERR_get_error(3)>.
dca3ee
 
dca3ee
diff --git a/test/bntest.c b/test/bntest.c
dca3ee
index 390dd800733e..1cab660bcafb 100644
dca3ee
--- a/test/bntest.c
dca3ee
+++ b/test/bntest.c
dca3ee
@@ -1729,8 +1729,17 @@ static int file_modsqrt(STANZA *s)
dca3ee
             || !TEST_ptr(ret2 = BN_new()))
dca3ee
         goto err;
dca3ee
 
dca3ee
+    if (BN_is_negative(mod_sqrt)) {
dca3ee
+        /* A negative testcase */
dca3ee
+        if (!TEST_ptr_null(BN_mod_sqrt(ret, a, p, ctx)))
dca3ee
+            goto err;
dca3ee
+
dca3ee
+        st = 1;
dca3ee
+        goto err;
dca3ee
+    }
dca3ee
+
dca3ee
     /* There are two possible answers. */
dca3ee
-    if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
dca3ee
+    if (!TEST_ptr(BN_mod_sqrt(ret, a, p, ctx))
dca3ee
             || !TEST_true(BN_sub(ret2, p, ret)))
dca3ee
         goto err;
dca3ee
 
dca3ee
diff --git a/test/recipes/10-test_bn_data/bnmod.txt b/test/recipes/10-test_bn_data/bnmod.txt
dca3ee
index 5ea4d031f271..e28cc6bfb02e 100644
dca3ee
--- a/test/recipes/10-test_bn_data/bnmod.txt
dca3ee
+++ b/test/recipes/10-test_bn_data/bnmod.txt
dca3ee
@@ -2799,3 +2799,15 @@ P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
dca3ee
 ModSqrt = a1d52989f12f204d3d2167d9b1e6c8a6174c0c786a979a5952383b7b8bd186
dca3ee
 A = 2eee37cf06228a387788188e650bc6d8a2ff402931443f69156a29155eca07dcb45f3aac238d92943c0c25c896098716baa433f25bd696a142f5a69d5d937e81
dca3ee
 P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
dca3ee
+
dca3ee
+# Negative testcases for BN_mod_sqrt()
dca3ee
+
dca3ee
+# This one triggers an infinite loop with unfixed implementation
dca3ee
+# It should just fail.
dca3ee
+ModSqrt = -1
dca3ee
+A = 20a7ee
dca3ee
+P = 460201
dca3ee
+
dca3ee
+ModSqrt = -1
dca3ee
+A = 65bebdb00a96fc814ec44b81f98b59fba3c30203928fa5214c51e0a97091645280c947b005847f239758482b9bfc45b066fde340d1fe32fc9c1bf02e1b2d0ed
dca3ee
+P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f