xref: /aosp_15_r20/external/boringssl/src/crypto/fipsmodule/bn/sqrt.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Written by Lenka Fibikova <[email protected]>
2  * and Bodo Moeller for the OpenSSL project. */
3 /* ====================================================================
4  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this
19  *    software must display the following acknowledgment:
20  *    "This product includes software developed by the OpenSSL Project
21  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22  *
23  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For written permission, please contact
26  *    [email protected].
27  *
28  * 5. Products derived from this software may not be called "OpenSSL"
29  *    nor may "OpenSSL" appear in their names without prior written
30  *    permission of the OpenSSL Project.
31  *
32  * 6. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by the OpenSSL Project
35  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This product includes cryptographic software written by Eric Young
52  * ([email protected]).  This product includes software written by Tim
53  * Hudson ([email protected]). */
54 
55 #include <openssl/bn.h>
56 
57 #include <openssl/err.h>
58 
59 #include "internal.h"
60 
61 
BN_mod_sqrt(BIGNUM * in,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx)62 BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
63   // Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm
64   // (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory",
65   // algorithm 1.5.1). |p| is assumed to be a prime.
66 
67   BIGNUM *ret = in;
68   int err = 1;
69   int r;
70   BIGNUM *A, *b, *q, *t, *x, *y;
71   int e, i, j;
72 
73   if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
74     if (BN_abs_is_word(p, 2)) {
75       if (ret == NULL) {
76         ret = BN_new();
77       }
78       if (ret == NULL ||
79           !BN_set_word(ret, BN_is_bit_set(a, 0))) {
80         if (ret != in) {
81           BN_free(ret);
82         }
83         return NULL;
84       }
85       return ret;
86     }
87 
88     OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
89     return NULL;
90   }
91 
92   if (BN_is_zero(a) || BN_is_one(a)) {
93     if (ret == NULL) {
94       ret = BN_new();
95     }
96     if (ret == NULL ||
97         !BN_set_word(ret, BN_is_one(a))) {
98       if (ret != in) {
99         BN_free(ret);
100       }
101       return NULL;
102     }
103     return ret;
104   }
105 
106   BN_CTX_start(ctx);
107   A = BN_CTX_get(ctx);
108   b = BN_CTX_get(ctx);
109   q = BN_CTX_get(ctx);
110   t = BN_CTX_get(ctx);
111   x = BN_CTX_get(ctx);
112   y = BN_CTX_get(ctx);
113   if (y == NULL) {
114     goto end;
115   }
116 
117   if (ret == NULL) {
118     ret = BN_new();
119   }
120   if (ret == NULL) {
121     goto end;
122   }
123 
124   // A = a mod p
125   if (!BN_nnmod(A, a, p, ctx)) {
126     goto end;
127   }
128 
129   // now write  |p| - 1  as  2^e*q  where  q  is odd
130   e = 1;
131   while (!BN_is_bit_set(p, e)) {
132     e++;
133   }
134   // we'll set  q  later (if needed)
135 
136   if (e == 1) {
137     // The easy case:  (|p|-1)/2  is odd, so 2 has an inverse
138     // modulo  (|p|-1)/2,  and square roots can be computed
139     // directly by modular exponentiation.
140     // We have
141     //     2 * (|p|+1)/4 == 1   (mod (|p|-1)/2),
142     // so we can use exponent  (|p|+1)/4,  i.e.  (|p|-3)/4 + 1.
143     if (!BN_rshift(q, p, 2)) {
144       goto end;
145     }
146     q->neg = 0;
147     if (!BN_add_word(q, 1) ||
148         !BN_mod_exp_mont(ret, A, q, p, ctx, NULL)) {
149       goto end;
150     }
151     err = 0;
152     goto vrfy;
153   }
154 
155   if (e == 2) {
156     // |p| == 5  (mod 8)
157     //
158     // In this case  2  is always a non-square since
159     // Legendre(2,p) = (-1)^((p^2-1)/8)  for any odd prime.
160     // So if  a  really is a square, then  2*a  is a non-square.
161     // Thus for
162     //      b := (2*a)^((|p|-5)/8),
163     //      i := (2*a)*b^2
164     // we have
165     //     i^2 = (2*a)^((1 + (|p|-5)/4)*2)
166     //         = (2*a)^((p-1)/2)
167     //         = -1;
168     // so if we set
169     //      x := a*b*(i-1),
170     // then
171     //     x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
172     //         = a^2 * b^2 * (-2*i)
173     //         = a*(-i)*(2*a*b^2)
174     //         = a*(-i)*i
175     //         = a.
176     //
177     // (This is due to A.O.L. Atkin,
178     // <URL:
179     //http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
180     // November 1992.)
181 
182     // t := 2*a
183     if (!bn_mod_lshift1_consttime(t, A, p, ctx)) {
184       goto end;
185     }
186 
187     // b := (2*a)^((|p|-5)/8)
188     if (!BN_rshift(q, p, 3)) {
189       goto end;
190     }
191     q->neg = 0;
192     if (!BN_mod_exp_mont(b, t, q, p, ctx, NULL)) {
193       goto end;
194     }
195 
196     // y := b^2
197     if (!BN_mod_sqr(y, b, p, ctx)) {
198       goto end;
199     }
200 
201     // t := (2*a)*b^2 - 1
202     if (!BN_mod_mul(t, t, y, p, ctx) ||
203         !BN_sub_word(t, 1)) {
204       goto end;
205     }
206 
207     // x = a*b*t
208     if (!BN_mod_mul(x, A, b, p, ctx) ||
209         !BN_mod_mul(x, x, t, p, ctx)) {
210       goto end;
211     }
212 
213     if (!BN_copy(ret, x)) {
214       goto end;
215     }
216     err = 0;
217     goto vrfy;
218   }
219 
220   // e > 2, so we really have to use the Tonelli/Shanks algorithm.
221   // First, find some  y  that is not a square.
222   if (!BN_copy(q, p)) {
223     goto end;  // use 'q' as temp
224   }
225   q->neg = 0;
226   i = 2;
227   do {
228     // For efficiency, try small numbers first;
229     // if this fails, try random numbers.
230     if (i < 22) {
231       if (!BN_set_word(y, i)) {
232         goto end;
233       }
234     } else {
235       if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) {
236         goto end;
237       }
238       if (BN_ucmp(y, p) >= 0) {
239         if (!(p->neg ? BN_add : BN_sub)(y, y, p)) {
240           goto end;
241         }
242       }
243       // now 0 <= y < |p|
244       if (BN_is_zero(y)) {
245         if (!BN_set_word(y, i)) {
246           goto end;
247         }
248       }
249     }
250 
251     r = bn_jacobi(y, q, ctx);  // here 'q' is |p|
252     if (r < -1) {
253       goto end;
254     }
255     if (r == 0) {
256       // m divides p
257       OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
258       goto end;
259     }
260   } while (r == 1 && ++i < 82);
261 
262   if (r != -1) {
263     // Many rounds and still no non-square -- this is more likely
264     // a bug than just bad luck.
265     // Even if  p  is not prime, we should have found some  y
266     // such that r == -1.
267     OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
268     goto end;
269   }
270 
271   // Here's our actual 'q':
272   if (!BN_rshift(q, q, e)) {
273     goto end;
274   }
275 
276   // Now that we have some non-square, we can find an element
277   // of order  2^e  by computing its q'th power.
278   if (!BN_mod_exp_mont(y, y, q, p, ctx, NULL)) {
279     goto end;
280   }
281   if (BN_is_one(y)) {
282     OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
283     goto end;
284   }
285 
286   // Now we know that (if  p  is indeed prime) there is an integer
287   // k,  0 <= k < 2^e,  such that
288   //
289   //      a^q * y^k == 1   (mod p).
290   //
291   // As  a^q  is a square and  y  is not,  k  must be even.
292   // q+1  is even, too, so there is an element
293   //
294   //     X := a^((q+1)/2) * y^(k/2),
295   //
296   // and it satisfies
297   //
298   //     X^2 = a^q * a     * y^k
299   //         = a,
300   //
301   // so it is the square root that we are looking for.
302 
303   // t := (q-1)/2  (note that  q  is odd)
304   if (!BN_rshift1(t, q)) {
305     goto end;
306   }
307 
308   // x := a^((q-1)/2)
309   if (BN_is_zero(t)) {  // special case: p = 2^e + 1
310     if (!BN_nnmod(t, A, p, ctx)) {
311       goto end;
312     }
313     if (BN_is_zero(t)) {
314       // special case: a == 0  (mod p)
315       BN_zero(ret);
316       err = 0;
317       goto end;
318     } else if (!BN_one(x)) {
319       goto end;
320     }
321   } else {
322     if (!BN_mod_exp_mont(x, A, t, p, ctx, NULL)) {
323       goto end;
324     }
325     if (BN_is_zero(x)) {
326       // special case: a == 0  (mod p)
327       BN_zero(ret);
328       err = 0;
329       goto end;
330     }
331   }
332 
333   // b := a*x^2  (= a^q)
334   if (!BN_mod_sqr(b, x, p, ctx) ||
335       !BN_mod_mul(b, b, A, p, ctx)) {
336     goto end;
337   }
338 
339   // x := a*x    (= a^((q+1)/2))
340   if (!BN_mod_mul(x, x, A, p, ctx)) {
341     goto end;
342   }
343 
344   while (1) {
345     // Now  b  is  a^q * y^k  for some even  k  (0 <= k < 2^E
346     // where  E  refers to the original value of  e,  which we
347     // don't keep in a variable),  and  x  is  a^((q+1)/2) * y^(k/2).
348     //
349     // We have  a*b = x^2,
350     //    y^2^(e-1) = -1,
351     //    b^2^(e-1) = 1.
352     if (BN_is_one(b)) {
353       if (!BN_copy(ret, x)) {
354         goto end;
355       }
356       err = 0;
357       goto vrfy;
358     }
359 
360     // Find the smallest i, 0 < i < e, such that b^(2^i) = 1
361     for (i = 1; i < e; i++) {
362       if (i == 1) {
363         if (!BN_mod_sqr(t, b, p, ctx)) {
364           goto end;
365         }
366       } else {
367         if (!BN_mod_mul(t, t, t, p, ctx)) {
368           goto end;
369         }
370       }
371       if (BN_is_one(t)) {
372         break;
373       }
374     }
375     // If not found, a is not a square or p is not a prime.
376     if (i >= e) {
377       OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
378       goto end;
379     }
380 
381     // t := y^2^(e - i - 1)
382     if (!BN_copy(t, y)) {
383       goto end;
384     }
385     for (j = e - i - 1; j > 0; j--) {
386       if (!BN_mod_sqr(t, t, p, ctx)) {
387         goto end;
388       }
389     }
390     if (!BN_mod_mul(y, t, t, p, ctx) ||
391         !BN_mod_mul(x, x, t, p, ctx) ||
392         !BN_mod_mul(b, b, y, p, ctx)) {
393       goto end;
394     }
395 
396     // e decreases each iteration, so this loop will terminate.
397     assert(i < e);
398     e = i;
399   }
400 
401 vrfy:
402   if (!err) {
403     // Verify the result. The input might have been not a square.
404     if (!BN_mod_sqr(x, ret, p, ctx)) {
405       err = 1;
406     }
407 
408     if (!err && 0 != BN_cmp(x, A)) {
409       OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
410       err = 1;
411     }
412   }
413 
414 end:
415   if (err) {
416     if (ret != in) {
417       BN_clear_free(ret);
418     }
419     ret = NULL;
420   }
421   BN_CTX_end(ctx);
422   return ret;
423 }
424 
BN_sqrt(BIGNUM * out_sqrt,const BIGNUM * in,BN_CTX * ctx)425 int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) {
426   BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2;
427   int ok = 0, last_delta_valid = 0;
428 
429   if (in->neg) {
430     OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
431     return 0;
432   }
433   if (BN_is_zero(in)) {
434     BN_zero(out_sqrt);
435     return 1;
436   }
437 
438   BN_CTX_start(ctx);
439   if (out_sqrt == in) {
440     estimate = BN_CTX_get(ctx);
441   } else {
442     estimate = out_sqrt;
443   }
444   tmp = BN_CTX_get(ctx);
445   last_delta = BN_CTX_get(ctx);
446   delta = BN_CTX_get(ctx);
447   if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) {
448     goto err;
449   }
450 
451   // We estimate that the square root of an n-bit number is 2^{n/2}.
452   if (!BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2)) {
453     goto err;
454   }
455 
456   // This is Newton's method for finding a root of the equation |estimate|^2 -
457   // |in| = 0.
458   for (;;) {
459     // |estimate| = 1/2 * (|estimate| + |in|/|estimate|)
460     if (!BN_div(tmp, NULL, in, estimate, ctx) ||
461         !BN_add(tmp, tmp, estimate) ||
462         !BN_rshift1(estimate, tmp) ||
463         // |tmp| = |estimate|^2
464         !BN_sqr(tmp, estimate, ctx) ||
465         // |delta| = |in| - |tmp|
466         !BN_sub(delta, in, tmp)) {
467       OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
468       goto err;
469     }
470 
471     delta->neg = 0;
472     // The difference between |in| and |estimate| squared is required to always
473     // decrease. This ensures that the loop always terminates, but I don't have
474     // a proof that it always finds the square root for a given square.
475     if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) {
476       break;
477     }
478 
479     last_delta_valid = 1;
480 
481     tmp2 = last_delta;
482     last_delta = delta;
483     delta = tmp2;
484   }
485 
486   if (BN_cmp(tmp, in) != 0) {
487     OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
488     goto err;
489   }
490 
491   ok = 1;
492 
493 err:
494   if (ok && out_sqrt == in && !BN_copy(out_sqrt, estimate)) {
495     ok = 0;
496   }
497   BN_CTX_end(ctx);
498   return ok;
499 }
500