/src/openssl/crypto/bn/bn_sqrt.c
Line | Count | Source |
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 | 0 | { |
21 | 0 | BIGNUM *ret = in; |
22 | 0 | int err = 1; |
23 | 0 | int r; |
24 | 0 | BIGNUM *A, *b, *q, *t, *x, *y; |
25 | 0 | int e, i, j; |
26 | 0 | int used_ctx = 0; |
27 | |
|
28 | 0 | 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 | 0 | if (BN_is_zero(a) || BN_is_one(a)) { |
48 | 0 | if (ret == NULL) |
49 | 0 | ret = BN_new(); |
50 | 0 | if (ret == NULL) |
51 | 0 | goto end; |
52 | 0 | 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 | 0 | bn_check_top(ret); |
58 | 0 | return ret; |
59 | 0 | } |
60 | | |
61 | 0 | BN_CTX_start(ctx); |
62 | 0 | used_ctx = 1; |
63 | 0 | A = BN_CTX_get(ctx); |
64 | 0 | b = BN_CTX_get(ctx); |
65 | 0 | q = BN_CTX_get(ctx); |
66 | 0 | t = BN_CTX_get(ctx); |
67 | 0 | x = BN_CTX_get(ctx); |
68 | 0 | y = BN_CTX_get(ctx); |
69 | 0 | if (y == NULL) |
70 | 0 | goto end; |
71 | | |
72 | 0 | if (ret == NULL) |
73 | 0 | ret = BN_new(); |
74 | 0 | if (ret == NULL) |
75 | 0 | goto end; |
76 | | |
77 | | /* A = a mod p */ |
78 | 0 | 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 | 0 | e = 1; |
83 | 0 | while (!BN_is_bit_set(p, e)) |
84 | 0 | e++; |
85 | | /* we'll set q later (if needed) */ |
86 | |
|
87 | 0 | 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 | 0 | if (!BN_rshift(q, p, 2)) |
97 | 0 | goto end; |
98 | 0 | q->neg = 0; |
99 | 0 | if (!BN_add_word(q, 1)) |
100 | 0 | goto end; |
101 | 0 | if (!BN_mod_exp(ret, A, q, p, ctx)) |
102 | 0 | goto end; |
103 | 0 | err = 0; |
104 | 0 | goto vrfy; |
105 | 0 | } |
106 | | |
107 | 0 | 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 | 0 | if (!BN_mod_lshift1_quick(t, A, p)) |
138 | 0 | goto end; |
139 | | |
140 | | /* b := (2*a)^((|p|-5)/8) */ |
141 | 0 | if (!BN_rshift(q, p, 3)) |
142 | 0 | goto end; |
143 | 0 | q->neg = 0; |
144 | 0 | if (!BN_mod_exp(b, t, q, p, ctx)) |
145 | 0 | goto end; |
146 | | |
147 | | /* y := b^2 */ |
148 | 0 | if (!BN_mod_sqr(y, b, p, ctx)) |
149 | 0 | goto end; |
150 | | |
151 | | /* t := (2*a)*b^2 - 1 */ |
152 | 0 | if (!BN_mod_mul(t, t, y, p, ctx)) |
153 | 0 | goto end; |
154 | 0 | if (!BN_sub_word(t, 1)) |
155 | 0 | goto end; |
156 | | |
157 | | /* x = a*b*t */ |
158 | 0 | if (!BN_mod_mul(x, A, b, p, ctx)) |
159 | 0 | goto end; |
160 | 0 | if (!BN_mod_mul(x, x, t, p, ctx)) |
161 | 0 | goto end; |
162 | | |
163 | 0 | if (!BN_copy(ret, x)) |
164 | 0 | goto end; |
165 | 0 | err = 0; |
166 | 0 | goto vrfy; |
167 | 0 | } |
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 | 0 | if (!BN_copy(q, p)) |
174 | 0 | goto end; /* use 'q' as temp */ |
175 | 0 | q->neg = 0; |
176 | 0 | i = 2; |
177 | 0 | do { |
178 | | /* |
179 | | * For efficiency, try small numbers first; if this fails, try random |
180 | | * numbers. |
181 | | */ |
182 | 0 | if (i < 22) { |
183 | 0 | if (!BN_set_word(y, i)) |
184 | 0 | goto end; |
185 | 0 | } else { |
186 | 0 | if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx)) |
187 | 0 | goto end; |
188 | 0 | if (BN_ucmp(y, p) >= 0) { |
189 | 0 | if (!(p->neg ? BN_add : BN_sub)(y, y, p)) |
190 | 0 | goto end; |
191 | 0 | } |
192 | | /* now 0 <= y < |p| */ |
193 | 0 | if (BN_is_zero(y)) |
194 | 0 | if (!BN_set_word(y, i)) |
195 | 0 | goto end; |
196 | 0 | } |
197 | | |
198 | 0 | r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ |
199 | 0 | if (r < -1) |
200 | 0 | goto end; |
201 | 0 | if (r == 0) { |
202 | | /* m divides p */ |
203 | 0 | ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); |
204 | 0 | goto end; |
205 | 0 | } |
206 | 0 | } while (r == 1 && ++i < 82); |
207 | | |
208 | 0 | 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 | 0 | ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
215 | 0 | goto end; |
216 | 0 | } |
217 | | |
218 | | /* Here's our actual 'q': */ |
219 | 0 | if (!BN_rshift(q, q, e)) |
220 | 0 | 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 | 0 | if (!BN_mod_exp(y, y, q, p, ctx)) |
227 | 0 | goto end; |
228 | 0 | if (BN_is_one(y)) { |
229 | 0 | ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME); |
230 | 0 | goto end; |
231 | 0 | } |
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 | 0 | if (!BN_rshift1(t, q)) |
254 | 0 | goto end; |
255 | | |
256 | | /* x := a^((q-1)/2) */ |
257 | 0 | if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */ |
258 | 0 | if (!BN_nnmod(t, A, p, ctx)) |
259 | 0 | goto end; |
260 | 0 | if (BN_is_zero(t)) { |
261 | | /* special case: a == 0 (mod p) */ |
262 | 0 | BN_zero(ret); |
263 | 0 | err = 0; |
264 | 0 | goto end; |
265 | 0 | } else if (!BN_one(x)) |
266 | 0 | goto end; |
267 | 0 | } else { |
268 | 0 | if (!BN_mod_exp(x, A, t, p, ctx)) |
269 | 0 | goto end; |
270 | 0 | if (BN_is_zero(x)) { |
271 | | /* special case: a == 0 (mod p) */ |
272 | 0 | BN_zero(ret); |
273 | 0 | err = 0; |
274 | 0 | goto end; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | | /* b := a*x^2 (= a^q) */ |
279 | 0 | if (!BN_mod_sqr(b, x, p, ctx)) |
280 | 0 | goto end; |
281 | 0 | if (!BN_mod_mul(b, b, A, p, ctx)) |
282 | 0 | goto end; |
283 | | |
284 | | /* x := a*x (= a^((q+1)/2)) */ |
285 | 0 | if (!BN_mod_mul(x, x, A, p, ctx)) |
286 | 0 | goto end; |
287 | | |
288 | 0 | 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 | 0 | if (BN_is_one(b)) { |
300 | 0 | if (!BN_copy(ret, x)) |
301 | 0 | goto end; |
302 | 0 | err = 0; |
303 | 0 | goto vrfy; |
304 | 0 | } |
305 | | |
306 | | /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ |
307 | 0 | for (i = 1; i < e; i++) { |
308 | 0 | if (i == 1) { |
309 | 0 | if (!BN_mod_sqr(t, b, p, ctx)) |
310 | 0 | goto end; |
311 | |
|
312 | 0 | } else { |
313 | 0 | if (!BN_mod_mul(t, t, t, p, ctx)) |
314 | 0 | goto end; |
315 | 0 | } |
316 | 0 | if (BN_is_one(t)) |
317 | 0 | break; |
318 | 0 | } |
319 | | /* If not found, a is not a square or p is not prime. */ |
320 | 0 | if (i >= e) { |
321 | 0 | ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
322 | 0 | goto end; |
323 | 0 | } |
324 | | |
325 | | /* t := y^2^(e - i - 1) */ |
326 | 0 | if (!BN_copy(t, y)) |
327 | 0 | goto end; |
328 | 0 | for (j = e - i - 1; j > 0; j--) { |
329 | 0 | if (!BN_mod_sqr(t, t, p, ctx)) |
330 | 0 | goto end; |
331 | 0 | } |
332 | 0 | if (!BN_mod_mul(y, t, t, p, ctx)) |
333 | 0 | goto end; |
334 | 0 | if (!BN_mod_mul(x, x, t, p, ctx)) |
335 | 0 | goto end; |
336 | 0 | if (!BN_mod_mul(b, b, y, p, ctx)) |
337 | 0 | goto end; |
338 | 0 | e = i; |
339 | 0 | } |
340 | | |
341 | 0 | vrfy: |
342 | 0 | 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 | 0 | if (!BN_mod_sqr(x, ret, p, ctx)) |
349 | 0 | err = 1; |
350 | |
|
351 | 0 | if (!err && 0 != BN_cmp(x, A)) { |
352 | 0 | ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
353 | 0 | err = 1; |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | 0 | end: |
358 | 0 | if (err) { |
359 | 0 | if (ret != in) |
360 | 0 | BN_clear_free(ret); |
361 | 0 | ret = NULL; |
362 | 0 | } |
363 | 0 | if (used_ctx) |
364 | 0 | BN_CTX_end(ctx); |
365 | 0 | bn_check_top(ret); |
366 | 0 | return ret; |
367 | 0 | } |