Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 2424d84 | 2017-05-01 07:46:28 -0700 | [diff] [blame] | 59 | #include "internal.h" |
| 60 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 61 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 62 | BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 63 | // Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm |
| 64 | // (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory", |
| 65 | // algorithm 1.5.1). |p| is assumed to be a prime. |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 66 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | BIGNUM *ret = in; |
| 68 | int err = 1; |
| 69 | int r; |
| 70 | BIGNUM *A, *b, *q, *t, *x, *y; |
| 71 | int e, i, j; |
| 72 | |
| 73 | if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { |
| 74 | if (BN_abs_is_word(p, 2)) { |
| 75 | if (ret == NULL) { |
| 76 | ret = BN_new(); |
| 77 | } |
| 78 | if (ret == NULL) { |
| 79 | goto end; |
| 80 | } |
| 81 | if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { |
| 82 | if (ret != in) { |
| 83 | BN_free(ret); |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | return ret; |
| 88 | } |
| 89 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 90 | OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 91 | return (NULL); |
| 92 | } |
| 93 | |
| 94 | if (BN_is_zero(a) || BN_is_one(a)) { |
| 95 | if (ret == NULL) { |
| 96 | ret = BN_new(); |
| 97 | } |
| 98 | if (ret == NULL) { |
| 99 | goto end; |
| 100 | } |
| 101 | if (!BN_set_word(ret, BN_is_one(a))) { |
| 102 | if (ret != in) { |
| 103 | BN_free(ret); |
| 104 | } |
| 105 | return NULL; |
| 106 | } |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | BN_CTX_start(ctx); |
| 111 | A = BN_CTX_get(ctx); |
| 112 | b = BN_CTX_get(ctx); |
| 113 | q = BN_CTX_get(ctx); |
| 114 | t = BN_CTX_get(ctx); |
| 115 | x = BN_CTX_get(ctx); |
| 116 | y = BN_CTX_get(ctx); |
| 117 | if (y == NULL) { |
| 118 | goto end; |
| 119 | } |
| 120 | |
| 121 | if (ret == NULL) { |
| 122 | ret = BN_new(); |
| 123 | } |
| 124 | if (ret == NULL) { |
| 125 | goto end; |
| 126 | } |
| 127 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 128 | // A = a mod p |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 129 | if (!BN_nnmod(A, a, p, ctx)) { |
| 130 | goto end; |
| 131 | } |
| 132 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 133 | // now write |p| - 1 as 2^e*q where q is odd |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 134 | e = 1; |
| 135 | while (!BN_is_bit_set(p, e)) { |
| 136 | e++; |
| 137 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 138 | // we'll set q later (if needed) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | |
| 140 | if (e == 1) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 141 | // The easy case: (|p|-1)/2 is odd, so 2 has an inverse |
| 142 | // modulo (|p|-1)/2, and square roots can be computed |
| 143 | // directly by modular exponentiation. |
| 144 | // We have |
| 145 | // 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2), |
| 146 | // so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 147 | if (!BN_rshift(q, p, 2)) { |
| 148 | goto end; |
| 149 | } |
| 150 | q->neg = 0; |
| 151 | if (!BN_add_word(q, 1) || |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 152 | !BN_mod_exp_mont(ret, A, q, p, ctx, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 153 | goto end; |
| 154 | } |
| 155 | err = 0; |
| 156 | goto vrfy; |
| 157 | } |
| 158 | |
| 159 | if (e == 2) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 160 | // |p| == 5 (mod 8) |
| 161 | // |
| 162 | // In this case 2 is always a non-square since |
| 163 | // Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. |
| 164 | // So if a really is a square, then 2*a is a non-square. |
| 165 | // Thus for |
| 166 | // b := (2*a)^((|p|-5)/8), |
| 167 | // i := (2*a)*b^2 |
| 168 | // we have |
| 169 | // i^2 = (2*a)^((1 + (|p|-5)/4)*2) |
| 170 | // = (2*a)^((p-1)/2) |
| 171 | // = -1; |
| 172 | // so if we set |
| 173 | // x := a*b*(i-1), |
| 174 | // then |
| 175 | // x^2 = a^2 * b^2 * (i^2 - 2*i + 1) |
| 176 | // = a^2 * b^2 * (-2*i) |
| 177 | // = a*(-i)*(2*a*b^2) |
| 178 | // = a*(-i)*i |
| 179 | // = a. |
| 180 | // |
| 181 | // (This is due to A.O.L. Atkin, |
| 182 | // <URL: |
| 183 | //http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>, |
| 184 | // November 1992.) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 186 | // t := 2*a |
Robert Sloan | 49d063b | 2018-04-03 11:30:38 -0700 | [diff] [blame] | 187 | if (!bn_mod_lshift1_consttime(t, A, p, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 188 | goto end; |
| 189 | } |
| 190 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 191 | // b := (2*a)^((|p|-5)/8) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 192 | if (!BN_rshift(q, p, 3)) { |
| 193 | goto end; |
| 194 | } |
| 195 | q->neg = 0; |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 196 | if (!BN_mod_exp_mont(b, t, q, p, ctx, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | goto end; |
| 198 | } |
| 199 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 200 | // y := b^2 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 201 | if (!BN_mod_sqr(y, b, p, ctx)) { |
| 202 | goto end; |
| 203 | } |
| 204 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 205 | // t := (2*a)*b^2 - 1 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 206 | if (!BN_mod_mul(t, t, y, p, ctx) || |
| 207 | !BN_sub_word(t, 1)) { |
| 208 | goto end; |
| 209 | } |
| 210 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 211 | // x = a*b*t |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 224 | // e > 2, so we really have to use the Tonelli/Shanks algorithm. |
| 225 | // First, find some y that is not a square. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | if (!BN_copy(q, p)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 227 | goto end; // use 'q' as temp |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | } |
| 229 | q->neg = 0; |
| 230 | i = 2; |
| 231 | do { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 232 | // For efficiency, try small numbers first; |
| 233 | // if this fails, try random numbers. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 234 | if (i < 22) { |
| 235 | if (!BN_set_word(y, i)) { |
| 236 | goto end; |
| 237 | } |
| 238 | } else { |
| 239 | if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) { |
| 240 | goto end; |
| 241 | } |
| 242 | if (BN_ucmp(y, p) >= 0) { |
| 243 | if (!(p->neg ? BN_add : BN_sub)(y, y, p)) { |
| 244 | goto end; |
| 245 | } |
| 246 | } |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 247 | // now 0 <= y < |p| |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | if (BN_is_zero(y)) { |
| 249 | if (!BN_set_word(y, i)) { |
| 250 | goto end; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 255 | r = bn_jacobi(y, q, ctx); // here 'q' is |p| |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | if (r < -1) { |
| 257 | goto end; |
| 258 | } |
| 259 | if (r == 0) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 260 | // m divides p |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 261 | OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | goto end; |
| 263 | } |
| 264 | } while (r == 1 && ++i < 82); |
| 265 | |
| 266 | if (r != -1) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 267 | // Many rounds and still no non-square -- this is more likely |
| 268 | // a bug than just bad luck. |
| 269 | // Even if p is not prime, we should have found some y |
| 270 | // such that r == -1. |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 271 | OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 272 | goto end; |
| 273 | } |
| 274 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 275 | // Here's our actual 'q': |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 276 | if (!BN_rshift(q, q, e)) { |
| 277 | goto end; |
| 278 | } |
| 279 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 280 | // Now that we have some non-square, we can find an element |
| 281 | // of order 2^e by computing its q'th power. |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 282 | if (!BN_mod_exp_mont(y, y, q, p, ctx, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | goto end; |
| 284 | } |
| 285 | if (BN_is_one(y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 286 | OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 287 | goto end; |
| 288 | } |
| 289 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 290 | // Now we know that (if p is indeed prime) there is an integer |
| 291 | // k, 0 <= k < 2^e, such that |
| 292 | // |
| 293 | // a^q * y^k == 1 (mod p). |
| 294 | // |
| 295 | // As a^q is a square and y is not, k must be even. |
| 296 | // q+1 is even, too, so there is an element |
| 297 | // |
| 298 | // X := a^((q+1)/2) * y^(k/2), |
| 299 | // |
| 300 | // and it satisfies |
| 301 | // |
| 302 | // X^2 = a^q * a * y^k |
| 303 | // = a, |
| 304 | // |
| 305 | // so it is the square root that we are looking for. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 306 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 307 | // t := (q-1)/2 (note that q is odd) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 308 | if (!BN_rshift1(t, q)) { |
| 309 | goto end; |
| 310 | } |
| 311 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 312 | // x := a^((q-1)/2) |
| 313 | if (BN_is_zero(t)) // special case: p = 2^e + 1 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 314 | { |
| 315 | if (!BN_nnmod(t, A, p, ctx)) { |
| 316 | goto end; |
| 317 | } |
| 318 | if (BN_is_zero(t)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 319 | // special case: a == 0 (mod p) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 320 | BN_zero(ret); |
| 321 | err = 0; |
| 322 | goto end; |
| 323 | } else if (!BN_one(x)) { |
| 324 | goto end; |
| 325 | } |
| 326 | } else { |
Robert Sloan | 7d422bc | 2017-03-06 10:04:29 -0800 | [diff] [blame] | 327 | if (!BN_mod_exp_mont(x, A, t, p, ctx, NULL)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | goto end; |
| 329 | } |
| 330 | if (BN_is_zero(x)) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 331 | // special case: a == 0 (mod p) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 332 | BN_zero(ret); |
| 333 | err = 0; |
| 334 | goto end; |
| 335 | } |
| 336 | } |
| 337 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 338 | // b := a*x^2 (= a^q) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 339 | if (!BN_mod_sqr(b, x, p, ctx) || |
| 340 | !BN_mod_mul(b, b, A, p, ctx)) { |
| 341 | goto end; |
| 342 | } |
| 343 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 344 | // x := a*x (= a^((q+1)/2)) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 345 | if (!BN_mod_mul(x, x, A, p, ctx)) { |
| 346 | goto end; |
| 347 | } |
| 348 | |
| 349 | while (1) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 350 | // Now b is a^q * y^k for some even k (0 <= k < 2^E |
| 351 | // where E refers to the original value of e, which we |
| 352 | // don't keep in a variable), and x is a^((q+1)/2) * y^(k/2). |
| 353 | // |
| 354 | // We have a*b = x^2, |
| 355 | // y^2^(e-1) = -1, |
| 356 | // b^2^(e-1) = 1. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 357 | |
| 358 | if (BN_is_one(b)) { |
| 359 | if (!BN_copy(ret, x)) { |
| 360 | goto end; |
| 361 | } |
| 362 | err = 0; |
| 363 | goto vrfy; |
| 364 | } |
| 365 | |
| 366 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 367 | // find smallest i such that b^(2^i) = 1 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 368 | i = 1; |
| 369 | if (!BN_mod_sqr(t, b, p, ctx)) { |
| 370 | goto end; |
| 371 | } |
| 372 | while (!BN_is_one(t)) { |
| 373 | i++; |
| 374 | if (i == e) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | goto end; |
| 377 | } |
| 378 | if (!BN_mod_mul(t, t, t, p, ctx)) { |
| 379 | goto end; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 384 | // t := y^2^(e - i - 1) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 385 | if (!BN_copy(t, y)) { |
| 386 | goto end; |
| 387 | } |
| 388 | for (j = e - i - 1; j > 0; j--) { |
| 389 | if (!BN_mod_sqr(t, t, p, ctx)) { |
| 390 | goto end; |
| 391 | } |
| 392 | } |
| 393 | if (!BN_mod_mul(y, t, t, p, ctx) || |
| 394 | !BN_mod_mul(x, x, t, p, ctx) || |
| 395 | !BN_mod_mul(b, b, y, p, ctx)) { |
| 396 | goto end; |
| 397 | } |
| 398 | e = i; |
| 399 | } |
| 400 | |
| 401 | vrfy: |
| 402 | if (!err) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 403 | // verify the result -- the input might have been not a square |
| 404 | // (test added in 0.9.8) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 405 | |
| 406 | if (!BN_mod_sqr(x, ret, p, ctx)) { |
| 407 | err = 1; |
| 408 | } |
| 409 | |
| 410 | if (!err && 0 != BN_cmp(x, A)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 411 | OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 412 | err = 1; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | end: |
| 417 | if (err) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 418 | if (ret != in) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 419 | BN_clear_free(ret); |
| 420 | } |
| 421 | ret = NULL; |
| 422 | } |
| 423 | BN_CTX_end(ctx); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) { |
| 428 | BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2; |
| 429 | int ok = 0, last_delta_valid = 0; |
| 430 | |
| 431 | if (in->neg) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 432 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | if (BN_is_zero(in)) { |
| 436 | BN_zero(out_sqrt); |
| 437 | return 1; |
| 438 | } |
| 439 | |
| 440 | BN_CTX_start(ctx); |
| 441 | if (out_sqrt == in) { |
| 442 | estimate = BN_CTX_get(ctx); |
| 443 | } else { |
| 444 | estimate = out_sqrt; |
| 445 | } |
| 446 | tmp = BN_CTX_get(ctx); |
| 447 | last_delta = BN_CTX_get(ctx); |
| 448 | delta = BN_CTX_get(ctx); |
| 449 | if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 450 | OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 451 | goto err; |
| 452 | } |
| 453 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 454 | // We estimate that the square root of an n-bit number is 2^{n/2}. |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 455 | if (!BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2)) { |
| 456 | goto err; |
| 457 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 458 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 459 | // This is Newton's method for finding a root of the equation |estimate|^2 - |
| 460 | // |in| = 0. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 461 | for (;;) { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 462 | // |estimate| = 1/2 * (|estimate| + |in|/|estimate|) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 463 | if (!BN_div(tmp, NULL, in, estimate, ctx) || |
| 464 | !BN_add(tmp, tmp, estimate) || |
| 465 | !BN_rshift1(estimate, tmp) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 466 | // |tmp| = |estimate|^2 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 467 | !BN_sqr(tmp, estimate, ctx) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 468 | // |delta| = |in| - |tmp| |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 469 | !BN_sub(delta, in, tmp)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 470 | OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 471 | goto err; |
| 472 | } |
| 473 | |
| 474 | delta->neg = 0; |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 475 | // The difference between |in| and |estimate| squared is required to always |
| 476 | // decrease. This ensures that the loop always terminates, but I don't have |
| 477 | // a proof that it always finds the square root for a given square. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 478 | if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) { |
| 479 | break; |
| 480 | } |
| 481 | |
| 482 | last_delta_valid = 1; |
| 483 | |
| 484 | tmp2 = last_delta; |
| 485 | last_delta = delta; |
| 486 | delta = tmp2; |
| 487 | } |
| 488 | |
| 489 | if (BN_cmp(tmp, in) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 490 | OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 491 | goto err; |
| 492 | } |
| 493 | |
| 494 | ok = 1; |
| 495 | |
| 496 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 497 | if (ok && out_sqrt == in && !BN_copy(out_sqrt, estimate)) { |
| 498 | ok = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 499 | } |
| 500 | BN_CTX_end(ctx); |
| 501 | return ok; |
| 502 | } |