aboutsummaryrefslogtreecommitdiff
path: root/crypto/bn/bn_sqrt.c
diff options
context:
space:
mode:
authorJung-uk Kim <jkim@FreeBSD.org>2022-03-15 22:18:15 +0000
committerJung-uk Kim <jkim@FreeBSD.org>2022-03-15 22:18:15 +0000
commitd6d3d400982465ee2c394caa850ba51c537b5a09 (patch)
treeb585b42a59bad0b1d2f9161d22da3b590692ea52 /crypto/bn/bn_sqrt.c
parent56eae1b760adf10835560a9ee595549a1f10410f (diff)
downloadsrc-d6d3d400982465ee2c394caa850ba51c537b5a09.tar.gz
src-d6d3d400982465ee2c394caa850ba51c537b5a09.zip
Import OpenSSL 1.1.1nvendor/openssl/1.1.1n
Diffstat (limited to 'crypto/bn/bn_sqrt.c')
-rw-r--r--crypto/bn/bn_sqrt.c32
1 files changed, 19 insertions, 13 deletions
diff --git a/crypto/bn/bn_sqrt.c b/crypto/bn/bn_sqrt.c
index 1723d5ded5a8..6a42ce8a9413 100644
--- a/crypto/bn/bn_sqrt.c
+++ b/crypto/bn/bn_sqrt.c
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -14,7 +14,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
* 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;
@@ -301,18 +302,23 @@ BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
goto vrfy;
}
- /* find smallest i such that b^(2^i) = 1 */
- i = 1;
- if (!BN_mod_sqr(t, b, p, ctx))
- goto end;
- while (!BN_is_one(t)) {
- i++;
- if (i == e) {
- BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
- goto end;
+ /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
+ for (i = 1; i < e; i++) {
+ if (i == 1) {
+ if (!BN_mod_sqr(t, b, p, ctx))
+ goto end;
+
+ } else {
+ if (!BN_mod_mul(t, t, t, p, ctx))
+ goto end;
}
- if (!BN_mod_mul(t, t, t, p, ctx))
- goto end;
+ if (BN_is_one(t))
+ 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) */