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