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