/src/openssl/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 | 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 | } |
207 | 0 | while (r == 1 && ++i < 82); |
208 | | |
209 | 0 | 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 | 0 | ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS); |
216 | 0 | goto end; |
217 | 0 | } |
218 | | |
219 | | /* Here's our actual 'q': */ |
220 | 0 | 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 | 0 | if (!BN_mod_exp(y, y, q, p, ctx)) |
228 | 0 | goto end; |
229 | 0 | 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 | 0 | if (!BN_rshift1(t, q)) |
255 | 0 | goto end; |
256 | | |
257 | | /* x := a^((q-1)/2) */ |
258 | 0 | if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */ |
259 | 0 | if (!BN_nnmod(t, A, p, ctx)) |
260 | 0 | goto end; |
261 | 0 | 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 | 0 | } else if (!BN_one(x)) |
267 | 0 | goto end; |
268 | 0 | } else { |
269 | 0 | if (!BN_mod_exp(x, A, t, p, ctx)) |
270 | 0 | goto end; |
271 | 0 | 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 | 0 | } |
278 | | |
279 | | /* b := a*x^2 (= a^q) */ |
280 | 0 | if (!BN_mod_sqr(b, x, p, ctx)) |
281 | 0 | goto end; |
282 | 0 | if (!BN_mod_mul(b, b, A, p, ctx)) |
283 | 0 | goto end; |
284 | | |
285 | | /* x := a*x (= a^((q+1)/2)) */ |
286 | 0 | if (!BN_mod_mul(x, x, A, p, ctx)) |
287 | 0 | goto end; |
288 | | |
289 | 0 | 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 | 0 | if (BN_is_one(b)) { |
301 | 0 | if (!BN_copy(ret, x)) |
302 | 0 | goto end; |
303 | 0 | err = 0; |
304 | 0 | goto vrfy; |
305 | 0 | } |
306 | | |
307 | | /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */ |
308 | 0 | for (i = 1; i < e; i++) { |
309 | 0 | if (i == 1) { |
310 | 0 | if (!BN_mod_sqr(t, b, p, ctx)) |
311 | 0 | goto end; |
312 | |
|
313 | 0 | } else { |
314 | 0 | if (!BN_mod_mul(t, t, t, p, ctx)) |
315 | 0 | goto end; |
316 | 0 | } |
317 | 0 | if (BN_is_one(t)) |
318 | 0 | break; |
319 | 0 | } |
320 | | /* If not found, a is not a square or p is not prime. */ |
321 | 0 | if (i >= e) { |
322 | 0 | ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
323 | 0 | goto end; |
324 | 0 | } |
325 | | |
326 | | /* t := y^2^(e - i - 1) */ |
327 | 0 | if (!BN_copy(t, y)) |
328 | 0 | goto end; |
329 | 0 | for (j = e - i - 1; j > 0; j--) { |
330 | 0 | if (!BN_mod_sqr(t, t, p, ctx)) |
331 | 0 | goto end; |
332 | 0 | } |
333 | 0 | if (!BN_mod_mul(y, t, t, p, ctx)) |
334 | 0 | goto end; |
335 | 0 | if (!BN_mod_mul(x, x, t, p, ctx)) |
336 | 0 | goto end; |
337 | 0 | if (!BN_mod_mul(b, b, y, p, ctx)) |
338 | 0 | goto end; |
339 | 0 | e = i; |
340 | 0 | } |
341 | | |
342 | 0 | vrfy: |
343 | 0 | 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 | 0 | if (!BN_mod_sqr(x, ret, p, ctx)) |
350 | 0 | err = 1; |
351 | |
|
352 | 0 | if (!err && 0 != BN_cmp(x, A)) { |
353 | 0 | ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE); |
354 | 0 | err = 1; |
355 | 0 | } |
356 | 0 | } |
357 | |
|
358 | 0 | end: |
359 | 0 | if (err) { |
360 | 0 | if (ret != in) |
361 | 0 | BN_clear_free(ret); |
362 | 0 | ret = NULL; |
363 | 0 | } |
364 | 0 | if (used_ctx) |
365 | 0 | BN_CTX_end(ctx); |
366 | 0 | bn_check_top(ret); |
367 | 0 | return ret; |
368 | 0 | } |