/src/openssl/crypto/bn/bn_sqrt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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_lcl.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! |
18 | | */ |
19 | 0 | { |
20 | 0 | BIGNUM *ret = in; |
21 | 0 | int err = 1; |
22 | 0 | int r; |
23 | 0 | BIGNUM *A, *b, *q, *t, *x, *y; |
24 | 0 | int e, i, j; |
25 | 0 |
|
26 | 0 | if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) { |
27 | 0 | if (BN_abs_is_word(p, 2)) { |
28 | 0 | if (ret == NULL) |
29 | 0 | ret = BN_new(); |
30 | 0 | if (ret == NULL) |
31 | 0 | goto end; |
32 | 0 | if (!BN_set_word(ret, BN_is_bit_set(a, 0))) { |
33 | 0 | if (ret != in) |
34 | 0 | BN_free(ret); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | 0 | bn_check_top(ret); |
38 | 0 | return ret; |
39 | 0 | } |
40 | 0 | |
41 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); |
42 | 0 | return NULL; |
43 | 0 | } |
44 | 0 | |
45 | 0 | if (BN_is_zero(a) || BN_is_one(a)) { |
46 | 0 | if (ret == NULL) |
47 | 0 | ret = BN_new(); |
48 | 0 | if (ret == NULL) |
49 | 0 | goto end; |
50 | 0 | if (!BN_set_word(ret, BN_is_one(a))) { |
51 | 0 | if (ret != in) |
52 | 0 | BN_free(ret); |
53 | 0 | return NULL; |
54 | 0 | } |
55 | 0 | bn_check_top(ret); |
56 | 0 | return ret; |
57 | 0 | } |
58 | 0 | |
59 | 0 | BN_CTX_start(ctx); |
60 | 0 | A = BN_CTX_get(ctx); |
61 | 0 | b = BN_CTX_get(ctx); |
62 | 0 | q = BN_CTX_get(ctx); |
63 | 0 | t = BN_CTX_get(ctx); |
64 | 0 | x = BN_CTX_get(ctx); |
65 | 0 | y = BN_CTX_get(ctx); |
66 | 0 | if (y == NULL) |
67 | 0 | goto end; |
68 | 0 | |
69 | 0 | if (ret == NULL) |
70 | 0 | ret = BN_new(); |
71 | 0 | if (ret == NULL) |
72 | 0 | goto end; |
73 | 0 | |
74 | 0 | /* A = a mod p */ |
75 | 0 | if (!BN_nnmod(A, a, p, ctx)) |
76 | 0 | goto end; |
77 | 0 | |
78 | 0 | /* now write |p| - 1 as 2^e*q where q is odd */ |
79 | 0 | e = 1; |
80 | 0 | while (!BN_is_bit_set(p, e)) |
81 | 0 | e++; |
82 | 0 | /* we'll set q later (if needed) */ |
83 | 0 |
|
84 | 0 | if (e == 1) { |
85 | 0 | /*- |
86 | 0 | * The easy case: (|p|-1)/2 is odd, so 2 has an inverse |
87 | 0 | * modulo (|p|-1)/2, and square roots can be computed |
88 | 0 | * directly by modular exponentiation. |
89 | 0 | * We have |
90 | 0 | * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2), |
91 | 0 | * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1. |
92 | 0 | */ |
93 | 0 | if (!BN_rshift(q, p, 2)) |
94 | 0 | goto end; |
95 | 0 | q->neg = 0; |
96 | 0 | if (!BN_add_word(q, 1)) |
97 | 0 | goto end; |
98 | 0 | if (!BN_mod_exp(ret, A, q, p, ctx)) |
99 | 0 | goto end; |
100 | 0 | err = 0; |
101 | 0 | goto vrfy; |
102 | 0 | } |
103 | 0 | |
104 | 0 | if (e == 2) { |
105 | 0 | /*- |
106 | 0 | * |p| == 5 (mod 8) |
107 | 0 | * |
108 | 0 | * In this case 2 is always a non-square since |
109 | 0 | * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime. |
110 | 0 | * So if a really is a square, then 2*a is a non-square. |
111 | 0 | * Thus for |
112 | 0 | * b := (2*a)^((|p|-5)/8), |
113 | 0 | * i := (2*a)*b^2 |
114 | 0 | * we have |
115 | 0 | * i^2 = (2*a)^((1 + (|p|-5)/4)*2) |
116 | 0 | * = (2*a)^((p-1)/2) |
117 | 0 | * = -1; |
118 | 0 | * so if we set |
119 | 0 | * x := a*b*(i-1), |
120 | 0 | * then |
121 | 0 | * x^2 = a^2 * b^2 * (i^2 - 2*i + 1) |
122 | 0 | * = a^2 * b^2 * (-2*i) |
123 | 0 | * = a*(-i)*(2*a*b^2) |
124 | 0 | * = a*(-i)*i |
125 | 0 | * = a. |
126 | 0 | * |
127 | 0 | * (This is due to A.O.L. Atkin, |
128 | 0 | * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>, |
129 | 0 | * November 1992.) |
130 | 0 | */ |
131 | 0 |
|
132 | 0 | /* t := 2*a */ |
133 | 0 | if (!BN_mod_lshift1_quick(t, A, p)) |
134 | 0 | goto end; |
135 | 0 | |
136 | 0 | /* b := (2*a)^((|p|-5)/8) */ |
137 | 0 | if (!BN_rshift(q, p, 3)) |
138 | 0 | goto end; |
139 | 0 | q->neg = 0; |
140 | 0 | if (!BN_mod_exp(b, t, q, p, ctx)) |
141 | 0 | goto end; |
142 | 0 | |
143 | 0 | /* y := b^2 */ |
144 | 0 | if (!BN_mod_sqr(y, b, p, ctx)) |
145 | 0 | goto end; |
146 | 0 | |
147 | 0 | /* t := (2*a)*b^2 - 1 */ |
148 | 0 | if (!BN_mod_mul(t, t, y, p, ctx)) |
149 | 0 | goto end; |
150 | 0 | if (!BN_sub_word(t, 1)) |
151 | 0 | goto end; |
152 | 0 | |
153 | 0 | /* x = a*b*t */ |
154 | 0 | if (!BN_mod_mul(x, A, b, p, ctx)) |
155 | 0 | goto end; |
156 | 0 | if (!BN_mod_mul(x, x, t, p, ctx)) |
157 | 0 | goto end; |
158 | 0 | |
159 | 0 | if (!BN_copy(ret, x)) |
160 | 0 | goto end; |
161 | 0 | err = 0; |
162 | 0 | goto vrfy; |
163 | 0 | } |
164 | 0 | |
165 | 0 | /* |
166 | 0 | * e > 2, so we really have to use the Tonelli/Shanks algorithm. First, |
167 | 0 | * find some y that is not a square. |
168 | 0 | */ |
169 | 0 | if (!BN_copy(q, p)) |
170 | 0 | goto end; /* use 'q' as temp */ |
171 | 0 | q->neg = 0; |
172 | 0 | i = 2; |
173 | 0 | do { |
174 | 0 | /* |
175 | 0 | * For efficiency, try small numbers first; if this fails, try random |
176 | 0 | * numbers. |
177 | 0 | */ |
178 | 0 | if (i < 22) { |
179 | 0 | if (!BN_set_word(y, i)) |
180 | 0 | goto end; |
181 | 0 | } else { |
182 | 0 | if (!BN_priv_rand(y, BN_num_bits(p), 0, 0)) |
183 | 0 | goto end; |
184 | 0 | if (BN_ucmp(y, p) >= 0) { |
185 | 0 | if (!(p->neg ? BN_add : BN_sub) (y, y, p)) |
186 | 0 | goto end; |
187 | 0 | } |
188 | 0 | /* now 0 <= y < |p| */ |
189 | 0 | if (BN_is_zero(y)) |
190 | 0 | if (!BN_set_word(y, i)) |
191 | 0 | goto end; |
192 | 0 | } |
193 | 0 | |
194 | 0 | r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */ |
195 | 0 | if (r < -1) |
196 | 0 | goto end; |
197 | 0 | if (r == 0) { |
198 | 0 | /* m divides p */ |
199 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); |
200 | 0 | goto end; |
201 | 0 | } |
202 | 0 | } |
203 | 0 | while (r == 1 && ++i < 82); |
204 | 0 |
|
205 | 0 | if (r != -1) { |
206 | 0 | /* |
207 | 0 | * Many rounds and still no non-square -- this is more likely a bug |
208 | 0 | * than just bad luck. Even if p is not prime, we should have found |
209 | 0 | * some y such that r == -1. |
210 | 0 | */ |
211 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS); |
212 | 0 | goto end; |
213 | 0 | } |
214 | 0 |
|
215 | 0 | /* Here's our actual 'q': */ |
216 | 0 | if (!BN_rshift(q, q, e)) |
217 | 0 | goto end; |
218 | 0 | |
219 | 0 | /* |
220 | 0 | * Now that we have some non-square, we can find an element of order 2^e |
221 | 0 | * by computing its q'th power. |
222 | 0 | */ |
223 | 0 | if (!BN_mod_exp(y, y, q, p, ctx)) |
224 | 0 | goto end; |
225 | 0 | if (BN_is_one(y)) { |
226 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME); |
227 | 0 | goto end; |
228 | 0 | } |
229 | 0 |
|
230 | 0 | /*- |
231 | 0 | * Now we know that (if p is indeed prime) there is an integer |
232 | 0 | * k, 0 <= k < 2^e, such that |
233 | 0 | * |
234 | 0 | * a^q * y^k == 1 (mod p). |
235 | 0 | * |
236 | 0 | * As a^q is a square and y is not, k must be even. |
237 | 0 | * q+1 is even, too, so there is an element |
238 | 0 | * |
239 | 0 | * X := a^((q+1)/2) * y^(k/2), |
240 | 0 | * |
241 | 0 | * and it satisfies |
242 | 0 | * |
243 | 0 | * X^2 = a^q * a * y^k |
244 | 0 | * = a, |
245 | 0 | * |
246 | 0 | * so it is the square root that we are looking for. |
247 | 0 | */ |
248 | 0 |
|
249 | 0 | /* t := (q-1)/2 (note that q is odd) */ |
250 | 0 | if (!BN_rshift1(t, q)) |
251 | 0 | goto end; |
252 | 0 | |
253 | 0 | /* x := a^((q-1)/2) */ |
254 | 0 | if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */ |
255 | 0 | if (!BN_nnmod(t, A, p, ctx)) |
256 | 0 | goto end; |
257 | 0 | if (BN_is_zero(t)) { |
258 | 0 | /* special case: a == 0 (mod p) */ |
259 | 0 | BN_zero(ret); |
260 | 0 | err = 0; |
261 | 0 | goto end; |
262 | 0 | } else if (!BN_one(x)) |
263 | 0 | goto end; |
264 | 0 | } else { |
265 | 0 | if (!BN_mod_exp(x, A, t, p, ctx)) |
266 | 0 | goto end; |
267 | 0 | if (BN_is_zero(x)) { |
268 | 0 | /* special case: a == 0 (mod p) */ |
269 | 0 | BN_zero(ret); |
270 | 0 | err = 0; |
271 | 0 | goto end; |
272 | 0 | } |
273 | 0 | } |
274 | 0 |
|
275 | 0 | /* b := a*x^2 (= a^q) */ |
276 | 0 | if (!BN_mod_sqr(b, x, p, ctx)) |
277 | 0 | goto end; |
278 | 0 | if (!BN_mod_mul(b, b, A, p, ctx)) |
279 | 0 | goto end; |
280 | 0 | |
281 | 0 | /* x := a*x (= a^((q+1)/2)) */ |
282 | 0 | if (!BN_mod_mul(x, x, A, p, ctx)) |
283 | 0 | goto end; |
284 | 0 | |
285 | 0 | while (1) { |
286 | 0 | /*- |
287 | 0 | * Now b is a^q * y^k for some even k (0 <= k < 2^E |
288 | 0 | * where E refers to the original value of e, which we |
289 | 0 | * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2). |
290 | 0 | * |
291 | 0 | * We have a*b = x^2, |
292 | 0 | * y^2^(e-1) = -1, |
293 | 0 | * b^2^(e-1) = 1. |
294 | 0 | */ |
295 | 0 |
|
296 | 0 | if (BN_is_one(b)) { |
297 | 0 | if (!BN_copy(ret, x)) |
298 | 0 | goto end; |
299 | 0 | err = 0; |
300 | 0 | goto vrfy; |
301 | 0 | } |
302 | 0 | |
303 | 0 | /* find smallest i such that b^(2^i) = 1 */ |
304 | 0 | i = 1; |
305 | 0 | if (!BN_mod_sqr(t, b, p, ctx)) |
306 | 0 | goto end; |
307 | 0 | while (!BN_is_one(t)) { |
308 | 0 | i++; |
309 | 0 | if (i == e) { |
310 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); |
311 | 0 | goto end; |
312 | 0 | } |
313 | 0 | if (!BN_mod_mul(t, t, t, p, ctx)) |
314 | 0 | goto end; |
315 | 0 | } |
316 | 0 |
|
317 | 0 | /* t := y^2^(e - i - 1) */ |
318 | 0 | if (!BN_copy(t, y)) |
319 | 0 | goto end; |
320 | 0 | for (j = e - i - 1; j > 0; j--) { |
321 | 0 | if (!BN_mod_sqr(t, t, p, ctx)) |
322 | 0 | goto end; |
323 | 0 | } |
324 | 0 | if (!BN_mod_mul(y, t, t, p, ctx)) |
325 | 0 | goto end; |
326 | 0 | if (!BN_mod_mul(x, x, t, p, ctx)) |
327 | 0 | goto end; |
328 | 0 | if (!BN_mod_mul(b, b, y, p, ctx)) |
329 | 0 | goto end; |
330 | 0 | e = i; |
331 | 0 | } |
332 | 0 |
|
333 | 0 | vrfy: |
334 | 0 | if (!err) { |
335 | 0 | /* |
336 | 0 | * verify the result -- the input might have been not a square (test |
337 | 0 | * added in 0.9.8) |
338 | 0 | */ |
339 | 0 |
|
340 | 0 | if (!BN_mod_sqr(x, ret, p, ctx)) |
341 | 0 | err = 1; |
342 | 0 |
|
343 | 0 | if (!err && 0 != BN_cmp(x, A)) { |
344 | 0 | BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE); |
345 | 0 | err = 1; |
346 | 0 | } |
347 | 0 | } |
348 | 0 |
|
349 | 0 | end: |
350 | 0 | if (err) { |
351 | 0 | if (ret != in) |
352 | 0 | BN_clear_free(ret); |
353 | 0 | ret = NULL; |
354 | 0 | } |
355 | 0 | BN_CTX_end(ctx); |
356 | 0 | bn_check_top(ret); |
357 | 0 | return ret; |
358 | 0 | } |