Blame SOURCES/0053-CVE-2022-0778.patch

bf760f
From 23f1773ddf92979006d0f438523f3c73320c384f Mon Sep 17 00:00:00 2001
bf760f
From: Tomas Mraz <tomas@openssl.org>
bf760f
Date: Mon, 28 Feb 2022 18:26:30 +0100
bf760f
Subject: [PATCH] Add documentation of BN_mod_sqrt()
bf760f
bf760f
---
bf760f
 doc/man3/BN_add.pod    | 15 +++++++++++++--
bf760f
 util/missingcrypto.txt |  1 -
bf760f
 2 files changed, 13 insertions(+), 3 deletions(-)
bf760f
bf760f
diff --git a/doc/man3/BN_add.pod b/doc/man3/BN_add.pod
bf760f
index 62d3ee7205..cf6c49c0e3 100644
bf760f
--- a/doc/man3/BN_add.pod
bf760f
+++ b/doc/man3/BN_add.pod
bf760f
@@ -3,7 +3,7 @@
bf760f
 =head1 NAME
bf760f
 
bf760f
 BN_add, BN_sub, BN_mul, BN_sqr, BN_div, BN_mod, BN_nnmod, BN_mod_add,
bf760f
-BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_exp, BN_mod_exp, BN_gcd -
bf760f
+BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_mod_sqrt, BN_exp, BN_mod_exp, BN_gcd -
bf760f
 arithmetic operations on BIGNUMs
bf760f
 
bf760f
 =head1 SYNOPSIS
bf760f
@@ -36,6 +36,8 @@ arithmetic operations on BIGNUMs
bf760f
 
bf760f
  int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
bf760f
 
bf760f
+ BIGNUM *BN_mod_sqrt(BIGNUM *in, BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
bf760f
+
bf760f
  int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
bf760f
 
bf760f
  int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
bf760f
@@ -87,6 +89,12 @@ L<BN_mod_mul_reciprocal(3)>.
bf760f
 BN_mod_sqr() takes the square of I modulo B<m> and places the
bf760f
 result in I<r>.
bf760f
 
bf760f
+BN_mod_sqrt() returns the modular square root of I such that
bf760f
+C<in^2 = a (mod p)>. The modulus I

must be a

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

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

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

is

bf760f
+not a prime), or NULL.
bf760f
+
bf760f
+For all remaining functions, 1 is returned for success, 0 on error. The return
bf760f
 value should always be checked (e.g., C<if (!BN_add(r,a,b)) goto err;>).
bf760f
 The error codes can be obtained by L<ERR_get_error(3)>.
bf760f
 
bf760f
diff --git a/util/missingcrypto.txt b/util/missingcrypto.txt
bf760f
index b61bdeb880..4d2fd7f6b7 100644
bf760f
--- a/util/missingcrypto.txt
bf760f
+++ b/util/missingcrypto.txt
bf760f
@@ -264,7 +264,6 @@ BN_mod_lshift(3)
bf760f
 BN_mod_lshift1(3)
bf760f
 BN_mod_lshift1_quick(3)
bf760f
 BN_mod_lshift_quick(3)
bf760f
-BN_mod_sqrt(3)
bf760f
 BN_mod_sub_quick(3)
bf760f
 BN_nist_mod_192(3)
bf760f
 BN_nist_mod_224(3)
bf760f
bf760f
From 46673310c9a755b2a56f53d115854983d6ada11a Mon Sep 17 00:00:00 2001
bf760f
From: Tomas Mraz <tomas@openssl.org>
bf760f
Date: Mon, 28 Feb 2022 18:26:35 +0100
bf760f
Subject: [PATCH] Add a negative testcase for BN_mod_sqrt
bf760f
bf760f
---
bf760f
 test/bntest.c                          | 11 ++++++++++-
bf760f
 test/recipes/10-test_bn_data/bnmod.txt | 12 ++++++++++++
bf760f
 2 files changed, 22 insertions(+), 1 deletion(-)
bf760f
bf760f
diff --git a/test/bntest.c b/test/bntest.c
bf760f
index efdb3ef963..d49f87373a 100644
bf760f
--- a/test/bntest.c
bf760f
+++ b/test/bntest.c
bf760f
@@ -1732,8 +1732,17 @@ static int file_modsqrt(STANZA *s)
bf760f
             || !TEST_ptr(ret2 = BN_new()))
bf760f
         goto err;
bf760f
 
bf760f
+    if (BN_is_negative(mod_sqrt)) {
bf760f
+        /* A negative testcase */
bf760f
+        if (!TEST_ptr_null(BN_mod_sqrt(ret, a, p, ctx)))
bf760f
+            goto err;
bf760f
+
bf760f
+        st = 1;
bf760f
+        goto err;
bf760f
+    }
bf760f
+
bf760f
     /* There are two possible answers. */
bf760f
-    if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
bf760f
+    if (!TEST_ptr(BN_mod_sqrt(ret, a, p, ctx))
bf760f
             || !TEST_true(BN_sub(ret2, p, ret)))
bf760f
         goto err;
bf760f
 
bf760f
diff --git a/test/recipes/10-test_bn_data/bnmod.txt b/test/recipes/10-test_bn_data/bnmod.txt
bf760f
index e22d656091..bc8a434ea5 100644
bf760f
--- a/test/recipes/10-test_bn_data/bnmod.txt
bf760f
+++ b/test/recipes/10-test_bn_data/bnmod.txt
bf760f
@@ -2799,3 +2799,15 @@ P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
bf760f
 ModSqrt = a1d52989f12f204d3d2167d9b1e6c8a6174c0c786a979a5952383b7b8bd186
bf760f
 A = 2eee37cf06228a387788188e650bc6d8a2ff402931443f69156a29155eca07dcb45f3aac238d92943c0c25c896098716baa433f25bd696a142f5a69d5d937e81
bf760f
 P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
bf760f
+
bf760f
+# Negative testcases for BN_mod_sqrt()
bf760f
+
bf760f
+# This one triggers an infinite loop with unfixed implementation
bf760f
+# It should just fail.
bf760f
+ModSqrt = -1
bf760f
+A = 20a7ee
bf760f
+P = 460201
bf760f
+
bf760f
+ModSqrt = -1
bf760f
+A = 65bebdb00a96fc814ec44b81f98b59fba3c30203928fa5214c51e0a97091645280c947b005847f239758482b9bfc45b066fde340d1fe32fc9c1bf02e1b2d0ed
bf760f
+P = 9df9d6cc20b8540411af4e5357ef2b0353cb1f2ab5ffc3e246b41c32f71e951f
bf760f
bf760f
From cafcc62d7719dea73f334c9ef763d1e215fcd94d Mon Sep 17 00:00:00 2001
bf760f
From: Tomas Mraz <tomas@openssl.org>
bf760f
Date: Mon, 28 Feb 2022 18:26:21 +0100
bf760f
Subject: [PATCH] Fix possible infinite loop in BN_mod_sqrt()
bf760f
bf760f
The calculation in some cases does not finish for non-prime p.
bf760f
bf760f
This fixes CVE-2022-0778.
bf760f
bf760f
Based on patch by David Benjamin <davidben@google.com>.
bf760f
---
bf760f
 crypto/bn/bn_sqrt.c | 30 ++++++++++++++++++------------
bf760f
 1 file changed, 18 insertions(+), 12 deletions(-)
bf760f
bf760f
diff --git a/crypto/bn/bn_sqrt.c b/crypto/bn/bn_sqrt.c
bf760f
index b663ae5ec5..c5ea7ab194 100644
bf760f
--- a/crypto/bn/bn_sqrt.c
bf760f
+++ b/crypto/bn/bn_sqrt.c
bf760f
@@ -14,7 +14,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
bf760f
 /*
bf760f
  * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
bf760f
  * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
bf760f
- * Theory", algorithm 1.5.1). 'p' must be prime!
bf760f
+ * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or
bf760f
+ * an incorrect "result" will be returned.
bf760f
  */
bf760f
 {
bf760f
     BIGNUM *ret = in;
bf760f
@@ -303,18 +304,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
bf760f
             goto vrfy;
bf760f
         }
bf760f
 
bf760f
-        /* find smallest  i  such that  b^(2^i) = 1 */
bf760f
-        i = 1;
bf760f
-        if (!BN_mod_sqr(t, b, p, ctx))
bf760f
-            goto end;
bf760f
-        while (!BN_is_one(t)) {
bf760f
-            i++;
bf760f
-            if (i == e) {
bf760f
-                ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
bf760f
-                goto end;
bf760f
+        /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
bf760f
+        for (i = 1; i < e; i++) {
bf760f
+            if (i == 1) {
bf760f
+                if (!BN_mod_sqr(t, b, p, ctx))
bf760f
+                    goto end;
bf760f
+
bf760f
+            } else {
bf760f
+                if (!BN_mod_mul(t, t, t, p, ctx))
bf760f
+                    goto end;
bf760f
             }
bf760f
-            if (!BN_mod_mul(t, t, t, p, ctx))
bf760f
-                goto end;
bf760f
+            if (BN_is_one(t))
bf760f
+                break;
bf760f
+        }
bf760f
+        /* If not found, a is not a square or p is not prime. */
bf760f
+        if (i >= e) {
bf760f
+            ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
bf760f
+            goto end;
bf760f
         }
bf760f
 
bf760f
         /* t := y^2^(e - i - 1) */
bf760f