1*2139Sjp161948 /* crypto/bn/bn_gf2m.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4*2139Sjp161948 *
5*2139Sjp161948 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6*2139Sjp161948 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7*2139Sjp161948 * to the OpenSSL project.
8*2139Sjp161948 *
9*2139Sjp161948 * The ECC Code is licensed pursuant to the OpenSSL open source
10*2139Sjp161948 * license provided below.
11*2139Sjp161948 *
12*2139Sjp161948 * In addition, Sun covenants to all licensees who provide a reciprocal
13*2139Sjp161948 * covenant with respect to their own patents if any, not to sue under
14*2139Sjp161948 * current and future patent claims necessarily infringed by the making,
15*2139Sjp161948 * using, practicing, selling, offering for sale and/or otherwise
16*2139Sjp161948 * disposing of the ECC Code as delivered hereunder (or portions thereof),
17*2139Sjp161948 * provided that such covenant shall not apply:
18*2139Sjp161948 * 1) for code that a licensee deletes from the ECC Code;
19*2139Sjp161948 * 2) separates from the ECC Code; or
20*2139Sjp161948 * 3) for infringements caused by:
21*2139Sjp161948 * i) the modification of the ECC Code or
22*2139Sjp161948 * ii) the combination of the ECC Code with other software or
23*2139Sjp161948 * devices where such combination causes the infringement.
24*2139Sjp161948 *
25*2139Sjp161948 * The software is originally written by Sheueling Chang Shantz and
26*2139Sjp161948 * Douglas Stebila of Sun Microsystems Laboratories.
27*2139Sjp161948 *
28*2139Sjp161948 */
29*2139Sjp161948
30*2139Sjp161948 /* NOTE: This file is licensed pursuant to the OpenSSL license below
31*2139Sjp161948 * and may be modified; but after modifications, the above covenant
32*2139Sjp161948 * may no longer apply! In such cases, the corresponding paragraph
33*2139Sjp161948 * ["In addition, Sun covenants ... causes the infringement."] and
34*2139Sjp161948 * this note can be edited out; but please keep the Sun copyright
35*2139Sjp161948 * notice and attribution. */
36*2139Sjp161948
37*2139Sjp161948 /* ====================================================================
38*2139Sjp161948 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
39*2139Sjp161948 *
40*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
41*2139Sjp161948 * modification, are permitted provided that the following conditions
42*2139Sjp161948 * are met:
43*2139Sjp161948 *
44*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
45*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
46*2139Sjp161948 *
47*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
48*2139Sjp161948 * notice, this list of conditions and the following disclaimer in
49*2139Sjp161948 * the documentation and/or other materials provided with the
50*2139Sjp161948 * distribution.
51*2139Sjp161948 *
52*2139Sjp161948 * 3. All advertising materials mentioning features or use of this
53*2139Sjp161948 * software must display the following acknowledgment:
54*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
55*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
56*2139Sjp161948 *
57*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
58*2139Sjp161948 * endorse or promote products derived from this software without
59*2139Sjp161948 * prior written permission. For written permission, please contact
60*2139Sjp161948 * openssl-core@openssl.org.
61*2139Sjp161948 *
62*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
63*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
64*2139Sjp161948 * permission of the OpenSSL Project.
65*2139Sjp161948 *
66*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
67*2139Sjp161948 * acknowledgment:
68*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
69*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
70*2139Sjp161948 *
71*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
72*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
75*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
77*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
78*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
80*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
81*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
82*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
83*2139Sjp161948 * ====================================================================
84*2139Sjp161948 *
85*2139Sjp161948 * This product includes cryptographic software written by Eric Young
86*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
87*2139Sjp161948 * Hudson (tjh@cryptsoft.com).
88*2139Sjp161948 *
89*2139Sjp161948 */
90*2139Sjp161948
91*2139Sjp161948 #include <assert.h>
92*2139Sjp161948 #include <limits.h>
93*2139Sjp161948 #include <stdio.h>
94*2139Sjp161948 #include "cryptlib.h"
95*2139Sjp161948 #include "bn_lcl.h"
96*2139Sjp161948
97*2139Sjp161948 /* Maximum number of iterations before BN_GF2m_mod_solve_quad_arr should fail. */
98*2139Sjp161948 #define MAX_ITERATIONS 50
99*2139Sjp161948
100*2139Sjp161948 static const BN_ULONG SQR_tb[16] =
101*2139Sjp161948 { 0, 1, 4, 5, 16, 17, 20, 21,
102*2139Sjp161948 64, 65, 68, 69, 80, 81, 84, 85 };
103*2139Sjp161948 /* Platform-specific macros to accelerate squaring. */
104*2139Sjp161948 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
105*2139Sjp161948 #define SQR1(w) \
106*2139Sjp161948 SQR_tb[(w) >> 60 & 0xF] << 56 | SQR_tb[(w) >> 56 & 0xF] << 48 | \
107*2139Sjp161948 SQR_tb[(w) >> 52 & 0xF] << 40 | SQR_tb[(w) >> 48 & 0xF] << 32 | \
108*2139Sjp161948 SQR_tb[(w) >> 44 & 0xF] << 24 | SQR_tb[(w) >> 40 & 0xF] << 16 | \
109*2139Sjp161948 SQR_tb[(w) >> 36 & 0xF] << 8 | SQR_tb[(w) >> 32 & 0xF]
110*2139Sjp161948 #define SQR0(w) \
111*2139Sjp161948 SQR_tb[(w) >> 28 & 0xF] << 56 | SQR_tb[(w) >> 24 & 0xF] << 48 | \
112*2139Sjp161948 SQR_tb[(w) >> 20 & 0xF] << 40 | SQR_tb[(w) >> 16 & 0xF] << 32 | \
113*2139Sjp161948 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
114*2139Sjp161948 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
115*2139Sjp161948 #endif
116*2139Sjp161948 #ifdef THIRTY_TWO_BIT
117*2139Sjp161948 #define SQR1(w) \
118*2139Sjp161948 SQR_tb[(w) >> 28 & 0xF] << 24 | SQR_tb[(w) >> 24 & 0xF] << 16 | \
119*2139Sjp161948 SQR_tb[(w) >> 20 & 0xF] << 8 | SQR_tb[(w) >> 16 & 0xF]
120*2139Sjp161948 #define SQR0(w) \
121*2139Sjp161948 SQR_tb[(w) >> 12 & 0xF] << 24 | SQR_tb[(w) >> 8 & 0xF] << 16 | \
122*2139Sjp161948 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
123*2139Sjp161948 #endif
124*2139Sjp161948 #ifdef SIXTEEN_BIT
125*2139Sjp161948 #define SQR1(w) \
126*2139Sjp161948 SQR_tb[(w) >> 12 & 0xF] << 8 | SQR_tb[(w) >> 8 & 0xF]
127*2139Sjp161948 #define SQR0(w) \
128*2139Sjp161948 SQR_tb[(w) >> 4 & 0xF] << 8 | SQR_tb[(w) & 0xF]
129*2139Sjp161948 #endif
130*2139Sjp161948 #ifdef EIGHT_BIT
131*2139Sjp161948 #define SQR1(w) \
132*2139Sjp161948 SQR_tb[(w) >> 4 & 0xF]
133*2139Sjp161948 #define SQR0(w) \
134*2139Sjp161948 SQR_tb[(w) & 15]
135*2139Sjp161948 #endif
136*2139Sjp161948
137*2139Sjp161948 /* Product of two polynomials a, b each with degree < BN_BITS2 - 1,
138*2139Sjp161948 * result is a polynomial r with degree < 2 * BN_BITS - 1
139*2139Sjp161948 * The caller MUST ensure that the variables have the right amount
140*2139Sjp161948 * of space allocated.
141*2139Sjp161948 */
142*2139Sjp161948 #ifdef EIGHT_BIT
bn_GF2m_mul_1x1(BN_ULONG * r1,BN_ULONG * r0,const BN_ULONG a,const BN_ULONG b)143*2139Sjp161948 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b)
144*2139Sjp161948 {
145*2139Sjp161948 register BN_ULONG h, l, s;
146*2139Sjp161948 BN_ULONG tab[4], top1b = a >> 7;
147*2139Sjp161948 register BN_ULONG a1, a2;
148*2139Sjp161948
149*2139Sjp161948 a1 = a & (0x7F); a2 = a1 << 1;
150*2139Sjp161948
151*2139Sjp161948 tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2;
152*2139Sjp161948
153*2139Sjp161948 s = tab[b & 0x3]; l = s;
154*2139Sjp161948 s = tab[b >> 2 & 0x3]; l ^= s << 2; h = s >> 6;
155*2139Sjp161948 s = tab[b >> 4 & 0x3]; l ^= s << 4; h ^= s >> 4;
156*2139Sjp161948 s = tab[b >> 6 ]; l ^= s << 6; h ^= s >> 2;
157*2139Sjp161948
158*2139Sjp161948 /* compensate for the top bit of a */
159*2139Sjp161948
160*2139Sjp161948 if (top1b & 01) { l ^= b << 7; h ^= b >> 1; }
161*2139Sjp161948
162*2139Sjp161948 *r1 = h; *r0 = l;
163*2139Sjp161948 }
164*2139Sjp161948 #endif
165*2139Sjp161948 #ifdef SIXTEEN_BIT
bn_GF2m_mul_1x1(BN_ULONG * r1,BN_ULONG * r0,const BN_ULONG a,const BN_ULONG b)166*2139Sjp161948 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b)
167*2139Sjp161948 {
168*2139Sjp161948 register BN_ULONG h, l, s;
169*2139Sjp161948 BN_ULONG tab[4], top1b = a >> 15;
170*2139Sjp161948 register BN_ULONG a1, a2;
171*2139Sjp161948
172*2139Sjp161948 a1 = a & (0x7FFF); a2 = a1 << 1;
173*2139Sjp161948
174*2139Sjp161948 tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2;
175*2139Sjp161948
176*2139Sjp161948 s = tab[b & 0x3]; l = s;
177*2139Sjp161948 s = tab[b >> 2 & 0x3]; l ^= s << 2; h = s >> 14;
178*2139Sjp161948 s = tab[b >> 4 & 0x3]; l ^= s << 4; h ^= s >> 12;
179*2139Sjp161948 s = tab[b >> 6 & 0x3]; l ^= s << 6; h ^= s >> 10;
180*2139Sjp161948 s = tab[b >> 8 & 0x3]; l ^= s << 8; h ^= s >> 8;
181*2139Sjp161948 s = tab[b >>10 & 0x3]; l ^= s << 10; h ^= s >> 6;
182*2139Sjp161948 s = tab[b >>12 & 0x3]; l ^= s << 12; h ^= s >> 4;
183*2139Sjp161948 s = tab[b >>14 ]; l ^= s << 14; h ^= s >> 2;
184*2139Sjp161948
185*2139Sjp161948 /* compensate for the top bit of a */
186*2139Sjp161948
187*2139Sjp161948 if (top1b & 01) { l ^= b << 15; h ^= b >> 1; }
188*2139Sjp161948
189*2139Sjp161948 *r1 = h; *r0 = l;
190*2139Sjp161948 }
191*2139Sjp161948 #endif
192*2139Sjp161948 #ifdef THIRTY_TWO_BIT
bn_GF2m_mul_1x1(BN_ULONG * r1,BN_ULONG * r0,const BN_ULONG a,const BN_ULONG b)193*2139Sjp161948 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b)
194*2139Sjp161948 {
195*2139Sjp161948 register BN_ULONG h, l, s;
196*2139Sjp161948 BN_ULONG tab[8], top2b = a >> 30;
197*2139Sjp161948 register BN_ULONG a1, a2, a4;
198*2139Sjp161948
199*2139Sjp161948 a1 = a & (0x3FFFFFFF); a2 = a1 << 1; a4 = a2 << 1;
200*2139Sjp161948
201*2139Sjp161948 tab[0] = 0; tab[1] = a1; tab[2] = a2; tab[3] = a1^a2;
202*2139Sjp161948 tab[4] = a4; tab[5] = a1^a4; tab[6] = a2^a4; tab[7] = a1^a2^a4;
203*2139Sjp161948
204*2139Sjp161948 s = tab[b & 0x7]; l = s;
205*2139Sjp161948 s = tab[b >> 3 & 0x7]; l ^= s << 3; h = s >> 29;
206*2139Sjp161948 s = tab[b >> 6 & 0x7]; l ^= s << 6; h ^= s >> 26;
207*2139Sjp161948 s = tab[b >> 9 & 0x7]; l ^= s << 9; h ^= s >> 23;
208*2139Sjp161948 s = tab[b >> 12 & 0x7]; l ^= s << 12; h ^= s >> 20;
209*2139Sjp161948 s = tab[b >> 15 & 0x7]; l ^= s << 15; h ^= s >> 17;
210*2139Sjp161948 s = tab[b >> 18 & 0x7]; l ^= s << 18; h ^= s >> 14;
211*2139Sjp161948 s = tab[b >> 21 & 0x7]; l ^= s << 21; h ^= s >> 11;
212*2139Sjp161948 s = tab[b >> 24 & 0x7]; l ^= s << 24; h ^= s >> 8;
213*2139Sjp161948 s = tab[b >> 27 & 0x7]; l ^= s << 27; h ^= s >> 5;
214*2139Sjp161948 s = tab[b >> 30 ]; l ^= s << 30; h ^= s >> 2;
215*2139Sjp161948
216*2139Sjp161948 /* compensate for the top two bits of a */
217*2139Sjp161948
218*2139Sjp161948 if (top2b & 01) { l ^= b << 30; h ^= b >> 2; }
219*2139Sjp161948 if (top2b & 02) { l ^= b << 31; h ^= b >> 1; }
220*2139Sjp161948
221*2139Sjp161948 *r1 = h; *r0 = l;
222*2139Sjp161948 }
223*2139Sjp161948 #endif
224*2139Sjp161948 #if defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG)
bn_GF2m_mul_1x1(BN_ULONG * r1,BN_ULONG * r0,const BN_ULONG a,const BN_ULONG b)225*2139Sjp161948 static void bn_GF2m_mul_1x1(BN_ULONG *r1, BN_ULONG *r0, const BN_ULONG a, const BN_ULONG b)
226*2139Sjp161948 {
227*2139Sjp161948 register BN_ULONG h, l, s;
228*2139Sjp161948 BN_ULONG tab[16], top3b = a >> 61;
229*2139Sjp161948 register BN_ULONG a1, a2, a4, a8;
230*2139Sjp161948
231*2139Sjp161948 a1 = a & (0x1FFFFFFFFFFFFFFFULL); a2 = a1 << 1; a4 = a2 << 1; a8 = a4 << 1;
232*2139Sjp161948
233*2139Sjp161948 tab[ 0] = 0; tab[ 1] = a1; tab[ 2] = a2; tab[ 3] = a1^a2;
234*2139Sjp161948 tab[ 4] = a4; tab[ 5] = a1^a4; tab[ 6] = a2^a4; tab[ 7] = a1^a2^a4;
235*2139Sjp161948 tab[ 8] = a8; tab[ 9] = a1^a8; tab[10] = a2^a8; tab[11] = a1^a2^a8;
236*2139Sjp161948 tab[12] = a4^a8; tab[13] = a1^a4^a8; tab[14] = a2^a4^a8; tab[15] = a1^a2^a4^a8;
237*2139Sjp161948
238*2139Sjp161948 s = tab[b & 0xF]; l = s;
239*2139Sjp161948 s = tab[b >> 4 & 0xF]; l ^= s << 4; h = s >> 60;
240*2139Sjp161948 s = tab[b >> 8 & 0xF]; l ^= s << 8; h ^= s >> 56;
241*2139Sjp161948 s = tab[b >> 12 & 0xF]; l ^= s << 12; h ^= s >> 52;
242*2139Sjp161948 s = tab[b >> 16 & 0xF]; l ^= s << 16; h ^= s >> 48;
243*2139Sjp161948 s = tab[b >> 20 & 0xF]; l ^= s << 20; h ^= s >> 44;
244*2139Sjp161948 s = tab[b >> 24 & 0xF]; l ^= s << 24; h ^= s >> 40;
245*2139Sjp161948 s = tab[b >> 28 & 0xF]; l ^= s << 28; h ^= s >> 36;
246*2139Sjp161948 s = tab[b >> 32 & 0xF]; l ^= s << 32; h ^= s >> 32;
247*2139Sjp161948 s = tab[b >> 36 & 0xF]; l ^= s << 36; h ^= s >> 28;
248*2139Sjp161948 s = tab[b >> 40 & 0xF]; l ^= s << 40; h ^= s >> 24;
249*2139Sjp161948 s = tab[b >> 44 & 0xF]; l ^= s << 44; h ^= s >> 20;
250*2139Sjp161948 s = tab[b >> 48 & 0xF]; l ^= s << 48; h ^= s >> 16;
251*2139Sjp161948 s = tab[b >> 52 & 0xF]; l ^= s << 52; h ^= s >> 12;
252*2139Sjp161948 s = tab[b >> 56 & 0xF]; l ^= s << 56; h ^= s >> 8;
253*2139Sjp161948 s = tab[b >> 60 ]; l ^= s << 60; h ^= s >> 4;
254*2139Sjp161948
255*2139Sjp161948 /* compensate for the top three bits of a */
256*2139Sjp161948
257*2139Sjp161948 if (top3b & 01) { l ^= b << 61; h ^= b >> 3; }
258*2139Sjp161948 if (top3b & 02) { l ^= b << 62; h ^= b >> 2; }
259*2139Sjp161948 if (top3b & 04) { l ^= b << 63; h ^= b >> 1; }
260*2139Sjp161948
261*2139Sjp161948 *r1 = h; *r0 = l;
262*2139Sjp161948 }
263*2139Sjp161948 #endif
264*2139Sjp161948
265*2139Sjp161948 /* Product of two polynomials a, b each with degree < 2 * BN_BITS2 - 1,
266*2139Sjp161948 * result is a polynomial r with degree < 4 * BN_BITS2 - 1
267*2139Sjp161948 * The caller MUST ensure that the variables have the right amount
268*2139Sjp161948 * of space allocated.
269*2139Sjp161948 */
bn_GF2m_mul_2x2(BN_ULONG * r,const BN_ULONG a1,const BN_ULONG a0,const BN_ULONG b1,const BN_ULONG b0)270*2139Sjp161948 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)
271*2139Sjp161948 {
272*2139Sjp161948 BN_ULONG m1, m0;
273*2139Sjp161948 /* r[3] = h1, r[2] = h0; r[1] = l1; r[0] = l0 */
274*2139Sjp161948 bn_GF2m_mul_1x1(r+3, r+2, a1, b1);
275*2139Sjp161948 bn_GF2m_mul_1x1(r+1, r, a0, b0);
276*2139Sjp161948 bn_GF2m_mul_1x1(&m1, &m0, a0 ^ a1, b0 ^ b1);
277*2139Sjp161948 /* Correction on m1 ^= l1 ^ h1; m0 ^= l0 ^ h0; */
278*2139Sjp161948 r[2] ^= m1 ^ r[1] ^ r[3]; /* h0 ^= m1 ^ l1 ^ h1; */
279*2139Sjp161948 r[1] = r[3] ^ r[2] ^ r[0] ^ m1 ^ m0; /* l1 ^= l0 ^ h0 ^ m0; */
280*2139Sjp161948 }
281*2139Sjp161948
282*2139Sjp161948
283*2139Sjp161948 /* Add polynomials a and b and store result in r; r could be a or b, a and b
284*2139Sjp161948 * could be equal; r is the bitwise XOR of a and b.
285*2139Sjp161948 */
BN_GF2m_add(BIGNUM * r,const BIGNUM * a,const BIGNUM * b)286*2139Sjp161948 int BN_GF2m_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
287*2139Sjp161948 {
288*2139Sjp161948 int i;
289*2139Sjp161948 const BIGNUM *at, *bt;
290*2139Sjp161948
291*2139Sjp161948 bn_check_top(a);
292*2139Sjp161948 bn_check_top(b);
293*2139Sjp161948
294*2139Sjp161948 if (a->top < b->top) { at = b; bt = a; }
295*2139Sjp161948 else { at = a; bt = b; }
296*2139Sjp161948
297*2139Sjp161948 bn_wexpand(r, at->top);
298*2139Sjp161948
299*2139Sjp161948 for (i = 0; i < bt->top; i++)
300*2139Sjp161948 {
301*2139Sjp161948 r->d[i] = at->d[i] ^ bt->d[i];
302*2139Sjp161948 }
303*2139Sjp161948 for (; i < at->top; i++)
304*2139Sjp161948 {
305*2139Sjp161948 r->d[i] = at->d[i];
306*2139Sjp161948 }
307*2139Sjp161948
308*2139Sjp161948 r->top = at->top;
309*2139Sjp161948 bn_correct_top(r);
310*2139Sjp161948
311*2139Sjp161948 return 1;
312*2139Sjp161948 }
313*2139Sjp161948
314*2139Sjp161948
315*2139Sjp161948 /* Some functions allow for representation of the irreducible polynomials
316*2139Sjp161948 * as an int[], say p. The irreducible f(t) is then of the form:
317*2139Sjp161948 * t^p[0] + t^p[1] + ... + t^p[k]
318*2139Sjp161948 * where m = p[0] > p[1] > ... > p[k] = 0.
319*2139Sjp161948 */
320*2139Sjp161948
321*2139Sjp161948
322*2139Sjp161948 /* Performs modular reduction of a and store result in r. r could be a. */
BN_GF2m_mod_arr(BIGNUM * r,const BIGNUM * a,const unsigned int p[])323*2139Sjp161948 int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[])
324*2139Sjp161948 {
325*2139Sjp161948 int j, k;
326*2139Sjp161948 int n, dN, d0, d1;
327*2139Sjp161948 BN_ULONG zz, *z;
328*2139Sjp161948
329*2139Sjp161948 bn_check_top(a);
330*2139Sjp161948
331*2139Sjp161948 if (!p[0])
332*2139Sjp161948 {
333*2139Sjp161948 /* reduction mod 1 => return 0 */
334*2139Sjp161948 BN_zero(r);
335*2139Sjp161948 return 1;
336*2139Sjp161948 }
337*2139Sjp161948
338*2139Sjp161948 /* Since the algorithm does reduction in the r value, if a != r, copy
339*2139Sjp161948 * the contents of a into r so we can do reduction in r.
340*2139Sjp161948 */
341*2139Sjp161948 if (a != r)
342*2139Sjp161948 {
343*2139Sjp161948 if (!bn_wexpand(r, a->top)) return 0;
344*2139Sjp161948 for (j = 0; j < a->top; j++)
345*2139Sjp161948 {
346*2139Sjp161948 r->d[j] = a->d[j];
347*2139Sjp161948 }
348*2139Sjp161948 r->top = a->top;
349*2139Sjp161948 }
350*2139Sjp161948 z = r->d;
351*2139Sjp161948
352*2139Sjp161948 /* start reduction */
353*2139Sjp161948 dN = p[0] / BN_BITS2;
354*2139Sjp161948 for (j = r->top - 1; j > dN;)
355*2139Sjp161948 {
356*2139Sjp161948 zz = z[j];
357*2139Sjp161948 if (z[j] == 0) { j--; continue; }
358*2139Sjp161948 z[j] = 0;
359*2139Sjp161948
360*2139Sjp161948 for (k = 1; p[k] != 0; k++)
361*2139Sjp161948 {
362*2139Sjp161948 /* reducing component t^p[k] */
363*2139Sjp161948 n = p[0] - p[k];
364*2139Sjp161948 d0 = n % BN_BITS2; d1 = BN_BITS2 - d0;
365*2139Sjp161948 n /= BN_BITS2;
366*2139Sjp161948 z[j-n] ^= (zz>>d0);
367*2139Sjp161948 if (d0) z[j-n-1] ^= (zz<<d1);
368*2139Sjp161948 }
369*2139Sjp161948
370*2139Sjp161948 /* reducing component t^0 */
371*2139Sjp161948 n = dN;
372*2139Sjp161948 d0 = p[0] % BN_BITS2;
373*2139Sjp161948 d1 = BN_BITS2 - d0;
374*2139Sjp161948 z[j-n] ^= (zz >> d0);
375*2139Sjp161948 if (d0) z[j-n-1] ^= (zz << d1);
376*2139Sjp161948 }
377*2139Sjp161948
378*2139Sjp161948 /* final round of reduction */
379*2139Sjp161948 while (j == dN)
380*2139Sjp161948 {
381*2139Sjp161948
382*2139Sjp161948 d0 = p[0] % BN_BITS2;
383*2139Sjp161948 zz = z[dN] >> d0;
384*2139Sjp161948 if (zz == 0) break;
385*2139Sjp161948 d1 = BN_BITS2 - d0;
386*2139Sjp161948
387*2139Sjp161948 if (d0) z[dN] = (z[dN] << d1) >> d1; /* clear up the top d1 bits */
388*2139Sjp161948 z[0] ^= zz; /* reduction t^0 component */
389*2139Sjp161948
390*2139Sjp161948 for (k = 1; p[k] != 0; k++)
391*2139Sjp161948 {
392*2139Sjp161948 BN_ULONG tmp_ulong;
393*2139Sjp161948
394*2139Sjp161948 /* reducing component t^p[k]*/
395*2139Sjp161948 n = p[k] / BN_BITS2;
396*2139Sjp161948 d0 = p[k] % BN_BITS2;
397*2139Sjp161948 d1 = BN_BITS2 - d0;
398*2139Sjp161948 z[n] ^= (zz << d0);
399*2139Sjp161948 tmp_ulong = zz >> d1;
400*2139Sjp161948 if (d0 && tmp_ulong)
401*2139Sjp161948 z[n+1] ^= tmp_ulong;
402*2139Sjp161948 }
403*2139Sjp161948
404*2139Sjp161948
405*2139Sjp161948 }
406*2139Sjp161948
407*2139Sjp161948 bn_correct_top(r);
408*2139Sjp161948 return 1;
409*2139Sjp161948 }
410*2139Sjp161948
411*2139Sjp161948 /* Performs modular reduction of a by p and store result in r. r could be a.
412*2139Sjp161948 *
413*2139Sjp161948 * This function calls down to the BN_GF2m_mod_arr implementation; this wrapper
414*2139Sjp161948 * function is only provided for convenience; for best performance, use the
415*2139Sjp161948 * BN_GF2m_mod_arr function.
416*2139Sjp161948 */
BN_GF2m_mod(BIGNUM * r,const BIGNUM * a,const BIGNUM * p)417*2139Sjp161948 int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p)
418*2139Sjp161948 {
419*2139Sjp161948 int ret = 0;
420*2139Sjp161948 const int max = BN_num_bits(p);
421*2139Sjp161948 unsigned int *arr=NULL;
422*2139Sjp161948 bn_check_top(a);
423*2139Sjp161948 bn_check_top(p);
424*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
425*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
426*2139Sjp161948 if (!ret || ret > max)
427*2139Sjp161948 {
428*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD,BN_R_INVALID_LENGTH);
429*2139Sjp161948 goto err;
430*2139Sjp161948 }
431*2139Sjp161948 ret = BN_GF2m_mod_arr(r, a, arr);
432*2139Sjp161948 bn_check_top(r);
433*2139Sjp161948 err:
434*2139Sjp161948 if (arr) OPENSSL_free(arr);
435*2139Sjp161948 return ret;
436*2139Sjp161948 }
437*2139Sjp161948
438*2139Sjp161948
439*2139Sjp161948 /* Compute the product of two polynomials a and b, reduce modulo p, and store
440*2139Sjp161948 * the result in r. r could be a or b; a could be b.
441*2139Sjp161948 */
BN_GF2m_mod_mul_arr(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const unsigned int p[],BN_CTX * ctx)442*2139Sjp161948 int BN_GF2m_mod_mul_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const unsigned int p[], BN_CTX *ctx)
443*2139Sjp161948 {
444*2139Sjp161948 int zlen, i, j, k, ret = 0;
445*2139Sjp161948 BIGNUM *s;
446*2139Sjp161948 BN_ULONG x1, x0, y1, y0, zz[4];
447*2139Sjp161948
448*2139Sjp161948 bn_check_top(a);
449*2139Sjp161948 bn_check_top(b);
450*2139Sjp161948
451*2139Sjp161948 if (a == b)
452*2139Sjp161948 {
453*2139Sjp161948 return BN_GF2m_mod_sqr_arr(r, a, p, ctx);
454*2139Sjp161948 }
455*2139Sjp161948
456*2139Sjp161948 BN_CTX_start(ctx);
457*2139Sjp161948 if ((s = BN_CTX_get(ctx)) == NULL) goto err;
458*2139Sjp161948
459*2139Sjp161948 zlen = a->top + b->top + 4;
460*2139Sjp161948 if (!bn_wexpand(s, zlen)) goto err;
461*2139Sjp161948 s->top = zlen;
462*2139Sjp161948
463*2139Sjp161948 for (i = 0; i < zlen; i++) s->d[i] = 0;
464*2139Sjp161948
465*2139Sjp161948 for (j = 0; j < b->top; j += 2)
466*2139Sjp161948 {
467*2139Sjp161948 y0 = b->d[j];
468*2139Sjp161948 y1 = ((j+1) == b->top) ? 0 : b->d[j+1];
469*2139Sjp161948 for (i = 0; i < a->top; i += 2)
470*2139Sjp161948 {
471*2139Sjp161948 x0 = a->d[i];
472*2139Sjp161948 x1 = ((i+1) == a->top) ? 0 : a->d[i+1];
473*2139Sjp161948 bn_GF2m_mul_2x2(zz, x1, x0, y1, y0);
474*2139Sjp161948 for (k = 0; k < 4; k++) s->d[i+j+k] ^= zz[k];
475*2139Sjp161948 }
476*2139Sjp161948 }
477*2139Sjp161948
478*2139Sjp161948 bn_correct_top(s);
479*2139Sjp161948 if (BN_GF2m_mod_arr(r, s, p))
480*2139Sjp161948 ret = 1;
481*2139Sjp161948 bn_check_top(r);
482*2139Sjp161948
483*2139Sjp161948 err:
484*2139Sjp161948 BN_CTX_end(ctx);
485*2139Sjp161948 return ret;
486*2139Sjp161948 }
487*2139Sjp161948
488*2139Sjp161948 /* Compute the product of two polynomials a and b, reduce modulo p, and store
489*2139Sjp161948 * the result in r. r could be a or b; a could equal b.
490*2139Sjp161948 *
491*2139Sjp161948 * This function calls down to the BN_GF2m_mod_mul_arr implementation; this wrapper
492*2139Sjp161948 * function is only provided for convenience; for best performance, use the
493*2139Sjp161948 * BN_GF2m_mod_mul_arr function.
494*2139Sjp161948 */
BN_GF2m_mod_mul(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * p,BN_CTX * ctx)495*2139Sjp161948 int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx)
496*2139Sjp161948 {
497*2139Sjp161948 int ret = 0;
498*2139Sjp161948 const int max = BN_num_bits(p);
499*2139Sjp161948 unsigned int *arr=NULL;
500*2139Sjp161948 bn_check_top(a);
501*2139Sjp161948 bn_check_top(b);
502*2139Sjp161948 bn_check_top(p);
503*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
504*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
505*2139Sjp161948 if (!ret || ret > max)
506*2139Sjp161948 {
507*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_MUL,BN_R_INVALID_LENGTH);
508*2139Sjp161948 goto err;
509*2139Sjp161948 }
510*2139Sjp161948 ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx);
511*2139Sjp161948 bn_check_top(r);
512*2139Sjp161948 err:
513*2139Sjp161948 if (arr) OPENSSL_free(arr);
514*2139Sjp161948 return ret;
515*2139Sjp161948 }
516*2139Sjp161948
517*2139Sjp161948
518*2139Sjp161948 /* Square a, reduce the result mod p, and store it in a. r could be a. */
BN_GF2m_mod_sqr_arr(BIGNUM * r,const BIGNUM * a,const unsigned int p[],BN_CTX * ctx)519*2139Sjp161948 int BN_GF2m_mod_sqr_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], BN_CTX *ctx)
520*2139Sjp161948 {
521*2139Sjp161948 int i, ret = 0;
522*2139Sjp161948 BIGNUM *s;
523*2139Sjp161948
524*2139Sjp161948 bn_check_top(a);
525*2139Sjp161948 BN_CTX_start(ctx);
526*2139Sjp161948 if ((s = BN_CTX_get(ctx)) == NULL) return 0;
527*2139Sjp161948 if (!bn_wexpand(s, 2 * a->top)) goto err;
528*2139Sjp161948
529*2139Sjp161948 for (i = a->top - 1; i >= 0; i--)
530*2139Sjp161948 {
531*2139Sjp161948 s->d[2*i+1] = SQR1(a->d[i]);
532*2139Sjp161948 s->d[2*i ] = SQR0(a->d[i]);
533*2139Sjp161948 }
534*2139Sjp161948
535*2139Sjp161948 s->top = 2 * a->top;
536*2139Sjp161948 bn_correct_top(s);
537*2139Sjp161948 if (!BN_GF2m_mod_arr(r, s, p)) goto err;
538*2139Sjp161948 bn_check_top(r);
539*2139Sjp161948 ret = 1;
540*2139Sjp161948 err:
541*2139Sjp161948 BN_CTX_end(ctx);
542*2139Sjp161948 return ret;
543*2139Sjp161948 }
544*2139Sjp161948
545*2139Sjp161948 /* Square a, reduce the result mod p, and store it in a. r could be a.
546*2139Sjp161948 *
547*2139Sjp161948 * This function calls down to the BN_GF2m_mod_sqr_arr implementation; this wrapper
548*2139Sjp161948 * function is only provided for convenience; for best performance, use the
549*2139Sjp161948 * BN_GF2m_mod_sqr_arr function.
550*2139Sjp161948 */
BN_GF2m_mod_sqr(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx)551*2139Sjp161948 int BN_GF2m_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
552*2139Sjp161948 {
553*2139Sjp161948 int ret = 0;
554*2139Sjp161948 const int max = BN_num_bits(p);
555*2139Sjp161948 unsigned int *arr=NULL;
556*2139Sjp161948
557*2139Sjp161948 bn_check_top(a);
558*2139Sjp161948 bn_check_top(p);
559*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
560*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
561*2139Sjp161948 if (!ret || ret > max)
562*2139Sjp161948 {
563*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_SQR,BN_R_INVALID_LENGTH);
564*2139Sjp161948 goto err;
565*2139Sjp161948 }
566*2139Sjp161948 ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
567*2139Sjp161948 bn_check_top(r);
568*2139Sjp161948 err:
569*2139Sjp161948 if (arr) OPENSSL_free(arr);
570*2139Sjp161948 return ret;
571*2139Sjp161948 }
572*2139Sjp161948
573*2139Sjp161948
574*2139Sjp161948 /* Invert a, reduce modulo p, and store the result in r. r could be a.
575*2139Sjp161948 * Uses Modified Almost Inverse Algorithm (Algorithm 10) from
576*2139Sjp161948 * Hankerson, D., Hernandez, J.L., and Menezes, A. "Software Implementation
577*2139Sjp161948 * of Elliptic Curve Cryptography Over Binary Fields".
578*2139Sjp161948 */
BN_GF2m_mod_inv(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx)579*2139Sjp161948 int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
580*2139Sjp161948 {
581*2139Sjp161948 BIGNUM *b, *c, *u, *v, *tmp;
582*2139Sjp161948 int ret = 0;
583*2139Sjp161948
584*2139Sjp161948 bn_check_top(a);
585*2139Sjp161948 bn_check_top(p);
586*2139Sjp161948
587*2139Sjp161948 BN_CTX_start(ctx);
588*2139Sjp161948
589*2139Sjp161948 b = BN_CTX_get(ctx);
590*2139Sjp161948 c = BN_CTX_get(ctx);
591*2139Sjp161948 u = BN_CTX_get(ctx);
592*2139Sjp161948 v = BN_CTX_get(ctx);
593*2139Sjp161948 if (v == NULL) goto err;
594*2139Sjp161948
595*2139Sjp161948 if (!BN_one(b)) goto err;
596*2139Sjp161948 if (!BN_GF2m_mod(u, a, p)) goto err;
597*2139Sjp161948 if (!BN_copy(v, p)) goto err;
598*2139Sjp161948
599*2139Sjp161948 if (BN_is_zero(u)) goto err;
600*2139Sjp161948
601*2139Sjp161948 while (1)
602*2139Sjp161948 {
603*2139Sjp161948 while (!BN_is_odd(u))
604*2139Sjp161948 {
605*2139Sjp161948 if (!BN_rshift1(u, u)) goto err;
606*2139Sjp161948 if (BN_is_odd(b))
607*2139Sjp161948 {
608*2139Sjp161948 if (!BN_GF2m_add(b, b, p)) goto err;
609*2139Sjp161948 }
610*2139Sjp161948 if (!BN_rshift1(b, b)) goto err;
611*2139Sjp161948 }
612*2139Sjp161948
613*2139Sjp161948 if (BN_abs_is_word(u, 1)) break;
614*2139Sjp161948
615*2139Sjp161948 if (BN_num_bits(u) < BN_num_bits(v))
616*2139Sjp161948 {
617*2139Sjp161948 tmp = u; u = v; v = tmp;
618*2139Sjp161948 tmp = b; b = c; c = tmp;
619*2139Sjp161948 }
620*2139Sjp161948
621*2139Sjp161948 if (!BN_GF2m_add(u, u, v)) goto err;
622*2139Sjp161948 if (!BN_GF2m_add(b, b, c)) goto err;
623*2139Sjp161948 }
624*2139Sjp161948
625*2139Sjp161948
626*2139Sjp161948 if (!BN_copy(r, b)) goto err;
627*2139Sjp161948 bn_check_top(r);
628*2139Sjp161948 ret = 1;
629*2139Sjp161948
630*2139Sjp161948 err:
631*2139Sjp161948 BN_CTX_end(ctx);
632*2139Sjp161948 return ret;
633*2139Sjp161948 }
634*2139Sjp161948
635*2139Sjp161948 /* Invert xx, reduce modulo p, and store the result in r. r could be xx.
636*2139Sjp161948 *
637*2139Sjp161948 * This function calls down to the BN_GF2m_mod_inv implementation; this wrapper
638*2139Sjp161948 * function is only provided for convenience; for best performance, use the
639*2139Sjp161948 * BN_GF2m_mod_inv function.
640*2139Sjp161948 */
BN_GF2m_mod_inv_arr(BIGNUM * r,const BIGNUM * xx,const unsigned int p[],BN_CTX * ctx)641*2139Sjp161948 int BN_GF2m_mod_inv_arr(BIGNUM *r, const BIGNUM *xx, const unsigned int p[], BN_CTX *ctx)
642*2139Sjp161948 {
643*2139Sjp161948 BIGNUM *field;
644*2139Sjp161948 int ret = 0;
645*2139Sjp161948
646*2139Sjp161948 bn_check_top(xx);
647*2139Sjp161948 BN_CTX_start(ctx);
648*2139Sjp161948 if ((field = BN_CTX_get(ctx)) == NULL) goto err;
649*2139Sjp161948 if (!BN_GF2m_arr2poly(p, field)) goto err;
650*2139Sjp161948
651*2139Sjp161948 ret = BN_GF2m_mod_inv(r, xx, field, ctx);
652*2139Sjp161948 bn_check_top(r);
653*2139Sjp161948
654*2139Sjp161948 err:
655*2139Sjp161948 BN_CTX_end(ctx);
656*2139Sjp161948 return ret;
657*2139Sjp161948 }
658*2139Sjp161948
659*2139Sjp161948
660*2139Sjp161948 #ifndef OPENSSL_SUN_GF2M_DIV
661*2139Sjp161948 /* Divide y by x, reduce modulo p, and store the result in r. r could be x
662*2139Sjp161948 * or y, x could equal y.
663*2139Sjp161948 */
BN_GF2m_mod_div(BIGNUM * r,const BIGNUM * y,const BIGNUM * x,const BIGNUM * p,BN_CTX * ctx)664*2139Sjp161948 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, BN_CTX *ctx)
665*2139Sjp161948 {
666*2139Sjp161948 BIGNUM *xinv = NULL;
667*2139Sjp161948 int ret = 0;
668*2139Sjp161948
669*2139Sjp161948 bn_check_top(y);
670*2139Sjp161948 bn_check_top(x);
671*2139Sjp161948 bn_check_top(p);
672*2139Sjp161948
673*2139Sjp161948 BN_CTX_start(ctx);
674*2139Sjp161948 xinv = BN_CTX_get(ctx);
675*2139Sjp161948 if (xinv == NULL) goto err;
676*2139Sjp161948
677*2139Sjp161948 if (!BN_GF2m_mod_inv(xinv, x, p, ctx)) goto err;
678*2139Sjp161948 if (!BN_GF2m_mod_mul(r, y, xinv, p, ctx)) goto err;
679*2139Sjp161948 bn_check_top(r);
680*2139Sjp161948 ret = 1;
681*2139Sjp161948
682*2139Sjp161948 err:
683*2139Sjp161948 BN_CTX_end(ctx);
684*2139Sjp161948 return ret;
685*2139Sjp161948 }
686*2139Sjp161948 #else
687*2139Sjp161948 /* Divide y by x, reduce modulo p, and store the result in r. r could be x
688*2139Sjp161948 * or y, x could equal y.
689*2139Sjp161948 * Uses algorithm Modular_Division_GF(2^m) from
690*2139Sjp161948 * Chang-Shantz, S. "From Euclid's GCD to Montgomery Multiplication to
691*2139Sjp161948 * the Great Divide".
692*2139Sjp161948 */
BN_GF2m_mod_div(BIGNUM * r,const BIGNUM * y,const BIGNUM * x,const BIGNUM * p,BN_CTX * ctx)693*2139Sjp161948 int BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p, BN_CTX *ctx)
694*2139Sjp161948 {
695*2139Sjp161948 BIGNUM *a, *b, *u, *v;
696*2139Sjp161948 int ret = 0;
697*2139Sjp161948
698*2139Sjp161948 bn_check_top(y);
699*2139Sjp161948 bn_check_top(x);
700*2139Sjp161948 bn_check_top(p);
701*2139Sjp161948
702*2139Sjp161948 BN_CTX_start(ctx);
703*2139Sjp161948
704*2139Sjp161948 a = BN_CTX_get(ctx);
705*2139Sjp161948 b = BN_CTX_get(ctx);
706*2139Sjp161948 u = BN_CTX_get(ctx);
707*2139Sjp161948 v = BN_CTX_get(ctx);
708*2139Sjp161948 if (v == NULL) goto err;
709*2139Sjp161948
710*2139Sjp161948 /* reduce x and y mod p */
711*2139Sjp161948 if (!BN_GF2m_mod(u, y, p)) goto err;
712*2139Sjp161948 if (!BN_GF2m_mod(a, x, p)) goto err;
713*2139Sjp161948 if (!BN_copy(b, p)) goto err;
714*2139Sjp161948
715*2139Sjp161948 while (!BN_is_odd(a))
716*2139Sjp161948 {
717*2139Sjp161948 if (!BN_rshift1(a, a)) goto err;
718*2139Sjp161948 if (BN_is_odd(u)) if (!BN_GF2m_add(u, u, p)) goto err;
719*2139Sjp161948 if (!BN_rshift1(u, u)) goto err;
720*2139Sjp161948 }
721*2139Sjp161948
722*2139Sjp161948 do
723*2139Sjp161948 {
724*2139Sjp161948 if (BN_GF2m_cmp(b, a) > 0)
725*2139Sjp161948 {
726*2139Sjp161948 if (!BN_GF2m_add(b, b, a)) goto err;
727*2139Sjp161948 if (!BN_GF2m_add(v, v, u)) goto err;
728*2139Sjp161948 do
729*2139Sjp161948 {
730*2139Sjp161948 if (!BN_rshift1(b, b)) goto err;
731*2139Sjp161948 if (BN_is_odd(v)) if (!BN_GF2m_add(v, v, p)) goto err;
732*2139Sjp161948 if (!BN_rshift1(v, v)) goto err;
733*2139Sjp161948 } while (!BN_is_odd(b));
734*2139Sjp161948 }
735*2139Sjp161948 else if (BN_abs_is_word(a, 1))
736*2139Sjp161948 break;
737*2139Sjp161948 else
738*2139Sjp161948 {
739*2139Sjp161948 if (!BN_GF2m_add(a, a, b)) goto err;
740*2139Sjp161948 if (!BN_GF2m_add(u, u, v)) goto err;
741*2139Sjp161948 do
742*2139Sjp161948 {
743*2139Sjp161948 if (!BN_rshift1(a, a)) goto err;
744*2139Sjp161948 if (BN_is_odd(u)) if (!BN_GF2m_add(u, u, p)) goto err;
745*2139Sjp161948 if (!BN_rshift1(u, u)) goto err;
746*2139Sjp161948 } while (!BN_is_odd(a));
747*2139Sjp161948 }
748*2139Sjp161948 } while (1);
749*2139Sjp161948
750*2139Sjp161948 if (!BN_copy(r, u)) goto err;
751*2139Sjp161948 bn_check_top(r);
752*2139Sjp161948 ret = 1;
753*2139Sjp161948
754*2139Sjp161948 err:
755*2139Sjp161948 BN_CTX_end(ctx);
756*2139Sjp161948 return ret;
757*2139Sjp161948 }
758*2139Sjp161948 #endif
759*2139Sjp161948
760*2139Sjp161948 /* Divide yy by xx, reduce modulo p, and store the result in r. r could be xx
761*2139Sjp161948 * or yy, xx could equal yy.
762*2139Sjp161948 *
763*2139Sjp161948 * This function calls down to the BN_GF2m_mod_div implementation; this wrapper
764*2139Sjp161948 * function is only provided for convenience; for best performance, use the
765*2139Sjp161948 * BN_GF2m_mod_div function.
766*2139Sjp161948 */
BN_GF2m_mod_div_arr(BIGNUM * r,const BIGNUM * yy,const BIGNUM * xx,const unsigned int p[],BN_CTX * ctx)767*2139Sjp161948 int BN_GF2m_mod_div_arr(BIGNUM *r, const BIGNUM *yy, const BIGNUM *xx, const unsigned int p[], BN_CTX *ctx)
768*2139Sjp161948 {
769*2139Sjp161948 BIGNUM *field;
770*2139Sjp161948 int ret = 0;
771*2139Sjp161948
772*2139Sjp161948 bn_check_top(yy);
773*2139Sjp161948 bn_check_top(xx);
774*2139Sjp161948
775*2139Sjp161948 BN_CTX_start(ctx);
776*2139Sjp161948 if ((field = BN_CTX_get(ctx)) == NULL) goto err;
777*2139Sjp161948 if (!BN_GF2m_arr2poly(p, field)) goto err;
778*2139Sjp161948
779*2139Sjp161948 ret = BN_GF2m_mod_div(r, yy, xx, field, ctx);
780*2139Sjp161948 bn_check_top(r);
781*2139Sjp161948
782*2139Sjp161948 err:
783*2139Sjp161948 BN_CTX_end(ctx);
784*2139Sjp161948 return ret;
785*2139Sjp161948 }
786*2139Sjp161948
787*2139Sjp161948
788*2139Sjp161948 /* Compute the bth power of a, reduce modulo p, and store
789*2139Sjp161948 * the result in r. r could be a.
790*2139Sjp161948 * Uses simple square-and-multiply algorithm A.5.1 from IEEE P1363.
791*2139Sjp161948 */
BN_GF2m_mod_exp_arr(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const unsigned int p[],BN_CTX * ctx)792*2139Sjp161948 int BN_GF2m_mod_exp_arr(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const unsigned int p[], BN_CTX *ctx)
793*2139Sjp161948 {
794*2139Sjp161948 int ret = 0, i, n;
795*2139Sjp161948 BIGNUM *u;
796*2139Sjp161948
797*2139Sjp161948 bn_check_top(a);
798*2139Sjp161948 bn_check_top(b);
799*2139Sjp161948
800*2139Sjp161948 if (BN_is_zero(b))
801*2139Sjp161948 return(BN_one(r));
802*2139Sjp161948
803*2139Sjp161948 if (BN_abs_is_word(b, 1))
804*2139Sjp161948 return (BN_copy(r, a) != NULL);
805*2139Sjp161948
806*2139Sjp161948 BN_CTX_start(ctx);
807*2139Sjp161948 if ((u = BN_CTX_get(ctx)) == NULL) goto err;
808*2139Sjp161948
809*2139Sjp161948 if (!BN_GF2m_mod_arr(u, a, p)) goto err;
810*2139Sjp161948
811*2139Sjp161948 n = BN_num_bits(b) - 1;
812*2139Sjp161948 for (i = n - 1; i >= 0; i--)
813*2139Sjp161948 {
814*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(u, u, p, ctx)) goto err;
815*2139Sjp161948 if (BN_is_bit_set(b, i))
816*2139Sjp161948 {
817*2139Sjp161948 if (!BN_GF2m_mod_mul_arr(u, u, a, p, ctx)) goto err;
818*2139Sjp161948 }
819*2139Sjp161948 }
820*2139Sjp161948 if (!BN_copy(r, u)) goto err;
821*2139Sjp161948 bn_check_top(r);
822*2139Sjp161948 ret = 1;
823*2139Sjp161948 err:
824*2139Sjp161948 BN_CTX_end(ctx);
825*2139Sjp161948 return ret;
826*2139Sjp161948 }
827*2139Sjp161948
828*2139Sjp161948 /* Compute the bth power of a, reduce modulo p, and store
829*2139Sjp161948 * the result in r. r could be a.
830*2139Sjp161948 *
831*2139Sjp161948 * This function calls down to the BN_GF2m_mod_exp_arr implementation; this wrapper
832*2139Sjp161948 * function is only provided for convenience; for best performance, use the
833*2139Sjp161948 * BN_GF2m_mod_exp_arr function.
834*2139Sjp161948 */
BN_GF2m_mod_exp(BIGNUM * r,const BIGNUM * a,const BIGNUM * b,const BIGNUM * p,BN_CTX * ctx)835*2139Sjp161948 int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx)
836*2139Sjp161948 {
837*2139Sjp161948 int ret = 0;
838*2139Sjp161948 const int max = BN_num_bits(p);
839*2139Sjp161948 unsigned int *arr=NULL;
840*2139Sjp161948 bn_check_top(a);
841*2139Sjp161948 bn_check_top(b);
842*2139Sjp161948 bn_check_top(p);
843*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
844*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
845*2139Sjp161948 if (!ret || ret > max)
846*2139Sjp161948 {
847*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_EXP,BN_R_INVALID_LENGTH);
848*2139Sjp161948 goto err;
849*2139Sjp161948 }
850*2139Sjp161948 ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx);
851*2139Sjp161948 bn_check_top(r);
852*2139Sjp161948 err:
853*2139Sjp161948 if (arr) OPENSSL_free(arr);
854*2139Sjp161948 return ret;
855*2139Sjp161948 }
856*2139Sjp161948
857*2139Sjp161948 /* Compute the square root of a, reduce modulo p, and store
858*2139Sjp161948 * the result in r. r could be a.
859*2139Sjp161948 * Uses exponentiation as in algorithm A.4.1 from IEEE P1363.
860*2139Sjp161948 */
BN_GF2m_mod_sqrt_arr(BIGNUM * r,const BIGNUM * a,const unsigned int p[],BN_CTX * ctx)861*2139Sjp161948 int BN_GF2m_mod_sqrt_arr(BIGNUM *r, const BIGNUM *a, const unsigned int p[], BN_CTX *ctx)
862*2139Sjp161948 {
863*2139Sjp161948 int ret = 0;
864*2139Sjp161948 BIGNUM *u;
865*2139Sjp161948
866*2139Sjp161948 bn_check_top(a);
867*2139Sjp161948
868*2139Sjp161948 if (!p[0])
869*2139Sjp161948 {
870*2139Sjp161948 /* reduction mod 1 => return 0 */
871*2139Sjp161948 BN_zero(r);
872*2139Sjp161948 return 1;
873*2139Sjp161948 }
874*2139Sjp161948
875*2139Sjp161948 BN_CTX_start(ctx);
876*2139Sjp161948 if ((u = BN_CTX_get(ctx)) == NULL) goto err;
877*2139Sjp161948
878*2139Sjp161948 if (!BN_set_bit(u, p[0] - 1)) goto err;
879*2139Sjp161948 ret = BN_GF2m_mod_exp_arr(r, a, u, p, ctx);
880*2139Sjp161948 bn_check_top(r);
881*2139Sjp161948
882*2139Sjp161948 err:
883*2139Sjp161948 BN_CTX_end(ctx);
884*2139Sjp161948 return ret;
885*2139Sjp161948 }
886*2139Sjp161948
887*2139Sjp161948 /* Compute the square root of a, reduce modulo p, and store
888*2139Sjp161948 * the result in r. r could be a.
889*2139Sjp161948 *
890*2139Sjp161948 * This function calls down to the BN_GF2m_mod_sqrt_arr implementation; this wrapper
891*2139Sjp161948 * function is only provided for convenience; for best performance, use the
892*2139Sjp161948 * BN_GF2m_mod_sqrt_arr function.
893*2139Sjp161948 */
BN_GF2m_mod_sqrt(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx)894*2139Sjp161948 int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
895*2139Sjp161948 {
896*2139Sjp161948 int ret = 0;
897*2139Sjp161948 const int max = BN_num_bits(p);
898*2139Sjp161948 unsigned int *arr=NULL;
899*2139Sjp161948 bn_check_top(a);
900*2139Sjp161948 bn_check_top(p);
901*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) * max)) == NULL) goto err;
902*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
903*2139Sjp161948 if (!ret || ret > max)
904*2139Sjp161948 {
905*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_SQRT,BN_R_INVALID_LENGTH);
906*2139Sjp161948 goto err;
907*2139Sjp161948 }
908*2139Sjp161948 ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx);
909*2139Sjp161948 bn_check_top(r);
910*2139Sjp161948 err:
911*2139Sjp161948 if (arr) OPENSSL_free(arr);
912*2139Sjp161948 return ret;
913*2139Sjp161948 }
914*2139Sjp161948
915*2139Sjp161948 /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0.
916*2139Sjp161948 * Uses algorithms A.4.7 and A.4.6 from IEEE P1363.
917*2139Sjp161948 */
BN_GF2m_mod_solve_quad_arr(BIGNUM * r,const BIGNUM * a_,const unsigned int p[],BN_CTX * ctx)918*2139Sjp161948 int BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const unsigned int p[], BN_CTX *ctx)
919*2139Sjp161948 {
920*2139Sjp161948 int ret = 0, count = 0;
921*2139Sjp161948 unsigned int j;
922*2139Sjp161948 BIGNUM *a, *z, *rho, *w, *w2, *tmp;
923*2139Sjp161948
924*2139Sjp161948 bn_check_top(a_);
925*2139Sjp161948
926*2139Sjp161948 if (!p[0])
927*2139Sjp161948 {
928*2139Sjp161948 /* reduction mod 1 => return 0 */
929*2139Sjp161948 BN_zero(r);
930*2139Sjp161948 return 1;
931*2139Sjp161948 }
932*2139Sjp161948
933*2139Sjp161948 BN_CTX_start(ctx);
934*2139Sjp161948 a = BN_CTX_get(ctx);
935*2139Sjp161948 z = BN_CTX_get(ctx);
936*2139Sjp161948 w = BN_CTX_get(ctx);
937*2139Sjp161948 if (w == NULL) goto err;
938*2139Sjp161948
939*2139Sjp161948 if (!BN_GF2m_mod_arr(a, a_, p)) goto err;
940*2139Sjp161948
941*2139Sjp161948 if (BN_is_zero(a))
942*2139Sjp161948 {
943*2139Sjp161948 BN_zero(r);
944*2139Sjp161948 ret = 1;
945*2139Sjp161948 goto err;
946*2139Sjp161948 }
947*2139Sjp161948
948*2139Sjp161948 if (p[0] & 0x1) /* m is odd */
949*2139Sjp161948 {
950*2139Sjp161948 /* compute half-trace of a */
951*2139Sjp161948 if (!BN_copy(z, a)) goto err;
952*2139Sjp161948 for (j = 1; j <= (p[0] - 1) / 2; j++)
953*2139Sjp161948 {
954*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err;
955*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err;
956*2139Sjp161948 if (!BN_GF2m_add(z, z, a)) goto err;
957*2139Sjp161948 }
958*2139Sjp161948
959*2139Sjp161948 }
960*2139Sjp161948 else /* m is even */
961*2139Sjp161948 {
962*2139Sjp161948 rho = BN_CTX_get(ctx);
963*2139Sjp161948 w2 = BN_CTX_get(ctx);
964*2139Sjp161948 tmp = BN_CTX_get(ctx);
965*2139Sjp161948 if (tmp == NULL) goto err;
966*2139Sjp161948 do
967*2139Sjp161948 {
968*2139Sjp161948 if (!BN_rand(rho, p[0], 0, 0)) goto err;
969*2139Sjp161948 if (!BN_GF2m_mod_arr(rho, rho, p)) goto err;
970*2139Sjp161948 BN_zero(z);
971*2139Sjp161948 if (!BN_copy(w, rho)) goto err;
972*2139Sjp161948 for (j = 1; j <= p[0] - 1; j++)
973*2139Sjp161948 {
974*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(z, z, p, ctx)) goto err;
975*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(w2, w, p, ctx)) goto err;
976*2139Sjp161948 if (!BN_GF2m_mod_mul_arr(tmp, w2, a, p, ctx)) goto err;
977*2139Sjp161948 if (!BN_GF2m_add(z, z, tmp)) goto err;
978*2139Sjp161948 if (!BN_GF2m_add(w, w2, rho)) goto err;
979*2139Sjp161948 }
980*2139Sjp161948 count++;
981*2139Sjp161948 } while (BN_is_zero(w) && (count < MAX_ITERATIONS));
982*2139Sjp161948 if (BN_is_zero(w))
983*2139Sjp161948 {
984*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR,BN_R_TOO_MANY_ITERATIONS);
985*2139Sjp161948 goto err;
986*2139Sjp161948 }
987*2139Sjp161948 }
988*2139Sjp161948
989*2139Sjp161948 if (!BN_GF2m_mod_sqr_arr(w, z, p, ctx)) goto err;
990*2139Sjp161948 if (!BN_GF2m_add(w, z, w)) goto err;
991*2139Sjp161948 if (BN_GF2m_cmp(w, a))
992*2139Sjp161948 {
993*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD_ARR, BN_R_NO_SOLUTION);
994*2139Sjp161948 goto err;
995*2139Sjp161948 }
996*2139Sjp161948
997*2139Sjp161948 if (!BN_copy(r, z)) goto err;
998*2139Sjp161948 bn_check_top(r);
999*2139Sjp161948
1000*2139Sjp161948 ret = 1;
1001*2139Sjp161948
1002*2139Sjp161948 err:
1003*2139Sjp161948 BN_CTX_end(ctx);
1004*2139Sjp161948 return ret;
1005*2139Sjp161948 }
1006*2139Sjp161948
1007*2139Sjp161948 /* Find r such that r^2 + r = a mod p. r could be a. If no r exists returns 0.
1008*2139Sjp161948 *
1009*2139Sjp161948 * This function calls down to the BN_GF2m_mod_solve_quad_arr implementation; this wrapper
1010*2139Sjp161948 * function is only provided for convenience; for best performance, use the
1011*2139Sjp161948 * BN_GF2m_mod_solve_quad_arr function.
1012*2139Sjp161948 */
BN_GF2m_mod_solve_quad(BIGNUM * r,const BIGNUM * a,const BIGNUM * p,BN_CTX * ctx)1013*2139Sjp161948 int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
1014*2139Sjp161948 {
1015*2139Sjp161948 int ret = 0;
1016*2139Sjp161948 const int max = BN_num_bits(p);
1017*2139Sjp161948 unsigned int *arr=NULL;
1018*2139Sjp161948 bn_check_top(a);
1019*2139Sjp161948 bn_check_top(p);
1020*2139Sjp161948 if ((arr = (unsigned int *)OPENSSL_malloc(sizeof(unsigned int) *
1021*2139Sjp161948 max)) == NULL) goto err;
1022*2139Sjp161948 ret = BN_GF2m_poly2arr(p, arr, max);
1023*2139Sjp161948 if (!ret || ret > max)
1024*2139Sjp161948 {
1025*2139Sjp161948 BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD,BN_R_INVALID_LENGTH);
1026*2139Sjp161948 goto err;
1027*2139Sjp161948 }
1028*2139Sjp161948 ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx);
1029*2139Sjp161948 bn_check_top(r);
1030*2139Sjp161948 err:
1031*2139Sjp161948 if (arr) OPENSSL_free(arr);
1032*2139Sjp161948 return ret;
1033*2139Sjp161948 }
1034*2139Sjp161948
1035*2139Sjp161948 /* Convert the bit-string representation of a polynomial
1036*2139Sjp161948 * ( \sum_{i=0}^n a_i * x^i , where a_0 is *not* zero) into an array
1037*2139Sjp161948 * of integers corresponding to the bits with non-zero coefficient.
1038*2139Sjp161948 * Up to max elements of the array will be filled. Return value is total
1039*2139Sjp161948 * number of coefficients that would be extracted if array was large enough.
1040*2139Sjp161948 */
BN_GF2m_poly2arr(const BIGNUM * a,unsigned int p[],int max)1041*2139Sjp161948 int BN_GF2m_poly2arr(const BIGNUM *a, unsigned int p[], int max)
1042*2139Sjp161948 {
1043*2139Sjp161948 int i, j, k = 0;
1044*2139Sjp161948 BN_ULONG mask;
1045*2139Sjp161948
1046*2139Sjp161948 if (BN_is_zero(a) || !BN_is_bit_set(a, 0))
1047*2139Sjp161948 /* a_0 == 0 => return error (the unsigned int array
1048*2139Sjp161948 * must be terminated by 0)
1049*2139Sjp161948 */
1050*2139Sjp161948 return 0;
1051*2139Sjp161948
1052*2139Sjp161948 for (i = a->top - 1; i >= 0; i--)
1053*2139Sjp161948 {
1054*2139Sjp161948 if (!a->d[i])
1055*2139Sjp161948 /* skip word if a->d[i] == 0 */
1056*2139Sjp161948 continue;
1057*2139Sjp161948 mask = BN_TBIT;
1058*2139Sjp161948 for (j = BN_BITS2 - 1; j >= 0; j--)
1059*2139Sjp161948 {
1060*2139Sjp161948 if (a->d[i] & mask)
1061*2139Sjp161948 {
1062*2139Sjp161948 if (k < max) p[k] = BN_BITS2 * i + j;
1063*2139Sjp161948 k++;
1064*2139Sjp161948 }
1065*2139Sjp161948 mask >>= 1;
1066*2139Sjp161948 }
1067*2139Sjp161948 }
1068*2139Sjp161948
1069*2139Sjp161948 return k;
1070*2139Sjp161948 }
1071*2139Sjp161948
1072*2139Sjp161948 /* Convert the coefficient array representation of a polynomial to a
1073*2139Sjp161948 * bit-string. The array must be terminated by 0.
1074*2139Sjp161948 */
BN_GF2m_arr2poly(const unsigned int p[],BIGNUM * a)1075*2139Sjp161948 int BN_GF2m_arr2poly(const unsigned int p[], BIGNUM *a)
1076*2139Sjp161948 {
1077*2139Sjp161948 int i;
1078*2139Sjp161948
1079*2139Sjp161948 bn_check_top(a);
1080*2139Sjp161948 BN_zero(a);
1081*2139Sjp161948 for (i = 0; p[i] != 0; i++)
1082*2139Sjp161948 {
1083*2139Sjp161948 BN_set_bit(a, p[i]);
1084*2139Sjp161948 }
1085*2139Sjp161948 BN_set_bit(a, 0);
1086*2139Sjp161948 bn_check_top(a);
1087*2139Sjp161948
1088*2139Sjp161948 return 1;
1089*2139Sjp161948 }
1090*2139Sjp161948
1091