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