| 1 |
/* |
| 2 |
* Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "internal/cryptlib.h" |
| 11 |
#include "bn_local.h" |
| 12 |
|
| 13 |
BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) |
| 14 |
/* |
| 15 |
* Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks |
| 16 |
* algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number |
| 17 |
* Theory", algorithm 1.5.1). 'p' must be prime, otherwise an error or |
| 18 |
* an incorrect "result" will be returned. |
| 19 |
*/ |
| 20 |
{ |
| 21 |
BIGNUM *ret = in; |
| 22 |
int err = 1; |
| 23 |
int r; |
| 24 |
BIGNUM *A, *b, *q, *t, *x, *y; |
| 25 |
int e, i, j; |
| 26 |
int used_ctx = 0; |
| 27 |
|
| 28 |
if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { |
| 29 |
if (BN_abs_is_word(p, 2)) { |
| 30 |
if (ret == NULL) |
| 31 |
ret = BN_new(); |
| 32 |
if (ret == NULL) |
| 33 |
goto end; |
| 34 |
if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { |
| 35 |
if (ret != in) |
| 36 |
BN_free(ret); |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
bn_check_top(ret); |
| 40 |
return ret; |
| 41 |
} |
| 42 |
|
| 43 |
ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); |
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
if (BN_is_zero(a) || BN_is_one(a)) { |
| 48 |
if (ret == NULL) |
| 49 |
ret = BN_new(); |
| 50 |
if (ret == NULL) |
| 51 |
goto end; |
| 52 |
if (!BN_set_word(ret, BN_is_one(a))) { |
| 53 |
if (ret != in) |
| 54 |
BN_free(ret); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
bn_check_top(ret); |
| 58 |
return ret; |
| 59 |
} |
| 60 |
|
| 61 |
BN_CTX_start(ctx); |
| 62 |
used_ctx = 1; |
| 63 |
A = BN_CTX_get(ctx); |
| 64 |
b = BN_CTX_get(ctx); |
| 65 |
q = BN_CTX_get(ctx); |
| 66 |
t = BN_CTX_get(ctx); |
| 67 |
x = BN_CTX_get(ctx); |
| 68 |
y = BN_CTX_get(ctx); |
| 69 |
if (y == NULL) |
| 70 |
goto end; |
| 71 |
|
| 72 |
if (ret == NULL) |
| 73 |
ret = BN_new(); |
| 74 |
if (ret == NULL) |
| 75 |
goto end; |
| 76 |
|
| 77 |
/* A = a mod p */ |
| 78 |
if (!BN_nnmod(A, a, p, ctx)) |
| 79 |
goto end; |
| 80 |
|
| 81 |
/* now write |p| - 1 as 2^e*q where q is odd */ |
| 82 |
e = 1; |
| 83 |
while (!BN_is_bit_set(p, e)) |
| 84 |
e++; |
| 85 |
/* we'll set q later (if needed) */ |
| 86 |
|
| 87 |
if (e == 1) { |
| 88 |
/*- |
| 89 |
* The easy case: (|p|-1)/2 is odd, so 2 has an inverse |
| 90 |
* modulo (|p|-1)/2, and square roots can be computed |
| 91 |
* directly by modular exponentiation. |
| 92 |
* We have |
| 93 |
* 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2), |
| 94 |
* so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1. |
| 95 |
*/ |
| 96 |
if (!BN_rshift(q, p, 2)) |
| 97 |
goto end; |
| 98 |
q->neg = 0; |
| 99 |
if (!BN_add_word(q, 1)) |
| 100 |
goto end; |
| 101 |
if (!BN_mod_exp(ret, A, q, p, ctx)) |
| 102 |
goto end; |
| 103 |
err = 0; |
| 104 |
goto vrfy; |
| 105 |
} |
| 106 |
|
| 107 |
if (e == 2) { |
| 108 |
/*- |
| 109 |
* |p| == 5 (mod 8) |
| 110 |
* |
| 111 |
* In this case 2 is always a non-square since |
| 112 |
* Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. |
| 113 |
* So if a really is a square, then 2*a is a non-square. |
| 114 |
* Thus for |
| 115 |
* b := (2*a)^((|p|-5)/8), |
| 116 |
* i := (2*a)*b^2 |
| 117 |
* we have |
| 118 |
* i^2 = (2*a)^((1 + (|p|-5)/4)*2) |
| 119 |
* = (2*a)^((p-1)/2) |
| 120 |
* = -1; |
| 121 |
* so if we set |
| 122 |
* x := a*b*(i-1), |
| 123 |
* then |
| 124 |
* x^2 = a^2 * b^2 * (i^2 - 2*i + 1) |
| 125 |
* = a^2 * b^2 * (-2*i) |
| 126 |
* = a*(-i)*(2*a*b^2) |
| 127 |
* = a*(-i)*i |
| 128 |
* = a. |
| 129 |
* |
| 130 |
* (This is due to A.O.L. Atkin, |
| 131 |
* Subject: Square Roots and Cognate Matters modulo p=8n+5. |
| 132 |
* URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026 |
| 133 |
* November 1992.) |
| 134 |
*/ |
| 135 |
|
| 136 |
/* t := 2*a */ |
| 137 |
if (!BN_mod_lshift1_quick(t, A, p)) |
| 138 |
goto end; |
| 139 |
|
| 140 |
/* b := (2*a)^((|p|-5)/8) */ |
| 141 |
if (!BN_rshift(q, p, 3)) |
| 142 |
goto end; |
| 143 |
q->neg = 0; |
| 144 |
if (!BN_mod_exp(b, t, q, p, ctx)) |
| 145 |
goto end; |
| 146 |
|
| 147 |
/* y := b^2 */ |
| 148 |
if (!BN_mod_sqr(y, b, p, ctx)) |
| 149 |
goto end; |
| 150 |
|
| 151 |
/* t := (2*a)*b^2 - 1 */ |
| 152 |
if (!BN_mod_mul(t, t, y, p, ctx)) |
| 153 |
goto end; |
| 154 |
if (!BN_sub_word(t, 1)) |
| 155 |
goto end; |
| 156 |
|
| 157 |
/* x = a*b*t */ |
| 158 |
if (!BN_mod_mul(x, A, b, p, ctx)) |
| 159 |
goto end; |
| 160 |
if (!BN_mod_mul(x, x, t, p, ctx)) |
| 161 |
goto end; |
| 162 |
|
| 163 |
if (!BN_copy(ret, x)) |
| 164 |
goto end; |
| 165 |
err = 0; |
| 166 |
goto vrfy; |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* e > 2, so we really have to use the Tonelli/Shanks algorithm. First, |
| 171 |
* find some y that is not a square. |
| 172 |
*/ |
| 173 |
if (!BN_copy(q, p)) |
| 174 |
goto end; /* use 'q' as temp */ |
| 175 |
q->neg = 0; |
| 176 |
i = 2; |
| 177 |
do { |
| 178 |
/* |
| 179 |
* For efficiency, try small numbers first; if this fails, try random |
| 180 |
* numbers. |
| 181 |
*/ |
| 182 |
if (i < 22) { |
| 183 |
if (!BN_set_word(y, i)) |
| 184 |
goto end; |
| 185 |
} else { |
| 186 |
if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx)) |
| 187 |
goto end; |
| 188 |
if (BN_ucmp(y, p) >= 0) { |
| 189 |
if (!(p->neg ? BN_add : BN_sub)(y, y, p)) |
| 190 |
goto end; |
| 191 |
} |
| 192 |
/* now 0 <= y < |p| */ |
| 193 |
if (BN_is_zero(y)) |
| 194 |
if (!BN_set_word(y, i)) |
| 195 |
goto end; |
| 196 |
} |
| 197 |
|
| 198 |
r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ |
| 199 |
if (r < -1) |
| 200 |
goto end; |
| 201 |
if (r == 0) { |
| 202 |
/* m divides p */ |
| 203 |
ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); |
| 204 |
goto end; |
| 205 |
} |
| 206 |
} while (r == 1 && ++i < 82); |
| 207 |
|
| 208 |
if (r != -1) { |
| 209 |
/* |
| 210 |
* Many rounds and still no non-square -- this is more likely a bug |
| 211 |
* than just bad luck. Even if p is not prime, we should have found |
| 212 |
* some y such that r == -1. |
| 213 |
*/ |
| 214 |
ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
| 215 |
goto end; |
| 216 |
} |
| 217 |
|
| 218 |
/* Here's our actual 'q': */ |
| 219 |
if (!BN_rshift(q, q, e)) |
| 220 |
goto end; |
| 221 |
|
| 222 |
/* |
| 223 |
* Now that we have some non-square, we can find an element of order 2^e |
| 224 |
* by computing its q'th power. |
| 225 |
*/ |
| 226 |
if (!BN_mod_exp(y, y, q, p, ctx)) |
| 227 |
goto end; |
| 228 |
if (BN_is_one(y)) { |
| 229 |
ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); |
| 230 |
goto end; |
| 231 |
} |
| 232 |
|
| 233 |
/*- |
| 234 |
* Now we know that (if p is indeed prime) there is an integer |
| 235 |
* k, 0 <= k < 2^e, such that |
| 236 |
* |
| 237 |
* a^q * y^k == 1 (mod p). |
| 238 |
* |
| 239 |
* As a^q is a square and y is not, k must be even. |
| 240 |
* q+1 is even, too, so there is an element |
| 241 |
* |
| 242 |
* X := a^((q+1)/2) * y^(k/2), |
| 243 |
* |
| 244 |
* and it satisfies |
| 245 |
* |
| 246 |
* X^2 = a^q * a * y^k |
| 247 |
* = a, |
| 248 |
* |
| 249 |
* so it is the square root that we are looking for. |
| 250 |
*/ |
| 251 |
|
| 252 |
/* t := (q-1)/2 (note that q is odd) */ |
| 253 |
if (!BN_rshift1(t, q)) |
| 254 |
goto end; |
| 255 |
|
| 256 |
/* x := a^((q-1)/2) */ |
| 257 |
if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */ |
| 258 |
if (!BN_nnmod(t, A, p, ctx)) |
| 259 |
goto end; |
| 260 |
if (BN_is_zero(t)) { |
| 261 |
/* special case: a == 0 (mod p) */ |
| 262 |
BN_zero(ret); |
| 263 |
err = 0; |
| 264 |
goto end; |
| 265 |
} else if (!BN_one(x)) |
| 266 |
goto end; |
| 267 |
} else { |
| 268 |
if (!BN_mod_exp(x, A, t, p, ctx)) |
| 269 |
goto end; |
| 270 |
if (BN_is_zero(x)) { |
| 271 |
/* special case: a == 0 (mod p) */ |
| 272 |
BN_zero(ret); |
| 273 |
err = 0; |
| 274 |
goto end; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/* b := a*x^2 (= a^q) */ |
| 279 |
if (!BN_mod_sqr(b, x, p, ctx)) |
| 280 |
goto end; |
| 281 |
if (!BN_mod_mul(b, b, A, p, ctx)) |
| 282 |
goto end; |
| 283 |
|
| 284 |
/* x := a*x (= a^((q+1)/2)) */ |
| 285 |
if (!BN_mod_mul(x, x, A, p, ctx)) |
| 286 |
goto end; |
| 287 |
|
| 288 |
while (1) { |
| 289 |
/*- |
| 290 |
* Now b is a^q * y^k for some even k (0 <= k < 2^E |
| 291 |
* where E refers to the original value of e, which we |
| 292 |
* don't keep in a variable), and x is a^((q+1)/2) * y^(k/2). |
| 293 |
* |
| 294 |
* We have a*b = x^2, |
| 295 |
* y^2^(e-1) = -1, |
| 296 |
* b^2^(e-1) = 1. |
| 297 |
*/ |
| 298 |
|
| 299 |
if (BN_is_one(b)) { |
| 300 |
if (!BN_copy(ret, x)) |
| 301 |
goto end; |
| 302 |
err = 0; |
| 303 |
goto vrfy; |
| 304 |
} |
| 305 |
|
| 306 |
/* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ |
| 307 |
for (i = 1; i < e; i++) { |
| 308 |
if (i == 1) { |
| 309 |
if (!BN_mod_sqr(t, b, p, ctx)) |
| 310 |
goto end; |
| 311 |
|
| 312 |
} else { |
| 313 |
if (!BN_mod_mul(t, t, t, p, ctx)) |
| 314 |
goto end; |
| 315 |
} |
| 316 |
if (BN_is_one(t)) |
| 317 |
break; |
| 318 |
} |
| 319 |
/* If not found, a is not a square or p is not prime. */ |
| 320 |
if (i >= e) { |
| 321 |
ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
| 322 |
goto end; |
| 323 |
} |
| 324 |
|
| 325 |
/* t := y^2^(e - i - 1) */ |
| 326 |
if (!BN_copy(t, y)) |
| 327 |
goto end; |
| 328 |
for (j = e - i - 1; j > 0; j--) { |
| 329 |
if (!BN_mod_sqr(t, t, p, ctx)) |
| 330 |
goto end; |
| 331 |
} |
| 332 |
if (!BN_mod_mul(y, t, t, p, ctx)) |
| 333 |
goto end; |
| 334 |
if (!BN_mod_mul(x, x, t, p, ctx)) |
| 335 |
goto end; |
| 336 |
if (!BN_mod_mul(b, b, y, p, ctx)) |
| 337 |
goto end; |
| 338 |
e = i; |
| 339 |
} |
| 340 |
|
| 341 |
vrfy: |
| 342 |
if (!err) { |
| 343 |
/* |
| 344 |
* verify the result -- the input might have been not a square (test |
| 345 |
* added in 0.9.8) |
| 346 |
*/ |
| 347 |
|
| 348 |
if (!BN_mod_sqr(x, ret, p, ctx)) |
| 349 |
err = 1; |
| 350 |
|
| 351 |
if (!err && 0 != BN_cmp(x, A)) { |
| 352 |
ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
| 353 |
err = 1; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
end: |
| 358 |
if (err) { |
| 359 |
if (ret != in) |
| 360 |
BN_clear_free(ret); |
| 361 |
ret = NULL; |
| 362 |
} |
| 363 |
if (used_ctx) |
| 364 |
BN_CTX_end(ctx); |
| 365 |
bn_check_top(ret); |
| 366 |
return ret; |
| 367 |
} |