Coverage Report

Created: 2026-06-15 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/fipsmodule/bn/sqrt.cc.inc
Line
Count
Source
1
// Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/bn.h>
16
17
#include <openssl/err.h>
18
19
#include "internal.h"
20
21
22
using namespace bssl;
23
24
4.12k
BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
25
  // Compute a square root of |a| mod |p| using the Tonelli/Shanks algorithm
26
  // (cf. Henri Cohen, "A Course in Algebraic Computational Number Theory",
27
  // algorithm 1.5.1). |p| is assumed to be a prime.
28
29
4.12k
  BIGNUM *ret = in;
30
4.12k
  int err = 1;
31
4.12k
  int r;
32
4.12k
  BIGNUM *A, *b, *q, *t, *x, *y;
33
4.12k
  int e, i, j;
34
35
4.12k
  if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
36
0
    if (BN_abs_is_word(p, 2)) {
37
0
      if (ret == nullptr) {
38
0
        ret = BN_new();
39
0
      }
40
0
      if (ret == nullptr || !BN_set_word(ret, BN_is_bit_set(a, 0))) {
41
0
        if (ret != in) {
42
0
          BN_free(ret);
43
0
        }
44
0
        return nullptr;
45
0
      }
46
0
      return ret;
47
0
    }
48
49
0
    OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
50
0
    return nullptr;
51
0
  }
52
53
4.12k
  if (BN_is_zero(a) || BN_is_one(a)) {
54
0
    if (ret == nullptr) {
55
0
      ret = BN_new();
56
0
    }
57
0
    if (ret == nullptr || !BN_set_word(ret, BN_is_one(a))) {
58
0
      if (ret != in) {
59
0
        BN_free(ret);
60
0
      }
61
0
      return nullptr;
62
0
    }
63
0
    return ret;
64
0
  }
65
66
4.12k
  BN_CTXScope scope(ctx);
67
4.12k
  A = BN_CTX_get(ctx);
68
4.12k
  b = BN_CTX_get(ctx);
69
4.12k
  q = BN_CTX_get(ctx);
70
4.12k
  t = BN_CTX_get(ctx);
71
4.12k
  x = BN_CTX_get(ctx);
72
4.12k
  y = BN_CTX_get(ctx);
73
4.12k
  if (y == nullptr) {
74
0
    goto end;
75
0
  }
76
77
4.12k
  if (ret == nullptr) {
78
0
    ret = BN_new();
79
0
  }
80
4.12k
  if (ret == nullptr) {
81
0
    goto end;
82
0
  }
83
84
  // A = a mod p
85
4.12k
  if (!BN_nnmod(A, a, p, ctx)) {
86
0
    goto end;
87
0
  }
88
89
  // now write  |p| - 1  as  2^e*q  where  q  is odd
90
4.12k
  e = 1;
91
281k
  while (!BN_is_bit_set(p, e)) {
92
277k
    e++;
93
277k
  }
94
  // we'll set  q  later (if needed)
95
96
4.12k
  if (e == 1) {
97
    // The easy case:  (|p|-1)/2  is odd, so 2 has an inverse
98
    // modulo  (|p|-1)/2,  and square roots can be computed
99
    // directly by modular exponentiation.
100
    // We have
101
    //     2 * (|p|+1)/4 == 1   (mod (|p|-1)/2),
102
    // so we can use exponent  (|p|+1)/4,  i.e.  (|p|-3)/4 + 1.
103
1.20k
    if (!BN_rshift(q, p, 2)) {
104
0
      goto end;
105
0
    }
106
1.20k
    q->neg = 0;
107
1.20k
    if (!BN_add_word(q, 1) || !BN_mod_exp_mont(ret, A, q, p, ctx, nullptr)) {
108
0
      goto end;
109
0
    }
110
1.20k
    err = 0;
111
1.20k
    goto vrfy;
112
1.20k
  }
113
114
2.92k
  if (e == 2) {
115
    // |p| == 5  (mod 8)
116
    //
117
    // In this case  2  is always a non-square since
118
    // Legendre(2,p) = (-1)^((p^2-1)/8)  for any odd prime.
119
    // So if  a  really is a square, then  2*a  is a non-square.
120
    // Thus for
121
    //      b := (2*a)^((|p|-5)/8),
122
    //      i := (2*a)*b^2
123
    // we have
124
    //     i^2 = (2*a)^((1 + (|p|-5)/4)*2)
125
    //         = (2*a)^((p-1)/2)
126
    //         = -1;
127
    // so if we set
128
    //      x := a*b*(i-1),
129
    // then
130
    //     x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
131
    //         = a^2 * b^2 * (-2*i)
132
    //         = a*(-i)*(2*a*b^2)
133
    //         = a*(-i)*i
134
    //         = a.
135
    //
136
    // (This is due to A.O.L. Atkin,
137
    // <URL:
138
    //http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
139
    // November 1992.)
140
141
    // t := 2*a
142
0
    if (!bn_mod_lshift1_consttime(t, A, p, ctx)) {
143
0
      goto end;
144
0
    }
145
146
    // b := (2*a)^((|p|-5)/8)
147
0
    if (!BN_rshift(q, p, 3)) {
148
0
      goto end;
149
0
    }
150
0
    q->neg = 0;
151
0
    if (!BN_mod_exp_mont(b, t, q, p, ctx, nullptr)) {
152
0
      goto end;
153
0
    }
154
155
    // y := b^2
156
0
    if (!BN_mod_sqr(y, b, p, ctx)) {
157
0
      goto end;
158
0
    }
159
160
    // t := (2*a)*b^2 - 1
161
0
    if (!BN_mod_mul(t, t, y, p, ctx) ||
162
0
        !BN_sub_word(t, 1)) {
163
0
      goto end;
164
0
    }
165
166
    // x = a*b*t
167
0
    if (!BN_mod_mul(x, A, b, p, ctx) ||
168
0
        !BN_mod_mul(x, x, t, p, ctx)) {
169
0
      goto end;
170
0
    }
171
172
0
    if (!BN_copy(ret, x)) {
173
0
      goto end;
174
0
    }
175
0
    err = 0;
176
0
    goto vrfy;
177
0
  }
178
179
  // e > 2, so we really have to use the Tonelli/Shanks algorithm.
180
  // First, find some  y  that is not a square.
181
2.92k
  if (!BN_copy(q, p)) {
182
0
    goto end;  // use 'q' as temp
183
0
  }
184
2.92k
  q->neg = 0;
185
2.92k
  i = 2;
186
29.2k
  do {
187
    // For efficiency, try small numbers first;
188
    // if this fails, try random numbers.
189
29.2k
    if (i < 22) {
190
29.2k
      if (!BN_set_word(y, i)) {
191
0
        goto end;
192
0
      }
193
29.2k
    } else {
194
0
      if (!BN_rand_range_ex(y, 22, p)) {
195
0
        goto end;
196
0
      }
197
0
    }
198
199
29.2k
    r = bn_jacobi(y, q, ctx);  // here 'q' is |p|
200
29.2k
    if (r < -1) {
201
0
      goto end;
202
0
    }
203
29.2k
    if (r == 0) {
204
      // m divides p
205
0
      OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
206
0
      goto end;
207
0
    }
208
29.2k
  } while (r == 1 && ++i < 82);
209
210
2.92k
  if (r != -1) {
211
    // Many rounds and still no non-square -- this is more likely
212
    // a bug than just bad luck.
213
    // Even if  p  is not prime, we should have found some  y
214
    // such that r == -1.
215
0
    OPENSSL_PUT_ERROR(BN, BN_R_TOO_MANY_ITERATIONS);
216
0
    goto end;
217
0
  }
218
219
  // Here's our actual 'q':
220
2.92k
  if (!BN_rshift(q, q, e)) {
221
0
    goto end;
222
0
  }
223
224
  // Now that we have some non-square, we can find an element
225
  // of order  2^e  by computing its q'th power.
226
2.92k
  if (!BN_mod_exp_mont(y, y, q, p, ctx, nullptr)) {
227
0
    goto end;
228
0
  }
229
2.92k
  if (BN_is_one(y)) {
230
0
    OPENSSL_PUT_ERROR(BN, BN_R_P_IS_NOT_PRIME);
231
0
    goto end;
232
0
  }
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
  // t := (q-1)/2  (note that  q  is odd)
252
2.92k
  if (!BN_rshift1(t, q)) {
253
0
    goto end;
254
0
  }
255
256
  // x := a^((q-1)/2)
257
2.92k
  if (BN_is_zero(t)) {  // special case: p = 2^e + 1
258
0
    if (!BN_nnmod(t, A, p, ctx)) {
259
0
      goto end;
260
0
    }
261
0
    if (BN_is_zero(t)) {
262
      // special case: a == 0  (mod p)
263
0
      BN_zero(ret);
264
0
      err = 0;
265
0
      goto end;
266
0
    } else if (!BN_one(x)) {
267
0
      goto end;
268
0
    }
269
2.92k
  } else {
270
2.92k
    if (!BN_mod_exp_mont(x, A, t, p, ctx, nullptr)) {
271
0
      goto end;
272
0
    }
273
2.92k
    if (BN_is_zero(x)) {
274
      // special case: a == 0  (mod p)
275
0
      BN_zero(ret);
276
0
      err = 0;
277
0
      goto end;
278
0
    }
279
2.92k
  }
280
281
  // b := a*x^2  (= a^q)
282
2.92k
  if (!BN_mod_sqr(b, x, p, ctx) ||
283
2.92k
      !BN_mod_mul(b, b, A, p, ctx)) {
284
0
    goto end;
285
0
  }
286
287
  // x := a*x    (= a^((q+1)/2))
288
2.92k
  if (!BN_mod_mul(x, x, A, p, ctx)) {
289
0
    goto end;
290
0
  }
291
292
112k
  while (1) {
293
    // Now  b  is  a^q * y^k  for some even  k  (0 <= k < 2^E
294
    // where  E  refers to the original value of  e,  which we
295
    // don't keep in a variable),  and  x  is  a^((q+1)/2) * y^(k/2).
296
    //
297
    // We have  a*b = x^2,
298
    //    y^2^(e-1) = -1,
299
    //    b^2^(e-1) = 1.
300
112k
    if (BN_is_one(b)) {
301
2.31k
      if (!BN_copy(ret, x)) {
302
0
        goto end;
303
0
      }
304
2.31k
      err = 0;
305
2.31k
      goto vrfy;
306
2.31k
    }
307
308
    // Find the smallest i, 0 < i < e, such that b^(2^i) = 1
309
5.34M
    for (i = 1; i < e; i++) {
310
5.34M
      if (i == 1) {
311
110k
        if (!BN_mod_sqr(t, b, p, ctx)) {
312
0
          goto end;
313
0
        }
314
5.23M
      } else {
315
5.23M
        if (!BN_mod_mul(t, t, t, p, ctx)) {
316
0
          goto end;
317
0
        }
318
5.23M
      }
319
5.34M
      if (BN_is_one(t)) {
320
109k
        break;
321
109k
      }
322
5.34M
    }
323
    // If not found, a is not a square or p is not a prime.
324
110k
    if (i >= e) {
325
603
      OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
326
603
      goto end;
327
603
    }
328
329
    // t := y^2^(e - i - 1)
330
109k
    if (!BN_copy(t, y)) {
331
0
      goto end;
332
0
    }
333
217k
    for (j = e - i - 1; j > 0; j--) {
334
107k
      if (!BN_mod_sqr(t, t, p, ctx)) {
335
0
        goto end;
336
0
      }
337
107k
    }
338
109k
    if (!BN_mod_mul(y, t, t, p, ctx) ||
339
109k
        !BN_mod_mul(x, x, t, p, ctx) ||
340
109k
        !BN_mod_mul(b, b, y, p, ctx)) {
341
0
      goto end;
342
0
    }
343
344
    // e decreases each iteration, so this loop will terminate.
345
109k
    assert(i < e);
346
109k
    e = i;
347
109k
  }
348
349
3.52k
vrfy:
350
3.52k
  if (!err) {
351
    // Verify the result. The input might have been not a square.
352
3.52k
    if (!BN_mod_sqr(x, ret, p, ctx)) {
353
0
      err = 1;
354
0
    }
355
356
3.52k
    if (!err && 0 != BN_cmp(x, A)) {
357
372
      OPENSSL_PUT_ERROR(BN, BN_R_NOT_A_SQUARE);
358
372
      err = 1;
359
372
    }
360
3.52k
  }
361
362
4.12k
end:
363
4.12k
  if (err) {
364
975
    if (ret != in) {
365
0
      BN_clear_free(ret);
366
0
    }
367
975
    ret = nullptr;
368
975
  }
369
4.12k
  return ret;
370
3.52k
}