mirror of
https://github.com/coolsnowwolf/lede.git
synced 2025-04-15 18:03:30 +00:00
mt7603e: fix possible infinite loop in BN_mod_sqrt() (#13368)
The calculation in some cases does not finish for non-prime p. This fixes CVE-2022-0778. Based on patch by David Benjamin <davidben@google.com>. Reviewed-by: Paul Dale <pauli@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org>
This commit is contained in:
parent
84884dd637
commit
8a984fc1dd
@ -6555,7 +6555,8 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
|||||||
/*
|
/*
|
||||||
* Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
|
* Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
|
||||||
* algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
|
* algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
|
||||||
* Theory", algorithm 1.5.1). 'p' must be prime!
|
* Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or
|
||||||
|
* an incorrect "result" will be returned.
|
||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
BIGNUM *ret = in;
|
BIGNUM *ret = in;
|
||||||
@ -6871,22 +6872,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
|
|||||||
goto vrfy;
|
goto vrfy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* find smallest i such that b^(2^i) = 1 */
|
/* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
|
||||||
i = 1;
|
for (i = 1; i < e; i++) {
|
||||||
|
if (i == 1) {
|
||||||
if (!BN_mod_sqr(t, b, p, ctx))
|
if (!BN_mod_sqr(t, b, p, ctx))
|
||||||
goto end;
|
goto end;
|
||||||
|
} else {
|
||||||
while (!BN_is_one(t)) {
|
if (!BN_mod_mul(t, t, t, p, ctx))
|
||||||
i++;
|
goto end;
|
||||||
|
|
||||||
if (i == e) {
|
|
||||||
BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!BN_mod_mul(t, t, t, p, ctx))
|
if (BN_is_one(t))
|
||||||
goto end;
|
break;
|
||||||
|
}
|
||||||
|
/* If not found, a is not a square or p is not prime. */
|
||||||
|
if (i >= e) {
|
||||||
|
BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* t := y^2^(e - i - 1) */
|
/* t := y^2^(e - i - 1) */
|
||||||
|
Loading…
Reference in New Issue
Block a user