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 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24*5697Smcpowers *
25*5697Smcpowers * Alternatively, the contents of this file may be used under the terms of
26*5697Smcpowers * either the GNU General Public License Version 2 or later (the "GPL"), or
27*5697Smcpowers * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28*5697Smcpowers * in which case the provisions of the GPL or the LGPL are applicable instead
29*5697Smcpowers * of those above. If you wish to allow use of your version of this file only
30*5697Smcpowers * under the terms of either the GPL or the LGPL, and not to allow others to
31*5697Smcpowers * use your version of this file under the terms of the MPL, indicate your
32*5697Smcpowers * decision by deleting the provisions above and replace them with the notice
33*5697Smcpowers * and other provisions required by the GPL or the LGPL. If you do not delete
34*5697Smcpowers * the provisions above, a recipient may use your version of this file under
35*5697Smcpowers * the terms of any one of the MPL, the GPL or the LGPL.
36*5697Smcpowers *
37*5697Smcpowers * ***** END LICENSE BLOCK ***** */
38*5697Smcpowers /*
39*5697Smcpowers * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40*5697Smcpowers * Use is subject to license terms.
41*5697Smcpowers *
42*5697Smcpowers * Sun elects to use this software under the MPL license.
43*5697Smcpowers */
44*5697Smcpowers
45*5697Smcpowers #pragma ident "%Z%%M% %I% %E% SMI"
46*5697Smcpowers
47*5697Smcpowers #include "ec2.h"
48*5697Smcpowers #include "mplogic.h"
49*5697Smcpowers #include "mp_gf2m.h"
50*5697Smcpowers #ifndef _KERNEL
51*5697Smcpowers #include <stdlib.h>
52*5697Smcpowers #endif
53*5697Smcpowers
54*5697Smcpowers /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
55*5697Smcpowers mp_err
ec_GF2m_pt_is_inf_aff(const mp_int * px,const mp_int * py)56*5697Smcpowers ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
57*5697Smcpowers {
58*5697Smcpowers
59*5697Smcpowers if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
60*5697Smcpowers return MP_YES;
61*5697Smcpowers } else {
62*5697Smcpowers return MP_NO;
63*5697Smcpowers }
64*5697Smcpowers
65*5697Smcpowers }
66*5697Smcpowers
67*5697Smcpowers /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
68*5697Smcpowers mp_err
ec_GF2m_pt_set_inf_aff(mp_int * px,mp_int * py)69*5697Smcpowers ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
70*5697Smcpowers {
71*5697Smcpowers mp_zero(px);
72*5697Smcpowers mp_zero(py);
73*5697Smcpowers return MP_OKAY;
74*5697Smcpowers }
75*5697Smcpowers
76*5697Smcpowers /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P,
77*5697Smcpowers * Q, and R can all be identical. Uses affine coordinates. */
78*5697Smcpowers mp_err
ec_GF2m_pt_add_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)79*5697Smcpowers ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
80*5697Smcpowers const mp_int *qy, mp_int *rx, mp_int *ry,
81*5697Smcpowers const ECGroup *group)
82*5697Smcpowers {
83*5697Smcpowers mp_err res = MP_OKAY;
84*5697Smcpowers mp_int lambda, tempx, tempy;
85*5697Smcpowers
86*5697Smcpowers MP_DIGITS(&lambda) = 0;
87*5697Smcpowers MP_DIGITS(&tempx) = 0;
88*5697Smcpowers MP_DIGITS(&tempy) = 0;
89*5697Smcpowers MP_CHECKOK(mp_init(&lambda, FLAG(px)));
90*5697Smcpowers MP_CHECKOK(mp_init(&tempx, FLAG(px)));
91*5697Smcpowers MP_CHECKOK(mp_init(&tempy, FLAG(px)));
92*5697Smcpowers /* if P = inf, then R = Q */
93*5697Smcpowers if (ec_GF2m_pt_is_inf_aff(px, py) == 0) {
94*5697Smcpowers MP_CHECKOK(mp_copy(qx, rx));
95*5697Smcpowers MP_CHECKOK(mp_copy(qy, ry));
96*5697Smcpowers res = MP_OKAY;
97*5697Smcpowers goto CLEANUP;
98*5697Smcpowers }
99*5697Smcpowers /* if Q = inf, then R = P */
100*5697Smcpowers if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) {
101*5697Smcpowers MP_CHECKOK(mp_copy(px, rx));
102*5697Smcpowers MP_CHECKOK(mp_copy(py, ry));
103*5697Smcpowers res = MP_OKAY;
104*5697Smcpowers goto CLEANUP;
105*5697Smcpowers }
106*5697Smcpowers /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2
107*5697Smcpowers * + lambda + px + qx */
108*5697Smcpowers if (mp_cmp(px, qx) != 0) {
109*5697Smcpowers MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
110*5697Smcpowers MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
111*5697Smcpowers MP_CHECKOK(group->meth->
112*5697Smcpowers field_div(&tempy, &tempx, &lambda, group->meth));
113*5697Smcpowers MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
114*5697Smcpowers MP_CHECKOK(group->meth->
115*5697Smcpowers field_add(&tempx, &lambda, &tempx, group->meth));
116*5697Smcpowers MP_CHECKOK(group->meth->
117*5697Smcpowers field_add(&tempx, &group->curvea, &tempx, group->meth));
118*5697Smcpowers MP_CHECKOK(group->meth->
119*5697Smcpowers field_add(&tempx, px, &tempx, group->meth));
120*5697Smcpowers MP_CHECKOK(group->meth->
121*5697Smcpowers field_add(&tempx, qx, &tempx, group->meth));
122*5697Smcpowers } else {
123*5697Smcpowers /* if py != qy or qx = 0, then R = inf */
124*5697Smcpowers if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
125*5697Smcpowers mp_zero(rx);
126*5697Smcpowers mp_zero(ry);
127*5697Smcpowers res = MP_OKAY;
128*5697Smcpowers goto CLEANUP;
129*5697Smcpowers }
130*5697Smcpowers /* lambda = qx + qy / qx */
131*5697Smcpowers MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
132*5697Smcpowers MP_CHECKOK(group->meth->
133*5697Smcpowers field_add(&lambda, qx, &lambda, group->meth));
134*5697Smcpowers /* tempx = a + lambda^2 + lambda */
135*5697Smcpowers MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
136*5697Smcpowers MP_CHECKOK(group->meth->
137*5697Smcpowers field_add(&tempx, &lambda, &tempx, group->meth));
138*5697Smcpowers MP_CHECKOK(group->meth->
139*5697Smcpowers field_add(&tempx, &group->curvea, &tempx, group->meth));
140*5697Smcpowers }
141*5697Smcpowers /* ry = (qx + tempx) * lambda + tempx + qy */
142*5697Smcpowers MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
143*5697Smcpowers MP_CHECKOK(group->meth->
144*5697Smcpowers field_mul(&tempy, &lambda, &tempy, group->meth));
145*5697Smcpowers MP_CHECKOK(group->meth->
146*5697Smcpowers field_add(&tempy, &tempx, &tempy, group->meth));
147*5697Smcpowers MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
148*5697Smcpowers /* rx = tempx */
149*5697Smcpowers MP_CHECKOK(mp_copy(&tempx, rx));
150*5697Smcpowers
151*5697Smcpowers CLEANUP:
152*5697Smcpowers mp_clear(&lambda);
153*5697Smcpowers mp_clear(&tempx);
154*5697Smcpowers mp_clear(&tempy);
155*5697Smcpowers return res;
156*5697Smcpowers }
157*5697Smcpowers
158*5697Smcpowers /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
159*5697Smcpowers * identical. Uses affine coordinates. */
160*5697Smcpowers mp_err
ec_GF2m_pt_sub_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)161*5697Smcpowers ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
162*5697Smcpowers const mp_int *qy, mp_int *rx, mp_int *ry,
163*5697Smcpowers const ECGroup *group)
164*5697Smcpowers {
165*5697Smcpowers mp_err res = MP_OKAY;
166*5697Smcpowers mp_int nqy;
167*5697Smcpowers
168*5697Smcpowers MP_DIGITS(&nqy) = 0;
169*5697Smcpowers MP_CHECKOK(mp_init(&nqy, FLAG(px)));
170*5697Smcpowers /* nqy = qx+qy */
171*5697Smcpowers MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
172*5697Smcpowers MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
173*5697Smcpowers CLEANUP:
174*5697Smcpowers mp_clear(&nqy);
175*5697Smcpowers return res;
176*5697Smcpowers }
177*5697Smcpowers
178*5697Smcpowers /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
179*5697Smcpowers * affine coordinates. */
180*5697Smcpowers mp_err
ec_GF2m_pt_dbl_aff(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)181*5697Smcpowers ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
182*5697Smcpowers mp_int *ry, const ECGroup *group)
183*5697Smcpowers {
184*5697Smcpowers return group->point_add(px, py, px, py, rx, ry, group);
185*5697Smcpowers }
186*5697Smcpowers
187*5697Smcpowers /* by default, this routine is unused and thus doesn't need to be compiled */
188*5697Smcpowers #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
189*5697Smcpowers /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
190*5697Smcpowers * R can be identical. Uses affine coordinates. */
191*5697Smcpowers mp_err
ec_GF2m_pt_mul_aff(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)192*5697Smcpowers ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
193*5697Smcpowers mp_int *rx, mp_int *ry, const ECGroup *group)
194*5697Smcpowers {
195*5697Smcpowers mp_err res = MP_OKAY;
196*5697Smcpowers mp_int k, k3, qx, qy, sx, sy;
197*5697Smcpowers int b1, b3, i, l;
198*5697Smcpowers
199*5697Smcpowers MP_DIGITS(&k) = 0;
200*5697Smcpowers MP_DIGITS(&k3) = 0;
201*5697Smcpowers MP_DIGITS(&qx) = 0;
202*5697Smcpowers MP_DIGITS(&qy) = 0;
203*5697Smcpowers MP_DIGITS(&sx) = 0;
204*5697Smcpowers MP_DIGITS(&sy) = 0;
205*5697Smcpowers MP_CHECKOK(mp_init(&k));
206*5697Smcpowers MP_CHECKOK(mp_init(&k3));
207*5697Smcpowers MP_CHECKOK(mp_init(&qx));
208*5697Smcpowers MP_CHECKOK(mp_init(&qy));
209*5697Smcpowers MP_CHECKOK(mp_init(&sx));
210*5697Smcpowers MP_CHECKOK(mp_init(&sy));
211*5697Smcpowers
212*5697Smcpowers /* if n = 0 then r = inf */
213*5697Smcpowers if (mp_cmp_z(n) == 0) {
214*5697Smcpowers mp_zero(rx);
215*5697Smcpowers mp_zero(ry);
216*5697Smcpowers res = MP_OKAY;
217*5697Smcpowers goto CLEANUP;
218*5697Smcpowers }
219*5697Smcpowers /* Q = P, k = n */
220*5697Smcpowers MP_CHECKOK(mp_copy(px, &qx));
221*5697Smcpowers MP_CHECKOK(mp_copy(py, &qy));
222*5697Smcpowers MP_CHECKOK(mp_copy(n, &k));
223*5697Smcpowers /* if n < 0 then Q = -Q, k = -k */
224*5697Smcpowers if (mp_cmp_z(n) < 0) {
225*5697Smcpowers MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
226*5697Smcpowers MP_CHECKOK(mp_neg(&k, &k));
227*5697Smcpowers }
228*5697Smcpowers #ifdef ECL_DEBUG /* basic double and add method */
229*5697Smcpowers l = mpl_significant_bits(&k) - 1;
230*5697Smcpowers MP_CHECKOK(mp_copy(&qx, &sx));
231*5697Smcpowers MP_CHECKOK(mp_copy(&qy, &sy));
232*5697Smcpowers for (i = l - 1; i >= 0; i--) {
233*5697Smcpowers /* S = 2S */
234*5697Smcpowers MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
235*5697Smcpowers /* if k_i = 1, then S = S + Q */
236*5697Smcpowers if (mpl_get_bit(&k, i) != 0) {
237*5697Smcpowers MP_CHECKOK(group->
238*5697Smcpowers point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
239*5697Smcpowers }
240*5697Smcpowers }
241*5697Smcpowers #else /* double and add/subtract method from
242*5697Smcpowers * standard */
243*5697Smcpowers /* k3 = 3 * k */
244*5697Smcpowers MP_CHECKOK(mp_set_int(&k3, 3));
245*5697Smcpowers MP_CHECKOK(mp_mul(&k, &k3, &k3));
246*5697Smcpowers /* S = Q */
247*5697Smcpowers MP_CHECKOK(mp_copy(&qx, &sx));
248*5697Smcpowers MP_CHECKOK(mp_copy(&qy, &sy));
249*5697Smcpowers /* l = index of high order bit in binary representation of 3*k */
250*5697Smcpowers l = mpl_significant_bits(&k3) - 1;
251*5697Smcpowers /* for i = l-1 downto 1 */
252*5697Smcpowers for (i = l - 1; i >= 1; i--) {
253*5697Smcpowers /* S = 2S */
254*5697Smcpowers MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
255*5697Smcpowers b3 = MP_GET_BIT(&k3, i);
256*5697Smcpowers b1 = MP_GET_BIT(&k, i);
257*5697Smcpowers /* if k3_i = 1 and k_i = 0, then S = S + Q */
258*5697Smcpowers if ((b3 == 1) && (b1 == 0)) {
259*5697Smcpowers MP_CHECKOK(group->
260*5697Smcpowers point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
261*5697Smcpowers /* if k3_i = 0 and k_i = 1, then S = S - Q */
262*5697Smcpowers } else if ((b3 == 0) && (b1 == 1)) {
263*5697Smcpowers MP_CHECKOK(group->
264*5697Smcpowers point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
265*5697Smcpowers }
266*5697Smcpowers }
267*5697Smcpowers #endif
268*5697Smcpowers /* output S */
269*5697Smcpowers MP_CHECKOK(mp_copy(&sx, rx));
270*5697Smcpowers MP_CHECKOK(mp_copy(&sy, ry));
271*5697Smcpowers
272*5697Smcpowers CLEANUP:
273*5697Smcpowers mp_clear(&k);
274*5697Smcpowers mp_clear(&k3);
275*5697Smcpowers mp_clear(&qx);
276*5697Smcpowers mp_clear(&qy);
277*5697Smcpowers mp_clear(&sx);
278*5697Smcpowers mp_clear(&sy);
279*5697Smcpowers return res;
280*5697Smcpowers }
281*5697Smcpowers #endif
282*5697Smcpowers
283*5697Smcpowers /* Validates a point on a GF2m curve. */
284*5697Smcpowers mp_err
ec_GF2m_validate_point(const mp_int * px,const mp_int * py,const ECGroup * group)285*5697Smcpowers ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
286*5697Smcpowers {
287*5697Smcpowers mp_err res = MP_NO;
288*5697Smcpowers mp_int accl, accr, tmp, pxt, pyt;
289*5697Smcpowers
290*5697Smcpowers MP_DIGITS(&accl) = 0;
291*5697Smcpowers MP_DIGITS(&accr) = 0;
292*5697Smcpowers MP_DIGITS(&tmp) = 0;
293*5697Smcpowers MP_DIGITS(&pxt) = 0;
294*5697Smcpowers MP_DIGITS(&pyt) = 0;
295*5697Smcpowers MP_CHECKOK(mp_init(&accl, FLAG(px)));
296*5697Smcpowers MP_CHECKOK(mp_init(&accr, FLAG(px)));
297*5697Smcpowers MP_CHECKOK(mp_init(&tmp, FLAG(px)));
298*5697Smcpowers MP_CHECKOK(mp_init(&pxt, FLAG(px)));
299*5697Smcpowers MP_CHECKOK(mp_init(&pyt, FLAG(px)));
300*5697Smcpowers
301*5697Smcpowers /* 1: Verify that publicValue is not the point at infinity */
302*5697Smcpowers if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
303*5697Smcpowers res = MP_NO;
304*5697Smcpowers goto CLEANUP;
305*5697Smcpowers }
306*5697Smcpowers /* 2: Verify that the coordinates of publicValue are elements
307*5697Smcpowers * of the field.
308*5697Smcpowers */
309*5697Smcpowers if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
310*5697Smcpowers (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
311*5697Smcpowers res = MP_NO;
312*5697Smcpowers goto CLEANUP;
313*5697Smcpowers }
314*5697Smcpowers /* 3: Verify that publicValue is on the curve. */
315*5697Smcpowers if (group->meth->field_enc) {
316*5697Smcpowers group->meth->field_enc(px, &pxt, group->meth);
317*5697Smcpowers group->meth->field_enc(py, &pyt, group->meth);
318*5697Smcpowers } else {
319*5697Smcpowers mp_copy(px, &pxt);
320*5697Smcpowers mp_copy(py, &pyt);
321*5697Smcpowers }
322*5697Smcpowers /* left-hand side: y^2 + x*y */
323*5697Smcpowers MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
324*5697Smcpowers MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
325*5697Smcpowers MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
326*5697Smcpowers /* right-hand side: x^3 + a*x^2 + b */
327*5697Smcpowers MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
328*5697Smcpowers MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
329*5697Smcpowers MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
330*5697Smcpowers MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
331*5697Smcpowers MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
332*5697Smcpowers /* check LHS - RHS == 0 */
333*5697Smcpowers MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
334*5697Smcpowers if (mp_cmp_z(&accr) != 0) {
335*5697Smcpowers res = MP_NO;
336*5697Smcpowers goto CLEANUP;
337*5697Smcpowers }
338*5697Smcpowers /* 4: Verify that the order of the curve times the publicValue
339*5697Smcpowers * is the point at infinity.
340*5697Smcpowers */
341*5697Smcpowers MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
342*5697Smcpowers if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
343*5697Smcpowers res = MP_NO;
344*5697Smcpowers goto CLEANUP;
345*5697Smcpowers }
346*5697Smcpowers
347*5697Smcpowers res = MP_YES;
348*5697Smcpowers
349*5697Smcpowers CLEANUP:
350*5697Smcpowers mp_clear(&accl);
351*5697Smcpowers mp_clear(&accr);
352*5697Smcpowers mp_clear(&tmp);
353*5697Smcpowers mp_clear(&pxt);
354*5697Smcpowers mp_clear(&pyt);
355*5697Smcpowers return res;
356*5697Smcpowers }
357