2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (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
14 #include "internal/cryptlib.h"
17 #ifndef OPENSSL_NO_EC2M
20 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
23 # define MAX_ITERATIONS 50
25 static const BN_ULONG SQR_tb[16] = { 0, 1, 4, 5, 16, 17, 20, 21,
26 64, 65, 68, 69, 80, 81, 84, 85
29 /* Platform-specific macros to accelerate squaring. */
30 # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
32 SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
33 SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
34 SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
35 SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
37 SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
38 SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
39 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
40 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
42 # ifdef THIRTY_TWO_BIT
44 SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
45 SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
47 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
48 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
51 # if !defined(OPENSSL_BN_ASM_GF2m)
53 * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
54 * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
55 * the variables have the right amount of space allocated.
57 # ifdef THIRTY_TWO_BIT
58 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
61 register BN_ULONG h, l, s;
62 BN_ULONG tab[8], top2b = a >> 30;
63 register BN_ULONG a1, a2, a4;
65 a1 = a & (0x3FFFFFFF);
76 tab[7] = a1 ^ a2 ^ a4;
80 s = tab[b >> 3 & 0x7];
83 s = tab[b >> 6 & 0x7];
86 s = tab[b >> 9 & 0x7];
89 s = tab[b >> 12 & 0x7];
92 s = tab[b >> 15 & 0x7];
95 s = tab[b >> 18 & 0x7];
98 s = tab[b >> 21 & 0x7];
101 s = tab[b >> 24 & 0x7];
104 s = tab[b >> 27 & 0x7];
111 /* compensate for the top two bits of a */
126 # if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
127 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
130 register BN_ULONG h, l, s;
131 BN_ULONG tab[16], top3b = a >> 61;
132 register BN_ULONG a1, a2, a4, a8;
134 a1 = a & (0x1FFFFFFFFFFFFFFFULL);
146 tab[7] = a1 ^ a2 ^ a4;
150 tab[11] = a1 ^ a2 ^ a8;
152 tab[13] = a1 ^ a4 ^ a8;
153 tab[14] = a2 ^ a4 ^ a8;
154 tab[15] = a1 ^ a2 ^ a4 ^ a8;
158 s = tab[b >> 4 & 0xF];
161 s = tab[b >> 8 & 0xF];
164 s = tab[b >> 12 & 0xF];
167 s = tab[b >> 16 & 0xF];
170 s = tab[b >> 20 & 0xF];
173 s = tab[b >> 24 & 0xF];
176 s = tab[b >> 28 & 0xF];
179 s = tab[b >> 32 & 0xF];
182 s = tab[b >> 36 & 0xF];
185 s = tab[b >> 40 & 0xF];
188 s = tab[b >> 44 & 0xF];
191 s = tab[b >> 48 & 0xF];
194 s = tab[b >> 52 & 0xF];
197 s = tab[b >> 56 & 0xF];
204 /* compensate for the top three bits of a */
225 * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
226 * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
227 * ensure that the variables have the right amount of space allocated.
229 static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
230 const BN_ULONG b1, const BN_ULONG b0)
233 /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
234 bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
235 bn_GF2m_mul_1x1(r + 1, r, a0, b0);
236 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
237 /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
238 r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
239 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
242 void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1,
247 * Add polynomials a and b and store result in r; r could be a or b, a and b
248 * could be equal; r is the bitwise XOR of a and b.
250 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
253 const BIGNUM *at, *bt;
258 if (a->top < b->top) {
266 if (bn_wexpand(r, at->top) == NULL)
269 for (i = 0; i < bt->top; i++) {
270 r->d[i] = at->d[i] ^ bt->d[i];
272 for (; i < at->top; i++) {
283 * Some functions allow for representation of the irreducible polynomials
284 * as an int[], say p. The irreducible f(t) is then of the form:
285 * t^p[0] + t^p[1] + ... + t^p[k]
286 * where m = p[0] > p[1] > ... > p[k] = 0.
289 /* Performs modular reduction of a and store result in r. r could be a. */
290 int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[])
299 /* reduction mod 1 => return 0 */
305 * Since the algorithm does reduction in the r value, if a != r, copy the
306 * contents of a into r so we can do reduction in r.
309 if (!bn_wexpand(r, a->top))
311 for (j = 0; j < a->top; j++) {
318 /* start reduction */
319 dN = p[0] / BN_BITS2;
320 for (j = r->top - 1; j > dN;) {
328 for (k = 1; p[k] != 0; k++) {
329 /* reducing component t^p[k] */
334 z[j - n] ^= (zz >> d0);
336 z[j - n - 1] ^= (zz << d1);
339 /* reducing component t^0 */
341 d0 = p[0] % BN_BITS2;
343 z[j - n] ^= (zz >> d0);
345 z[j - n - 1] ^= (zz << d1);
348 /* final round of reduction */
351 d0 = p[0] % BN_BITS2;
357 /* clear up the top d1 bits */
359 z[dN] = (z[dN] << d1) >> d1;
362 z[0] ^= zz; /* reduction t^0 component */
364 for (k = 1; p[k] != 0; k++) {
367 /* reducing component t^p[k] */
369 d0 = p[k] % BN_BITS2;
372 if (d0 && (tmp_ulong = zz >> d1))
373 z[n + 1] ^= tmp_ulong;
383 * Performs modular reduction of a by p and store result in r. r could be a.
384 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
385 * function is only provided for convenience; for best performance, use the
386 * BN_GF2m_mod_arr function.
388 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
394 ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr));
395 if (!ret || ret > (int)OSSL_NELEM(arr)) {
396 BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH);
399 ret = BN_GF2m_mod_arr(r, a, arr);
405 * Compute the product of two polynomials a and b, reduce modulo p, and store
406 * the result in r. r could be a or b; a could be b.
408 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
409 const int p[], BN_CTX *ctx)
411 int zlen, i, j, k, ret = 0;
413 BN_ULONG x1, x0, y1, y0, zz[4];
419 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
423 if ((s = BN_CTX_get(ctx)) == NULL)
426 zlen = a->top + b->top + 4;
427 if (!bn_wexpand(s, zlen))
431 for (i = 0; i < zlen; i++)
434 for (j = 0; j < b->top; j += 2) {
436 y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
437 for (i = 0; i < a->top; i += 2) {
439 x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
440 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
441 for (k = 0; k < 4; k++)
442 s->d[i + j + k] ^= zz[k];
447 if (BN_GF2m_mod_arr(r, s, p))
457 * Compute the product of two polynomials a and b, reduce modulo p, and store
458 * the result in r. r could be a or b; a could equal b. This function calls
459 * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
460 * only provided for convenience; for best performance, use the
461 * BN_GF2m_mod_mul_arr function.
463 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
464 const BIGNUM *p, BN_CTX *ctx)
467 const int max = BN_num_bits(p) + 1;
472 if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
474 ret = BN_GF2m_poly2arr(p, arr, max);
475 if (!ret || ret > max) {
476 BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);
479 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
486 /* Square a, reduce the result mod p, and store it in a. r could be a. */
487 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const int p[],
495 if ((s = BN_CTX_get(ctx)) == NULL)
497 if (!bn_wexpand(s, 2 * a->top))
500 for (i = a->top - 1; i >= 0; i--) {
501 s->d[2 * i + 1] = SQR1(a->d[i]);
502 s->d[2 * i] = SQR0(a->d[i]);
507 if (!BN_GF2m_mod_arr(r, s, p))
517 * Square a, reduce the result mod p, and store it in a. r could be a. This
518 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
519 * wrapper function is only provided for convenience; for best performance,
520 * use the BN_GF2m_mod_sqr_arr function.
522 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
525 const int max = BN_num_bits(p) + 1;
530 if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
532 ret = BN_GF2m_poly2arr(p, arr, max);
533 if (!ret || ret > max) {
534 BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
537 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
545 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
546 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
547 * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
548 * Curve Cryptography Over Binary Fields".
550 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
552 BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
560 if ((b = BN_CTX_get(ctx)) == NULL)
562 if ((c = BN_CTX_get(ctx)) == NULL)
564 if ((u = BN_CTX_get(ctx)) == NULL)
566 if ((v = BN_CTX_get(ctx)) == NULL)
569 if (!BN_GF2m_mod(u, a, p))
581 while (!BN_is_odd(u)) {
584 if (!BN_rshift1(u, u))
587 if (!BN_GF2m_add(b, b, p))
590 if (!BN_rshift1(b, b))
594 if (BN_abs_is_word(u, 1))
597 if (BN_num_bits(u) < BN_num_bits(v)) {
606 if (!BN_GF2m_add(u, u, v))
608 if (!BN_GF2m_add(b, b, c))
614 int ubits = BN_num_bits(u);
615 int vbits = BN_num_bits(v); /* v is copy of p */
617 BN_ULONG *udp, *bdp, *vdp, *cdp;
619 if (!bn_wexpand(u, top))
622 for (i = u->top; i < top; i++)
625 if (!bn_wexpand(b, top))
629 for (i = 1; i < top; i++)
632 if (!bn_wexpand(c, top))
635 for (i = 0; i < top; i++)
638 vdp = v->d; /* It pays off to "cache" *->d pointers,
639 * because it allows optimizer to be more
640 * aggressive. But we don't have to "cache"
641 * p->d, because *p is declared 'const'... */
643 while (ubits && !(udp[0] & 1)) {
644 BN_ULONG u0, u1, b0, b1, mask;
648 mask = (BN_ULONG)0 - (b0 & 1);
649 b0 ^= p->d[0] & mask;
650 for (i = 0; i < top - 1; i++) {
652 udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
654 b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
655 bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
663 if (ubits <= BN_BITS2) {
664 if (udp[0] == 0) /* poly was reducible */
685 for (i = 0; i < top; i++) {
689 if (ubits == vbits) {
691 int utop = (ubits - 1) / BN_BITS2;
693 while ((ul = udp[utop]) == 0 && utop)
695 ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
708 # ifdef BN_DEBUG /* BN_CTX_end would complain about the
719 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
720 * This function calls down to the BN_GF2m_mod_inv implementation; this
721 * wrapper function is only provided for convenience; for best performance,
722 * use the BN_GF2m_mod_inv function.
724 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const int p[],
732 if ((field = BN_CTX_get(ctx)) == NULL)
734 if (!BN_GF2m_arr2poly(p, field))
737 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
745 # ifndef OPENSSL_SUN_GF2M_DIV
747 * Divide y by x, reduce modulo p, and store the result in r. r could be x
748 * or y, x could equal y.
750 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
751 const BIGNUM *p, BN_CTX *ctx)
761 xinv = BN_CTX_get(ctx);
765 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
767 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
778 * Divide y by x, reduce modulo p, and store the result in r. r could be x
779 * or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
780 * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
783 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
784 const BIGNUM *p, BN_CTX *ctx)
786 BIGNUM *a, *b, *u, *v;
802 /* reduce x and y mod p */
803 if (!BN_GF2m_mod(u, y, p))
805 if (!BN_GF2m_mod(a, x, p))
810 while (!BN_is_odd(a)) {
811 if (!BN_rshift1(a, a))
814 if (!BN_GF2m_add(u, u, p))
816 if (!BN_rshift1(u, u))
821 if (BN_GF2m_cmp(b, a) > 0) {
822 if (!BN_GF2m_add(b, b, a))
824 if (!BN_GF2m_add(v, v, u))
827 if (!BN_rshift1(b, b))
830 if (!BN_GF2m_add(v, v, p))
832 if (!BN_rshift1(v, v))
834 } while (!BN_is_odd(b));
835 } else if (BN_abs_is_word(a, 1))
838 if (!BN_GF2m_add(a, a, b))
840 if (!BN_GF2m_add(u, u, v))
843 if (!BN_rshift1(a, a))
846 if (!BN_GF2m_add(u, u, p))
848 if (!BN_rshift1(u, u))
850 } while (!BN_is_odd(a));
866 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
867 * * or yy, xx could equal yy. This function calls down to the
868 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
869 * convenience; for best performance, use the BN_GF2m_mod_div function.
871 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
872 const int p[], BN_CTX *ctx)
881 if ((field = BN_CTX_get(ctx)) == NULL)
883 if (!BN_GF2m_arr2poly(p, field))
886 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
895 * Compute the bth power of a, reduce modulo p, and store the result in r. r
896 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
899 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
900 const int p[], BN_CTX *ctx)
911 if (BN_abs_is_word(b, 1))
912 return (BN_copy(r, a) != NULL);
915 if ((u = BN_CTX_get(ctx)) == NULL)
918 if (!BN_GF2m_mod_arr(u, a, p))
921 n = BN_num_bits(b) - 1;
922 for (i = n - 1; i >= 0; i--) {
923 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
925 if (BN_is_bit_set(b, i)) {
926 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
940 * Compute the bth power of a, reduce modulo p, and store the result in r. r
941 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
942 * implementation; this wrapper function is only provided for convenience;
943 * for best performance, use the BN_GF2m_mod_exp_arr function.
945 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
946 const BIGNUM *p, BN_CTX *ctx)
949 const int max = BN_num_bits(p) + 1;
954 if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
956 ret = BN_GF2m_poly2arr(p, arr, max);
957 if (!ret || ret > max) {
958 BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH);
961 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
969 * Compute the square root of a, reduce modulo p, and store the result in r.
970 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
972 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const int p[],
981 /* reduction mod 1 => return 0 */
987 if ((u = BN_CTX_get(ctx)) == NULL)
990 if (!BN_set_bit(u, p[0] - 1))
992 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
1001 * Compute the square root of a, reduce modulo p, and store the result in r.
1002 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
1003 * implementation; this wrapper function is only provided for convenience;
1004 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
1006 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1009 const int max = BN_num_bits(p) + 1;
1013 if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
1015 ret = BN_GF2m_poly2arr(p, arr, max);
1016 if (!ret || ret > max) {
1017 BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
1020 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
1028 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1029 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
1031 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1034 int ret = 0, count = 0, j;
1035 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1040 /* reduction mod 1 => return 0 */
1046 a = BN_CTX_get(ctx);
1047 z = BN_CTX_get(ctx);
1048 w = BN_CTX_get(ctx);
1052 if (!BN_GF2m_mod_arr(a, a_, p))
1055 if (BN_is_zero(a)) {
1061 if (p[0] & 0x1) { /* m is odd */
1062 /* compute half-trace of a */
1065 for (j = 1; j <= (p[0] - 1) / 2; j++) {
1066 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1068 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1070 if (!BN_GF2m_add(z, z, a))
1074 } else { /* m is even */
1076 rho = BN_CTX_get(ctx);
1077 w2 = BN_CTX_get(ctx);
1078 tmp = BN_CTX_get(ctx);
1082 if (!BN_rand(rho, p[0], BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
1084 if (!BN_GF2m_mod_arr(rho, rho, p))
1087 if (!BN_copy(w, rho))
1089 for (j = 1; j <= p[0] - 1; j++) {
1090 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1092 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1094 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1096 if (!BN_GF2m_add(z, z, tmp))
1098 if (!BN_GF2m_add(w, w2, rho))
1102 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1103 if (BN_is_zero(w)) {
1104 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);
1109 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1111 if (!BN_GF2m_add(w, z, w))
1113 if (BN_GF2m_cmp(w, a)) {
1114 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
1130 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1131 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1132 * implementation; this wrapper function is only provided for convenience;
1133 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1135 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1139 const int max = BN_num_bits(p) + 1;
1143 if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL)
1145 ret = BN_GF2m_poly2arr(p, arr, max);
1146 if (!ret || ret > max) {
1147 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
1150 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1158 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1159 * x^i) into an array of integers corresponding to the bits with non-zero
1160 * coefficient. Array is terminated with -1. Up to max elements of the array
1161 * will be filled. Return value is total number of array elements that would
1162 * be filled if array was large enough.
1164 int BN_GF2m_poly2arr(const BIGNUM *a, int p[], int max)
1172 for (i = a->top - 1; i >= 0; i--) {
1174 /* skip word if a->d[i] == 0 */
1177 for (j = BN_BITS2 - 1; j >= 0; j--) {
1178 if (a->d[i] & mask) {
1180 p[k] = BN_BITS2 * i + j;
1196 * Convert the coefficient array representation of a polynomial to a
1197 * bit-string. The array must be terminated by -1.
1199 int BN_GF2m_arr2poly(const int p[], BIGNUM *a)
1205 for (i = 0; p[i] != -1; i++) {
1206 if (BN_set_bit(a, p[i]) == 0)