xref: /onnv-gate/usr/src/common/crypto/ecc/ec2_163.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 163-bit curve. Assumes reduction
59*5697Smcpowers  * polynomial with terms {163, 7, 6, 3, 0}. */
60*5697Smcpowers mp_err
ec_GF2m_163_mod(const mp_int * a,mp_int * r,const GFMethod * meth)61*5697Smcpowers ec_GF2m_163_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) < 6) {
71*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 6));
72*5697Smcpowers 	}
73*5697Smcpowers 	u = MP_DIGITS(r);
74*5697Smcpowers 	MP_USED(r) = 6;
75*5697Smcpowers 
76*5697Smcpowers 	/* u[5] only has 6 significant bits */
77*5697Smcpowers 	z = u[5];
78*5697Smcpowers 	u[2] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
79*5697Smcpowers 	z = u[4];
80*5697Smcpowers 	u[2] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
81*5697Smcpowers 	u[1] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
82*5697Smcpowers 	z = u[3];
83*5697Smcpowers 	u[1] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
84*5697Smcpowers 	u[0] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
85*5697Smcpowers 	z = u[2] >> 35;				/* z only has 29 significant bits */
86*5697Smcpowers 	u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
87*5697Smcpowers 	/* clear bits above 163 */
88*5697Smcpowers 	u[5] = u[4] = u[3] = 0;
89*5697Smcpowers 	u[2] ^= z << 35;
90*5697Smcpowers #else
91*5697Smcpowers 	if (MP_USED(r) < 11) {
92*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 11));
93*5697Smcpowers 	}
94*5697Smcpowers 	u = MP_DIGITS(r);
95*5697Smcpowers 	MP_USED(r) = 11;
96*5697Smcpowers 
97*5697Smcpowers 	/* u[11] only has 6 significant bits */
98*5697Smcpowers 	z = u[10];
99*5697Smcpowers 	u[5] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
100*5697Smcpowers 	u[4] ^= (z << 29);
101*5697Smcpowers 	z = u[9];
102*5697Smcpowers 	u[5] ^= (z >> 28) ^ (z >> 29);
103*5697Smcpowers 	u[4] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
104*5697Smcpowers 	u[3] ^= (z << 29);
105*5697Smcpowers 	z = u[8];
106*5697Smcpowers 	u[4] ^= (z >> 28) ^ (z >> 29);
107*5697Smcpowers 	u[3] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
108*5697Smcpowers 	u[2] ^= (z << 29);
109*5697Smcpowers 	z = u[7];
110*5697Smcpowers 	u[3] ^= (z >> 28) ^ (z >> 29);
111*5697Smcpowers 	u[2] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
112*5697Smcpowers 	u[1] ^= (z << 29);
113*5697Smcpowers 	z = u[6];
114*5697Smcpowers 	u[2] ^= (z >> 28) ^ (z >> 29);
115*5697Smcpowers 	u[1] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
116*5697Smcpowers 	u[0] ^= (z << 29);
117*5697Smcpowers 	z = u[5] >> 3;				/* z only has 29 significant bits */
118*5697Smcpowers 	u[1] ^= (z >> 25) ^ (z >> 26);
119*5697Smcpowers 	u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
120*5697Smcpowers 	/* clear bits above 163 */
121*5697Smcpowers 	u[11] = u[10] = u[9] = u[8] = u[7] = u[6] = 0;
122*5697Smcpowers 	u[5] ^= z << 3;
123*5697Smcpowers #endif
124*5697Smcpowers 	s_mp_clamp(r);
125*5697Smcpowers 
126*5697Smcpowers   CLEANUP:
127*5697Smcpowers 	return res;
128*5697Smcpowers }
129*5697Smcpowers 
130*5697Smcpowers /* Fast squaring for polynomials over a 163-bit curve. Assumes reduction
131*5697Smcpowers  * polynomial with terms {163, 7, 6, 3, 0}. */
132*5697Smcpowers mp_err
ec_GF2m_163_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)133*5697Smcpowers ec_GF2m_163_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
134*5697Smcpowers {
135*5697Smcpowers 	mp_err res = MP_OKAY;
136*5697Smcpowers 	mp_digit *u, *v;
137*5697Smcpowers 
138*5697Smcpowers 	v = MP_DIGITS(a);
139*5697Smcpowers 
140*5697Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
141*5697Smcpowers 	if (MP_USED(a) < 3) {
142*5697Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
143*5697Smcpowers 	}
144*5697Smcpowers 	if (MP_USED(r) < 6) {
145*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 6));
146*5697Smcpowers 	}
147*5697Smcpowers 	MP_USED(r) = 6;
148*5697Smcpowers #else
149*5697Smcpowers 	if (MP_USED(a) < 6) {
150*5697Smcpowers 		return mp_bsqrmod(a, meth->irr_arr, r);
151*5697Smcpowers 	}
152*5697Smcpowers 	if (MP_USED(r) < 12) {
153*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 12));
154*5697Smcpowers 	}
155*5697Smcpowers 	MP_USED(r) = 12;
156*5697Smcpowers #endif
157*5697Smcpowers 	u = MP_DIGITS(r);
158*5697Smcpowers 
159*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
160*5697Smcpowers 	u[11] = gf2m_SQR1(v[5]);
161*5697Smcpowers 	u[10] = gf2m_SQR0(v[5]);
162*5697Smcpowers 	u[9] = gf2m_SQR1(v[4]);
163*5697Smcpowers 	u[8] = gf2m_SQR0(v[4]);
164*5697Smcpowers 	u[7] = gf2m_SQR1(v[3]);
165*5697Smcpowers 	u[6] = gf2m_SQR0(v[3]);
166*5697Smcpowers #endif
167*5697Smcpowers 	u[5] = gf2m_SQR1(v[2]);
168*5697Smcpowers 	u[4] = gf2m_SQR0(v[2]);
169*5697Smcpowers 	u[3] = gf2m_SQR1(v[1]);
170*5697Smcpowers 	u[2] = gf2m_SQR0(v[1]);
171*5697Smcpowers 	u[1] = gf2m_SQR1(v[0]);
172*5697Smcpowers 	u[0] = gf2m_SQR0(v[0]);
173*5697Smcpowers 	return ec_GF2m_163_mod(r, r, meth);
174*5697Smcpowers 
175*5697Smcpowers   CLEANUP:
176*5697Smcpowers 	return res;
177*5697Smcpowers }
178*5697Smcpowers 
179*5697Smcpowers /* Fast multiplication for polynomials over a 163-bit curve. Assumes
180*5697Smcpowers  * reduction polynomial with terms {163, 7, 6, 3, 0}. */
181*5697Smcpowers mp_err
ec_GF2m_163_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)182*5697Smcpowers ec_GF2m_163_mul(const mp_int *a, const mp_int *b, mp_int *r,
183*5697Smcpowers 				const GFMethod *meth)
184*5697Smcpowers {
185*5697Smcpowers 	mp_err res = MP_OKAY;
186*5697Smcpowers 	mp_digit a2 = 0, a1 = 0, a0, b2 = 0, b1 = 0, b0;
187*5697Smcpowers 
188*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
189*5697Smcpowers 	mp_digit a5 = 0, a4 = 0, a3 = 0, b5 = 0, b4 = 0, b3 = 0;
190*5697Smcpowers 	mp_digit rm[6];
191*5697Smcpowers #endif
192*5697Smcpowers 
193*5697Smcpowers 	if (a == b) {
194*5697Smcpowers 		return ec_GF2m_163_sqr(a, r, meth);
195*5697Smcpowers 	} else {
196*5697Smcpowers 		switch (MP_USED(a)) {
197*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
198*5697Smcpowers 		case 6:
199*5697Smcpowers 			a5 = MP_DIGIT(a, 5);
200*5697Smcpowers 		case 5:
201*5697Smcpowers 			a4 = MP_DIGIT(a, 4);
202*5697Smcpowers 		case 4:
203*5697Smcpowers 			a3 = MP_DIGIT(a, 3);
204*5697Smcpowers #endif
205*5697Smcpowers 		case 3:
206*5697Smcpowers 			a2 = MP_DIGIT(a, 2);
207*5697Smcpowers 		case 2:
208*5697Smcpowers 			a1 = MP_DIGIT(a, 1);
209*5697Smcpowers 		default:
210*5697Smcpowers 			a0 = MP_DIGIT(a, 0);
211*5697Smcpowers 		}
212*5697Smcpowers 		switch (MP_USED(b)) {
213*5697Smcpowers #ifdef ECL_THIRTY_TWO_BIT
214*5697Smcpowers 		case 6:
215*5697Smcpowers 			b5 = MP_DIGIT(b, 5);
216*5697Smcpowers 		case 5:
217*5697Smcpowers 			b4 = MP_DIGIT(b, 4);
218*5697Smcpowers 		case 4:
219*5697Smcpowers 			b3 = MP_DIGIT(b, 3);
220*5697Smcpowers #endif
221*5697Smcpowers 		case 3:
222*5697Smcpowers 			b2 = MP_DIGIT(b, 2);
223*5697Smcpowers 		case 2:
224*5697Smcpowers 			b1 = MP_DIGIT(b, 1);
225*5697Smcpowers 		default:
226*5697Smcpowers 			b0 = MP_DIGIT(b, 0);
227*5697Smcpowers 		}
228*5697Smcpowers #ifdef ECL_SIXTY_FOUR_BIT
229*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 6));
230*5697Smcpowers 		s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
231*5697Smcpowers 		MP_USED(r) = 6;
232*5697Smcpowers 		s_mp_clamp(r);
233*5697Smcpowers #else
234*5697Smcpowers 		MP_CHECKOK(s_mp_pad(r, 12));
235*5697Smcpowers 		s_bmul_3x3(MP_DIGITS(r) + 6, a5, a4, a3, b5, b4, b3);
236*5697Smcpowers 		s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
237*5697Smcpowers 		s_bmul_3x3(rm, a5 ^ a2, a4 ^ a1, a3 ^ a0, b5 ^ b2, b4 ^ b1,
238*5697Smcpowers 				   b3 ^ b0);
239*5697Smcpowers 		rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 11);
240*5697Smcpowers 		rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 10);
241*5697Smcpowers 		rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 9);
242*5697Smcpowers 		rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 8);
243*5697Smcpowers 		rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 7);
244*5697Smcpowers 		rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 6);
245*5697Smcpowers 		MP_DIGIT(r, 8) ^= rm[5];
246*5697Smcpowers 		MP_DIGIT(r, 7) ^= rm[4];
247*5697Smcpowers 		MP_DIGIT(r, 6) ^= rm[3];
248*5697Smcpowers 		MP_DIGIT(r, 5) ^= rm[2];
249*5697Smcpowers 		MP_DIGIT(r, 4) ^= rm[1];
250*5697Smcpowers 		MP_DIGIT(r, 3) ^= rm[0];
251*5697Smcpowers 		MP_USED(r) = 12;
252*5697Smcpowers 		s_mp_clamp(r);
253*5697Smcpowers #endif
254*5697Smcpowers 		return ec_GF2m_163_mod(r, r, meth);
255*5697Smcpowers 	}
256*5697Smcpowers 
257*5697Smcpowers   CLEANUP:
258*5697Smcpowers 	return res;
259*5697Smcpowers }
260*5697Smcpowers 
261*5697Smcpowers /* Wire in fast field arithmetic for 163-bit curves. */
262*5697Smcpowers mp_err
ec_group_set_gf2m163(ECGroup * group,ECCurveName name)263*5697Smcpowers ec_group_set_gf2m163(ECGroup *group, ECCurveName name)
264*5697Smcpowers {
265*5697Smcpowers 	group->meth->field_mod = &ec_GF2m_163_mod;
266*5697Smcpowers 	group->meth->field_mul = &ec_GF2m_163_mul;
267*5697Smcpowers 	group->meth->field_sqr = &ec_GF2m_163_sqr;
268*5697Smcpowers 	return MP_OKAY;
269*5697Smcpowers }
270