1 /* crypto/bn/bn_gf2m.c */
2 /* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
12 * In addition, Sun covenants to all licensees who provide a reciprocal
13 * covenant with respect to their own patents if any, not to sue under
14 * current and future patent claims necessarily infringed by the making,
15 * using, practicing, selling, offering for sale and/or otherwise
16 * disposing of the ECC Code as delivered hereunder (or portions thereof),
17 * provided that such covenant shall not apply:
18 * 1) for code that a licensee deletes from the ECC Code;
19 * 2) separates from the ECC Code; or
20 * 3) for infringements caused by:
21 * i) the modification of the ECC Code or
22 * ii) the combination of the ECC Code with other software or
23 * devices where such combination causes the infringement.
25 * The software is originally written by Sheueling Chang Shantz and
26 * Douglas Stebila of Sun Microsystems Laboratories.
31 * NOTE: This file is licensed pursuant to the OpenSSL license below and may
32 * be modified; but after modifications, the above covenant may no longer
33 * apply! In such cases, the corresponding paragraph ["In addition, Sun
34 * covenants ... causes the infringement."] and this note can be edited out;
35 * but please keep the Sun copyright notice and attribution.
38 /* ====================================================================
39 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in
50 * the documentation and/or other materials provided with the
53 * 3. All advertising materials mentioning features or use of this
54 * software must display the following acknowledgment:
55 * "This product includes software developed by the OpenSSL Project
56 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
58 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
59 * endorse or promote products derived from this software without
60 * prior written permission. For written permission, please contact
61 * openssl-core@openssl.org.
63 * 5. Products derived from this software may not be called "OpenSSL"
64 * nor may "OpenSSL" appear in their names without prior written
65 * permission of the OpenSSL Project.
67 * 6. Redistributions of any form whatsoever must retain the following
69 * "This product includes software developed by the OpenSSL Project
70 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
72 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
73 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
75 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
76 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
77 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
78 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
80 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
81 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
82 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
83 * OF THE POSSIBILITY OF SUCH DAMAGE.
84 * ====================================================================
86 * This product includes cryptographic software written by Eric Young
87 * (eay@cryptsoft.com). This product includes software written by Tim
88 * Hudson (tjh@cryptsoft.com).
99 * Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should
102 #define MAX_ITERATIONS 50
104 static const BN_ULONG SQR_tb[16] = { 0, 1, 4, 5, 16, 17, 20, 21,
105 64, 65, 68, 69, 80, 81, 84, 85
108 /* Platform-specific macros to accelerate squaring. */
109 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
111 SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
112 SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
113 SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
114 SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
116 SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
117 SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
118 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
119 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
121 #ifdef THIRTY_TWO_BIT
123 SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
124 SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
126 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
127 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
131 SQR_tb[(w) >> 12 & 0xF] << 8 | SQR_tb[(w) >> 8 & 0xF]
133 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
137 SQR_tb[(w) >> 4 & 0xF]
143 * Product of two polynomials a, b each with degree < BN_BITS2 - 1, result is
144 * a polynomial r with degree < 2 * BN_BITS - 1 The caller MUST ensure that
145 * the variables have the right amount of space allocated.
148 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
151 register BN_ULONG h, l, s;
152 BN_ULONG tab[4], top1b = a >> 7;
153 register BN_ULONG a1, a2;
165 s = tab[b >> 2 & 0x3];
168 s = tab[b >> 4 & 0x3];
175 /* compensate for the top bit of a */
187 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
190 register BN_ULONG h, l, s;
191 BN_ULONG tab[4], top1b = a >> 15;
192 register BN_ULONG a1, a2;
204 s = tab[b >> 2 & 0x3];
207 s = tab[b >> 4 & 0x3];
210 s = tab[b >> 6 & 0x3];
213 s = tab[b >> 8 & 0x3];
216 s = tab[b >> 10 & 0x3];
219 s = tab[b >> 12 & 0x3];
226 /* compensate for the top bit of a */
237 #ifdef THIRTY_TWO_BIT
238 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
241 register BN_ULONG h, l, s;
242 BN_ULONG tab[8], top2b = a >> 30;
243 register BN_ULONG a1, a2, a4;
245 a1 = a & (0x3FFFFFFF);
256 tab[7] = a1 ^ a2 ^ a4;
260 s = tab[b >> 3 & 0x7];
263 s = tab[b >> 6 & 0x7];
266 s = tab[b >> 9 & 0x7];
269 s = tab[b >> 12 & 0x7];
272 s = tab[b >> 15 & 0x7];
275 s = tab[b >> 18 & 0x7];
278 s = tab[b >> 21 & 0x7];
281 s = tab[b >> 24 & 0x7];
284 s = tab[b >> 27 & 0x7];
291 /* compensate for the top two bits of a */
306 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
307 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a,
310 register BN_ULONG h, l, s;
311 BN_ULONG tab[16], top3b = a >> 61;
312 register BN_ULONG a1, a2, a4, a8;
314 a1 = a & (0x1FFFFFFFFFFFFFFFULL);
326 tab[7] = a1 ^ a2 ^ a4;
330 tab[11] = a1 ^ a2 ^ a8;
332 tab[13] = a1 ^ a4 ^ a8;
333 tab[14] = a2 ^ a4 ^ a8;
334 tab[15] = a1 ^ a2 ^ a4 ^ a8;
338 s = tab[b >> 4 & 0xF];
341 s = tab[b >> 8 & 0xF];
344 s = tab[b >> 12 & 0xF];
347 s = tab[b >> 16 & 0xF];
350 s = tab[b >> 20 & 0xF];
353 s = tab[b >> 24 & 0xF];
356 s = tab[b >> 28 & 0xF];
359 s = tab[b >> 32 & 0xF];
362 s = tab[b >> 36 & 0xF];
365 s = tab[b >> 40 & 0xF];
368 s = tab[b >> 44 & 0xF];
371 s = tab[b >> 48 & 0xF];
374 s = tab[b >> 52 & 0xF];
377 s = tab[b >> 56 & 0xF];
384 /* compensate for the top three bits of a */
405 * Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
406 * result is a polynomial r with degree < 4 * BN_BITS2 - 1 The caller MUST
407 * ensure that the variables have the right amount of space allocated.
409 static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0,
410 const BN_ULONG b1, const BN_ULONG b0)
413 /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
414 bn_GF2m_mul_1x1(r + 3, r + 2, a1, b1);
415 bn_GF2m_mul_1x1(r + 1, r, a0, b0);
416 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
417 /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
418 r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
419 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
423 * Add polynomials a and b and store result in r; r could be a or b, a and b
424 * could be equal; r is the bitwise XOR of a and b.
426 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
429 const BIGNUM *at, *bt;
434 if (a->top < b->top) {
442 if (bn_wexpand(r, at->top) == NULL)
445 for (i = 0; i < bt->top; i++) {
446 r->d[i] = at->d[i] ^ bt->d[i];
448 for (; i < at->top; i++) {
459 * Some functions allow for representation of the irreducible polynomials
460 * as an int[], say p. The irreducible f(t) is then of the form:
461 * t^p[0] + t^p[1] + ... + t^p[k]
462 * where m = p[0] > p[1] > ... > p[k] = 0.
465 /* Performs modular reduction of a and store result in r. r could be a. */
466 int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
475 /* reduction mod 1 => return 0 */
481 * Since the algorithm does reduction in the r value, if a != r, copy the
482 * contents of a into r so we can do reduction in r.
485 if (!bn_wexpand(r, a->top))
487 for (j = 0; j < a->top; j++) {
494 /* start reduction */
495 dN = p[0] / BN_BITS2;
496 for (j = r->top - 1; j > dN;) {
504 for (k = 1; p[k] != 0; k++) {
505 /* reducing component t^p[k] */
510 z[j - n] ^= (zz >> d0);
512 z[j - n - 1] ^= (zz << d1);
515 /* reducing component t^0 */
517 d0 = p[0] % BN_BITS2;
519 z[j - n] ^= (zz >> d0);
521 z[j - n - 1] ^= (zz << d1);
524 /* final round of reduction */
527 d0 = p[0] % BN_BITS2;
533 /* clear up the top d1 bits */
535 z[dN] = (z[dN] << d1) >> d1;
538 z[0] ^= zz; /* reduction t^0 component */
540 for (k = 1; p[k] != 0; k++) {
543 /* reducing component t^p[k] */
545 d0 = p[k] % BN_BITS2;
548 tmp_ulong = zz >> d1;
550 z[n + 1] ^= tmp_ulong;
560 * Performs modular reduction of a by p and store result in r. r could be a.
561 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
562 * function is only provided for convenience; for best performance, use the
563 * BN_GF2m_mod_arr function.
565 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
568 const int max = BN_num_bits(p);
569 unsigned int *arr = NULL;
573 (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
575 ret = BN_GF2m_poly2arr(p, arr, max);
576 if (!ret || ret > max) {
577 BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH);
580 ret = BN_GF2m_mod_arr(r, a, arr);
589 * Compute the product of two polynomials a and b, reduce modulo p, and store
590 * the result in r. r could be a or b; a could be b.
592 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
593 const unsigned int p[], BN_CTX *ctx)
595 int zlen, i, j, k, ret = 0;
597 BN_ULONG x1, x0, y1, y0, zz[4];
603 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
607 if ((s = BN_CTX_get(ctx)) == NULL)
610 zlen = a->top + b->top + 4;
611 if (!bn_wexpand(s, zlen))
615 for (i = 0; i < zlen; i++)
618 for (j = 0; j < b->top; j += 2) {
620 y1 = ((j + 1) == b->top) ? 0 : b->d[j + 1];
621 for (i = 0; i < a->top; i += 2) {
623 x1 = ((i + 1) == a->top) ? 0 : a->d[i + 1];
624 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
625 for (k = 0; k < 4; k++)
626 s->d[i + j + k] ^= zz[k];
631 if (BN_GF2m_mod_arr(r, s, p))
641 * Compute the product of two polynomials a and b, reduce modulo p, and store
642 * the result in r. r could be a or b; a could equal b. This function calls
643 * down to the BN_GF2m_mod_mul_arr implementation; this wrapper function is
644 * only provided for convenience; for best performance, use the
645 * BN_GF2m_mod_mul_arr function.
647 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
648 const BIGNUM *p, BN_CTX *ctx)
651 const int max = BN_num_bits(p);
652 unsigned int *arr = NULL;
657 (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
659 ret = BN_GF2m_poly2arr(p, arr, max);
660 if (!ret || ret > max) {
661 BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH);
664 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
672 /* Square a, reduce the result mod p, and store it in a. r could be a. */
673 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[],
681 if ((s = BN_CTX_get(ctx)) == NULL)
683 if (!bn_wexpand(s, 2 * a->top))
686 for (i = a->top - 1; i >= 0; i--) {
687 s->d[2 * i + 1] = SQR1(a->d[i]);
688 s->d[2 * i] = SQR0(a->d[i]);
693 if (!BN_GF2m_mod_arr(r, s, p))
703 * Square a, reduce the result mod p, and store it in a. r could be a. This
704 * function calls down to the BN_GF2m_mod_sqr_arr implementation; this
705 * wrapper function is only provided for convenience; for best performance,
706 * use the BN_GF2m_mod_sqr_arr function.
708 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
711 const int max = BN_num_bits(p);
712 unsigned int *arr = NULL;
717 (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
719 ret = BN_GF2m_poly2arr(p, arr, max);
720 if (!ret || ret > max) {
721 BNerr(BN_F_BN_GF2M_MOD_SQR, BN_R_INVALID_LENGTH);
724 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
733 * Invert a, reduce modulo p, and store the result in r. r could be a. Uses
734 * Modified Almost Inverse Algorithm (Algorithm 10) from Hankerson, D.,
735 * Hernandez, J.L., and Menezes, A. "Software Implementation of Elliptic
736 * Curve Cryptography Over Binary Fields".
738 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
740 BIGNUM *b, *c, *u, *v, *tmp;
757 if (!BN_GF2m_mod(u, a, p))
766 while (!BN_is_odd(u)) {
769 if (!BN_rshift1(u, u))
772 if (!BN_GF2m_add(b, b, p))
775 if (!BN_rshift1(b, b))
779 if (BN_abs_is_word(u, 1))
782 if (BN_num_bits(u) < BN_num_bits(v)) {
791 if (!BN_GF2m_add(u, u, v))
793 if (!BN_GF2m_add(b, b, c))
808 * Invert xx, reduce modulo p, and store the result in r. r could be xx.
809 * This function calls down to the BN_GF2m_mod_inv implementation; this
810 * wrapper function is only provided for convenience; for best performance,
811 * use the BN_GF2m_mod_inv function.
813 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const unsigned int p[],
821 if ((field = BN_CTX_get(ctx)) == NULL)
823 if (!BN_GF2m_arr2poly(p, field))
826 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
834 #ifndef OPENSSL_SUN_GF2M_DIV
836 * Divide y by x, reduce modulo p, and store the result in r. r could be x
837 * or y, x could equal y.
839 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
840 const BIGNUM *p, BN_CTX *ctx)
850 xinv = BN_CTX_get(ctx);
854 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
856 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx))
867 * Divide y by x, reduce modulo p, and store the result in r. r could be x
868 * or y, x could equal y. Uses algorithm Modular_Division_GF(2^m) from
869 * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to the
872 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x,
873 const BIGNUM *p, BN_CTX *ctx)
875 BIGNUM *a, *b, *u, *v;
891 /* reduce x and y mod p */
892 if (!BN_GF2m_mod(u, y, p))
894 if (!BN_GF2m_mod(a, x, p))
899 while (!BN_is_odd(a)) {
900 if (!BN_rshift1(a, a))
903 if (!BN_GF2m_add(u, u, p))
905 if (!BN_rshift1(u, u))
910 if (BN_GF2m_cmp(b, a) > 0) {
911 if (!BN_GF2m_add(b, b, a))
913 if (!BN_GF2m_add(v, v, u))
916 if (!BN_rshift1(b, b))
919 if (!BN_GF2m_add(v, v, p))
921 if (!BN_rshift1(v, v))
923 } while (!BN_is_odd(b));
924 } else if (BN_abs_is_word(a, 1))
927 if (!BN_GF2m_add(a, a, b))
929 if (!BN_GF2m_add(u, u, v))
932 if (!BN_rshift1(a, a))
935 if (!BN_GF2m_add(u, u, p))
937 if (!BN_rshift1(u, u))
939 } while (!BN_is_odd(a));
955 * Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
956 * * or yy, xx could equal yy. This function calls down to the
957 * BN_GF2m_mod_div implementation; this wrapper function is only provided for
958 * convenience; for best performance, use the BN_GF2m_mod_div function.
960 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx,
961 const unsigned int p[], BN_CTX *ctx)
970 if ((field = BN_CTX_get(ctx)) == NULL)
972 if (!BN_GF2m_arr2poly(p, field))
975 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
984 * Compute the bth power of a, reduce modulo p, and store the result in r. r
985 * could be a. Uses simple square-and-multiply algorithm A.5.1 from IEEE
988 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
989 const unsigned int p[], BN_CTX *ctx)
1000 if (BN_abs_is_word(b, 1))
1001 return (BN_copy(r, a) != NULL);
1004 if ((u = BN_CTX_get(ctx)) == NULL)
1007 if (!BN_GF2m_mod_arr(u, a, p))
1010 n = BN_num_bits(b) - 1;
1011 for (i = n - 1; i >= 0; i--) {
1012 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx))
1014 if (BN_is_bit_set(b, i)) {
1015 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx))
1029 * Compute the bth power of a, reduce modulo p, and store the result in r. r
1030 * could be a. This function calls down to the BN_GF2m_mod_exp_arr
1031 * implementation; this wrapper function is only provided for convenience;
1032 * for best performance, use the BN_GF2m_mod_exp_arr function.
1034 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
1035 const BIGNUM *p, BN_CTX *ctx)
1038 const int max = BN_num_bits(p);
1039 unsigned int *arr = NULL;
1044 (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
1046 ret = BN_GF2m_poly2arr(p, arr, max);
1047 if (!ret || ret > max) {
1048 BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH);
1051 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
1060 * Compute the square root of a, reduce modulo p, and store the result in r.
1061 * r could be a. Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
1063 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[],
1072 /* reduction mod 1 => return 0 */
1078 if ((u = BN_CTX_get(ctx)) == NULL)
1081 if (!BN_set_bit(u, p[0] - 1))
1083 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
1092 * Compute the square root of a, reduce modulo p, and store the result in r.
1093 * r could be a. This function calls down to the BN_GF2m_mod_sqrt_arr
1094 * implementation; this wrapper function is only provided for convenience;
1095 * for best performance, use the BN_GF2m_mod_sqrt_arr function.
1097 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1100 const int max = BN_num_bits(p);
1101 unsigned int *arr = NULL;
1105 (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL)
1107 ret = BN_GF2m_poly2arr(p, arr, max);
1108 if (!ret || ret > max) {
1109 BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH);
1112 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
1121 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1122 * 0. Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
1124 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_,
1125 const unsigned int p[], BN_CTX *ctx)
1127 int ret = 0, count = 0;
1129 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
1134 /* reduction mod 1 => return 0 */
1140 a = BN_CTX_get(ctx);
1141 z = BN_CTX_get(ctx);
1142 w = BN_CTX_get(ctx);
1146 if (!BN_GF2m_mod_arr(a, a_, p))
1149 if (BN_is_zero(a)) {
1155 if (p[0] & 0x1) { /* m is odd */
1156 /* compute half-trace of a */
1159 for (j = 1; j <= (p[0] - 1) / 2; j++) {
1160 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1162 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1164 if (!BN_GF2m_add(z, z, a))
1168 } else { /* m is even */
1170 rho = BN_CTX_get(ctx);
1171 w2 = BN_CTX_get(ctx);
1172 tmp = BN_CTX_get(ctx);
1176 if (!BN_rand(rho, p[0], 0, 0))
1178 if (!BN_GF2m_mod_arr(rho, rho, p))
1181 if (!BN_copy(w, rho))
1183 for (j = 1; j <= p[0] - 1; j++) {
1184 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx))
1186 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx))
1188 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx))
1190 if (!BN_GF2m_add(z, z, tmp))
1192 if (!BN_GF2m_add(w, w2, rho))
1196 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
1197 if (BN_is_zero(w)) {
1198 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_TOO_MANY_ITERATIONS);
1203 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx))
1205 if (!BN_GF2m_add(w, z, w))
1207 if (BN_GF2m_cmp(w, a)) {
1208 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
1224 * Find r such that r^2 + r = a mod p. r could be a. If no r exists returns
1225 * 0. This function calls down to the BN_GF2m_mod_solve_quad_arr
1226 * implementation; this wrapper function is only provided for convenience;
1227 * for best performance, use the BN_GF2m_mod_solve_quad_arr function.
1229 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
1233 const int max = BN_num_bits(p);
1234 unsigned int *arr = NULL;
1237 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) *
1240 ret = BN_GF2m_poly2arr(p, arr, max);
1241 if (!ret || ret > max) {
1242 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH);
1245 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1254 * Convert the bit-string representation of a polynomial ( \sum_{i=0}^n a_i *
1255 * x^i , where a_0 is *not* zero) into an array of integers corresponding to
1256 * the bits with non-zero coefficient. Up to max elements of the array will
1257 * be filled. Return value is total number of coefficients that would be
1258 * extracted if array was large enough.
1260 int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max)
1265 if (BN_is_zero(a) || !BN_is_bit_set(a, 0))
1267 * a_0 == 0 => return error (the unsigned int array must be
1272 for (i = a->top - 1; i >= 0; i--) {
1274 /* skip word if a->d[i] == 0 */
1277 for (j = BN_BITS2 - 1; j >= 0; j--) {
1278 if (a->d[i] & mask) {
1280 p[k] = BN_BITS2 * i + j;
1291 * Convert the coefficient array representation of a polynomial to a
1292 * bit-string. The array must be terminated by 0.
1294 int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a)
1300 for (i = 0; p[i] != 0; i++) {
1301 if (BN_set_bit(a, p[i]) == 0)
1311 * Constant-time conditional swap of a and b.
1312 * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
1313 * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
1314 * and that no more than nwords are used by either a or b.
1315 * a and b cannot be the same number
1317 void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
1322 bn_wcheck_size(a, nwords);
1323 bn_wcheck_size(b, nwords);
1326 assert((condition & (condition - 1)) == 0);
1327 assert(sizeof(BN_ULONG) >= sizeof(int));
1329 condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
1331 t = (a->top ^ b->top) & condition;
1335 #define BN_CONSTTIME_SWAP(ind) \
1337 t = (a->d[ind] ^ b->d[ind]) & condition; \
1344 for (i = 10; i < nwords; i++)
1345 BN_CONSTTIME_SWAP(i);
1348 BN_CONSTTIME_SWAP(9); /* Fallthrough */
1350 BN_CONSTTIME_SWAP(8); /* Fallthrough */
1352 BN_CONSTTIME_SWAP(7); /* Fallthrough */
1354 BN_CONSTTIME_SWAP(6); /* Fallthrough */
1356 BN_CONSTTIME_SWAP(5); /* Fallthrough */
1358 BN_CONSTTIME_SWAP(4); /* Fallthrough */
1360 BN_CONSTTIME_SWAP(3); /* Fallthrough */
1362 BN_CONSTTIME_SWAP(2); /* Fallthrough */
1364 BN_CONSTTIME_SWAP(1); /* Fallthrough */
1366 BN_CONSTTIME_SWAP(0);
1368 #undef BN_CONSTTIME_SWAP