]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bn/bn_sqrt.c
mark all block comments that need format preserving so that
[thirdparty/openssl.git] / crypto / bn / bn_sqrt.c
CommitLineData
fea4280a 1/* crypto/bn/bn_sqrt.c */
cd2eebfd
BM
2/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * and Bodo Moeller for the OpenSSL project. */
4/* ====================================================================
5 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23 *
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
28 *
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
51 *
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
55 *
56 */
57
58#include "cryptlib.h"
59#include "bn_lcl.h"
60
61
62BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
63/* Returns 'ret' such that
64 * ret^2 == a (mod p),
65 * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course
66 * in Algebraic Computational Number Theory", algorithm 1.5.1).
67 * 'p' must be prime!
68 */
69 {
70 BIGNUM *ret = in;
71 int err = 1;
72 int r;
6fb60a84 73 BIGNUM *A, *b, *q, *t, *x, *y;
cd2eebfd
BM
74 int e, i, j;
75
76 if (!BN_is_odd(p) || BN_abs_is_word(p, 1))
77 {
78 if (BN_abs_is_word(p, 2))
79 {
80 if (ret == NULL)
81 ret = BN_new();
82 if (ret == NULL)
83 goto end;
84 if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
85 {
7534d131
BM
86 if (ret != in)
87 BN_free(ret);
cd2eebfd
BM
88 return NULL;
89 }
d870740c 90 bn_check_top(ret);
cd2eebfd
BM
91 return ret;
92 }
93
94 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
95 return(NULL);
96 }
97
bac68541
BM
98 if (BN_is_zero(a) || BN_is_one(a))
99 {
100 if (ret == NULL)
101 ret = BN_new();
102 if (ret == NULL)
103 goto end;
104 if (!BN_set_word(ret, BN_is_one(a)))
105 {
7534d131
BM
106 if (ret != in)
107 BN_free(ret);
bac68541
BM
108 return NULL;
109 }
d870740c 110 bn_check_top(ret);
bac68541
BM
111 return ret;
112 }
113
cd2eebfd 114 BN_CTX_start(ctx);
6fb60a84 115 A = BN_CTX_get(ctx);
cd2eebfd
BM
116 b = BN_CTX_get(ctx);
117 q = BN_CTX_get(ctx);
118 t = BN_CTX_get(ctx);
119 x = BN_CTX_get(ctx);
120 y = BN_CTX_get(ctx);
121 if (y == NULL) goto end;
122
123 if (ret == NULL)
124 ret = BN_new();
125 if (ret == NULL) goto end;
126
6fb60a84
BM
127 /* A = a mod p */
128 if (!BN_nnmod(A, a, p, ctx)) goto end;
129
cd2eebfd
BM
130 /* now write |p| - 1 as 2^e*q where q is odd */
131 e = 1;
132 while (!BN_is_bit_set(p, e))
133 e++;
80d89e6a 134 /* we'll set q later (if needed) */
cd2eebfd
BM
135
136 if (e == 1)
137 {
3e9a08ec
TH
138 /*-
139 * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
80d89e6a 140 * modulo (|p|-1)/2, and square roots can be computed
cd2eebfd
BM
141 * directly by modular exponentiation.
142 * We have
80d89e6a
BM
143 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
144 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
cd2eebfd 145 */
bac68541 146 if (!BN_rshift(q, p, 2)) goto end;
bc5f2740 147 q->neg = 0;
bac68541 148 if (!BN_add_word(q, 1)) goto end;
6fb60a84 149 if (!BN_mod_exp(ret, A, q, p, ctx)) goto end;
cd2eebfd 150 err = 0;
6fb60a84 151 goto vrfy;
cd2eebfd
BM
152 }
153
bac68541
BM
154 if (e == 2)
155 {
3e9a08ec
TH
156 /*-
157 * |p| == 5 (mod 8)
bac68541
BM
158 *
159 * In this case 2 is always a non-square since
160 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
161 * So if a really is a square, then 2*a is a non-square.
162 * Thus for
80d89e6a 163 * b := (2*a)^((|p|-5)/8),
bac68541
BM
164 * i := (2*a)*b^2
165 * we have
80d89e6a 166 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
bac68541
BM
167 * = (2*a)^((p-1)/2)
168 * = -1;
169 * so if we set
170 * x := a*b*(i-1),
171 * then
172 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
173 * = a^2 * b^2 * (-2*i)
174 * = a*(-i)*(2*a*b^2)
175 * = a*(-i)*i
176 * = a.
177 *
178 * (This is due to A.O.L. Atkin,
179 * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
180 * November 1992.)
181 */
182
bac68541 183 /* t := 2*a */
6fb60a84 184 if (!BN_mod_lshift1_quick(t, A, p)) goto end;
bac68541 185
80d89e6a 186 /* b := (2*a)^((|p|-5)/8) */
bac68541 187 if (!BN_rshift(q, p, 3)) goto end;
bc5f2740 188 q->neg = 0;
bac68541
BM
189 if (!BN_mod_exp(b, t, q, p, ctx)) goto end;
190
191 /* y := b^2 */
192 if (!BN_mod_sqr(y, b, p, ctx)) goto end;
193
194 /* t := (2*a)*b^2 - 1*/
195 if (!BN_mod_mul(t, t, y, p, ctx)) goto end;
aa66eba7 196 if (!BN_sub_word(t, 1)) goto end;
bac68541
BM
197
198 /* x = a*b*t */
6fb60a84 199 if (!BN_mod_mul(x, A, b, p, ctx)) goto end;
bac68541
BM
200 if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
201
202 if (!BN_copy(ret, x)) goto end;
203 err = 0;
6fb60a84 204 goto vrfy;
bac68541
BM
205 }
206
207 /* e > 2, so we really have to use the Tonelli/Shanks algorithm.
cd2eebfd 208 * First, find some y that is not a square. */
80d89e6a
BM
209 if (!BN_copy(q, p)) goto end; /* use 'q' as temp */
210 q->neg = 0;
25439b76 211 i = 2;
cd2eebfd
BM
212 do
213 {
214 /* For efficiency, try small numbers first;
215 * if this fails, try random numbers.
216 */
25439b76 217 if (i < 22)
cd2eebfd
BM
218 {
219 if (!BN_set_word(y, i)) goto end;
220 }
221 else
222 {
223 if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;
224 if (BN_ucmp(y, p) >= 0)
225 {
226 if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;
227 }
228 /* now 0 <= y < |p| */
229 if (BN_is_zero(y))
230 if (!BN_set_word(y, i)) goto end;
231 }
232
80d89e6a 233 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
cd2eebfd
BM
234 if (r < -1) goto end;
235 if (r == 0)
236 {
237 /* m divides p */
238 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
239 goto end;
240 }
241 }
25439b76 242 while (r == 1 && ++i < 82);
cd2eebfd
BM
243
244 if (r != -1)
245 {
246 /* Many rounds and still no non-square -- this is more likely
247 * a bug than just bad luck.
248 * Even if p is not prime, we should have found some y
249 * such that r == -1.
250 */
251 BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
252 goto end;
253 }
254
80d89e6a
BM
255 /* Here's our actual 'q': */
256 if (!BN_rshift(q, q, e)) goto end;
cd2eebfd
BM
257
258 /* Now that we have some non-square, we can find an element
259 * of order 2^e by computing its q'th power. */
260 if (!BN_mod_exp(y, y, q, p, ctx)) goto end;
261 if (BN_is_one(y))
262 {
263 BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
264 goto end;
265 }
266
3e9a08ec
TH
267 /*-
268 * Now we know that (if p is indeed prime) there is an integer
cd2eebfd
BM
269 * k, 0 <= k < 2^e, such that
270 *
271 * a^q * y^k == 1 (mod p).
272 *
273 * As a^q is a square and y is not, k must be even.
274 * q+1 is even, too, so there is an element
275 *
276 * X := a^((q+1)/2) * y^(k/2),
277 *
278 * and it satisfies
279 *
280 * X^2 = a^q * a * y^k
281 * = a,
282 *
283 * so it is the square root that we are looking for.
284 */
285
286 /* t := (q-1)/2 (note that q is odd) */
287 if (!BN_rshift1(t, q)) goto end;
288
289 /* x := a^((q-1)/2) */
290 if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
291 {
6fb60a84 292 if (!BN_nnmod(t, A, p, ctx)) goto end;
cd2eebfd
BM
293 if (BN_is_zero(t))
294 {
295 /* special case: a == 0 (mod p) */
b6358c89 296 BN_zero(ret);
cd2eebfd
BM
297 err = 0;
298 goto end;
299 }
300 else
301 if (!BN_one(x)) goto end;
302 }
303 else
304 {
6fb60a84 305 if (!BN_mod_exp(x, A, t, p, ctx)) goto end;
cd2eebfd
BM
306 if (BN_is_zero(x))
307 {
308 /* special case: a == 0 (mod p) */
b6358c89 309 BN_zero(ret);
cd2eebfd
BM
310 err = 0;
311 goto end;
312 }
313 }
314
315 /* b := a*x^2 (= a^q) */
316 if (!BN_mod_sqr(b, x, p, ctx)) goto end;
6fb60a84 317 if (!BN_mod_mul(b, b, A, p, ctx)) goto end;
cd2eebfd
BM
318
319 /* x := a*x (= a^((q+1)/2)) */
6fb60a84 320 if (!BN_mod_mul(x, x, A, p, ctx)) goto end;
cd2eebfd
BM
321
322 while (1)
323 {
3e9a08ec
TH
324 /*-
325 * Now b is a^q * y^k for some even k (0 <= k < 2^E
cd2eebfd
BM
326 * where E refers to the original value of e, which we
327 * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
328 *
329 * We have a*b = x^2,
330 * y^2^(e-1) = -1,
331 * b^2^(e-1) = 1.
332 */
333
334 if (BN_is_one(b))
335 {
336 if (!BN_copy(ret, x)) goto end;
337 err = 0;
6fb60a84 338 goto vrfy;
cd2eebfd
BM
339 }
340
341
342 /* find smallest i such that b^(2^i) = 1 */
343 i = 1;
344 if (!BN_mod_sqr(t, b, p, ctx)) goto end;
345 while (!BN_is_one(t))
346 {
347 i++;
348 if (i == e)
349 {
350 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
351 goto end;
352 }
353 if (!BN_mod_mul(t, t, t, p, ctx)) goto end;
354 }
355
356
357 /* t := y^2^(e - i - 1) */
358 if (!BN_copy(t, y)) goto end;
359 for (j = e - i - 1; j > 0; j--)
360 {
361 if (!BN_mod_sqr(t, t, p, ctx)) goto end;
362 }
363 if (!BN_mod_mul(y, t, t, p, ctx)) goto end;
364 if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
365 if (!BN_mod_mul(b, b, y, p, ctx)) goto end;
366 e = i;
367 }
368
6fb60a84
BM
369 vrfy:
370 if (!err)
371 {
372 /* verify the result -- the input might have been not a square
373 * (test added in 0.9.8) */
374
375 if (!BN_mod_sqr(x, ret, p, ctx))
376 err = 1;
377
378 if (!err && 0 != BN_cmp(x, A))
379 {
380 BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
381 err = 1;
382 }
383 }
384
cd2eebfd
BM
385 end:
386 if (err)
387 {
388 if (ret != NULL && ret != in)
389 {
390 BN_clear_free(ret);
391 }
392 ret = NULL;
393 }
394 BN_CTX_end(ctx);
d870740c 395 bn_check_top(ret);
cd2eebfd
BM
396 return ret;
397 }