Blame SOURCES/openssl-1.0.2o-cve-2022-0778.patch

446cd6
From 3118eb64934499d93db3230748a452351d1d9a65 Mon Sep 17 00:00:00 2001
446cd6
From: Tomas Mraz <tomas@openssl.org>
446cd6
Date: Mon, 28 Feb 2022 18:26:21 +0100
446cd6
Subject: [PATCH] Fix possible infinite loop in BN_mod_sqrt()
446cd6
446cd6
The calculation in some cases does not finish for non-prime p.
446cd6
446cd6
This fixes CVE-2022-0778.
446cd6
446cd6
Based on patch by David Benjamin <davidben@google.com>.
446cd6
446cd6
Reviewed-by: Paul Dale <pauli@openssl.org>
446cd6
Reviewed-by: Matt Caswell <matt@openssl.org>
446cd6
---
446cd6
 crypto/bn/bn_sqrt.c | 30 ++++++++++++++++++------------
446cd6
 1 file changed, 18 insertions(+), 12 deletions(-)
446cd6
446cd6
diff --git a/crypto/bn/bn_sqrt.c b/crypto/bn/bn_sqrt.c
446cd6
index 1723d5ded5a8..53b0f559855c 100644
446cd6
--- a/crypto/bn/bn_sqrt.c
446cd6
+++ b/crypto/bn/bn_sqrt.c
446cd6
@@ -14,7 +14,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
446cd6
 /*
446cd6
  * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
446cd6
  * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
446cd6
- * Theory", algorithm 1.5.1). 'p' must be prime!
446cd6
+ * Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or
446cd6
+ * an incorrect "result" will be returned.
446cd6
  */
446cd6
 {
446cd6
     BIGNUM *ret = in;
446cd6
@@ -301,18 +302,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
446cd6
             goto vrfy;
446cd6
         }
446cd6
 
446cd6
-        /* find smallest  i  such that  b^(2^i) = 1 */
446cd6
-        i = 1;
446cd6
-        if (!BN_mod_sqr(t, b, p, ctx))
446cd6
-            goto end;
446cd6
-        while (!BN_is_one(t)) {
446cd6
-            i++;
446cd6
-            if (i == e) {
446cd6
-                BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
446cd6
-                goto end;
446cd6
+        /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
446cd6
+        for (i = 1; i < e; i++) {
446cd6
+            if (i == 1) {
446cd6
+                if (!BN_mod_sqr(t, b, p, ctx))
446cd6
+                    goto end;
446cd6
+
446cd6
+            } else {
446cd6
+                if (!BN_mod_mul(t, t, t, p, ctx))
446cd6
+                    goto end;
446cd6
             }
446cd6
-            if (!BN_mod_mul(t, t, t, p, ctx))
446cd6
-                goto end;
446cd6
+            if (BN_is_one(t))
446cd6
+                break;
446cd6
+        }
446cd6
+        /* If not found, a is not a square or p is not prime. */
446cd6
+        if (i >= e) {
446cd6
+            BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
446cd6
+            goto end;
446cd6
         }
446cd6
 
446cd6
         /* t := y^2^(e - i - 1) */