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