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 "mplogic.h"
51*f9fbec18Smcpowers #include "mp_gf2m.h"
52*f9fbec18Smcpowers #ifndef _KERNEL
53*f9fbec18Smcpowers #include <stdlib.h>
54*f9fbec18Smcpowers #endif
55*f9fbec18Smcpowers
56*f9fbec18Smcpowers /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery
57*f9fbec18Smcpowers * projective coordinates. Uses algorithm Mdouble in appendix of Lopez, J.
58*f9fbec18Smcpowers * and Dahab, R. "Fast multiplication on elliptic curves over GF(2^m)
59*f9fbec18Smcpowers * without precomputation". modified to not require precomputation of
60*f9fbec18Smcpowers * c=b^{2^{m-1}}. */
61*f9fbec18Smcpowers static mp_err
gf2m_Mdouble(mp_int * x,mp_int * z,const ECGroup * group,int kmflag)62*f9fbec18Smcpowers gf2m_Mdouble(mp_int *x, mp_int *z, const ECGroup *group, int kmflag)
63*f9fbec18Smcpowers {
64*f9fbec18Smcpowers mp_err res = MP_OKAY;
65*f9fbec18Smcpowers mp_int t1;
66*f9fbec18Smcpowers
67*f9fbec18Smcpowers MP_DIGITS(&t1) = 0;
68*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t1, kmflag));
69*f9fbec18Smcpowers
70*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
71*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(z, &t1, group->meth));
72*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(x, &t1, z, group->meth));
73*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
74*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth));
75*f9fbec18Smcpowers MP_CHECKOK(group->meth->
76*f9fbec18Smcpowers field_mul(&group->curveb, &t1, &t1, group->meth));
77*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(x, &t1, x, group->meth));
78*f9fbec18Smcpowers
79*f9fbec18Smcpowers CLEANUP:
80*f9fbec18Smcpowers mp_clear(&t1);
81*f9fbec18Smcpowers return res;
82*f9fbec18Smcpowers }
83*f9fbec18Smcpowers
84*f9fbec18Smcpowers /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in
85*f9fbec18Smcpowers * Montgomery projective coordinates. Uses algorithm Madd in appendix of
86*f9fbec18Smcpowers * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
87*f9fbec18Smcpowers * GF(2^m) without precomputation". */
88*f9fbec18Smcpowers static mp_err
gf2m_Madd(const mp_int * x,mp_int * x1,mp_int * z1,mp_int * x2,mp_int * z2,const ECGroup * group,int kmflag)89*f9fbec18Smcpowers gf2m_Madd(const mp_int *x, mp_int *x1, mp_int *z1, mp_int *x2, mp_int *z2,
90*f9fbec18Smcpowers const ECGroup *group, int kmflag)
91*f9fbec18Smcpowers {
92*f9fbec18Smcpowers mp_err res = MP_OKAY;
93*f9fbec18Smcpowers mp_int t1, t2;
94*f9fbec18Smcpowers
95*f9fbec18Smcpowers MP_DIGITS(&t1) = 0;
96*f9fbec18Smcpowers MP_DIGITS(&t2) = 0;
97*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t1, kmflag));
98*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t2, kmflag));
99*f9fbec18Smcpowers
100*f9fbec18Smcpowers MP_CHECKOK(mp_copy(x, &t1));
101*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(x1, z2, x1, group->meth));
102*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z1, x2, z1, group->meth));
103*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(x1, z1, &t2, group->meth));
104*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
105*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(z1, z1, group->meth));
106*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z1, &t1, x1, group->meth));
107*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(x1, &t2, x1, group->meth));
108*f9fbec18Smcpowers
109*f9fbec18Smcpowers CLEANUP:
110*f9fbec18Smcpowers mp_clear(&t1);
111*f9fbec18Smcpowers mp_clear(&t2);
112*f9fbec18Smcpowers return res;
113*f9fbec18Smcpowers }
114*f9fbec18Smcpowers
115*f9fbec18Smcpowers /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
116*f9fbec18Smcpowers * using Montgomery point multiplication algorithm Mxy() in appendix of
117*f9fbec18Smcpowers * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over
118*f9fbec18Smcpowers * GF(2^m) without precomputation". Returns: 0 on error 1 if return value
119*f9fbec18Smcpowers * should be the point at infinity 2 otherwise */
120*f9fbec18Smcpowers static int
gf2m_Mxy(const mp_int * x,const mp_int * y,mp_int * x1,mp_int * z1,mp_int * x2,mp_int * z2,const ECGroup * group)121*f9fbec18Smcpowers gf2m_Mxy(const mp_int *x, const mp_int *y, mp_int *x1, mp_int *z1,
122*f9fbec18Smcpowers mp_int *x2, mp_int *z2, const ECGroup *group)
123*f9fbec18Smcpowers {
124*f9fbec18Smcpowers mp_err res = MP_OKAY;
125*f9fbec18Smcpowers int ret = 0;
126*f9fbec18Smcpowers mp_int t3, t4, t5;
127*f9fbec18Smcpowers
128*f9fbec18Smcpowers MP_DIGITS(&t3) = 0;
129*f9fbec18Smcpowers MP_DIGITS(&t4) = 0;
130*f9fbec18Smcpowers MP_DIGITS(&t5) = 0;
131*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t3, FLAG(x2)));
132*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t4, FLAG(x2)));
133*f9fbec18Smcpowers MP_CHECKOK(mp_init(&t5, FLAG(x2)));
134*f9fbec18Smcpowers
135*f9fbec18Smcpowers if (mp_cmp_z(z1) == 0) {
136*f9fbec18Smcpowers mp_zero(x2);
137*f9fbec18Smcpowers mp_zero(z2);
138*f9fbec18Smcpowers ret = 1;
139*f9fbec18Smcpowers goto CLEANUP;
140*f9fbec18Smcpowers }
141*f9fbec18Smcpowers
142*f9fbec18Smcpowers if (mp_cmp_z(z2) == 0) {
143*f9fbec18Smcpowers MP_CHECKOK(mp_copy(x, x2));
144*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(x, y, z2, group->meth));
145*f9fbec18Smcpowers ret = 2;
146*f9fbec18Smcpowers goto CLEANUP;
147*f9fbec18Smcpowers }
148*f9fbec18Smcpowers
149*f9fbec18Smcpowers MP_CHECKOK(mp_set_int(&t5, 1));
150*f9fbec18Smcpowers if (group->meth->field_enc) {
151*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_enc(&t5, &t5, group->meth));
152*f9fbec18Smcpowers }
153*f9fbec18Smcpowers
154*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z1, z2, &t3, group->meth));
155*f9fbec18Smcpowers
156*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z1, x, z1, group->meth));
157*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
158*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z2, x, z2, group->meth));
159*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z2, x1, x1, group->meth));
160*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(z2, x2, z2, group->meth));
161*f9fbec18Smcpowers
162*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z2, z1, z2, group->meth));
163*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(x, &t4, group->meth));
164*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(&t4, y, &t4, group->meth));
165*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(&t4, &t3, &t4, group->meth));
166*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(&t4, z2, &t4, group->meth));
167*f9fbec18Smcpowers
168*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(&t3, x, &t3, group->meth));
169*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_div(&t5, &t3, &t3, group->meth));
170*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(&t3, &t4, &t4, group->meth));
171*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(x1, &t3, x2, group->meth));
172*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(x2, x, z2, group->meth));
173*f9fbec18Smcpowers
174*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_mul(z2, &t4, z2, group->meth));
175*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(z2, y, z2, group->meth));
176*f9fbec18Smcpowers
177*f9fbec18Smcpowers ret = 2;
178*f9fbec18Smcpowers
179*f9fbec18Smcpowers CLEANUP:
180*f9fbec18Smcpowers mp_clear(&t3);
181*f9fbec18Smcpowers mp_clear(&t4);
182*f9fbec18Smcpowers mp_clear(&t5);
183*f9fbec18Smcpowers if (res == MP_OKAY) {
184*f9fbec18Smcpowers return ret;
185*f9fbec18Smcpowers } else {
186*f9fbec18Smcpowers return 0;
187*f9fbec18Smcpowers }
188*f9fbec18Smcpowers }
189*f9fbec18Smcpowers
190*f9fbec18Smcpowers /* Computes R = nP based on algorithm 2P of Lopex, J. and Dahab, R. "Fast
191*f9fbec18Smcpowers * multiplication on elliptic curves over GF(2^m) without
192*f9fbec18Smcpowers * precomputation". Elliptic curve points P and R can be identical. Uses
193*f9fbec18Smcpowers * Montgomery projective coordinates. */
194*f9fbec18Smcpowers mp_err
ec_GF2m_pt_mul_mont(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)195*f9fbec18Smcpowers ec_GF2m_pt_mul_mont(const mp_int *n, const mp_int *px, const mp_int *py,
196*f9fbec18Smcpowers mp_int *rx, mp_int *ry, const ECGroup *group)
197*f9fbec18Smcpowers {
198*f9fbec18Smcpowers mp_err res = MP_OKAY;
199*f9fbec18Smcpowers mp_int x1, x2, z1, z2;
200*f9fbec18Smcpowers int i, j;
201*f9fbec18Smcpowers mp_digit top_bit, mask;
202*f9fbec18Smcpowers
203*f9fbec18Smcpowers MP_DIGITS(&x1) = 0;
204*f9fbec18Smcpowers MP_DIGITS(&x2) = 0;
205*f9fbec18Smcpowers MP_DIGITS(&z1) = 0;
206*f9fbec18Smcpowers MP_DIGITS(&z2) = 0;
207*f9fbec18Smcpowers MP_CHECKOK(mp_init(&x1, FLAG(n)));
208*f9fbec18Smcpowers MP_CHECKOK(mp_init(&x2, FLAG(n)));
209*f9fbec18Smcpowers MP_CHECKOK(mp_init(&z1, FLAG(n)));
210*f9fbec18Smcpowers MP_CHECKOK(mp_init(&z2, FLAG(n)));
211*f9fbec18Smcpowers
212*f9fbec18Smcpowers /* if result should be point at infinity */
213*f9fbec18Smcpowers if ((mp_cmp_z(n) == 0) || (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES)) {
214*f9fbec18Smcpowers MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
215*f9fbec18Smcpowers goto CLEANUP;
216*f9fbec18Smcpowers }
217*f9fbec18Smcpowers
218*f9fbec18Smcpowers MP_CHECKOK(mp_copy(px, &x1)); /* x1 = px */
219*f9fbec18Smcpowers MP_CHECKOK(mp_set_int(&z1, 1)); /* z1 = 1 */
220*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(&x1, &z2, group->meth)); /* z2 =
221*f9fbec18Smcpowers * x1^2 =
222*f9fbec18Smcpowers * px^2 */
223*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_sqr(&z2, &x2, group->meth));
224*f9fbec18Smcpowers MP_CHECKOK(group->meth->field_add(&x2, &group->curveb, &x2, group->meth)); /* x2
225*f9fbec18Smcpowers * =
226*f9fbec18Smcpowers * px^4
227*f9fbec18Smcpowers * +
228*f9fbec18Smcpowers * b
229*f9fbec18Smcpowers */
230*f9fbec18Smcpowers
231*f9fbec18Smcpowers /* find top-most bit and go one past it */
232*f9fbec18Smcpowers i = MP_USED(n) - 1;
233*f9fbec18Smcpowers j = MP_DIGIT_BIT - 1;
234*f9fbec18Smcpowers top_bit = 1;
235*f9fbec18Smcpowers top_bit <<= MP_DIGIT_BIT - 1;
236*f9fbec18Smcpowers mask = top_bit;
237*f9fbec18Smcpowers while (!(MP_DIGITS(n)[i] & mask)) {
238*f9fbec18Smcpowers mask >>= 1;
239*f9fbec18Smcpowers j--;
240*f9fbec18Smcpowers }
241*f9fbec18Smcpowers mask >>= 1;
242*f9fbec18Smcpowers j--;
243*f9fbec18Smcpowers
244*f9fbec18Smcpowers /* if top most bit was at word break, go to next word */
245*f9fbec18Smcpowers if (!mask) {
246*f9fbec18Smcpowers i--;
247*f9fbec18Smcpowers j = MP_DIGIT_BIT - 1;
248*f9fbec18Smcpowers mask = top_bit;
249*f9fbec18Smcpowers }
250*f9fbec18Smcpowers
251*f9fbec18Smcpowers for (; i >= 0; i--) {
252*f9fbec18Smcpowers for (; j >= 0; j--) {
253*f9fbec18Smcpowers if (MP_DIGITS(n)[i] & mask) {
254*f9fbec18Smcpowers MP_CHECKOK(gf2m_Madd(px, &x1, &z1, &x2, &z2, group, FLAG(n)));
255*f9fbec18Smcpowers MP_CHECKOK(gf2m_Mdouble(&x2, &z2, group, FLAG(n)));
256*f9fbec18Smcpowers } else {
257*f9fbec18Smcpowers MP_CHECKOK(gf2m_Madd(px, &x2, &z2, &x1, &z1, group, FLAG(n)));
258*f9fbec18Smcpowers MP_CHECKOK(gf2m_Mdouble(&x1, &z1, group, FLAG(n)));
259*f9fbec18Smcpowers }
260*f9fbec18Smcpowers mask >>= 1;
261*f9fbec18Smcpowers }
262*f9fbec18Smcpowers j = MP_DIGIT_BIT - 1;
263*f9fbec18Smcpowers mask = top_bit;
264*f9fbec18Smcpowers }
265*f9fbec18Smcpowers
266*f9fbec18Smcpowers /* convert out of "projective" coordinates */
267*f9fbec18Smcpowers i = gf2m_Mxy(px, py, &x1, &z1, &x2, &z2, group);
268*f9fbec18Smcpowers if (i == 0) {
269*f9fbec18Smcpowers res = MP_BADARG;
270*f9fbec18Smcpowers goto CLEANUP;
271*f9fbec18Smcpowers } else if (i == 1) {
272*f9fbec18Smcpowers MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
273*f9fbec18Smcpowers } else {
274*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&x2, rx));
275*f9fbec18Smcpowers MP_CHECKOK(mp_copy(&z2, ry));
276*f9fbec18Smcpowers }
277*f9fbec18Smcpowers
278*f9fbec18Smcpowers CLEANUP:
279*f9fbec18Smcpowers mp_clear(&x1);
280*f9fbec18Smcpowers mp_clear(&x2);
281*f9fbec18Smcpowers mp_clear(&z1);
282*f9fbec18Smcpowers mp_clear(&z2);
283*f9fbec18Smcpowers return res;
284*f9fbec18Smcpowers }
285