xref: /onnv-gate/usr/src/common/crypto/ecc/ec2_233.c (revision 5697:324be5104707)
1*5697Smcpowers /*
2*5697Smcpowers  * ***** BEGIN LICENSE BLOCK *****
3*5697Smcpowers  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4*5697Smcpowers  *
5*5697Smcpowers  * The contents of this file are subject to the Mozilla Public License Version
6*5697Smcpowers  * 1.1 (the "License"); you may not use this file except in compliance with
7*5697Smcpowers  * the License. You may obtain a copy of the License at
8*5697Smcpowers  * http://www.mozilla.org/MPL/
9*5697Smcpowers  *
10*5697Smcpowers  * Software distributed under the License is distributed on an "AS IS" basis,
11*5697Smcpowers  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12*5697Smcpowers  * for the specific language governing rights and limitations under the
13*5697Smcpowers  * License.
14*5697Smcpowers  *
15*5697Smcpowers  * The Original Code is the elliptic curve math library for binary polynomial field curves.
16*5697Smcpowers  *
17*5697Smcpowers  * The Initial Developer of the Original Code is
18*5697Smcpowers  * Sun Microsystems, Inc.
19*5697Smcpowers  * Portions created by the Initial Developer are Copyright (C) 2003
20*5697Smcpowers  * the Initial Developer. All Rights Reserved.
21*5697Smcpowers  *
22*5697Smcpowers  * Contributor(s):
23*5697Smcpowers  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
24*5697Smcpowers  *   Stephen Fung <fungstep@hotmail.com>, and
25*5697Smcpowers  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
26*5697Smcpowers  *
27*5697Smcpowers  * Alternatively, the contents of this file may be used under the terms of
28*5697Smcpowers  * either the GNU General Public License Version 2 or later (the "GPL"), or
29*5697Smcpowers  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30*5697Smcpowers  * in which case the provisions of the GPL or the LGPL are applicable instead
31*5697Smcpowers  * of those above. If you wish to allow use of your version of this file only
32*5697Smcpowers  * under the terms of either the GPL or the LGPL, and not to allow others to
33*5697Smcpowers  * use your version of this file under the terms of the MPL, indicate your
34*5697Smcpowers  * decision by deleting the provisions above and replace them with the notice
35*5697Smcpowers  * and other provisions required by the GPL or the LGPL. If you do not delete
36*5697Smcpowers  * the provisions above, a recipient may use your version of this file under
37*5697Smcpowers  * the terms of any one of the MPL, the GPL or the LGPL.
38*5697Smcpowers  *
39*5697Smcpowers  * ***** END LICENSE BLOCK ***** */
40*5697Smcpowers /*
41*5697Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
42*5697Smcpowers  * Use is subject to license terms.
43*5697Smcpowers  *
44*5697Smcpowers  * Sun elects to use this software under the MPL license.
45*5697Smcpowers  */
46*5697Smcpowers 
47*5697Smcpowers #pragma ident	"%Z%%M%	%I%	%E% SMI"
48*5697Smcpowers 
49*5697Smcpowers #include "ec2.h"
50*5697Smcpowers #include "mp_gf2m.h"
51*5697Smcpowers #include "mp_gf2m-priv.h"
52*5697Smcpowers #include "mpi.h"
53*5697Smcpowers #include "mpi-priv.h"
54*5697Smcpowers #ifndef _KERNEL
55*5697Smcpowers #include <stdlib.h>
56*5697Smcpowers #endif
57*5697Smcpowers 
58*5697Smcpowers /* Fast reduction for polynomials over a 233-bit curve. Assumes reduction
59*5697Smcpowers  * polynomial with terms {233, 74, 0}. */
60*5697Smcpowers mp_err
ec_GF2m_233_mod(const mp_int * a,mp_int * r,const GFMethod * meth)61*5697Smcpowers ec_GF2m_233_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
62*5697Smcpowers {
63*5697Smcpowers 	mp_err res = MP_OKAY;
64*5697Smcpowers 	mp_digit *u, z;
65*5697Smcpowers 
66*5697Smcpowers 	if (a != r) {
67*5697Smcpowers 		MP_CHECKOK(mp_copy(a, r));
68*5697Smcpowers 	}
69*5697Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
70*5697Smcpowers 	if (MP_USED(r) < 8) {
71*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 8));
72*5697Smcpowers 	}
73*5697Smcpowers 	u = MP_DIGITS(r);
74*5697Smcpowers 	MP_USED(r) = 8;
75*5697Smcpowers 
76*5697Smcpowers 	/* u[7] only has 18 significant bits */
77*5697Smcpowers 	z = u[7];
78*5697Smcpowers 	u[4] ^= (z << 33) ^ (z >> 41);
79*5697Smcpowers 	u[3] ^= (z << 23);
80*5697Smcpowers 	z = u[6];
81*5697Smcpowers 	u[4] ^= (z >> 31);
82*5697Smcpowers 	u[3] ^= (z << 33) ^ (z >> 41);
83*5697Smcpowers 	u[2] ^= (z << 23);
84*5697Smcpowers 	z = u[5];
85*5697Smcpowers 	u[3] ^= (z >> 31);
86*5697Smcpowers 	u[2] ^= (z << 33) ^ (z >> 41);
87*5697Smcpowers 	u[1] ^= (z << 23);
88*5697Smcpowers 	z = u[4];
89*5697Smcpowers 	u[2] ^= (z >> 31);
90*5697Smcpowers 	u[1] ^= (z << 33) ^ (z >> 41);
91*5697Smcpowers 	u[0] ^= (z << 23);
92*5697Smcpowers 	z = u[3] >> 41;				/* z only has 23 significant bits */
93*5697Smcpowers 	u[1] ^= (z << 10);
94*5697Smcpowers 	u[0] ^= z;
95*5697Smcpowers 	/* clear bits above 233 */
96*5697Smcpowers 	u[7] = u[6] = u[5] = u[4] = 0;
97*5697Smcpowers 	u[3] ^= z << 41;
98*5697Smcpowers #else
99*5697Smcpowers 	if (MP_USED(r) < 15) {
100*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 15));
101*5697Smcpowers 	}
102*5697Smcpowers 	u = MP_DIGITS(r);
103*5697Smcpowers 	MP_USED(r) = 15;
104*5697Smcpowers 
105*5697Smcpowers 	/* u[14] only has 18 significant bits */
106*5697Smcpowers 	z = u[14];
107*5697Smcpowers 	u[9] ^= (z << 1);
108*5697Smcpowers 	u[7] ^= (z >> 9);
109*5697Smcpowers 	u[6] ^= (z << 23);
110*5697Smcpowers 	z = u[13];
111*5697Smcpowers 	u[9] ^= (z >> 31);
112*5697Smcpowers 	u[8] ^= (z << 1);
113*5697Smcpowers 	u[6] ^= (z >> 9);
114*5697Smcpowers 	u[5] ^= (z << 23);
115*5697Smcpowers 	z = u[12];
116*5697Smcpowers 	u[8] ^= (z >> 31);
117*5697Smcpowers 	u[7] ^= (z << 1);
118*5697Smcpowers 	u[5] ^= (z >> 9);
119*5697Smcpowers 	u[4] ^= (z << 23);
120*5697Smcpowers 	z = u[11];
121*5697Smcpowers 	u[7] ^= (z >> 31);
122*5697Smcpowers 	u[6] ^= (z << 1);
123*5697Smcpowers 	u[4] ^= (z >> 9);
124*5697Smcpowers 	u[3] ^= (z << 23);
125*5697Smcpowers 	z = u[10];
126*5697Smcpowers 	u[6] ^= (z >> 31);
127*5697Smcpowers 	u[5] ^= (z << 1);
128*5697Smcpowers 	u[3] ^= (z >> 9);
129*5697Smcpowers 	u[2] ^= (z << 23);
130*5697Smcpowers 	z = u[9];
131*5697Smcpowers 	u[5] ^= (z >> 31);
132*5697Smcpowers 	u[4] ^= (z << 1);
133*5697Smcpowers 	u[2] ^= (z >> 9);
134*5697Smcpowers 	u[1] ^= (z << 23);
135*5697Smcpowers 	z = u[8];
136*5697Smcpowers 	u[4] ^= (z >> 31);
137*5697Smcpowers 	u[3] ^= (z << 1);
138*5697Smcpowers 	u[1] ^= (z >> 9);
139*5697Smcpowers 	u[0] ^= (z << 23);
140*5697Smcpowers 	z = u[7] >> 9;				/* z only has 23 significant bits */
141*5697Smcpowers 	u[3] ^= (z >> 22);
142*5697Smcpowers 	u[2] ^= (z << 10);
143*5697Smcpowers 	u[0] ^= z;
144*5697Smcpowers 	/* clear bits above 233 */
145*5697Smcpowers 	u[14] = u[13] = u[12] = u[11] = u[10] = u[9] = u[8] = 0;
146*5697Smcpowers 	u[7] ^= z << 9;
147*5697Smcpowers #endif
148*5697Smcpowers 	s_mp_clamp(r);
149*5697Smcpowers 
150*5697Smcpowers   CLEANUP:
151*5697Smcpowers 	return res;
152*5697Smcpowers }
153*5697Smcpowers 
154*5697Smcpowers /* Fast squaring for polynomials over a 233-bit curve. Assumes reduction
155*5697Smcpowers  * polynomial with terms {233, 74, 0}. */
156*5697Smcpowers mp_err
ec_GF2m_233_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)157*5697Smcpowers ec_GF2m_233_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
158*5697Smcpowers {
159*5697Smcpowers 	mp_err res = MP_OKAY;
160*5697Smcpowers 	mp_digit *u, *v;
161*5697Smcpowers 
162*5697Smcpowers 	v = MP_DIGITS(a);
163*5697Smcpowers 
164*5697Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
165*5697Smcpowers 	if (MP_USED(a) < 4) {
166*5697Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
167*5697Smcpowers 	}
168*5697Smcpowers 	if (MP_USED(r) < 8) {
169*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 8));
170*5697Smcpowers 	}
171*5697Smcpowers 	MP_USED(r) = 8;
172*5697Smcpowers #else
173*5697Smcpowers 	if (MP_USED(a) < 8) {
174*5697Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
175*5697Smcpowers 	}
176*5697Smcpowers 	if (MP_USED(r) < 15) {
177*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 15));
178*5697Smcpowers 	}
179*5697Smcpowers 	MP_USED(r) = 15;
180*5697Smcpowers #endif
181*5697Smcpowers 	u = MP_DIGITS(r);
182*5697Smcpowers 
183*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
184*5697Smcpowers 	u[14] = gf2m_SQR0(v[7]);
185*5697Smcpowers 	u[13] = gf2m_SQR1(v[6]);
186*5697Smcpowers 	u[12] = gf2m_SQR0(v[6]);
187*5697Smcpowers 	u[11] = gf2m_SQR1(v[5]);
188*5697Smcpowers 	u[10] = gf2m_SQR0(v[5]);
189*5697Smcpowers 	u[9] = gf2m_SQR1(v[4]);
190*5697Smcpowers 	u[8] = gf2m_SQR0(v[4]);
191*5697Smcpowers #endif
192*5697Smcpowers 	u[7] = gf2m_SQR1(v[3]);
193*5697Smcpowers 	u[6] = gf2m_SQR0(v[3]);
194*5697Smcpowers 	u[5] = gf2m_SQR1(v[2]);
195*5697Smcpowers 	u[4] = gf2m_SQR0(v[2]);
196*5697Smcpowers 	u[3] = gf2m_SQR1(v[1]);
197*5697Smcpowers 	u[2] = gf2m_SQR0(v[1]);
198*5697Smcpowers 	u[1] = gf2m_SQR1(v[0]);
199*5697Smcpowers 	u[0] = gf2m_SQR0(v[0]);
200*5697Smcpowers 	return ec_GF2m_233_mod(r, r, meth);
201*5697Smcpowers 
202*5697Smcpowers   CLEANUP:
203*5697Smcpowers 	return res;
204*5697Smcpowers }
205*5697Smcpowers 
206*5697Smcpowers /* Fast multiplication for polynomials over a 233-bit curve. Assumes
207*5697Smcpowers  * reduction polynomial with terms {233, 74, 0}. */
208*5697Smcpowers mp_err
ec_GF2m_233_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)209*5697Smcpowers ec_GF2m_233_mul(const mp_int *a, const mp_int *b, mp_int *r,
210*5697Smcpowers 				const GFMethod *meth)
211*5697Smcpowers {
212*5697Smcpowers 	mp_err res = MP_OKAY;
213*5697Smcpowers 	mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
214*5697Smcpowers 
215*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
216*5697Smcpowers 	mp_digit a7 = 0, a6 = 0, a5 = 0, a4 = 0, b7 = 0, b6 = 0, b5 = 0, b4 =
217*5697Smcpowers 		0;
218*5697Smcpowers 	mp_digit rm[8];
219*5697Smcpowers #endif
220*5697Smcpowers 
221*5697Smcpowers 	if (a == b) {
222*5697Smcpowers 		return ec_GF2m_233_sqr(a, r, meth);
223*5697Smcpowers 	} else {
224*5697Smcpowers 		switch (MP_USED(a)) {
225*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
226*5697Smcpowers 		case 8:
227*5697Smcpowers 			a7 = MP_DIGIT(a, 7);
228*5697Smcpowers 		case 7:
229*5697Smcpowers 			a6 = MP_DIGIT(a, 6);
230*5697Smcpowers 		case 6:
231*5697Smcpowers 			a5 = MP_DIGIT(a, 5);
232*5697Smcpowers 		case 5:
233*5697Smcpowers 			a4 = MP_DIGIT(a, 4);
234*5697Smcpowers #endif
235*5697Smcpowers 		case 4:
236*5697Smcpowers 			a3 = MP_DIGIT(a, 3);
237*5697Smcpowers 		case 3:
238*5697Smcpowers 			a2 = MP_DIGIT(a, 2);
239*5697Smcpowers 		case 2:
240*5697Smcpowers 			a1 = MP_DIGIT(a, 1);
241*5697Smcpowers 		default:
242*5697Smcpowers 			a0 = MP_DIGIT(a, 0);
243*5697Smcpowers 		}
244*5697Smcpowers 		switch (MP_USED(b)) {
245*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
246*5697Smcpowers 		case 8:
247*5697Smcpowers 			b7 = MP_DIGIT(b, 7);
248*5697Smcpowers 		case 7:
249*5697Smcpowers 			b6 = MP_DIGIT(b, 6);
250*5697Smcpowers 		case 6:
251*5697Smcpowers 			b5 = MP_DIGIT(b, 5);
252*5697Smcpowers 		case 5:
253*5697Smcpowers 			b4 = MP_DIGIT(b, 4);
254*5697Smcpowers #endif
255*5697Smcpowers 		case 4:
256*5697Smcpowers 			b3 = MP_DIGIT(b, 3);
257*5697Smcpowers 		case 3:
258*5697Smcpowers 			b2 = MP_DIGIT(b, 2);
259*5697Smcpowers 		case 2:
260*5697Smcpowers 			b1 = MP_DIGIT(b, 1);
261*5697Smcpowers 		default:
262*5697Smcpowers 			b0 = MP_DIGIT(b, 0);
263*5697Smcpowers 		}
264*5697Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
265*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 8));
266*5697Smcpowers 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
267*5697Smcpowers 		MP_USED(r) = 8;
268*5697Smcpowers 		s_mp_clamp(r);
269*5697Smcpowers #else
270*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 16));
271*5697Smcpowers 		s_bmul_4x4(MP_DIGITS(r) + 8, a7, a6, a5, a4, b7, b6, b5, b4);
272*5697Smcpowers 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
273*5697Smcpowers 		s_bmul_4x4(rm, a7 ^ a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b7 ^ b3,
274*5697Smcpowers 				   b6 ^ b2, b5 ^ b1, b4 ^ b0);
275*5697Smcpowers 		rm[7] ^= MP_DIGIT(r, 7) ^ MP_DIGIT(r, 15);
276*5697Smcpowers 		rm[6] ^= MP_DIGIT(r, 6) ^ MP_DIGIT(r, 14);
277*5697Smcpowers 		rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
278*5697Smcpowers 		rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
279*5697Smcpowers 		rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
280*5697Smcpowers 		rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
281*5697Smcpowers 		rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
282*5697Smcpowers 		rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
283*5697Smcpowers 		MP_DIGIT(r, 11) ^= rm[7];
284*5697Smcpowers 		MP_DIGIT(r, 10) ^= rm[6];
285*5697Smcpowers 		MP_DIGIT(r, 9) ^= rm[5];
286*5697Smcpowers 		MP_DIGIT(r, 8) ^= rm[4];
287*5697Smcpowers 		MP_DIGIT(r, 7) ^= rm[3];
288*5697Smcpowers 		MP_DIGIT(r, 6) ^= rm[2];
289*5697Smcpowers 		MP_DIGIT(r, 5) ^= rm[1];
290*5697Smcpowers 		MP_DIGIT(r, 4) ^= rm[0];
291*5697Smcpowers 		MP_USED(r) = 16;
292*5697Smcpowers 		s_mp_clamp(r);
293*5697Smcpowers #endif
294*5697Smcpowers 		return ec_GF2m_233_mod(r, r, meth);
295*5697Smcpowers 	}
296*5697Smcpowers 
297*5697Smcpowers   CLEANUP:
298*5697Smcpowers 	return res;
299*5697Smcpowers }
300*5697Smcpowers 
301*5697Smcpowers /* Wire in fast field arithmetic for 233-bit curves. */
302*5697Smcpowers mp_err
ec_group_set_gf2m233(ECGroup * group,ECCurveName name)303*5697Smcpowers ec_group_set_gf2m233(ECGroup *group, ECCurveName name)
304*5697Smcpowers {
305*5697Smcpowers 	group->meth->field_mod = &ec_GF2m_233_mod;
306*5697Smcpowers 	group->meth->field_mul = &ec_GF2m_233_mul;
307*5697Smcpowers 	group->meth->field_sqr = &ec_GF2m_233_sqr;
308*5697Smcpowers 	return MP_OKAY;
309*5697Smcpowers }
310