sl@0: /* crypto/bn/bn_sqrt.c */ sl@0: /* Written by Lenka Fibikova sl@0: * and Bodo Moeller for the OpenSSL project. */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: sl@0: EXPORT_C BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) sl@0: /* Returns 'ret' such that sl@0: * ret^2 == a (mod p), sl@0: * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course sl@0: * in Algebraic Computational Number Theory", algorithm 1.5.1). sl@0: * 'p' must be prime! sl@0: */ sl@0: { sl@0: BIGNUM *ret = in; sl@0: int err = 1; sl@0: int r; sl@0: BIGNUM *A, *b, *q, *t, *x, *y; sl@0: int e, i, j; sl@0: sl@0: if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) sl@0: { sl@0: if (BN_abs_is_word(p, 2)) sl@0: { sl@0: if (ret == NULL) sl@0: ret = BN_new(); sl@0: if (ret == NULL) sl@0: goto end; sl@0: if (!BN_set_word(ret, BN_is_bit_set(a, 0))) sl@0: { sl@0: if (ret != in) sl@0: BN_free(ret); sl@0: return NULL; sl@0: } sl@0: bn_check_top(ret); sl@0: return ret; sl@0: } sl@0: sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); sl@0: return(NULL); sl@0: } sl@0: sl@0: if (BN_is_zero(a) || BN_is_one(a)) sl@0: { sl@0: if (ret == NULL) sl@0: ret = BN_new(); sl@0: if (ret == NULL) sl@0: goto end; sl@0: if (!BN_set_word(ret, BN_is_one(a))) sl@0: { sl@0: if (ret != in) sl@0: BN_free(ret); sl@0: return NULL; sl@0: } sl@0: bn_check_top(ret); sl@0: return ret; sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: A = BN_CTX_get(ctx); sl@0: b = BN_CTX_get(ctx); sl@0: q = BN_CTX_get(ctx); sl@0: t = BN_CTX_get(ctx); sl@0: x = BN_CTX_get(ctx); sl@0: y = BN_CTX_get(ctx); sl@0: if (y == NULL) goto end; sl@0: sl@0: if (ret == NULL) sl@0: ret = BN_new(); sl@0: if (ret == NULL) goto end; sl@0: sl@0: /* A = a mod p */ sl@0: if (!BN_nnmod(A, a, p, ctx)) goto end; sl@0: sl@0: /* now write |p| - 1 as 2^e*q where q is odd */ sl@0: e = 1; sl@0: while (!BN_is_bit_set(p, e)) sl@0: e++; sl@0: /* we'll set q later (if needed) */ sl@0: sl@0: if (e == 1) sl@0: { sl@0: /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse sl@0: * modulo (|p|-1)/2, and square roots can be computed sl@0: * directly by modular exponentiation. sl@0: * We have sl@0: * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2), sl@0: * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1. sl@0: */ sl@0: if (!BN_rshift(q, p, 2)) goto end; sl@0: q->neg = 0; sl@0: if (!BN_add_word(q, 1)) goto end; sl@0: if (!BN_mod_exp(ret, A, q, p, ctx)) goto end; sl@0: err = 0; sl@0: goto vrfy; sl@0: } sl@0: sl@0: if (e == 2) sl@0: { sl@0: /* |p| == 5 (mod 8) sl@0: * sl@0: * In this case 2 is always a non-square since sl@0: * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. sl@0: * So if a really is a square, then 2*a is a non-square. sl@0: * Thus for sl@0: * b := (2*a)^((|p|-5)/8), sl@0: * i := (2*a)*b^2 sl@0: * we have sl@0: * i^2 = (2*a)^((1 + (|p|-5)/4)*2) sl@0: * = (2*a)^((p-1)/2) sl@0: * = -1; sl@0: * so if we set sl@0: * x := a*b*(i-1), sl@0: * then sl@0: * x^2 = a^2 * b^2 * (i^2 - 2*i + 1) sl@0: * = a^2 * b^2 * (-2*i) sl@0: * = a*(-i)*(2*a*b^2) sl@0: * = a*(-i)*i sl@0: * = a. sl@0: * sl@0: * (This is due to A.O.L. Atkin, sl@0: * , sl@0: * November 1992.) sl@0: */ sl@0: sl@0: /* t := 2*a */ sl@0: if (!BN_mod_lshift1_quick(t, A, p)) goto end; sl@0: sl@0: /* b := (2*a)^((|p|-5)/8) */ sl@0: if (!BN_rshift(q, p, 3)) goto end; sl@0: q->neg = 0; sl@0: if (!BN_mod_exp(b, t, q, p, ctx)) goto end; sl@0: sl@0: /* y := b^2 */ sl@0: if (!BN_mod_sqr(y, b, p, ctx)) goto end; sl@0: sl@0: /* t := (2*a)*b^2 - 1*/ sl@0: if (!BN_mod_mul(t, t, y, p, ctx)) goto end; sl@0: if (!BN_sub_word(t, 1)) goto end; sl@0: sl@0: /* x = a*b*t */ sl@0: if (!BN_mod_mul(x, A, b, p, ctx)) goto end; sl@0: if (!BN_mod_mul(x, x, t, p, ctx)) goto end; sl@0: sl@0: if (!BN_copy(ret, x)) goto end; sl@0: err = 0; sl@0: goto vrfy; sl@0: } sl@0: sl@0: /* e > 2, so we really have to use the Tonelli/Shanks algorithm. sl@0: * First, find some y that is not a square. */ sl@0: if (!BN_copy(q, p)) goto end; /* use 'q' as temp */ sl@0: q->neg = 0; sl@0: i = 2; sl@0: do sl@0: { sl@0: /* For efficiency, try small numbers first; sl@0: * if this fails, try random numbers. sl@0: */ sl@0: if (i < 22) sl@0: { sl@0: if (!BN_set_word(y, i)) goto end; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end; sl@0: if (BN_ucmp(y, p) >= 0) sl@0: { sl@0: if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end; sl@0: } sl@0: /* now 0 <= y < |p| */ sl@0: if (BN_is_zero(y)) sl@0: if (!BN_set_word(y, i)) goto end; sl@0: } sl@0: sl@0: r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ sl@0: if (r < -1) goto end; sl@0: if (r == 0) sl@0: { sl@0: /* m divides p */ sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); sl@0: goto end; sl@0: } sl@0: } sl@0: while (r == 1 && ++i < 82); sl@0: sl@0: if (r != -1) sl@0: { sl@0: /* Many rounds and still no non-square -- this is more likely sl@0: * a bug than just bad luck. sl@0: * Even if p is not prime, we should have found some y sl@0: * such that r == -1. sl@0: */ sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS); sl@0: goto end; sl@0: } sl@0: sl@0: /* Here's our actual 'q': */ sl@0: if (!BN_rshift(q, q, e)) goto end; sl@0: sl@0: /* Now that we have some non-square, we can find an element sl@0: * of order 2^e by computing its q'th power. */ sl@0: if (!BN_mod_exp(y, y, q, p, ctx)) goto end; sl@0: if (BN_is_one(y)) sl@0: { sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); sl@0: goto end; sl@0: } sl@0: sl@0: /* Now we know that (if p is indeed prime) there is an integer sl@0: * k, 0 <= k < 2^e, such that sl@0: * sl@0: * a^q * y^k == 1 (mod p). sl@0: * sl@0: * As a^q is a square and y is not, k must be even. sl@0: * q+1 is even, too, so there is an element sl@0: * sl@0: * X := a^((q+1)/2) * y^(k/2), sl@0: * sl@0: * and it satisfies sl@0: * sl@0: * X^2 = a^q * a * y^k sl@0: * = a, sl@0: * sl@0: * so it is the square root that we are looking for. sl@0: */ sl@0: sl@0: /* t := (q-1)/2 (note that q is odd) */ sl@0: if (!BN_rshift1(t, q)) goto end; sl@0: sl@0: /* x := a^((q-1)/2) */ sl@0: if (BN_is_zero(t)) /* special case: p = 2^e + 1 */ sl@0: { sl@0: if (!BN_nnmod(t, A, p, ctx)) goto end; sl@0: if (BN_is_zero(t)) sl@0: { sl@0: /* special case: a == 0 (mod p) */ sl@0: BN_zero(ret); sl@0: err = 0; sl@0: goto end; sl@0: } sl@0: else sl@0: if (!BN_one(x)) goto end; sl@0: } sl@0: else sl@0: { sl@0: if (!BN_mod_exp(x, A, t, p, ctx)) goto end; sl@0: if (BN_is_zero(x)) sl@0: { sl@0: /* special case: a == 0 (mod p) */ sl@0: BN_zero(ret); sl@0: err = 0; sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: /* b := a*x^2 (= a^q) */ sl@0: if (!BN_mod_sqr(b, x, p, ctx)) goto end; sl@0: if (!BN_mod_mul(b, b, A, p, ctx)) goto end; sl@0: sl@0: /* x := a*x (= a^((q+1)/2)) */ sl@0: if (!BN_mod_mul(x, x, A, p, ctx)) goto end; sl@0: sl@0: while (1) sl@0: { sl@0: /* Now b is a^q * y^k for some even k (0 <= k < 2^E sl@0: * where E refers to the original value of e, which we sl@0: * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2). sl@0: * sl@0: * We have a*b = x^2, sl@0: * y^2^(e-1) = -1, sl@0: * b^2^(e-1) = 1. sl@0: */ sl@0: sl@0: if (BN_is_one(b)) sl@0: { sl@0: if (!BN_copy(ret, x)) goto end; sl@0: err = 0; sl@0: goto vrfy; sl@0: } sl@0: sl@0: sl@0: /* find smallest i such that b^(2^i) = 1 */ sl@0: i = 1; sl@0: if (!BN_mod_sqr(t, b, p, ctx)) goto end; sl@0: while (!BN_is_one(t)) sl@0: { sl@0: i++; sl@0: if (i == e) sl@0: { sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); sl@0: goto end; sl@0: } sl@0: if (!BN_mod_mul(t, t, t, p, ctx)) goto end; sl@0: } sl@0: sl@0: sl@0: /* t := y^2^(e - i - 1) */ sl@0: if (!BN_copy(t, y)) goto end; sl@0: for (j = e - i - 1; j > 0; j--) sl@0: { sl@0: if (!BN_mod_sqr(t, t, p, ctx)) goto end; sl@0: } sl@0: if (!BN_mod_mul(y, t, t, p, ctx)) goto end; sl@0: if (!BN_mod_mul(x, x, t, p, ctx)) goto end; sl@0: if (!BN_mod_mul(b, b, y, p, ctx)) goto end; sl@0: e = i; sl@0: } sl@0: sl@0: vrfy: sl@0: if (!err) sl@0: { sl@0: /* verify the result -- the input might have been not a square sl@0: * (test added in 0.9.8) */ sl@0: sl@0: if (!BN_mod_sqr(x, ret, p, ctx)) sl@0: err = 1; sl@0: sl@0: if (!err && 0 != BN_cmp(x, A)) sl@0: { sl@0: BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); sl@0: err = 1; sl@0: } sl@0: } sl@0: sl@0: end: sl@0: if (err) sl@0: { sl@0: if (ret != NULL && ret != in) sl@0: { sl@0: BN_clear_free(ret); sl@0: } sl@0: ret = NULL; sl@0: } sl@0: BN_CTX_end(ctx); sl@0: bn_check_top(ret); sl@0: return ret; sl@0: }