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