VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/crypto/bn/bn_sqrt.c@ 94096

最後變更 在這個檔案從94096是 94082,由 vboxsync 提交於 3 年 前

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

檔案大小: 9.3 KB
 
1/*
2 * Copyright 2000-2021 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
13BIGNUM *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{
20 BIGNUM *ret = in;
21 int err = 1;
22 int r;
23 BIGNUM *A, *b, *q, *t, *x, *y;
24 int e, i, j;
25 int used_ctx = 0;
26
27 if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
28 if (BN_abs_is_word(p, 2)) {
29 if (ret == NULL)
30 ret = BN_new();
31 if (ret == NULL)
32 goto end;
33 if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
34 if (ret != in)
35 BN_free(ret);
36 return NULL;
37 }
38 bn_check_top(ret);
39 return ret;
40 }
41
42 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
43 return NULL;
44 }
45
46 if (BN_is_zero(a) || BN_is_one(a)) {
47 if (ret == NULL)
48 ret = BN_new();
49 if (ret == NULL)
50 goto end;
51 if (!BN_set_word(ret, BN_is_one(a))) {
52 if (ret != in)
53 BN_free(ret);
54 return NULL;
55 }
56 bn_check_top(ret);
57 return ret;
58 }
59
60 BN_CTX_start(ctx);
61 used_ctx = 1;
62 A = BN_CTX_get(ctx);
63 b = BN_CTX_get(ctx);
64 q = BN_CTX_get(ctx);
65 t = BN_CTX_get(ctx);
66 x = BN_CTX_get(ctx);
67 y = BN_CTX_get(ctx);
68 if (y == NULL)
69 goto end;
70
71 if (ret == NULL)
72 ret = BN_new();
73 if (ret == NULL)
74 goto end;
75
76 /* A = a mod p */
77 if (!BN_nnmod(A, a, p, ctx))
78 goto end;
79
80 /* now write |p| - 1 as 2^e*q where q is odd */
81 e = 1;
82 while (!BN_is_bit_set(p, e))
83 e++;
84 /* we'll set q later (if needed) */
85
86 if (e == 1) {
87 /*-
88 * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
89 * modulo (|p|-1)/2, and square roots can be computed
90 * directly by modular exponentiation.
91 * We have
92 * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
93 * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
94 */
95 if (!BN_rshift(q, p, 2))
96 goto end;
97 q->neg = 0;
98 if (!BN_add_word(q, 1))
99 goto end;
100 if (!BN_mod_exp(ret, A, q, p, ctx))
101 goto end;
102 err = 0;
103 goto vrfy;
104 }
105
106 if (e == 2) {
107 /*-
108 * |p| == 5 (mod 8)
109 *
110 * In this case 2 is always a non-square since
111 * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
112 * So if a really is a square, then 2*a is a non-square.
113 * Thus for
114 * b := (2*a)^((|p|-5)/8),
115 * i := (2*a)*b^2
116 * we have
117 * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
118 * = (2*a)^((p-1)/2)
119 * = -1;
120 * so if we set
121 * x := a*b*(i-1),
122 * then
123 * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
124 * = a^2 * b^2 * (-2*i)
125 * = a*(-i)*(2*a*b^2)
126 * = a*(-i)*i
127 * = a.
128 *
129 * (This is due to A.O.L. Atkin,
130 * Subject: Square Roots and Cognate Matters modulo p=8n+5.
131 * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
132 * November 1992.)
133 */
134
135 /* t := 2*a */
136 if (!BN_mod_lshift1_quick(t, A, p))
137 goto end;
138
139 /* b := (2*a)^((|p|-5)/8) */
140 if (!BN_rshift(q, p, 3))
141 goto end;
142 q->neg = 0;
143 if (!BN_mod_exp(b, t, q, p, ctx))
144 goto end;
145
146 /* y := b^2 */
147 if (!BN_mod_sqr(y, b, p, ctx))
148 goto end;
149
150 /* t := (2*a)*b^2 - 1 */
151 if (!BN_mod_mul(t, t, y, p, ctx))
152 goto end;
153 if (!BN_sub_word(t, 1))
154 goto end;
155
156 /* x = a*b*t */
157 if (!BN_mod_mul(x, A, b, p, ctx))
158 goto end;
159 if (!BN_mod_mul(x, x, t, p, ctx))
160 goto end;
161
162 if (!BN_copy(ret, x))
163 goto end;
164 err = 0;
165 goto vrfy;
166 }
167
168 /*
169 * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
170 * find some y that is not a square.
171 */
172 if (!BN_copy(q, p))
173 goto end; /* use 'q' as temp */
174 q->neg = 0;
175 i = 2;
176 do {
177 /*
178 * For efficiency, try small numbers first; if this fails, try random
179 * numbers.
180 */
181 if (i < 22) {
182 if (!BN_set_word(y, i))
183 goto end;
184 } else {
185 if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx))
186 goto end;
187 if (BN_ucmp(y, p) >= 0) {
188 if (!(p->neg ? BN_add : BN_sub) (y, y, p))
189 goto end;
190 }
191 /* now 0 <= y < |p| */
192 if (BN_is_zero(y))
193 if (!BN_set_word(y, i))
194 goto end;
195 }
196
197 r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
198 if (r < -1)
199 goto end;
200 if (r == 0) {
201 /* m divides p */
202 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
203 goto end;
204 }
205 }
206 while (r == 1 && ++i < 82);
207
208 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 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
215 goto end;
216 }
217
218 /* Here's our actual 'q': */
219 if (!BN_rshift(q, q, e))
220 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 if (!BN_mod_exp(y, y, q, p, ctx))
227 goto end;
228 if (BN_is_one(y)) {
229 ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
230 goto end;
231 }
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 if (!BN_rshift1(t, q))
254 goto end;
255
256 /* x := a^((q-1)/2) */
257 if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
258 if (!BN_nnmod(t, A, p, ctx))
259 goto end;
260 if (BN_is_zero(t)) {
261 /* special case: a == 0 (mod p) */
262 BN_zero(ret);
263 err = 0;
264 goto end;
265 } else if (!BN_one(x))
266 goto end;
267 } else {
268 if (!BN_mod_exp(x, A, t, p, ctx))
269 goto end;
270 if (BN_is_zero(x)) {
271 /* special case: a == 0 (mod p) */
272 BN_zero(ret);
273 err = 0;
274 goto end;
275 }
276 }
277
278 /* b := a*x^2 (= a^q) */
279 if (!BN_mod_sqr(b, x, p, ctx))
280 goto end;
281 if (!BN_mod_mul(b, b, A, p, ctx))
282 goto end;
283
284 /* x := a*x (= a^((q+1)/2)) */
285 if (!BN_mod_mul(x, x, A, p, ctx))
286 goto end;
287
288 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 if (BN_is_one(b)) {
300 if (!BN_copy(ret, x))
301 goto end;
302 err = 0;
303 goto vrfy;
304 }
305
306 /* find smallest i such that b^(2^i) = 1 */
307 i = 1;
308 if (!BN_mod_sqr(t, b, p, ctx))
309 goto end;
310 while (!BN_is_one(t)) {
311 i++;
312 if (i == e) {
313 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
314 goto end;
315 }
316 if (!BN_mod_mul(t, t, t, p, ctx))
317 goto end;
318 }
319
320 /* t := y^2^(e - i - 1) */
321 if (!BN_copy(t, y))
322 goto end;
323 for (j = e - i - 1; j > 0; j--) {
324 if (!BN_mod_sqr(t, t, p, ctx))
325 goto end;
326 }
327 if (!BN_mod_mul(y, t, t, p, ctx))
328 goto end;
329 if (!BN_mod_mul(x, x, t, p, ctx))
330 goto end;
331 if (!BN_mod_mul(b, b, y, p, ctx))
332 goto end;
333 e = i;
334 }
335
336 vrfy:
337 if (!err) {
338 /*
339 * verify the result -- the input might have been not a square (test
340 * added in 0.9.8)
341 */
342
343 if (!BN_mod_sqr(x, ret, p, ctx))
344 err = 1;
345
346 if (!err && 0 != BN_cmp(x, A)) {
347 ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
348 err = 1;
349 }
350 }
351
352 end:
353 if (err) {
354 if (ret != in)
355 BN_clear_free(ret);
356 ret = NULL;
357 }
358 if (used_ctx)
359 BN_CTX_end(ctx);
360 bn_check_top(ret);
361 return ret;
362}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette