/src/openssl30/crypto/bn/bn_sqrt.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 | 61.4k | { | 
| 21 | 61.4k |     BIGNUM *ret = in; | 
| 22 | 61.4k |     int err = 1; | 
| 23 | 61.4k |     int r; | 
| 24 | 61.4k |     BIGNUM *A, *b, *q, *t, *x, *y; | 
| 25 | 61.4k |     int e, i, j; | 
| 26 | 61.4k |     int used_ctx = 0; | 
| 27 |  |  | 
| 28 | 61.4k |     if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { | 
| 29 | 0 |         if (BN_abs_is_word(p, 2)) { | 
| 30 | 0 |             if (ret == NULL) | 
| 31 | 0 |                 ret = BN_new(); | 
| 32 | 0 |             if (ret == NULL) | 
| 33 | 0 |                 goto end; | 
| 34 | 0 |             if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { | 
| 35 | 0 |                 if (ret != in) | 
| 36 | 0 |                     BN_free(ret); | 
| 37 | 0 |                 return NULL; | 
| 38 | 0 |             } | 
| 39 | 0 |             bn_check_top(ret); | 
| 40 | 0 |             return ret; | 
| 41 | 0 |         } | 
| 42 |  |  | 
| 43 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); | 
| 44 | 0 |         return NULL; | 
| 45 | 0 |     } | 
| 46 |  |  | 
| 47 | 61.4k |     if (BN_is_zero(a) || BN_is_one(a)) { | 
| 48 | 167 |         if (ret == NULL) | 
| 49 | 0 |             ret = BN_new(); | 
| 50 | 167 |         if (ret == NULL) | 
| 51 | 0 |             goto end; | 
| 52 | 167 |         if (!BN_set_word(ret, BN_is_one(a))) { | 
| 53 | 0 |             if (ret != in) | 
| 54 | 0 |                 BN_free(ret); | 
| 55 | 0 |             return NULL; | 
| 56 | 0 |         } | 
| 57 | 167 |         bn_check_top(ret); | 
| 58 | 167 |         return ret; | 
| 59 | 167 |     } | 
| 60 |  |  | 
| 61 | 61.2k |     BN_CTX_start(ctx); | 
| 62 | 61.2k |     used_ctx = 1; | 
| 63 | 61.2k |     A = BN_CTX_get(ctx); | 
| 64 | 61.2k |     b = BN_CTX_get(ctx); | 
| 65 | 61.2k |     q = BN_CTX_get(ctx); | 
| 66 | 61.2k |     t = BN_CTX_get(ctx); | 
| 67 | 61.2k |     x = BN_CTX_get(ctx); | 
| 68 | 61.2k |     y = BN_CTX_get(ctx); | 
| 69 | 61.2k |     if (y == NULL) | 
| 70 | 0 |         goto end; | 
| 71 |  |  | 
| 72 | 61.2k |     if (ret == NULL) | 
| 73 | 0 |         ret = BN_new(); | 
| 74 | 61.2k |     if (ret == NULL) | 
| 75 | 0 |         goto end; | 
| 76 |  |  | 
| 77 |  |     /* A = a mod p */ | 
| 78 | 61.2k |     if (!BN_nnmod(A, a, p, ctx)) | 
| 79 | 0 |         goto end; | 
| 80 |  |  | 
| 81 |  |     /* now write  |p| - 1  as  2^e*q  where  q  is odd */ | 
| 82 | 61.2k |     e = 1; | 
| 83 | 1.62M |     while (!BN_is_bit_set(p, e)) | 
| 84 | 1.56M |         e++; | 
| 85 |  |     /* we'll set  q  later (if needed) */ | 
| 86 |  |  | 
| 87 | 61.2k |     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 | 40.7k |         if (!BN_rshift(q, p, 2)) | 
| 97 | 0 |             goto end; | 
| 98 | 40.7k |         q->neg = 0; | 
| 99 | 40.7k |         if (!BN_add_word(q, 1)) | 
| 100 | 0 |             goto end; | 
| 101 | 40.7k |         if (!BN_mod_exp(ret, A, q, p, ctx)) | 
| 102 | 0 |             goto end; | 
| 103 | 40.7k |         err = 0; | 
| 104 | 40.7k |         goto vrfy; | 
| 105 | 40.7k |     } | 
| 106 |  |  | 
| 107 | 20.4k |     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 | 2.71k |         if (!BN_mod_lshift1_quick(t, A, p)) | 
| 138 | 0 |             goto end; | 
| 139 |  |  | 
| 140 |  |         /* b := (2*a)^((|p|-5)/8) */ | 
| 141 | 2.71k |         if (!BN_rshift(q, p, 3)) | 
| 142 | 0 |             goto end; | 
| 143 | 2.71k |         q->neg = 0; | 
| 144 | 2.71k |         if (!BN_mod_exp(b, t, q, p, ctx)) | 
| 145 | 0 |             goto end; | 
| 146 |  |  | 
| 147 |  |         /* y := b^2 */ | 
| 148 | 2.71k |         if (!BN_mod_sqr(y, b, p, ctx)) | 
| 149 | 0 |             goto end; | 
| 150 |  |  | 
| 151 |  |         /* t := (2*a)*b^2 - 1 */ | 
| 152 | 2.71k |         if (!BN_mod_mul(t, t, y, p, ctx)) | 
| 153 | 0 |             goto end; | 
| 154 | 2.71k |         if (!BN_sub_word(t, 1)) | 
| 155 | 0 |             goto end; | 
| 156 |  |  | 
| 157 |  |         /* x = a*b*t */ | 
| 158 | 2.71k |         if (!BN_mod_mul(x, A, b, p, ctx)) | 
| 159 | 0 |             goto end; | 
| 160 | 2.71k |         if (!BN_mod_mul(x, x, t, p, ctx)) | 
| 161 | 0 |             goto end; | 
| 162 |  |  | 
| 163 | 2.71k |         if (!BN_copy(ret, x)) | 
| 164 | 0 |             goto end; | 
| 165 | 2.71k |         err = 0; | 
| 166 | 2.71k |         goto vrfy; | 
| 167 | 2.71k |     } | 
| 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 | 17.7k |     if (!BN_copy(q, p)) | 
| 174 | 0 |         goto end;               /* use 'q' as temp */ | 
| 175 | 17.7k |     q->neg = 0; | 
| 176 | 17.7k |     i = 2; | 
| 177 | 175k |     do { | 
| 178 |  |         /* | 
| 179 |  |          * For efficiency, try small numbers first; if this fails, try random | 
| 180 |  |          * numbers. | 
| 181 |  |          */ | 
| 182 | 175k |         if (i < 22) { | 
| 183 | 169k |             if (!BN_set_word(y, i)) | 
| 184 | 0 |                 goto end; | 
| 185 | 169k |         } else { | 
| 186 | 6.54k |             if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx)) | 
| 187 | 0 |                 goto end; | 
| 188 | 6.54k |             if (BN_ucmp(y, p) >= 0) { | 
| 189 | 2.52k |                 if (!(p->neg ? BN_add : BN_sub) (y, y, p)) | 
| 190 | 0 |                     goto end; | 
| 191 | 2.52k |             } | 
| 192 |  |             /* now 0 <= y < |p| */ | 
| 193 | 6.54k |             if (BN_is_zero(y)) | 
| 194 | 0 |                 if (!BN_set_word(y, i)) | 
| 195 | 0 |                     goto end; | 
| 196 | 6.54k |         } | 
| 197 |  |  | 
| 198 | 175k |         r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ | 
| 199 | 175k |         if (r < -1) | 
| 200 | 0 |             goto end; | 
| 201 | 175k |         if (r == 0) { | 
| 202 |  |             /* m divides p */ | 
| 203 | 163 |             ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); | 
| 204 | 163 |             goto end; | 
| 205 | 163 |         } | 
| 206 | 175k |     } | 
| 207 | 175k |     while (r == 1 && ++i < 82); | 
| 208 |  |  | 
| 209 | 17.5k |     if (r != -1) { | 
| 210 |  |         /* | 
| 211 |  |          * Many rounds and still no non-square -- this is more likely a bug | 
| 212 |  |          * than just bad luck. Even if p is not prime, we should have found | 
| 213 |  |          * some y such that r == -1. | 
| 214 |  |          */ | 
| 215 | 107 |         ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); | 
| 216 | 107 |         goto end; | 
| 217 | 107 |     } | 
| 218 |  |  | 
| 219 |  |     /* Here's our actual 'q': */ | 
| 220 | 17.4k |     if (!BN_rshift(q, q, e)) | 
| 221 | 0 |         goto end; | 
| 222 |  |  | 
| 223 |  |     /* | 
| 224 |  |      * Now that we have some non-square, we can find an element of order 2^e | 
| 225 |  |      * by computing its q'th power. | 
| 226 |  |      */ | 
| 227 | 17.4k |     if (!BN_mod_exp(y, y, q, p, ctx)) | 
| 228 | 0 |         goto end; | 
| 229 | 17.4k |     if (BN_is_one(y)) { | 
| 230 | 0 |         ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); | 
| 231 | 0 |         goto end; | 
| 232 | 0 |     } | 
| 233 |  |  | 
| 234 |  |     /*- | 
| 235 |  |      * Now we know that (if  p  is indeed prime) there is an integer | 
| 236 |  |      * k,  0 <= k < 2^e,  such that | 
| 237 |  |      * | 
| 238 |  |      *      a^q * y^k == 1   (mod p). | 
| 239 |  |      * | 
| 240 |  |      * As  a^q  is a square and  y  is not,  k  must be even. | 
| 241 |  |      * q+1  is even, too, so there is an element | 
| 242 |  |      * | 
| 243 |  |      *     X := a^((q+1)/2) * y^(k/2), | 
| 244 |  |      * | 
| 245 |  |      * and it satisfies | 
| 246 |  |      * | 
| 247 |  |      *     X^2 = a^q * a     * y^k | 
| 248 |  |      *         = a, | 
| 249 |  |      * | 
| 250 |  |      * so it is the square root that we are looking for. | 
| 251 |  |      */ | 
| 252 |  |  | 
| 253 |  |     /* t := (q-1)/2  (note that  q  is odd) */ | 
| 254 | 17.4k |     if (!BN_rshift1(t, q)) | 
| 255 | 0 |         goto end; | 
| 256 |  |  | 
| 257 |  |     /* x := a^((q-1)/2) */ | 
| 258 | 17.4k |     if (BN_is_zero(t)) {        /* special case: p = 2^e + 1 */ | 
| 259 | 14 |         if (!BN_nnmod(t, A, p, ctx)) | 
| 260 | 0 |             goto end; | 
| 261 | 14 |         if (BN_is_zero(t)) { | 
| 262 |  |             /* special case: a == 0  (mod p) */ | 
| 263 | 0 |             BN_zero(ret); | 
| 264 | 0 |             err = 0; | 
| 265 | 0 |             goto end; | 
| 266 | 14 |         } else if (!BN_one(x)) | 
| 267 | 0 |             goto end; | 
| 268 | 17.4k |     } else { | 
| 269 | 17.4k |         if (!BN_mod_exp(x, A, t, p, ctx)) | 
| 270 | 0 |             goto end; | 
| 271 | 17.4k |         if (BN_is_zero(x)) { | 
| 272 |  |             /* special case: a == 0  (mod p) */ | 
| 273 | 0 |             BN_zero(ret); | 
| 274 | 0 |             err = 0; | 
| 275 | 0 |             goto end; | 
| 276 | 0 |         } | 
| 277 | 17.4k |     } | 
| 278 |  |  | 
| 279 |  |     /* b := a*x^2  (= a^q) */ | 
| 280 | 17.4k |     if (!BN_mod_sqr(b, x, p, ctx)) | 
| 281 | 0 |         goto end; | 
| 282 | 17.4k |     if (!BN_mod_mul(b, b, A, p, ctx)) | 
| 283 | 0 |         goto end; | 
| 284 |  |  | 
| 285 |  |     /* x := a*x    (= a^((q+1)/2)) */ | 
| 286 | 17.4k |     if (!BN_mod_mul(x, x, A, p, ctx)) | 
| 287 | 0 |         goto end; | 
| 288 |  |  | 
| 289 | 654k |     while (1) { | 
| 290 |  |         /*- | 
| 291 |  |          * Now  b  is  a^q * y^k  for some even  k  (0 <= k < 2^E | 
| 292 |  |          * where  E  refers to the original value of  e,  which we | 
| 293 |  |          * don't keep in a variable),  and  x  is  a^((q+1)/2) * y^(k/2). | 
| 294 |  |          * | 
| 295 |  |          * We have  a*b = x^2, | 
| 296 |  |          *    y^2^(e-1) = -1, | 
| 297 |  |          *    b^2^(e-1) = 1. | 
| 298 |  |          */ | 
| 299 |  |  | 
| 300 | 654k |         if (BN_is_one(b)) { | 
| 301 | 14.2k |             if (!BN_copy(ret, x)) | 
| 302 | 0 |                 goto end; | 
| 303 | 14.2k |             err = 0; | 
| 304 | 14.2k |             goto vrfy; | 
| 305 | 14.2k |         } | 
| 306 |  |  | 
| 307 |  |         /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ | 
| 308 | 30.2M |         for (i = 1; i < e; i++) { | 
| 309 | 30.2M |             if (i == 1) { | 
| 310 | 640k |                 if (!BN_mod_sqr(t, b, p, ctx)) | 
| 311 | 0 |                     goto end; | 
| 312 |  |  | 
| 313 | 29.6M |             } else { | 
| 314 | 29.6M |                 if (!BN_mod_mul(t, t, t, p, ctx)) | 
| 315 | 0 |                     goto end; | 
| 316 | 29.6M |             } | 
| 317 | 30.2M |             if (BN_is_one(t)) | 
| 318 | 637k |                 break; | 
| 319 | 30.2M |         } | 
| 320 |  |         /* If not found, a is not a square or p is not prime. */ | 
| 321 | 640k |         if (i >= e) { | 
| 322 | 3.20k |             ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); | 
| 323 | 3.20k |             goto end; | 
| 324 | 3.20k |         } | 
| 325 |  |  | 
| 326 |  |         /* t := y^2^(e - i - 1) */ | 
| 327 | 637k |         if (!BN_copy(t, y)) | 
| 328 | 0 |             goto end; | 
| 329 | 1.27M |         for (j = e - i - 1; j > 0; j--) { | 
| 330 | 638k |             if (!BN_mod_sqr(t, t, p, ctx)) | 
| 331 | 0 |                 goto end; | 
| 332 | 638k |         } | 
| 333 | 637k |         if (!BN_mod_mul(y, t, t, p, ctx)) | 
| 334 | 0 |             goto end; | 
| 335 | 637k |         if (!BN_mod_mul(x, x, t, p, ctx)) | 
| 336 | 0 |             goto end; | 
| 337 | 637k |         if (!BN_mod_mul(b, b, y, p, ctx)) | 
| 338 | 0 |             goto end; | 
| 339 | 637k |         e = i; | 
| 340 | 637k |     } | 
| 341 |  |  | 
| 342 | 57.7k |  vrfy: | 
| 343 | 57.7k |     if (!err) { | 
| 344 |  |         /* | 
| 345 |  |          * verify the result -- the input might have been not a square (test | 
| 346 |  |          * added in 0.9.8) | 
| 347 |  |          */ | 
| 348 |  |  | 
| 349 | 57.7k |         if (!BN_mod_sqr(x, ret, p, ctx)) | 
| 350 | 0 |             err = 1; | 
| 351 |  |  | 
| 352 | 57.7k |         if (!err && 0 != BN_cmp(x, A)) { | 
| 353 | 15.0k |             ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); | 
| 354 | 15.0k |             err = 1; | 
| 355 | 15.0k |         } | 
| 356 | 57.7k |     } | 
| 357 |  |  | 
| 358 | 61.2k |  end: | 
| 359 | 61.2k |     if (err) { | 
| 360 | 18.5k |         if (ret != in) | 
| 361 | 0 |             BN_clear_free(ret); | 
| 362 | 18.5k |         ret = NULL; | 
| 363 | 18.5k |     } | 
| 364 | 61.2k |     if (used_ctx) | 
| 365 | 61.2k |         BN_CTX_end(ctx); | 
| 366 | 61.2k |     bn_check_top(ret); | 
| 367 | 61.2k |     return ret; | 
| 368 | 57.7k | } |