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

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

must be a

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

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

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

is

97bb94
+not a prime), or NULL.
97bb94
+
97bb94
+For all remaining functions, 1 is returned for success, 0 on error. The return
97bb94
 value should always be checked (e.g., C<if (!BN_add(r,a,b)) goto err;>).
97bb94
 The error codes can be obtained by L<ERR_get_error(3)>.
97bb94
 
97bb94
From 3ef5c3034e5c545f34d6929568f3f2b10ac4bdf0 Mon Sep 17 00:00:00 2001
97bb94
From: Tomas Mraz <tomas@openssl.org>
97bb94
Date: Mon, 28 Feb 2022 18:26:35 +0100
97bb94
Subject: [PATCH] Add a negative testcase for BN_mod_sqrt
97bb94
97bb94
Reviewed-by: Paul Dale <pauli@openssl.org>
97bb94
Reviewed-by: Matt Caswell <matt@openssl.org>
97bb94
---
97bb94
 test/bntest.c                          | 11 ++++++++++-
97bb94
 test/recipes/10-test_bn_data/bnmod.txt | 12 ++++++++++++
97bb94
 2 files changed, 22 insertions(+), 1 deletion(-)
97bb94
97bb94
diff --git a/test/bntest.c b/test/bntest.c
97bb94
index 390dd800733e..1cab660bcafb 100644
97bb94
--- a/test/bntest.c
97bb94
+++ b/test/bntest.c
97bb94
@@ -1729,8 +1729,17 @@ static int file_modsqrt(STANZA *s)
97bb94
             || !TEST_ptr(ret2 = BN_new()))
97bb94
         goto err;
97bb94
 
97bb94
+    if (BN_is_negative(mod_sqrt)) {
97bb94
+        /* A negative testcase */
97bb94
+        if (!TEST_ptr_null(BN_mod_sqrt(ret, a, p, ctx)))
97bb94
+            goto err;
97bb94
+
97bb94
+        st = 1;
97bb94
+        goto err;
97bb94
+    }
97bb94
+
97bb94
     /* There are two possible answers. */
97bb94
-    if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
97bb94
+    if (!TEST_ptr(BN_mod_sqrt(ret, a, p, ctx))
97bb94
             || !TEST_true(BN_sub(ret2, p, ret)))
97bb94
         goto err;
97bb94
 
97bb94
diff --git a/test/recipes/10-test_bn_data/bnmod.txt b/test/recipes/10-test_bn_data/bnmod.txt
97bb94
index 5ea4d031f271..e28cc6bfb02e 100644
97bb94
--- a/test/recipes/10-test_bn_data/bnmod.txt
97bb94
+++ b/test/recipes/10-test_bn_data/bnmod.txt
97bb94
@@ -2799,3 +2799,15 @@ P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
97bb94
 ModSqrt = a1d52989f12f204d3d2167d9b1e6c8a6174c0c786a979a5952383b7b8bd186
97bb94
 A = 2eee37cf06228a387788188e650bc6d8a2ff402931443f69156a29155eca07dcb45f3aac238d92943c0c25c896098716baa433f25bd696a142f5a69d5d937e81
97bb94
 P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
97bb94
+
97bb94
+# Negative testcases for BN_mod_sqrt()
97bb94
+
97bb94
+# This one triggers an infinite loop with unfixed implementation
97bb94
+# It should just fail.
97bb94
+ModSqrt = -1
97bb94
+A = 20a7ee
97bb94
+P = 460201
97bb94
+
97bb94
+ModSqrt = -1
97bb94
+A = 65bebdb00a96fc814ec44b81f98b59fba3c30203928fa5214c51e0a97091645280c947b005847f239758482b9bfc45b066fde340d1fe32fc9c1bf02e1b2d0ed
97bb94
+P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f