1/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2 * and Bodo Moeller for the OpenSSL project. */ 3/* ==================================================================== 4 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * 3. All advertising materials mentioning features or use of this 19 * software must display the following acknowledgment: 20 * "This product includes software developed by the OpenSSL Project 21 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 22 * 23 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 24 * endorse or promote products derived from this software without 25 * prior written permission. For written permission, please contact 26 * openssl-core@openssl.org. 27 * 28 * 5. Products derived from this software may not be called "OpenSSL" 29 * nor may "OpenSSL" appear in their names without prior written 30 * permission of the OpenSSL Project. 31 * 32 * 6. Redistributions of any form whatsoever must retain the following 33 * acknowledgment: 34 * "This product includes software developed by the OpenSSL Project 35 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 36 * 37 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 38 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 48 * OF THE POSSIBILITY OF SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This product includes cryptographic software written by Eric Young 52 * (eay@cryptsoft.com). This product includes software written by Tim 53 * Hudson (tjh@cryptsoft.com). */ 54 55#include <openssl/bn.h> 56 57#include <openssl/err.h> 58 59 60BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { 61 /* Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm 62 * (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory", 63 * algorithm 1.5.1). |p| is assumed to be a prime. */ 64 65 BIGNUM *ret = in; 66 int err = 1; 67 int r; 68 BIGNUM *A, *b, *q, *t, *x, *y; 69 int e, i, j; 70 71 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { 72 if (BN_abs_is_word(p, 2)) { 73 if (ret == NULL) { 74 ret = BN_new(); 75 } 76 if (ret == NULL) { 77 goto end; 78 } 79 if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { 80 if (ret != in) { 81 BN_free(ret); 82 } 83 return NULL; 84 } 85 return ret; 86 } 87 88 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); 89 return (NULL); 90 } 91 92 if (BN_is_zero(a) || BN_is_one(a)) { 93 if (ret == NULL) { 94 ret = BN_new(); 95 } 96 if (ret == NULL) { 97 goto end; 98 } 99 if (!BN_set_word(ret, BN_is_one(a))) { 100 if (ret != in) { 101 BN_free(ret); 102 } 103 return NULL; 104 } 105 return ret; 106 } 107 108 BN_CTX_start(ctx); 109 A = BN_CTX_get(ctx); 110 b = BN_CTX_get(ctx); 111 q = BN_CTX_get(ctx); 112 t = BN_CTX_get(ctx); 113 x = BN_CTX_get(ctx); 114 y = BN_CTX_get(ctx); 115 if (y == NULL) { 116 goto end; 117 } 118 119 if (ret == NULL) { 120 ret = BN_new(); 121 } 122 if (ret == NULL) { 123 goto end; 124 } 125 126 /* A = a mod p */ 127 if (!BN_nnmod(A, a, p, ctx)) { 128 goto end; 129 } 130 131 /* now write |p| - 1 as 2^e*q where q is odd */ 132 e = 1; 133 while (!BN_is_bit_set(p, e)) { 134 e++; 135 } 136 /* we'll set q later (if needed) */ 137 138 if (e == 1) { 139 /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse 140 * modulo (|p|-1)/2, and square roots can be computed 141 * directly by modular exponentiation. 142 * We have 143 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2), 144 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1. 145 */ 146 if (!BN_rshift(q, p, 2)) { 147 goto end; 148 } 149 q->neg = 0; 150 if (!BN_add_word(q, 1) || 151 !BN_mod_exp_mont(ret, A, q, p, ctx, NULL)) { 152 goto end; 153 } 154 err = 0; 155 goto vrfy; 156 } 157 158 if (e == 2) { 159 /* |p| == 5 (mod 8) 160 * 161 * In this case 2 is always a non-square since 162 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. 163 * So if a really is a square, then 2*a is a non-square. 164 * Thus for 165 * b := (2*a)^((|p|-5)/8), 166 * i := (2*a)*b^2 167 * we have 168 * i^2 = (2*a)^((1 + (|p|-5)/4)*2) 169 * = (2*a)^((p-1)/2) 170 * = -1; 171 * so if we set 172 * x := a*b*(i-1), 173 * then 174 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1) 175 * = a^2 * b^2 * (-2*i) 176 * = a*(-i)*(2*a*b^2) 177 * = a*(-i)*i 178 * = a. 179 * 180 * (This is due to A.O.L. Atkin, 181 * <URL: 182 *http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>, 183 * November 1992.) 184 */ 185 186 /* t := 2*a */ 187 if (!BN_mod_lshift1_quick(t, A, p)) { 188 goto end; 189 } 190 191 /* b := (2*a)^((|p|-5)/8) */ 192 if (!BN_rshift(q, p, 3)) { 193 goto end; 194 } 195 q->neg = 0; 196 if (!BN_mod_exp_mont(b, t, q, p, ctx, NULL)) { 197 goto end; 198 } 199 200 /* y := b^2 */ 201 if (!BN_mod_sqr(y, b, p, ctx)) { 202 goto end; 203 } 204 205 /* t := (2*a)*b^2 - 1*/ 206 if (!BN_mod_mul(t, t, y, p, ctx) || 207 !BN_sub_word(t, 1)) { 208 goto end; 209 } 210 211 /* x = a*b*t */ 212 if (!BN_mod_mul(x, A, b, p, ctx) || 213 !BN_mod_mul(x, x, t, p, ctx)) { 214 goto end; 215 } 216 217 if (!BN_copy(ret, x)) { 218 goto end; 219 } 220 err = 0; 221 goto vrfy; 222 } 223 224 /* e > 2, so we really have to use the Tonelli/Shanks algorithm. 225 * First, find some y that is not a square. */ 226 if (!BN_copy(q, p)) { 227 goto end; /* use 'q' as temp */ 228 } 229 q->neg = 0; 230 i = 2; 231 do { 232 /* For efficiency, try small numbers first; 233 * if this fails, try random numbers. 234 */ 235 if (i < 22) { 236 if (!BN_set_word(y, i)) { 237 goto end; 238 } 239 } else { 240 if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) { 241 goto end; 242 } 243 if (BN_ucmp(y, p) >= 0) { 244 if (!(p->neg ? BN_add : BN_sub)(y, y, p)) { 245 goto end; 246 } 247 } 248 /* now 0 <= y < |p| */ 249 if (BN_is_zero(y)) { 250 if (!BN_set_word(y, i)) { 251 goto end; 252 } 253 } 254 } 255 256 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ 257 if (r < -1) { 258 goto end; 259 } 260 if (r == 0) { 261 /* m divides p */ 262 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); 263 goto end; 264 } 265 } while (r == 1 && ++i < 82); 266 267 if (r != -1) { 268 /* Many rounds and still no non-square -- this is more likely 269 * a bug than just bad luck. 270 * Even if p is not prime, we should have found some y 271 * such that r == -1. 272 */ 273 OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS); 274 goto end; 275 } 276 277 /* Here's our actual 'q': */ 278 if (!BN_rshift(q, q, e)) { 279 goto end; 280 } 281 282 /* Now that we have some non-square, we can find an element 283 * of order 2^e by computing its q'th power. */ 284 if (!BN_mod_exp_mont(y, y, q, p, ctx, NULL)) { 285 goto end; 286 } 287 if (BN_is_one(y)) { 288 OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); 289 goto end; 290 } 291 292 /* Now we know that (if p is indeed prime) there is an integer 293 * k, 0 <= k < 2^e, such that 294 * 295 * a^q * y^k == 1 (mod p). 296 * 297 * As a^q is a square and y is not, k must be even. 298 * q+1 is even, too, so there is an element 299 * 300 * X := a^((q+1)/2) * y^(k/2), 301 * 302 * and it satisfies 303 * 304 * X^2 = a^q * a * y^k 305 * = a, 306 * 307 * so it is the square root that we are looking for. 308 */ 309 310 /* t := (q-1)/2 (note that q is odd) */ 311 if (!BN_rshift1(t, q)) { 312 goto end; 313 } 314 315 /* x := a^((q-1)/2) */ 316 if (BN_is_zero(t)) /* special case: p = 2^e + 1 */ 317 { 318 if (!BN_nnmod(t, A, p, ctx)) { 319 goto end; 320 } 321 if (BN_is_zero(t)) { 322 /* special case: a == 0 (mod p) */ 323 BN_zero(ret); 324 err = 0; 325 goto end; 326 } else if (!BN_one(x)) { 327 goto end; 328 } 329 } else { 330 if (!BN_mod_exp_mont(x, A, t, p, ctx, NULL)) { 331 goto end; 332 } 333 if (BN_is_zero(x)) { 334 /* special case: a == 0 (mod p) */ 335 BN_zero(ret); 336 err = 0; 337 goto end; 338 } 339 } 340 341 /* b := a*x^2 (= a^q) */ 342 if (!BN_mod_sqr(b, x, p, ctx) || 343 !BN_mod_mul(b, b, A, p, ctx)) { 344 goto end; 345 } 346 347 /* x := a*x (= a^((q+1)/2)) */ 348 if (!BN_mod_mul(x, x, A, p, ctx)) { 349 goto end; 350 } 351 352 while (1) { 353 /* Now b is a^q * y^k for some even k (0 <= k < 2^E 354 * where E refers to the original value of e, which we 355 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2). 356 * 357 * We have a*b = x^2, 358 * y^2^(e-1) = -1, 359 * b^2^(e-1) = 1. 360 */ 361 362 if (BN_is_one(b)) { 363 if (!BN_copy(ret, x)) { 364 goto end; 365 } 366 err = 0; 367 goto vrfy; 368 } 369 370 371 /* find smallest i such that b^(2^i) = 1 */ 372 i = 1; 373 if (!BN_mod_sqr(t, b, p, ctx)) { 374 goto end; 375 } 376 while (!BN_is_one(t)) { 377 i++; 378 if (i == e) { 379 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); 380 goto end; 381 } 382 if (!BN_mod_mul(t, t, t, p, ctx)) { 383 goto end; 384 } 385 } 386 387 388 /* t := y^2^(e - i - 1) */ 389 if (!BN_copy(t, y)) { 390 goto end; 391 } 392 for (j = e - i - 1; j > 0; j--) { 393 if (!BN_mod_sqr(t, t, p, ctx)) { 394 goto end; 395 } 396 } 397 if (!BN_mod_mul(y, t, t, p, ctx) || 398 !BN_mod_mul(x, x, t, p, ctx) || 399 !BN_mod_mul(b, b, y, p, ctx)) { 400 goto end; 401 } 402 e = i; 403 } 404 405vrfy: 406 if (!err) { 407 /* verify the result -- the input might have been not a square 408 * (test added in 0.9.8) */ 409 410 if (!BN_mod_sqr(x, ret, p, ctx)) { 411 err = 1; 412 } 413 414 if (!err && 0 != BN_cmp(x, A)) { 415 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); 416 err = 1; 417 } 418 } 419 420end: 421 if (err) { 422 if (ret != in) { 423 BN_clear_free(ret); 424 } 425 ret = NULL; 426 } 427 BN_CTX_end(ctx); 428 return ret; 429} 430 431int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) { 432 BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2; 433 int ok = 0, last_delta_valid = 0; 434 435 if (in->neg) { 436 OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); 437 return 0; 438 } 439 if (BN_is_zero(in)) { 440 BN_zero(out_sqrt); 441 return 1; 442 } 443 444 BN_CTX_start(ctx); 445 if (out_sqrt == in) { 446 estimate = BN_CTX_get(ctx); 447 } else { 448 estimate = out_sqrt; 449 } 450 tmp = BN_CTX_get(ctx); 451 last_delta = BN_CTX_get(ctx); 452 delta = BN_CTX_get(ctx); 453 if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) { 454 OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); 455 goto err; 456 } 457 458 /* We estimate that the square root of an n-bit number is 2^{n/2}. */ 459 if (!BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2)) { 460 goto err; 461 } 462 463 /* This is Newton's method for finding a root of the equation |estimate|^2 - 464 * |in| = 0. */ 465 for (;;) { 466 /* |estimate| = 1/2 * (|estimate| + |in|/|estimate|) */ 467 if (!BN_div(tmp, NULL, in, estimate, ctx) || 468 !BN_add(tmp, tmp, estimate) || 469 !BN_rshift1(estimate, tmp) || 470 /* |tmp| = |estimate|^2 */ 471 !BN_sqr(tmp, estimate, ctx) || 472 /* |delta| = |in| - |tmp| */ 473 !BN_sub(delta, in, tmp)) { 474 OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB); 475 goto err; 476 } 477 478 delta->neg = 0; 479 /* The difference between |in| and |estimate| squared is required to always 480 * decrease. This ensures that the loop always terminates, but I don't have 481 * a proof that it always finds the square root for a given square. */ 482 if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) { 483 break; 484 } 485 486 last_delta_valid = 1; 487 488 tmp2 = last_delta; 489 last_delta = delta; 490 delta = tmp2; 491 } 492 493 if (BN_cmp(tmp, in) != 0) { 494 OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); 495 goto err; 496 } 497 498 ok = 1; 499 500err: 501 if (ok && out_sqrt == in && !BN_copy(out_sqrt, estimate)) { 502 ok = 0; 503 } 504 BN_CTX_end(ctx); 505 return ok; 506} 507