Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bn/bn_gf2m.c

raw
1 /*
2 * Copyright 2002-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 #include <assert.h>
12 #include <limits.h>
13 #include <stdio.h>
14 #include "internal/cryptlib.h"
15 #include "bn_local.h"
16
17 #ifndef OPENSSL_NO_EC2M
18 #include <openssl/ec.h>
19
20 /*
21 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
22 * fail.
23 */
24 #define MAX_ITERATIONS 50
25
26 #define SQR_nibble(w) ((((w) & 8) << 3) \
27 | (((w) & 4) << 2) \
28 | (((w) & 2) << 1) \
29 | ((w) & 1))
30
31 /* Platform-specific macros to accelerate squaring. */
32 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
33 #define SQR1(w) \
34 SQR_nibble((w) >> 60) << 56 | SQR_nibble((w) >> 56) << 48 | SQR_nibble((w) >> 52) << 40 | SQR_nibble((w) >> 48) << 32 | SQR_nibble((w) >> 44) << 24 | SQR_nibble((w) >> 40) << 16 | SQR_nibble((w) >> 36) << 8 | SQR_nibble((w) >> 32)
35 #define SQR0(w) \
36 SQR_nibble((w) >> 28) << 56 | SQR_nibble((w) >> 24) << 48 | SQR_nibble((w) >> 20) << 40 | SQR_nibble((w) >> 16) << 32 | SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | SQR_nibble((w) >> 4) << 8 | SQR_nibble((w))
37 #endif
38 #ifdef THIRTY_TWO_BIT
39 #define SQR1(w) \
40 SQR_nibble((w) >> 28) << 24 | SQR_nibble((w) >> 24) << 16 | SQR_nibble((w) >> 20) << 8 | SQR_nibble((w) >> 16)
41 #define SQR0(w) \
42 SQR_nibble((w) >> 12) << 24 | SQR_nibble((w) >> 8) << 16 | SQR_nibble((w) >> 4) << 8 | SQR_nibble((w))
43 #endif
44
45 #if !defined(OPENSSL_BN_ASM_GF2m)
46 /*
47 * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
48 * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
49 * the variables have the right amount of space allocated.
50 */
51 #ifdef THIRTY_TWO_BIT
52 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
53 const BN_ULONG b)
54 {
55 register BN_ULONG h, l, s;
56 BN_ULONG tab[8], top2b = a >> 30;
57 register BN_ULONG a1, a2, a4;
58
59 a1 = a & (0x3FFFFFFF);
60 a2 = a1 << 1;
61 a4 = a2 << 1;
62
63 tab[0] = 0;
64 tab[1] = a1;
65 tab[2] = a2;
66 tab[3] = a1 ^ a2;
67 tab[4] = a4;
68 tab[5] = a1 ^ a4;
69 tab[6] = a2 ^ a4;
70 tab[7] = a1 ^ a2 ^ a4;
71
72 s = tab[b & 0x7];
73 l = s;
74 s = tab[b >> 3 & 0x7];
75 l ^= s << 3;
76 h = s >> 29;
77 s = tab[b >> 6 & 0x7];
78 l ^= s << 6;
79 h ^= s >> 26;
80 s = tab[b >> 9 & 0x7];
81 l ^= s << 9;
82 h ^= s >> 23;
83 s = tab[b >> 12 & 0x7];
84 l ^= s << 12;
85 h ^= s >> 20;
86 s = tab[b >> 15 & 0x7];
87 l ^= s << 15;
88 h ^= s >> 17;
89 s = tab[b >> 18 & 0x7];
90 l ^= s << 18;
91 h ^= s >> 14;
92 s = tab[b >> 21 & 0x7];
93 l ^= s << 21;
94 h ^= s >> 11;
95 s = tab[b >> 24 & 0x7];
96 l ^= s << 24;
97 h ^= s >> 8;
98 s = tab[b >> 27 & 0x7];
99 l ^= s << 27;
100 h ^= s >> 5;
101 s = tab[b >> 30];
102 l ^= s << 30;
103 h ^= s >> 2;
104
105 /* compensate for the top two bits of a */
106
107 if (top2b & 01) {
108 l ^= b << 30;
109 h ^= b >> 2;
110 }
111 if (top2b & 02) {
112 l ^= b << 31;
113 h ^= b >> 1;
114 }
115
116 *r1 = h;
117 *r0 = l;
118 }
119 #endif
120 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
121 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
122 const BN_ULONG b)
123 {
124 register BN_ULONG h, l, s;
125 BN_ULONG tab[16], top3b = a >> 61;
126 register BN_ULONG a1, a2, a4, a8;
127
128 a1 = a & (0x1FFFFFFFFFFFFFFFULL);
129 a2 = a1 << 1;
130 a4 = a2 << 1;
131 a8 = a4 << 1;
132
133 tab[0] = 0;
134 tab[1] = a1;
135 tab[2] = a2;
136 tab[3] = a1 ^ a2;
137 tab[4] = a4;
138 tab[5] = a1 ^ a4;
139 tab[6] = a2 ^ a4;
140 tab[7] = a1 ^ a2 ^ a4;
141 tab[8] = a8;
142 tab[9] = a1 ^ a8;
143 tab[10] = a2 ^ a8;
144 tab[11] = a1 ^ a2 ^ a8;
145 tab[12] = a4 ^ a8;
146 tab[13] = a1 ^ a4 ^ a8;
147 tab[14] = a2 ^ a4 ^ a8;
148 tab[15] = a1 ^ a2 ^ a4 ^ a8;
149
150 s = tab[b & 0xF];
151 l = s;
152 s = tab[b >> 4 & 0xF];
153 l ^= s << 4;
154 h = s >> 60;
155 s = tab[b >> 8 & 0xF];
156 l ^= s << 8;
157 h ^= s >> 56;
158 s = tab[b >> 12 & 0xF];
159 l ^= s << 12;
160 h ^= s >> 52;
161 s = tab[b >> 16 & 0xF];
162 l ^= s << 16;
163 h ^= s >> 48;
164 s = tab[b >> 20 & 0xF];
165 l ^= s << 20;
166 h ^= s >> 44;
167 s = tab[b >> 24 & 0xF];
168 l ^= s << 24;
169 h ^= s >> 40;
170 s = tab[b >> 28 & 0xF];
171 l ^= s << 28;
172 h ^= s >> 36;
173 s = tab[b >> 32 & 0xF];
174 l ^= s << 32;
175 h ^= s >> 32;
176 s = tab[b >> 36 & 0xF];
177 l ^= s << 36;
178 h ^= s >> 28;
179 s = tab[b >> 40 & 0xF];
180 l ^= s << 40;
181 h ^= s >> 24;
182 s = tab[b >> 44 & 0xF];
183 l ^= s << 44;
184 h ^= s >> 20;
185 s = tab[b >> 48 & 0xF];
186 l ^= s << 48;
187 h ^= s >> 16;
188 s = tab[b >> 52 & 0xF];
189 l ^= s << 52;
190 h ^= s >> 12;
191 s = tab[b >> 56 & 0xF];
192 l ^= s << 56;
193 h ^= s >> 8;
194 s = tab[b >> 60];
195 l ^= s << 60;
196 h ^= s >> 4;
197
198 /* compensate for the top three bits of a */
199
200 if (top3b & 01) {
201 l ^= b << 61;
202 h ^= b >> 3;
203 }
204 if (top3b & 02) {
205 l ^= b << 62;
206 h ^= b >> 2;
207 }
208 if (top3b & 04) {
209 l ^= b << 63;
210 h ^= b >> 1;
211 }
212
213 *r1 = h;
214 *r0 = l;
215 }
216 #endif
217
218 /*
219 * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
220 * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
221 * ensure that the variables have the right amount of space allocated.
222 */
223 static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
224 const BN_ULONG b1, const BN_ULONG b0)
225 {
226 BN_ULONG m1, m0;
227 /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
228 bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
229 bn_GF2m_mul_1x1(r + 1, r, a0, b0);
230 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
231 /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
232 r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
233 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
234 }
235 #else
236 void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
237 BN_ULONG b0);
238 #endif
239
240 /*
241 * Add polynomials a and b and store result in r; r could be a or b, a and b
242 * could be equal; r is the bitwise XOR of a and b.
243 */
244 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
245 {
246 int i;
247 const BIGNUM *at, *bt;
248
249 bn_check_top(a);
250 bn_check_top(b);
251
252 if (a->top < b->top) {
253 at = b;
254 bt = a;
255 } else {
256 at = a;
257 bt = b;
258 }
259
260 if (bn_wexpand(r, at->top) == NULL)
261 return 0;
262
263 for (i = 0; i < bt->top; i++) {
264 r->d[i] = at->d[i] ^ bt->d[i];
265 }
266 for (; i < at->top; i++) {
267 r->d[i] = at->d[i];
268 }
269
270 r->top = at->top;
271 bn_correct_top(r);
272
273 return 1;
274 }
275
276 /*-
277 * Some functions allow for representation of the irreducible polynomials
278 * as an int[], say p. The irreducible f(t) is then of the form:
279 * t^p[0] + t^p[1] + ... + t^p[k]
280 * where m = p[0] > p[1] > ... > p[k] = 0.
281 */
282
283 /* Performs modular reduction of a and store result in r. r could be a. */
284 int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
285 {
286 int j, k;
287 int n, dN, d0, d1;
288 BN_ULONG zz, *z;
289
290 bn_check_top(a);
291
292 if (p[0] == 0) {
293 /* reduction mod 1 => return 0 */
294 BN_zero(r);
295 return 1;
296 }
297
298 /*
299 * Since the algorithm does reduction in the r value, if a != r, copy the
300 * contents of a into r so we can do reduction in r.
301 */
302 if (a != r) {
303 if (!bn_wexpand(r, a->top))
304 return 0;
305 for (j = 0; j < a->top; j++) {
306 r->d[j] = a->d[j];
307 }
308 r->top = a->top;
309 }
310 z = r->d;
311
312 /* start reduction */
313 dN = p[0] / BN_BITS2;
314 for (j = r->top - 1; j > dN;) {
315 zz = z[j];
316 if (z[j] == 0) {
317 j--;
318 continue;
319 }
320 z[j] = 0;
321
322 for (k = 1; p[k] != 0; k++) {
323 /* reducing component t^p[k] */
324 n = p[0] - p[k];
325 d0 = n % BN_BITS2;
326 d1 = BN_BITS2 - d0;
327 n /= BN_BITS2;
328 z[j - n] ^= (zz >> d0);
329 if (d0)
330 z[j - n - 1] ^= (zz << d1);
331 }
332
333 /* reducing component t^0 */
334 n = dN;
335 d0 = p[0] % BN_BITS2;
336 d1 = BN_BITS2 - d0;
337 z[j - n] ^= (zz >> d0);
338 if (ossl_likely(d0))
339 z[j - n - 1] ^= (zz << d1);
340 }
341
342 /* final round of reduction */
343 while (j == dN) {
344
345 d0 = p[0] % BN_BITS2;
346 zz = z[dN] >> d0;
347 if (zz == 0)
348 break;
349 d1 = BN_BITS2 - d0;
350
351 /* clear up the top d1 bits */
352 if (ossl_likely(d0))
353 z[dN] = (z[dN] << d1) >> d1;
354 else
355 z[dN] = 0;
356 z[0] ^= zz; /* reduction t^0 component */
357
358 for (k = 1; p[k] != 0; k++) {
359 BN_ULONG tmp_ulong;
360
361 /* reducing component t^p[k] */
362 n = p[k] / BN_BITS2;
363 d0 = p[k] % BN_BITS2;
364 d1 = BN_BITS2 - d0;
365 z[n] ^= (zz << d0);
366 if (d0 && (tmp_ulong = zz >> d1))
367 z[n + 1] ^= tmp_ulong;
368 }
369 }
370
371 bn_correct_top(r);
372 return 1;
373 }
374
375 /*
376 * Performs modular reduction of a by p and store result in r. r could be a.
377 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
378 * function is only provided for convenience; for best performance, use the
379 * BN_GF2m_mod_arr function.
380 */
381 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
382 {
383 int ret = 0;
384 int arr[6];
385 bn_check_top(a);
386 bn_check_top(p);
387 ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
388 if (!ret || ret > (int)OSSL_NELEM(arr)) {
389 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
390 return 0;
391 }
392 ret = BN_GF2m_mod_arr(r, a, arr);
393 bn_check_top(r);
394 return ret;
395 }
396
397 /*
398 * Compute the product of two polynomials a and b, reduce modulo p, and store
399 * the result in r. r could be a or b; a could be b.
400 */
401 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
402 const int p[], BN_CTX *ctx)
403 {
404 int zlen, i, j, k, ret = 0;
405 BIGNUM *s;
406 BN_ULONG x1, x0, y1, y0, zz[4];
407
408 bn_check_top(a);
409 bn_check_top(b);
410
411 if (a == b) {
412 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
413 }
414
415 BN_CTX_start(ctx);
416 if ((s = BN_CTX_get(ctx)) == NULL)
417 goto err;
418
419 zlen = a->top + b->top + 4;
420 if (!bn_wexpand(s, zlen))
421 goto err;
422 s->top = zlen;
423
424 for (i = 0; i < zlen; i++)
425 s->d[i] = 0;
426
427 for (j = 0; j < b->top; j += 2) {
428 y0 = b->d[j];
429 y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
430 for (i = 0; i < a->top; i += 2) {
431 x0 = a->d[i];
432 x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
433 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
434 for (k = 0; k < 4; k++)
435 s->d[i + j + k] ^= zz[k];
436 }
437 }
438
439 bn_correct_top(s);
440 if (BN_GF2m_mod_arr(r, s, p))
441 ret = 1;
442 bn_check_top(r);
443
444 err:
445 BN_CTX_end(ctx);
446 return ret;
447 }
448
449 /*
450 * Compute the product of two polynomials a and b, reduce modulo p, and store
451 * the result in r. r could be a or b; a could equal b. This function calls
452 * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
453 * only provided for convenience; for best performance, use the
454 * BN_GF2m_mod_mul_arr function.
455 */
456 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
457 const BIGNUM *p, BN_CTX *ctx)
458 {
459 int ret = 0;
460 const int max = BN_num_bits(p) + 1;
461 int *arr;
462
463 bn_check_top(a);
464 bn_check_top(b);
465 bn_check_top(p);
466
467 arr = OPENSSL_malloc_array(max, sizeof(*arr));
468 if (arr == NULL)
469 return 0;
470 ret = BN_GF2m_poly2arr(p, arr, max);
471 if (!ret || ret > max) {
472 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
473 goto err;
474 }
475 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
476 bn_check_top(r);
477 err:
478 OPENSSL_free(arr);
479 return ret;
480 }
481
482 /* Square a, reduce the result mod p, and store it in a. r could be a. */
483 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
484 BN_CTX *ctx)
485 {
486 int i, ret = 0;
487 BIGNUM *s;
488
489 bn_check_top(a);
490 BN_CTX_start(ctx);
491 if ((s = BN_CTX_get(ctx)) == NULL)
492 goto err;
493 if (!bn_wexpand(s, 2 * a->top))
494 goto err;
495
496 for (i = a->top - 1; i >= 0; i--) {
497 s->d[2 * i + 1] = SQR1(a->d[i]);
498 s->d[2 * i] = SQR0(a->d[i]);
499 }
500
501 s->top = 2 * a->top;
502 bn_correct_top(s);
503 if (!BN_GF2m_mod_arr(r, s, p))
504 goto err;
505 bn_check_top(r);
506 ret = 1;
507 err:
508 BN_CTX_end(ctx);
509 return ret;
510 }
511
512 /*
513 * Square a, reduce the result mod p, and store it in a. r could be a. This
514 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
515 * wrapper function is only provided for convenience; for best performance,
516 * use the BN_GF2m_mod_sqr_arr function.
517 */
518 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
519 {
520 int ret = 0;
521 const int max = BN_num_bits(p) + 1;
522 int *arr;
523
524 bn_check_top(a);
525 bn_check_top(p);
526
527 arr = OPENSSL_malloc_array(max, sizeof(*arr));
528 if (arr == NULL)
529 return 0;
530 ret = BN_GF2m_poly2arr(p, arr, max);
531 if (!ret || ret > max) {
532 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
533 goto err;
534 }
535 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
536 bn_check_top(r);
537 err:
538 OPENSSL_free(arr);
539 return ret;
540 }
541
542 /*
543 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
544 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
545 * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
546 * Curve Cryptography Over Binary Fields".
547 */
548 static int BN_GF2m_mod_inv_vartime(BIGNUM *r, const BIGNUM *a,
549 const BIGNUM *p, BN_CTX *ctx)
550 {
551 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
552 int ret = 0;
553
554 bn_check_top(a);
555 bn_check_top(p);
556
557 BN_CTX_start(ctx);
558
559 b = BN_CTX_get(ctx);
560 c = BN_CTX_get(ctx);
561 u = BN_CTX_get(ctx);
562 v = BN_CTX_get(ctx);
563 if (v == NULL)
564 goto err;
565
566 if (!BN_GF2m_mod(u, a, p))
567 goto err;
568 if (BN_is_zero(u))
569 goto err;
570
571 if (!BN_copy(v, p))
572 goto err;
573 #if 0
574 if (!BN_one(b))
575 goto err;
576
577 while (1) {
578 while (!BN_is_odd(u)) {
579 if (BN_is_zero(u))
580 goto err;
581 if (!BN_rshift1(u, u))
582 goto err;
583 if (BN_is_odd(b)) {
584 if (!BN_GF2m_add(b, b, p))
585 goto err;
586 }
587 if (!BN_rshift1(b, b))
588 goto err;
589 }
590
591 if (BN_abs_is_word(u, 1))
592 break;
593
594 if (BN_num_bits(u) < BN_num_bits(v)) {
595 tmp = u;
596 u = v;
597 v = tmp;
598 tmp = b;
599 b = c;
600 c = tmp;
601 }
602
603 if (!BN_GF2m_add(u, u, v))
604 goto err;
605 if (!BN_GF2m_add(b, b, c))
606 goto err;
607 }
608 #else
609 {
610 int i;
611 int ubits = BN_num_bits(u);
612 int vbits = BN_num_bits(v); /* v is copy of p */
613 int top = p->top;
614 BN_ULONG *udp, *bdp, *vdp, *cdp;
615
616 if (!bn_wexpand(u, top))
617 goto err;
618 udp = u->d;
619 for (i = u->top; i < top; i++)
620 udp[i] = 0;
621 u->top = top;
622 if (!bn_wexpand(b, top))
623 goto err;
624 bdp = b->d;
625 bdp[0] = 1;
626 for (i = 1; i < top; i++)
627 bdp[i] = 0;
628 b->top = top;
629 if (!bn_wexpand(c, top))
630 goto err;
631 cdp = c->d;
632 for (i = 0; i < top; i++)
633 cdp[i] = 0;
634 c->top = top;
635 vdp = v->d; /* It pays off to "cache" *->d pointers,
636 * because it allows optimizer to be more
637 * aggressive. But we don't have to "cache"
638 * p->d, because *p is declared 'const'... */
639 while (1) {
640 while (ubits && !(udp[0] & 1)) {
641 BN_ULONG u0, u1, b0, b1, mask;
642
643 u0 = udp[0];
644 b0 = bdp[0];
645 mask = (BN_ULONG)0 - (b0 & 1);
646 b0 ^= p->d[0] & mask;
647 for (i = 0; i < top - 1; i++) {
648 u1 = udp[i + 1];
649 udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
650 u0 = u1;
651 b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
652 bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
653 b0 = b1;
654 }
655 udp[i] = u0 >> 1;
656 bdp[i] = b0 >> 1;
657 ubits--;
658 }
659
660 if (ubits <= BN_BITS2) {
661 if (udp[0] == 0) /* poly was reducible */
662 goto err;
663 if (udp[0] == 1)
664 break;
665 }
666
667 if (ubits < vbits) {
668 i = ubits;
669 ubits = vbits;
670 vbits = i;
671 tmp = u;
672 u = v;
673 v = tmp;
674 tmp = b;
675 b = c;
676 c = tmp;
677 udp = vdp;
678 vdp = v->d;
679 bdp = cdp;
680 cdp = c->d;
681 }
682 for (i = 0; i < top; i++) {
683 udp[i] ^= vdp[i];
684 bdp[i] ^= cdp[i];
685 }
686 if (ubits == vbits) {
687 BN_ULONG ul;
688 int utop = (ubits - 1) / BN_BITS2;
689
690 while ((ul = udp[utop]) == 0 && utop)
691 utop--;
692 ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
693 }
694 }
695 bn_correct_top(b);
696 }
697 #endif
698
699 if (!BN_copy(r, b))
700 goto err;
701 bn_check_top(r);
702 ret = 1;
703
704 err:
705 #ifdef BN_DEBUG
706 /* BN_CTX_end would complain about the expanded form */
707 bn_correct_top(c);
708 bn_correct_top(u);
709 bn_correct_top(v);
710 #endif
711 BN_CTX_end(ctx);
712 return ret;
713 }
714
715 /*-
716 * Wrapper for BN_GF2m_mod_inv_vartime that blinds the input before calling.
717 * This is not constant time.
718 * But it does eliminate first order deduction on the input.
719 */
720 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
721 {
722 BIGNUM *b = NULL;
723 int ret = 0;
724 int numbits;
725
726 BN_CTX_start(ctx);
727 if ((b = BN_CTX_get(ctx)) == NULL)
728 goto err;
729
730 /* Fail on a non-sensical input p value */
731 numbits = BN_num_bits(p);
732 if (numbits <= 1)
733 goto err;
734
735 /* generate blinding value */
736 do {
737 if (!BN_priv_rand_ex(b, numbits - 1,
738 BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY, 0, ctx))
739 goto err;
740 } while (BN_is_zero(b));
741
742 /* r := a * b */
743 if (!BN_GF2m_mod_mul(r, a, b, p, ctx))
744 goto err;
745
746 /* r := 1/(a * b) */
747 if (!BN_GF2m_mod_inv_vartime(r, r, p, ctx))
748 goto err;
749
750 /* r := b/(a * b) = 1/a */
751 if (!BN_GF2m_mod_mul(r, r, b, p, ctx))
752 goto err;
753
754 ret = 1;
755
756 err:
757 BN_CTX_end(ctx);
758 return ret;
759 }
760
761 /*
762 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
763 * This function calls down to the BN_GF2m_mod_inv implementation; this
764 * wrapper function is only provided for convenience; for best performance,
765 * use the BN_GF2m_mod_inv function.
766 */
767 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
768 BN_CTX *ctx)
769 {
770 BIGNUM *field;
771 int ret = 0;
772
773 bn_check_top(xx);
774 BN_CTX_start(ctx);
775 if ((field = BN_CTX_get(ctx)) == NULL)
776 goto err;
777 if (!BN_GF2m_arr2poly(p, field))
778 goto err;
779
780 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
781 bn_check_top(r);
782
783 err:
784 BN_CTX_end(ctx);
785 return ret;
786 }
787
788 /*
789 * Divide y by x, reduce modulo p, and store the result in r. r could be x
790 * or y, x could equal y.
791 */
792 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
793 const BIGNUM *p, BN_CTX *ctx)
794 {
795 BIGNUM *xinv = NULL;
796 int ret = 0;
797
798 bn_check_top(y);
799 bn_check_top(x);
800 bn_check_top(p);
801
802 BN_CTX_start(ctx);
803 xinv = BN_CTX_get(ctx);
804 if (xinv == NULL)
805 goto err;
806
807 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
808 goto err;
809 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
810 goto err;
811 bn_check_top(r);
812 ret = 1;
813
814 err:
815 BN_CTX_end(ctx);
816 return ret;
817 }
818
819 /*
820 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
821 * * or yy, xx could equal yy. This function calls down to the
822 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
823 * convenience; for best performance, use the BN_GF2m_mod_div function.
824 */
825 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
826 const int p[], BN_CTX *ctx)
827 {
828 BIGNUM *field;
829 int ret = 0;
830
831 bn_check_top(yy);
832 bn_check_top(xx);
833
834 BN_CTX_start(ctx);
835 if ((field = BN_CTX_get(ctx)) == NULL)
836 goto err;
837 if (!BN_GF2m_arr2poly(p, field))
838 goto err;
839
840 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
841 bn_check_top(r);
842
843 err:
844 BN_CTX_end(ctx);
845 return ret;
846 }
847
848 /*
849 * Compute the bth power of a, reduce modulo p, and store the result in r. r
850 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
851 * P1363.
852 */
853 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
854 const int p[], BN_CTX *ctx)
855 {
856 int ret = 0, i, n;
857 BIGNUM *u;
858
859 bn_check_top(a);
860 bn_check_top(b);
861
862 if (BN_is_zero(b))
863 return BN_one(r);
864
865 if (BN_abs_is_word(b, 1))
866 return (BN_copy(r, a) != NULL);
867
868 BN_CTX_start(ctx);
869 if ((u = BN_CTX_get(ctx)) == NULL)
870 goto err;
871
872 if (!BN_GF2m_mod_arr(u, a, p))
873 goto err;
874
875 n = BN_num_bits(b) - 1;
876 for (i = n - 1; i >= 0; i--) {
877 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
878 goto err;
879 if (BN_is_bit_set(b, i)) {
880 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
881 goto err;
882 }
883 }
884 if (!BN_copy(r, u))
885 goto err;
886 bn_check_top(r);
887 ret = 1;
888 err:
889 BN_CTX_end(ctx);
890 return ret;
891 }
892
893 /*
894 * Compute the bth power of a, reduce modulo p, and store the result in r. r
895 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
896 * implementation; this wrapper function is only provided for convenience;
897 * for best performance, use the BN_GF2m_mod_exp_arr function.
898 */
899 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
900 const BIGNUM *p, BN_CTX *ctx)
901 {
902 int ret = 0;
903 const int max = BN_num_bits(p) + 1;
904 int *arr;
905
906 bn_check_top(a);
907 bn_check_top(b);
908 bn_check_top(p);
909
910 arr = OPENSSL_malloc_array(max, sizeof(*arr));
911 if (arr == NULL)
912 return 0;
913 ret = BN_GF2m_poly2arr(p, arr, max);
914 if (!ret || ret > max) {
915 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
916 goto err;
917 }
918 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
919 bn_check_top(r);
920 err:
921 OPENSSL_free(arr);
922 return ret;
923 }
924
925 /*
926 * Compute the square root of a, reduce modulo p, and store the result in r.
927 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
928 */
929 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
930 BN_CTX *ctx)
931 {
932 int ret = 0;
933 BIGNUM *u;
934
935 bn_check_top(a);
936
937 if (p[0] == 0) {
938 /* reduction mod 1 => return 0 */
939 BN_zero(r);
940 return 1;
941 }
942
943 BN_CTX_start(ctx);
944 if ((u = BN_CTX_get(ctx)) == NULL)
945 goto err;
946
947 if (!BN_set_bit(u, p[0] - 1))
948 goto err;
949 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
950 bn_check_top(r);
951
952 err:
953 BN_CTX_end(ctx);
954 return ret;
955 }
956
957 /*
958 * Compute the square root of a, reduce modulo p, and store the result in r.
959 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
960 * implementation; this wrapper function is only provided for convenience;
961 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
962 */
963 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
964 {
965 int ret = 0;
966 const int max = BN_num_bits(p) + 1;
967 int *arr;
968
969 bn_check_top(a);
970 bn_check_top(p);
971
972 arr = OPENSSL_malloc_array(max, sizeof(*arr));
973 if (arr == NULL)
974 return 0;
975 ret = BN_GF2m_poly2arr(p, arr, max);
976 if (!ret || ret > max) {
977 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
978 goto err;
979 }
980 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
981 bn_check_top(r);
982 err:
983 OPENSSL_free(arr);
984 return ret;
985 }
986
987 /*
988 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
989 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
990 */
991 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
992 BN_CTX *ctx)
993 {
994 int ret = 0, count = 0, j;
995 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
996
997 bn_check_top(a_);
998
999 if (p[0] == 0) {
1000 /* reduction mod 1 => return 0 */
1001 BN_zero(r);
1002 return 1;
1003 }
1004
1005 BN_CTX_start(ctx);
1006 a = BN_CTX_get(ctx);
1007 z = BN_CTX_get(ctx);
1008 w = BN_CTX_get(ctx);
1009 if (w == NULL)
1010 goto err;
1011
1012 if (!BN_GF2m_mod_arr(a, a_, p))
1013 goto err;
1014
1015 if (BN_is_zero(a)) {
1016 BN_zero(r);
1017 ret = 1;
1018 goto err;
1019 }
1020
1021 if (p[0] & 0x1) { /* m is odd */
1022 /* compute half-trace of a */
1023 if (!BN_copy(z, a))
1024 goto err;
1025 for (j = 1; j <= (p[0] - 1) / 2; j++) {
1026 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1027 goto err;
1028 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1029 goto err;
1030 if (!BN_GF2m_add(z, z, a))
1031 goto err;
1032 }
1033
1034 } else { /* m is even */
1035
1036 rho = BN_CTX_get(ctx);
1037 w2 = BN_CTX_get(ctx);
1038 tmp = BN_CTX_get(ctx);
1039 if (tmp == NULL)
1040 goto err;
1041 do {
1042 if (!BN_priv_rand_ex(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY,
1043 0, ctx))
1044 goto err;
1045 if (!BN_GF2m_mod_arr(rho, rho, p))
1046 goto err;
1047 BN_zero(z);
1048 if (!BN_copy(w, rho))
1049 goto err;
1050 for (j = 1; j <= p[0] - 1; j++) {
1051 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1052 goto err;
1053 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1054 goto err;
1055 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1056 goto err;
1057 if (!BN_GF2m_add(z, z, tmp))
1058 goto err;
1059 if (!BN_GF2m_add(w, w2, rho))
1060 goto err;
1061 }
1062 count++;
1063 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1064 if (BN_is_zero(w)) {
1065 ERR_raise(ERR_LIB_BN, BN_R_TOO_MANY_ITERATIONS);
1066 goto err;
1067 }
1068 }
1069
1070 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1071 goto err;
1072 if (!BN_GF2m_add(w, z, w))
1073 goto err;
1074 if (BN_GF2m_cmp(w, a)) {
1075 ERR_raise(ERR_LIB_BN, BN_R_NO_SOLUTION);
1076 goto err;
1077 }
1078
1079 if (!BN_copy(r, z))
1080 goto err;
1081 bn_check_top(r);
1082
1083 ret = 1;
1084
1085 err:
1086 BN_CTX_end(ctx);
1087 return ret;
1088 }
1089
1090 /*
1091 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1092 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1093 * implementation; this wrapper function is only provided for convenience;
1094 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1095 */
1096 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1097 BN_CTX *ctx)
1098 {
1099 int ret = 0;
1100 const int max = BN_num_bits(p) + 1;
1101 int *arr;
1102
1103 bn_check_top(a);
1104 bn_check_top(p);
1105
1106 arr = OPENSSL_malloc_array(max, sizeof(*arr));
1107 if (arr == NULL)
1108 goto err;
1109 ret = BN_GF2m_poly2arr(p, arr, max);
1110 if (!ret || ret > max) {
1111 ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
1112 goto err;
1113 }
1114 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1115 bn_check_top(r);
1116 err:
1117 OPENSSL_free(arr);
1118 return ret;
1119 }
1120
1121 /*
1122 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1123 * x^i) into an array of integers corresponding to the bits with non-zero
1124 * coefficient. The array is intended to be suitable for use with
1125 * `BN_GF2m_mod_arr()`, and so the constant term of the polynomial must not be
1126 * zero. This translates to a requirement that the input BIGNUM `a` is odd.
1127 *
1128 * Given sufficient room, the array is terminated with -1. Up to max elements
1129 * of the array will be filled.
1130 *
1131 * The return value is total number of array elements that would be filled if
1132 * array was large enough, including the terminating `-1`. It is `0` when `a`
1133 * is not odd or the constant term is zero contrary to requirement.
1134 *
1135 * The return value is also `0` when the leading exponent exceeds
1136 * `OPENSSL_ECC_MAX_FIELD_BITS`, this guards against CPU exhaustion attacks,
1137 */
1138 int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1139 {
1140 int i, j, k = 0;
1141 BN_ULONG mask;
1142
1143 if (!BN_is_odd(a))
1144 return 0;
1145
1146 for (i = a->top - 1; i >= 0; i--) {
1147 if (!a->d[i])
1148 /* skip word if a->d[i] == 0 */
1149 continue;
1150 mask = BN_TBIT;
1151 for (j = BN_BITS2 - 1; j >= 0; j--) {
1152 if (a->d[i] & mask) {
1153 if (k < max)
1154 p[k] = BN_BITS2 * i + j;
1155 k++;
1156 }
1157 mask >>= 1;
1158 }
1159 }
1160
1161 if (k > 0 && p[0] > OPENSSL_ECC_MAX_FIELD_BITS)
1162 return 0;
1163
1164 if (k < max)
1165 p[k] = -1;
1166
1167 return k + 1;
1168 }
1169
1170 /*
1171 * Convert the coefficient array representation of a polynomial to a
1172 * bit-string. The array must be terminated by -1.
1173 */
1174 int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1175 {
1176 int i;
1177
1178 bn_check_top(a);
1179 BN_zero(a);
1180 for (i = 0; p[i] != -1; i++) {
1181 if (BN_set_bit(a, p[i]) == 0)
1182 return 0;
1183 }
1184 bn_check_top(a);
1185
1186 return 1;
1187 }
1188
1189 #endif