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
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, otherwise an error or
18 * an incorrect "result" will be returned.
19 */
20{
21    BIGNUM *ret = in;
22    int err = 1;
23    int r;
24    BIGNUM *A, *b, *q, *t, *x, *y;
25    int e, i, j;
26    int used_ctx = 0;
27
28    if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
29        if (BN_abs_is_word(p, 2)) {
30            if (ret == NULL)
31                ret = BN_new();
32            if (ret == NULL)
33                goto end;
34            if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
35                if (ret != in)
36                    BN_free(ret);
37                return NULL;
38            }
39            bn_check_top(ret);
40            return ret;
41        }
42
43        ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
44        return NULL;
45    }
46
47    if (BN_is_zero(a) || BN_is_one(a)) {
48        if (ret == NULL)
49            ret = BN_new();
50        if (ret == NULL)
51            goto end;
52        if (!BN_set_word(ret, BN_is_one(a))) {
53            if (ret != in)
54                BN_free(ret);
55            return NULL;
56        }
57        bn_check_top(ret);
58        return ret;
59    }
60
61    BN_CTX_start(ctx);
62    used_ctx = 1;
63    A = BN_CTX_get(ctx);
64    b = BN_CTX_get(ctx);
65    q = BN_CTX_get(ctx);
66    t = BN_CTX_get(ctx);
67    x = BN_CTX_get(ctx);
68    y = BN_CTX_get(ctx);
69    if (y == NULL)
70        goto end;
71
72    if (ret == NULL)
73        ret = BN_new();
74    if (ret == NULL)
75        goto end;
76
77    /* A = a mod p */
78    if (!BN_nnmod(A, a, p, ctx))
79        goto end;
80
81    /* now write  |p| - 1  as  2^e*q  where  q  is odd */
82    e = 1;
83    while (!BN_is_bit_set(p, e))
84        e++;
85    /* we'll set  q  later (if needed) */
86
87    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        if (!BN_rshift(q, p, 2))
97            goto end;
98        q->neg = 0;
99        if (!BN_add_word(q, 1))
100            goto end;
101        if (!BN_mod_exp(ret, A, q, p, ctx))
102            goto end;
103        err = 0;
104        goto vrfy;
105    }
106
107    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        if (!BN_mod_lshift1_quick(t, A, p))
138            goto end;
139
140        /* b := (2*a)^((|p|-5)/8) */
141        if (!BN_rshift(q, p, 3))
142            goto end;
143        q->neg = 0;
144        if (!BN_mod_exp(b, t, q, p, ctx))
145            goto end;
146
147        /* y := b^2 */
148        if (!BN_mod_sqr(y, b, p, ctx))
149            goto end;
150
151        /* t := (2*a)*b^2 - 1 */
152        if (!BN_mod_mul(t, t, y, p, ctx))
153            goto end;
154        if (!BN_sub_word(t, 1))
155            goto end;
156
157        /* x = a*b*t */
158        if (!BN_mod_mul(x, A, b, p, ctx))
159            goto end;
160        if (!BN_mod_mul(x, x, t, p, ctx))
161            goto end;
162
163        if (!BN_copy(ret, x))
164            goto end;
165        err = 0;
166        goto vrfy;
167    }
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    if (!BN_copy(q, p))
174        goto end;               /* use 'q' as temp */
175    q->neg = 0;
176    i = 2;
177    do {
178        /*
179         * For efficiency, try small numbers first; if this fails, try random
180         * numbers.
181         */
182        if (i < 22) {
183            if (!BN_set_word(y, i))
184                goto end;
185        } else {
186            if (!BN_priv_rand_ex(y, BN_num_bits(p), 0, 0, 0, ctx))
187                goto end;
188            if (BN_ucmp(y, p) >= 0) {
189                if (!(p->neg ? BN_add : BN_sub) (y, y, p))
190                    goto end;
191            }
192            /* now 0 <= y < |p| */
193            if (BN_is_zero(y))
194                if (!BN_set_word(y, i))
195                    goto end;
196        }
197
198        r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
199        if (r < -1)
200            goto end;
201        if (r == 0) {
202            /* m divides p */
203            ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
204            goto end;
205        }
206    }
207    while (r == 1 && ++i < 82);
208
209    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        ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
216        goto end;
217    }
218
219    /* Here's our actual 'q': */
220    if (!BN_rshift(q, q, e))
221        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    if (!BN_mod_exp(y, y, q, p, ctx))
228        goto end;
229    if (BN_is_one(y)) {
230        ERR_raise(ERR_LIB_BN, BN_R_P_IS_NOT_PRIME);
231        goto end;
232    }
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    if (!BN_rshift1(t, q))
255        goto end;
256
257    /* x := a^((q-1)/2) */
258    if (BN_is_zero(t)) {        /* special case: p = 2^e + 1 */
259        if (!BN_nnmod(t, A, p, ctx))
260            goto end;
261        if (BN_is_zero(t)) {
262            /* special case: a == 0  (mod p) */
263            BN_zero(ret);
264            err = 0;
265            goto end;
266        } else if (!BN_one(x))
267            goto end;
268    } else {
269        if (!BN_mod_exp(x, A, t, p, ctx))
270            goto end;
271        if (BN_is_zero(x)) {
272            /* special case: a == 0  (mod p) */
273            BN_zero(ret);
274            err = 0;
275            goto end;
276        }
277    }
278
279    /* b := a*x^2  (= a^q) */
280    if (!BN_mod_sqr(b, x, p, ctx))
281        goto end;
282    if (!BN_mod_mul(b, b, A, p, ctx))
283        goto end;
284
285    /* x := a*x    (= a^((q+1)/2)) */
286    if (!BN_mod_mul(x, x, A, p, ctx))
287        goto end;
288
289    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        if (BN_is_one(b)) {
301            if (!BN_copy(ret, x))
302                goto end;
303            err = 0;
304            goto vrfy;
305        }
306
307        /* Find the smallest i, 0 < i < e, such that b^(2^i) = 1. */
308        for (i = 1; i < e; i++) {
309            if (i == 1) {
310                if (!BN_mod_sqr(t, b, p, ctx))
311                    goto end;
312
313            } else {
314                if (!BN_mod_mul(t, t, t, p, ctx))
315                    goto end;
316            }
317            if (BN_is_one(t))
318                break;
319        }
320        /* If not found, a is not a square or p is not prime. */
321        if (i >= e) {
322            ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
323            goto end;
324        }
325
326        /* t := y^2^(e - i - 1) */
327        if (!BN_copy(t, y))
328            goto end;
329        for (j = e - i - 1; j > 0; j--) {
330            if (!BN_mod_sqr(t, t, p, ctx))
331                goto end;
332        }
333        if (!BN_mod_mul(y, t, t, p, ctx))
334            goto end;
335        if (!BN_mod_mul(x, x, t, p, ctx))
336            goto end;
337        if (!BN_mod_mul(b, b, y, p, ctx))
338            goto end;
339        e = i;
340    }
341
342 vrfy:
343    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        if (!BN_mod_sqr(x, ret, p, ctx))
350            err = 1;
351
352        if (!err && 0 != BN_cmp(x, A)) {
353            ERR_raise(ERR_LIB_BN, BN_R_NOT_A_SQUARE);
354            err = 1;
355        }
356    }
357
358 end:
359    if (err) {
360        if (ret != in)
361            BN_clear_free(ret);
362        ret = NULL;
363    }
364    if (used_ctx)
365        BN_CTX_end(ctx);
366    bn_check_top(ret);
367    return ret;
368}
369