sl@0: /* crypto/bn/bn_gf2m.c */ sl@0: /* ==================================================================== sl@0: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * sl@0: * The Elliptic Curve Public-Key Crypto Library (ECC Code) included sl@0: * herein is developed by SUN MICROSYSTEMS, INC., and is contributed sl@0: * to the OpenSSL project. sl@0: * sl@0: * The ECC Code is licensed pursuant to the OpenSSL open source sl@0: * license provided below. sl@0: * sl@0: * In addition, Sun covenants to all licensees who provide a reciprocal sl@0: * covenant with respect to their own patents if any, not to sue under sl@0: * current and future patent claims necessarily infringed by the making, sl@0: * using, practicing, selling, offering for sale and/or otherwise sl@0: * disposing of the ECC Code as delivered hereunder (or portions thereof), sl@0: * provided that such covenant shall not apply: sl@0: * 1) for code that a licensee deletes from the ECC Code; sl@0: * 2) separates from the ECC Code; or sl@0: * 3) for infringements caused by: sl@0: * i) the modification of the ECC Code or sl@0: * ii) the combination of the ECC Code with other software or sl@0: * devices where such combination causes the infringement. sl@0: * sl@0: * The software is originally written by Sheueling Chang Shantz and sl@0: * Douglas Stebila of Sun Microsystems Laboratories. sl@0: * sl@0: */ sl@0: sl@0: /* NOTE: This file is licensed pursuant to the OpenSSL license below sl@0: * and may be modified; but after modifications, the above covenant sl@0: * may no longer apply! In such cases, the corresponding paragraph sl@0: * ["In addition, Sun covenants ... causes the infringement."] and sl@0: * this note can be edited out; but please keep the Sun copyright sl@0: * notice and attribution. */ sl@0: sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: sl@0: /* Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should fail. */ sl@0: #define MAX_ITERATIONS 50 sl@0: sl@0: static const BN_ULONG SQR_tb[16] = sl@0: { 0, 1, 4, 5, 16, 17, 20, 21, sl@0: 64, 65, 68, 69, 80, 81, 84, 85 }; sl@0: /* Platform-specific macros to accelerate squaring. */ sl@0: #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) sl@0: #define SQR1(w) \ sl@0: SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \ sl@0: SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \ sl@0: SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \ sl@0: SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF] sl@0: #define SQR0(w) \ sl@0: SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \ sl@0: SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \ sl@0: SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \ sl@0: SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF] sl@0: #endif sl@0: #ifdef THIRTY_TWO_BIT sl@0: #define SQR1(w) \ sl@0: SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \ sl@0: SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF] sl@0: #define SQR0(w) \ sl@0: SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \ sl@0: SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF] sl@0: #endif sl@0: #ifdef SIXTEEN_BIT sl@0: #define SQR1(w) \ sl@0: SQR_tb[(w) >> 12 & 0xF] << 8 | SQR_tb[(w) >> 8 & 0xF] sl@0: #define SQR0(w) \ sl@0: SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF] sl@0: #endif sl@0: #ifdef EIGHT_BIT sl@0: #define SQR1(w) \ sl@0: SQR_tb[(w) >> 4 & 0xF] sl@0: #define SQR0(w) \ sl@0: SQR_tb[(w) & 15] sl@0: #endif sl@0: sl@0: /* Product of two polynomials a, b each with degree < BN_BITS2 - 1, sl@0: * result is a polynomial r with degree < 2 * BN_BITS - 1 sl@0: * The caller MUST ensure that the variables have the right amount sl@0: * of space allocated. sl@0: */ sl@0: #ifdef EIGHT_BIT sl@0: static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) sl@0: { sl@0: register BN_ULONG h, l, s; sl@0: BN_ULONG tab[4], top1b = a >> 7; sl@0: register BN_ULONG a1, a2; sl@0: sl@0: a1 = a & (0x7F); a2 = a1 << 1; sl@0: sl@0: tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2; sl@0: sl@0: s = tab[b & 0x3]; l = s; sl@0: s = tab[b >> 2 & 0x3]; l ^= s << 2; h = s >> 6; sl@0: s = tab[b >> 4 & 0x3]; l ^= s << 4; h ^= s >> 4; sl@0: s = tab[b >> 6 ]; l ^= s << 6; h ^= s >> 2; sl@0: sl@0: /* compensate for the top bit of a */ sl@0: sl@0: if (top1b & 01) { l ^= b << 7; h ^= b >> 1; } sl@0: sl@0: *r1 = h; *r0 = l; sl@0: } sl@0: #endif sl@0: #ifdef SIXTEEN_BIT sl@0: static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) sl@0: { sl@0: register BN_ULONG h, l, s; sl@0: BN_ULONG tab[4], top1b = a >> 15; sl@0: register BN_ULONG a1, a2; sl@0: sl@0: a1 = a & (0x7FFF); a2 = a1 << 1; sl@0: sl@0: tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2; sl@0: sl@0: s = tab[b & 0x3]; l = s; sl@0: s = tab[b >> 2 & 0x3]; l ^= s << 2; h = s >> 14; sl@0: s = tab[b >> 4 & 0x3]; l ^= s << 4; h ^= s >> 12; sl@0: s = tab[b >> 6 & 0x3]; l ^= s << 6; h ^= s >> 10; sl@0: s = tab[b >> 8 & 0x3]; l ^= s << 8; h ^= s >> 8; sl@0: s = tab[b >>10 & 0x3]; l ^= s << 10; h ^= s >> 6; sl@0: s = tab[b >>12 & 0x3]; l ^= s << 12; h ^= s >> 4; sl@0: s = tab[b >>14 ]; l ^= s << 14; h ^= s >> 2; sl@0: sl@0: /* compensate for the top bit of a */ sl@0: sl@0: if (top1b & 01) { l ^= b << 15; h ^= b >> 1; } sl@0: sl@0: *r1 = h; *r0 = l; sl@0: } sl@0: #endif sl@0: #ifdef THIRTY_TWO_BIT sl@0: static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) sl@0: { sl@0: register BN_ULONG h, l, s; sl@0: BN_ULONG tab[8], top2b = a >> 30; sl@0: register BN_ULONG a1, a2, a4; sl@0: sl@0: a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1; sl@0: sl@0: tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2; sl@0: tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4; sl@0: sl@0: s = tab[b & 0x7]; l = s; sl@0: s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29; sl@0: s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26; sl@0: s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23; sl@0: s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20; sl@0: s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17; sl@0: s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14; sl@0: s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11; sl@0: s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8; sl@0: s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5; sl@0: s = tab[b >> 30 ]; l ^= s << 30; h ^= s >> 2; sl@0: sl@0: /* compensate for the top two bits of a */ sl@0: sl@0: if (top2b & 01) { l ^= b << 30; h ^= b >> 2; } sl@0: if (top2b & 02) { l ^= b << 31; h ^= b >> 1; } sl@0: sl@0: *r1 = h; *r0 = l; sl@0: } sl@0: #endif sl@0: #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) sl@0: static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b) sl@0: { sl@0: register BN_ULONG h, l, s; sl@0: BN_ULONG tab[16], top3b = a >> 61; sl@0: register BN_ULONG a1, a2, a4, a8; sl@0: sl@0: a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1; sl@0: sl@0: tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2; sl@0: tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4; sl@0: tab[ 8] = a8; tab[ 9] = a1^a8; tab[10] = a2^a8; tab[11] = a1^a2^a8; sl@0: tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8; sl@0: sl@0: s = tab[b & 0xF]; l = s; sl@0: s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60; sl@0: s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56; sl@0: s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52; sl@0: s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48; sl@0: s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44; sl@0: s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40; sl@0: s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36; sl@0: s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32; sl@0: s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28; sl@0: s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24; sl@0: s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20; sl@0: s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16; sl@0: s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12; sl@0: s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8; sl@0: s = tab[b >> 60 ]; l ^= s << 60; h ^= s >> 4; sl@0: sl@0: /* compensate for the top three bits of a */ sl@0: sl@0: if (top3b & 01) { l ^= b << 61; h ^= b >> 3; } sl@0: if (top3b & 02) { l ^= b << 62; h ^= b >> 2; } sl@0: if (top3b & 04) { l ^= b << 63; h ^= b >> 1; } sl@0: sl@0: *r1 = h; *r0 = l; sl@0: } sl@0: #endif sl@0: sl@0: /* Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1, sl@0: * result is a polynomial r with degree < 4 * BN_BITS2 - 1 sl@0: * The caller MUST ensure that the variables have the right amount sl@0: * of space allocated. sl@0: */ sl@0: static void bn_GF2m_mul_2x2(BN_ULONG *r, const BN_ULONG a1, const BN_ULONG a0, const BN_ULONG b1, const BN_ULONG b0) sl@0: { sl@0: BN_ULONG m1, m0; sl@0: /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */ sl@0: bn_GF2m_mul_1x1(r+3, r+2, a1, b1); sl@0: bn_GF2m_mul_1x1(r+1, r, a0, b0); sl@0: bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1); sl@0: /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */ sl@0: r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */ sl@0: r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */ sl@0: } sl@0: sl@0: sl@0: /* Add polynomials a and b and store result in r; r could be a or b, a and b sl@0: * could be equal; r is the bitwise XOR of a and b. sl@0: */ sl@0: EXPORT_C int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int i; sl@0: const BIGNUM *at, *bt; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: if (a->top < b->top) { at = b; bt = a; } sl@0: else { at = a; bt = b; } sl@0: sl@0: bn_wexpand(r, at->top); sl@0: sl@0: for (i = 0; i < bt->top; i++) sl@0: { sl@0: r->d[i] = at->d[i] ^ bt->d[i]; sl@0: } sl@0: for (; i < at->top; i++) sl@0: { sl@0: r->d[i] = at->d[i]; sl@0: } sl@0: sl@0: r->top = at->top; sl@0: bn_correct_top(r); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: /* Some functions allow for representation of the irreducible polynomials sl@0: * as an int[], say p. The irreducible f(t) is then of the form: sl@0: * t^p[0] + t^p[1] + ... + t^p[k] sl@0: * where m = p[0] > p[1] > ... > p[k] = 0. sl@0: */ sl@0: sl@0: sl@0: /* Performs modular reduction of a and store result in r. r could be a. */ sl@0: EXPORT_C int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[]) sl@0: { sl@0: int j, k; sl@0: int n, dN, d0, d1; sl@0: BN_ULONG zz, *z; sl@0: sl@0: bn_check_top(a); sl@0: sl@0: if (!p[0]) sl@0: { sl@0: /* reduction mod 1 => return 0 */ sl@0: BN_zero(r); sl@0: return 1; sl@0: } sl@0: sl@0: /* Since the algorithm does reduction in the r value, if a != r, copy sl@0: * the contents of a into r so we can do reduction in r. sl@0: */ sl@0: if (a != r) sl@0: { sl@0: if (!bn_wexpand(r, a->top)) return 0; sl@0: for (j = 0; j < a->top; j++) sl@0: { sl@0: r->d[j] = a->d[j]; sl@0: } sl@0: r->top = a->top; sl@0: } sl@0: z = r->d; sl@0: sl@0: /* start reduction */ sl@0: dN = p[0] / BN_BITS2; sl@0: for (j = r->top - 1; j > dN;) sl@0: { sl@0: zz = z[j]; sl@0: if (z[j] == 0) { j--; continue; } sl@0: z[j] = 0; sl@0: sl@0: for (k = 1; p[k] != 0; k++) sl@0: { sl@0: /* reducing component t^p[k] */ sl@0: n = p[0] - p[k]; sl@0: d0 = n % BN_BITS2; d1 = BN_BITS2 - d0; sl@0: n /= BN_BITS2; sl@0: z[j-n] ^= (zz>>d0); sl@0: if (d0) z[j-n-1] ^= (zz<> d0); sl@0: if (d0) z[j-n-1] ^= (zz << d1); sl@0: } sl@0: sl@0: /* final round of reduction */ sl@0: while (j == dN) sl@0: { sl@0: sl@0: d0 = p[0] % BN_BITS2; sl@0: zz = z[dN] >> d0; sl@0: if (zz == 0) break; sl@0: d1 = BN_BITS2 - d0; sl@0: sl@0: if (d0) z[dN] = (z[dN] << d1) >> d1; /* clear up the top d1 bits */ sl@0: z[0] ^= zz; /* reduction t^0 component */ sl@0: sl@0: for (k = 1; p[k] != 0; k++) sl@0: { sl@0: BN_ULONG tmp_ulong; sl@0: sl@0: /* reducing component t^p[k]*/ sl@0: n = p[k] / BN_BITS2; sl@0: d0 = p[k] % BN_BITS2; sl@0: d1 = BN_BITS2 - d0; sl@0: z[n] ^= (zz << d0); sl@0: tmp_ulong = zz >> d1; sl@0: if (d0 && tmp_ulong) sl@0: z[n+1] ^= tmp_ulong; sl@0: } sl@0: sl@0: sl@0: } sl@0: sl@0: bn_correct_top(r); sl@0: return 1; sl@0: } sl@0: sl@0: /* Performs modular reduction of a by p and store result in r. r could be a. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: bn_check_top(a); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_arr(r, a, arr); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: /* Compute the product of two polynomials a and b, reduce modulo p, and store sl@0: * the result in r. r could be a or b; a could be b. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: int zlen, i, j, k, ret = 0; sl@0: BIGNUM *s; sl@0: BN_ULONG x1, x0, y1, y0, zz[4]; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: if (a == b) sl@0: { sl@0: return BN_GF2m_mod_sqr_arr(r, a, p, ctx); sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: if ((s = BN_CTX_get(ctx)) == NULL) goto err; sl@0: sl@0: zlen = a->top + b->top + 4; sl@0: if (!bn_wexpand(s, zlen)) goto err; sl@0: s->top = zlen; sl@0: sl@0: for (i = 0; i < zlen; i++) s->d[i] = 0; sl@0: sl@0: for (j = 0; j < b->top; j += 2) sl@0: { sl@0: y0 = b->d[j]; sl@0: y1 = ((j+1) == b->top) ? 0 : b->d[j+1]; sl@0: for (i = 0; i < a->top; i += 2) sl@0: { sl@0: x0 = a->d[i]; sl@0: x1 = ((i+1) == a->top) ? 0 : a->d[i+1]; sl@0: bn_GF2m_mul_2x2(zz, x1, x0, y1, y0); sl@0: for (k = 0; k < 4; k++) s->d[i+j+k] ^= zz[k]; sl@0: } sl@0: } sl@0: sl@0: bn_correct_top(s); sl@0: if (BN_GF2m_mod_arr(r, s, p)) sl@0: ret = 1; sl@0: bn_check_top(r); sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Compute the product of two polynomials a and b, reduce modulo p, and store sl@0: * the result in r. r could be a or b; a could equal b. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_mul_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_mul_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_MUL,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: /* Square a, reduce the result mod p, and store it in a. r could be a. */ sl@0: EXPORT_C int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: int i, ret = 0; sl@0: BIGNUM *s; sl@0: sl@0: bn_check_top(a); sl@0: BN_CTX_start(ctx); sl@0: if ((s = BN_CTX_get(ctx)) == NULL) return 0; sl@0: if (!bn_wexpand(s, 2 * a->top)) goto err; sl@0: sl@0: for (i = a->top - 1; i >= 0; i--) sl@0: { sl@0: s->d[2*i+1] = SQR1(a->d[i]); sl@0: s->d[2*i ] = SQR0(a->d[i]); sl@0: } sl@0: sl@0: s->top = 2 * a->top; sl@0: bn_correct_top(s); sl@0: if (!BN_GF2m_mod_arr(r, s, p)) goto err; sl@0: bn_check_top(r); sl@0: ret = 1; sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Square a, reduce the result mod p, and store it in a. r could be a. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_sqr_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_sqr_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_SQR,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: /* Invert a, reduce modulo p, and store the result in r. r could be a. sl@0: * Uses Modified Almost Inverse Algorithm (Algorithm 10) from sl@0: * Hankerson, D., Hernandez, J.L., and Menezes, A. "Software Implementation sl@0: * of Elliptic Curve Cryptography Over Binary Fields". sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *b, *c, *u, *v, *tmp; sl@0: int ret = 0; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(p); sl@0: sl@0: BN_CTX_start(ctx); sl@0: sl@0: b = BN_CTX_get(ctx); sl@0: c = BN_CTX_get(ctx); sl@0: u = BN_CTX_get(ctx); sl@0: v = BN_CTX_get(ctx); sl@0: if (v == NULL) goto err; sl@0: sl@0: if (!BN_one(b)) goto err; sl@0: if (!BN_GF2m_mod(u, a, p)) goto err; sl@0: if (!BN_copy(v, p)) goto err; sl@0: sl@0: if (BN_is_zero(u)) goto err; sl@0: sl@0: while (1) sl@0: { sl@0: while (!BN_is_odd(u)) sl@0: { sl@0: if (!BN_rshift1(u, u)) goto err; sl@0: if (BN_is_odd(b)) sl@0: { sl@0: if (!BN_GF2m_add(b, b, p)) goto err; sl@0: } sl@0: if (!BN_rshift1(b, b)) goto err; sl@0: } sl@0: sl@0: if (BN_abs_is_word(u, 1)) break; sl@0: sl@0: if (BN_num_bits(u) < BN_num_bits(v)) sl@0: { sl@0: tmp = u; u = v; v = tmp; sl@0: tmp = b; b = c; c = tmp; sl@0: } sl@0: sl@0: if (!BN_GF2m_add(u, u, v)) goto err; sl@0: if (!BN_GF2m_add(b, b, c)) goto err; sl@0: } sl@0: sl@0: sl@0: if (!BN_copy(r, b)) goto err; sl@0: bn_check_top(r); sl@0: ret = 1; sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Invert xx, reduce modulo p, and store the result in r. r could be xx. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_inv implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_inv function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: BIGNUM *field; sl@0: int ret = 0; sl@0: sl@0: bn_check_top(xx); sl@0: BN_CTX_start(ctx); sl@0: if ((field = BN_CTX_get(ctx)) == NULL) goto err; sl@0: if (!BN_GF2m_arr2poly(p, field)) goto err; sl@0: sl@0: ret = BN_GF2m_mod_inv(r, xx, field, ctx); sl@0: bn_check_top(r); sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: #ifndef OPENSSL_SUN_GF2M_DIV sl@0: /* Divide y by x, reduce modulo p, and store the result in r. r could be x sl@0: * or y, x could equal y. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *xinv = NULL; sl@0: int ret = 0; sl@0: sl@0: bn_check_top(y); sl@0: bn_check_top(x); sl@0: bn_check_top(p); sl@0: sl@0: BN_CTX_start(ctx); sl@0: xinv = BN_CTX_get(ctx); sl@0: if (xinv == NULL) goto err; sl@0: sl@0: if (!BN_GF2m_mod_inv(xinv, x, p, ctx)) goto err; sl@0: if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx)) goto err; sl@0: bn_check_top(r); sl@0: ret = 1; sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: #else sl@0: /* Divide y by x, reduce modulo p, and store the result in r. r could be x sl@0: * or y, x could equal y. sl@0: * Uses algorithm Modular_Division_GF(2^m) from sl@0: * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to sl@0: * the Great Divide". sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: BIGNUM *a, *b, *u, *v; sl@0: int ret = 0; sl@0: sl@0: bn_check_top(y); sl@0: bn_check_top(x); sl@0: bn_check_top(p); sl@0: sl@0: BN_CTX_start(ctx); sl@0: sl@0: a = BN_CTX_get(ctx); sl@0: b = BN_CTX_get(ctx); sl@0: u = BN_CTX_get(ctx); sl@0: v = BN_CTX_get(ctx); sl@0: if (v == NULL) goto err; sl@0: sl@0: /* reduce x and y mod p */ sl@0: if (!BN_GF2m_mod(u, y, p)) goto err; sl@0: if (!BN_GF2m_mod(a, x, p)) goto err; sl@0: if (!BN_copy(b, p)) goto err; sl@0: sl@0: while (!BN_is_odd(a)) sl@0: { sl@0: if (!BN_rshift1(a, a)) goto err; sl@0: if (BN_is_odd(u)) if (!BN_GF2m_add(u, u, p)) goto err; sl@0: if (!BN_rshift1(u, u)) goto err; sl@0: } sl@0: sl@0: do sl@0: { sl@0: if (BN_GF2m_cmp(b, a) > 0) sl@0: { sl@0: if (!BN_GF2m_add(b, b, a)) goto err; sl@0: if (!BN_GF2m_add(v, v, u)) goto err; sl@0: do sl@0: { sl@0: if (!BN_rshift1(b, b)) goto err; sl@0: if (BN_is_odd(v)) if (!BN_GF2m_add(v, v, p)) goto err; sl@0: if (!BN_rshift1(v, v)) goto err; sl@0: } while (!BN_is_odd(b)); sl@0: } sl@0: else if (BN_abs_is_word(a, 1)) sl@0: break; sl@0: else sl@0: { sl@0: if (!BN_GF2m_add(a, a, b)) goto err; sl@0: if (!BN_GF2m_add(u, u, v)) goto err; sl@0: do sl@0: { sl@0: if (!BN_rshift1(a, a)) goto err; sl@0: if (BN_is_odd(u)) if (!BN_GF2m_add(u, u, p)) goto err; sl@0: if (!BN_rshift1(u, u)) goto err; sl@0: } while (!BN_is_odd(a)); sl@0: } sl@0: } while (1); sl@0: sl@0: if (!BN_copy(r, u)) goto err; sl@0: bn_check_top(r); sl@0: ret = 1; sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: #endif sl@0: sl@0: /* Divide yy by xx, reduce modulo p, and store the result in r. r could be xx sl@0: * or yy, xx could equal yy. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_div implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_div function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: BIGNUM *field; sl@0: int ret = 0; sl@0: sl@0: bn_check_top(yy); sl@0: bn_check_top(xx); sl@0: sl@0: BN_CTX_start(ctx); sl@0: if ((field = BN_CTX_get(ctx)) == NULL) goto err; sl@0: if (!BN_GF2m_arr2poly(p, field)) goto err; sl@0: sl@0: ret = BN_GF2m_mod_div(r, yy, xx, field, ctx); sl@0: bn_check_top(r); sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: /* Compute the bth power of a, reduce modulo p, and store sl@0: * the result in r. r could be a. sl@0: * Uses simple square-and-multiply algorithm A.5.1 from IEEE P1363. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: int ret = 0, i, n; sl@0: BIGNUM *u; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: if (BN_is_zero(b)) sl@0: return(BN_one(r)); sl@0: sl@0: if (BN_abs_is_word(b, 1)) sl@0: return (BN_copy(r, a) != NULL); sl@0: sl@0: BN_CTX_start(ctx); sl@0: if ((u = BN_CTX_get(ctx)) == NULL) goto err; sl@0: sl@0: if (!BN_GF2m_mod_arr(u, a, p)) goto err; sl@0: sl@0: n = BN_num_bits(b) - 1; sl@0: for (i = n - 1; i >= 0; i--) sl@0: { sl@0: if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx)) goto err; sl@0: if (BN_is_bit_set(b, i)) sl@0: { sl@0: if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx)) goto err; sl@0: } sl@0: } sl@0: if (!BN_copy(r, u)) goto err; sl@0: bn_check_top(r); sl@0: ret = 1; sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Compute the bth power of a, reduce modulo p, and store sl@0: * the result in r. r could be a. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_exp_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_exp_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_EXP,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: /* Compute the square root of a, reduce modulo p, and store sl@0: * the result in r. r could be a. sl@0: * Uses exponentiation as in algorithm A.4.1 from IEEE P1363. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: BIGNUM *u; sl@0: sl@0: bn_check_top(a); sl@0: sl@0: if (!p[0]) sl@0: { sl@0: /* reduction mod 1 => return 0 */ sl@0: BN_zero(r); sl@0: return 1; sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: if ((u = BN_CTX_get(ctx)) == NULL) goto err; sl@0: sl@0: if (!BN_set_bit(u, p[0] - 1)) goto err; sl@0: ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx); sl@0: bn_check_top(r); sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Compute the square root of a, reduce modulo p, and store sl@0: * the result in r. r could be a. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_sqrt_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_sqrt_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: bn_check_top(a); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_SQRT,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0. sl@0: * Uses algorithms A.4.7 and A.4.6 from IEEE P1363. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const unsigned int p[], BN_CTX *ctx) sl@0: { sl@0: int ret = 0, count = 0; sl@0: unsigned int j; sl@0: BIGNUM *a, *z, *rho, *w, *w2, *tmp; sl@0: sl@0: bn_check_top(a_); sl@0: sl@0: if (!p[0]) sl@0: { sl@0: /* reduction mod 1 => return 0 */ sl@0: BN_zero(r); sl@0: return 1; sl@0: } sl@0: sl@0: BN_CTX_start(ctx); sl@0: a = BN_CTX_get(ctx); sl@0: z = BN_CTX_get(ctx); sl@0: w = BN_CTX_get(ctx); sl@0: if (w == NULL) goto err; sl@0: sl@0: if (!BN_GF2m_mod_arr(a, a_, p)) goto err; sl@0: sl@0: if (BN_is_zero(a)) sl@0: { sl@0: BN_zero(r); sl@0: ret = 1; sl@0: goto err; sl@0: } sl@0: sl@0: if (p[0] & 0x1) /* m is odd */ sl@0: { sl@0: /* compute half-trace of a */ sl@0: if (!BN_copy(z, a)) goto err; sl@0: for (j = 1; j <= (p[0] - 1) / 2; j++) sl@0: { sl@0: if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; sl@0: if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; sl@0: if (!BN_GF2m_add(z, z, a)) goto err; sl@0: } sl@0: sl@0: } sl@0: else /* m is even */ sl@0: { sl@0: rho = BN_CTX_get(ctx); sl@0: w2 = BN_CTX_get(ctx); sl@0: tmp = BN_CTX_get(ctx); sl@0: if (tmp == NULL) goto err; sl@0: do sl@0: { sl@0: if (!BN_rand(rho, p[0], 0, 0)) goto err; sl@0: if (!BN_GF2m_mod_arr(rho, rho, p)) goto err; sl@0: BN_zero(z); sl@0: if (!BN_copy(w, rho)) goto err; sl@0: for (j = 1; j <= p[0] - 1; j++) sl@0: { sl@0: if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err; sl@0: if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx)) goto err; sl@0: if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx)) goto err; sl@0: if (!BN_GF2m_add(z, z, tmp)) goto err; sl@0: if (!BN_GF2m_add(w, w2, rho)) goto err; sl@0: } sl@0: count++; sl@0: } while (BN_is_zero(w) && (count < MAX_ITERATIONS)); sl@0: if (BN_is_zero(w)) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR,BN_R_TOO_MANY_ITERATIONS); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx)) goto err; sl@0: if (!BN_GF2m_add(w, z, w)) goto err; sl@0: if (BN_GF2m_cmp(w, a)) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION); sl@0: goto err; sl@0: } sl@0: sl@0: if (!BN_copy(r, z)) goto err; sl@0: bn_check_top(r); sl@0: sl@0: ret = 1; sl@0: sl@0: err: sl@0: BN_CTX_end(ctx); sl@0: return ret; sl@0: } sl@0: sl@0: /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0. sl@0: * sl@0: * This function calls down to the BN_GF2m_mod_solve_quad_arr implementation; this wrapper sl@0: * function is only provided for convenience; for best performance, use the sl@0: * BN_GF2m_mod_solve_quad_arr function. sl@0: */ sl@0: EXPORT_C int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) sl@0: { sl@0: int ret = 0; sl@0: const int max = BN_num_bits(p); sl@0: unsigned int *arr=NULL; sl@0: bn_check_top(a); sl@0: bn_check_top(p); sl@0: if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * sl@0: max)) == NULL) goto err; sl@0: ret = BN_GF2m_poly2arr(p, arr, max); sl@0: if (!ret || ret > max) sl@0: { sl@0: BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD,BN_R_INVALID_LENGTH); sl@0: goto err; sl@0: } sl@0: ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx); sl@0: bn_check_top(r); sl@0: err: sl@0: if (arr) OPENSSL_free(arr); sl@0: return ret; sl@0: } sl@0: sl@0: /* Convert the bit-string representation of a polynomial sl@0: * ( \sum_{i=0}^n a_i * x^i , where a_0 is *not* zero) into an array sl@0: * of integers corresponding to the bits with non-zero coefficient. sl@0: * Up to max elements of the array will be filled. Return value is total sl@0: * number of coefficients that would be extracted if array was large enough. sl@0: */ sl@0: EXPORT_C int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max) sl@0: { sl@0: int i, j, k = 0; sl@0: BN_ULONG mask; sl@0: sl@0: if (BN_is_zero(a) || !BN_is_bit_set(a, 0)) sl@0: /* a_0 == 0 => return error (the unsigned int array sl@0: * must be terminated by 0) sl@0: */ sl@0: return 0; sl@0: sl@0: for (i = a->top - 1; i >= 0; i--) sl@0: { sl@0: if (!a->d[i]) sl@0: /* skip word if a->d[i] == 0 */ sl@0: continue; sl@0: mask = BN_TBIT; sl@0: for (j = BN_BITS2 - 1; j >= 0; j--) sl@0: { sl@0: if (a->d[i] & mask) sl@0: { sl@0: if (k < max) p[k] = BN_BITS2 * i + j; sl@0: k++; sl@0: } sl@0: mask >>= 1; sl@0: } sl@0: } sl@0: sl@0: return k; sl@0: } sl@0: sl@0: /* Convert the coefficient array representation of a polynomial to a sl@0: * bit-string. The array must be terminated by 0. sl@0: */ sl@0: EXPORT_C int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a) sl@0: { sl@0: int i; sl@0: sl@0: bn_check_top(a); sl@0: BN_zero(a); sl@0: for (i = 0; p[i] != 0; i++) sl@0: { sl@0: if (BN_set_bit(a, p[i]) == 0) sl@0: return 0; sl@0: sl@0: } sl@0: BN_set_bit(a, 0); sl@0: bn_check_top(a); sl@0: sl@0: return 1; sl@0: } sl@0: