1 /*
2  * ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the elliptic curve math library.
16  *
17  * The Initial Developer of the Original Code is
18  * Sun Microsystems, Inc.
19  * Portions created by the Initial Developer are Copyright (C) 2003
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 /*
39  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
40  * Use is subject to license terms.
41  *
42  * Sun elects to use this software under the MPL license.
43  */
44 
45 #include "mpi.h"
46 #include "mplogic.h"
47 #include "ecl.h"
48 #include "ecl-priv.h"
49 #ifndef _KERNEL
50 #include <stdlib.h>
51 #endif
52 
53 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k * P(x,
54  * y).  If x, y = NULL, then P is assumed to be the generator (base point)
55  * of the group of points on the elliptic curve. Input and output values
56  * are assumed to be NOT field-encoded. */
57 mp_err
ECPoint_mul(const ECGroup * group,const mp_int * k,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry)58 ECPoint_mul(const ECGroup *group, const mp_int *k, const mp_int *px,
59 			const mp_int *py, mp_int *rx, mp_int *ry)
60 {
61 	mp_err res = MP_OKAY;
62 	mp_int kt;
63 
64 	ARGCHK((k != NULL) && (group != NULL), MP_BADARG);
65 	MP_DIGITS(&kt) = 0;
66 
67 	/* want scalar to be less than or equal to group order */
68 	if (mp_cmp(k, &group->order) > 0) {
69 		MP_CHECKOK(mp_init(&kt, FLAG(k)));
70 		MP_CHECKOK(mp_mod(k, &group->order, &kt));
71 	} else {
72 		MP_SIGN(&kt) = MP_ZPOS;
73 		MP_USED(&kt) = MP_USED(k);
74 		MP_ALLOC(&kt) = MP_ALLOC(k);
75 		MP_DIGITS(&kt) = MP_DIGITS(k);
76 	}
77 
78 	if ((px == NULL) || (py == NULL)) {
79 		if (group->base_point_mul) {
80 			MP_CHECKOK(group->base_point_mul(&kt, rx, ry, group));
81 		} else {
82 			MP_CHECKOK(group->
83 					   point_mul(&kt, &group->genx, &group->geny, rx, ry,
84 								 group));
85 		}
86 	} else {
87 		if (group->meth->field_enc) {
88 			MP_CHECKOK(group->meth->field_enc(px, rx, group->meth));
89 			MP_CHECKOK(group->meth->field_enc(py, ry, group->meth));
90 			MP_CHECKOK(group->point_mul(&kt, rx, ry, rx, ry, group));
91 		} else {
92 			MP_CHECKOK(group->point_mul(&kt, px, py, rx, ry, group));
93 		}
94 	}
95 	if (group->meth->field_dec) {
96 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
97 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
98 	}
99 
100   CLEANUP:
101 	if (MP_DIGITS(&kt) != MP_DIGITS(k)) {
102 		mp_clear(&kt);
103 	}
104 	return res;
105 }
106 
107 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
108  * k2 * P(x, y), where G is the generator (base point) of the group of
109  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
110  * Input and output values are assumed to be NOT field-encoded. */
111 mp_err
ec_pts_mul_basic(const mp_int * k1,const mp_int * k2,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)112 ec_pts_mul_basic(const mp_int *k1, const mp_int *k2, const mp_int *px,
113 				 const mp_int *py, mp_int *rx, mp_int *ry,
114 				 const ECGroup *group)
115 {
116 	mp_err res = MP_OKAY;
117 	mp_int sx, sy;
118 
119 	ARGCHK(group != NULL, MP_BADARG);
120 	ARGCHK(!((k1 == NULL)
121 			 && ((k2 == NULL) || (px == NULL)
122 				 || (py == NULL))), MP_BADARG);
123 
124 	/* if some arguments are not defined used ECPoint_mul */
125 	if (k1 == NULL) {
126 		return ECPoint_mul(group, k2, px, py, rx, ry);
127 	} else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
128 		return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
129 	}
130 
131 	MP_DIGITS(&sx) = 0;
132 	MP_DIGITS(&sy) = 0;
133 	MP_CHECKOK(mp_init(&sx, FLAG(k1)));
134 	MP_CHECKOK(mp_init(&sy, FLAG(k1)));
135 
136 	MP_CHECKOK(ECPoint_mul(group, k1, NULL, NULL, &sx, &sy));
137 	MP_CHECKOK(ECPoint_mul(group, k2, px, py, rx, ry));
138 
139 	if (group->meth->field_enc) {
140 		MP_CHECKOK(group->meth->field_enc(&sx, &sx, group->meth));
141 		MP_CHECKOK(group->meth->field_enc(&sy, &sy, group->meth));
142 		MP_CHECKOK(group->meth->field_enc(rx, rx, group->meth));
143 		MP_CHECKOK(group->meth->field_enc(ry, ry, group->meth));
144 	}
145 
146 	MP_CHECKOK(group->point_add(&sx, &sy, rx, ry, rx, ry, group));
147 
148 	if (group->meth->field_dec) {
149 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
150 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
151 	}
152 
153   CLEANUP:
154 	mp_clear(&sx);
155 	mp_clear(&sy);
156 	return res;
157 }
158 
159 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
160  * k2 * P(x, y), where G is the generator (base point) of the group of
161  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
162  * Input and output values are assumed to be NOT field-encoded. Uses
163  * algorithm 15 (simultaneous multiple point multiplication) from Brown,
164  * Hankerson, Lopez, Menezes. Software Implementation of the NIST
165  * Elliptic Curves over Prime Fields. */
166 mp_err
ec_pts_mul_simul_w2(const mp_int * k1,const mp_int * k2,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)167 ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2, const mp_int *px,
168 					const mp_int *py, mp_int *rx, mp_int *ry,
169 					const ECGroup *group)
170 {
171 	mp_err res = MP_OKAY;
172 	mp_int precomp[4][4][2];
173 	const mp_int *a, *b;
174 	int i, j;
175 	int ai, bi, d;
176 
177 	ARGCHK(group != NULL, MP_BADARG);
178 	ARGCHK(!((k1 == NULL)
179 			 && ((k2 == NULL) || (px == NULL)
180 				 || (py == NULL))), MP_BADARG);
181 
182 	/* if some arguments are not defined used ECPoint_mul */
183 	if (k1 == NULL) {
184 		return ECPoint_mul(group, k2, px, py, rx, ry);
185 	} else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
186 		return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
187 	}
188 
189 	/* initialize precomputation table */
190 	for (i = 0; i < 4; i++) {
191 		for (j = 0; j < 4; j++) {
192 			MP_DIGITS(&precomp[i][j][0]) = 0;
193 			MP_DIGITS(&precomp[i][j][1]) = 0;
194 		}
195 	}
196 	for (i = 0; i < 4; i++) {
197 		for (j = 0; j < 4; j++) {
198 			 MP_CHECKOK( mp_init_size(&precomp[i][j][0],
199 					 ECL_MAX_FIELD_SIZE_DIGITS, FLAG(k1)) );
200 			 MP_CHECKOK( mp_init_size(&precomp[i][j][1],
201 					 ECL_MAX_FIELD_SIZE_DIGITS, FLAG(k1)) );
202 		}
203 	}
204 
205 	/* fill precomputation table */
206 	/* assign {k1, k2} = {a, b} such that len(a) >= len(b) */
207 	if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) {
208 		a = k2;
209 		b = k1;
210 		if (group->meth->field_enc) {
211 			MP_CHECKOK(group->meth->
212 					   field_enc(px, &precomp[1][0][0], group->meth));
213 			MP_CHECKOK(group->meth->
214 					   field_enc(py, &precomp[1][0][1], group->meth));
215 		} else {
216 			MP_CHECKOK(mp_copy(px, &precomp[1][0][0]));
217 			MP_CHECKOK(mp_copy(py, &precomp[1][0][1]));
218 		}
219 		MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0]));
220 		MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1]));
221 	} else {
222 		a = k1;
223 		b = k2;
224 		MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0]));
225 		MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1]));
226 		if (group->meth->field_enc) {
227 			MP_CHECKOK(group->meth->
228 					   field_enc(px, &precomp[0][1][0], group->meth));
229 			MP_CHECKOK(group->meth->
230 					   field_enc(py, &precomp[0][1][1], group->meth));
231 		} else {
232 			MP_CHECKOK(mp_copy(px, &precomp[0][1][0]));
233 			MP_CHECKOK(mp_copy(py, &precomp[0][1][1]));
234 		}
235 	}
236 	/* precompute [*][0][*] */
237 	mp_zero(&precomp[0][0][0]);
238 	mp_zero(&precomp[0][0][1]);
239 	MP_CHECKOK(group->
240 			   point_dbl(&precomp[1][0][0], &precomp[1][0][1],
241 						 &precomp[2][0][0], &precomp[2][0][1], group));
242 	MP_CHECKOK(group->
243 			   point_add(&precomp[1][0][0], &precomp[1][0][1],
244 						 &precomp[2][0][0], &precomp[2][0][1],
245 						 &precomp[3][0][0], &precomp[3][0][1], group));
246 	/* precompute [*][1][*] */
247 	for (i = 1; i < 4; i++) {
248 		MP_CHECKOK(group->
249 				   point_add(&precomp[0][1][0], &precomp[0][1][1],
250 							 &precomp[i][0][0], &precomp[i][0][1],
251 							 &precomp[i][1][0], &precomp[i][1][1], group));
252 	}
253 	/* precompute [*][2][*] */
254 	MP_CHECKOK(group->
255 			   point_dbl(&precomp[0][1][0], &precomp[0][1][1],
256 						 &precomp[0][2][0], &precomp[0][2][1], group));
257 	for (i = 1; i < 4; i++) {
258 		MP_CHECKOK(group->
259 				   point_add(&precomp[0][2][0], &precomp[0][2][1],
260 							 &precomp[i][0][0], &precomp[i][0][1],
261 							 &precomp[i][2][0], &precomp[i][2][1], group));
262 	}
263 	/* precompute [*][3][*] */
264 	MP_CHECKOK(group->
265 			   point_add(&precomp[0][1][0], &precomp[0][1][1],
266 						 &precomp[0][2][0], &precomp[0][2][1],
267 						 &precomp[0][3][0], &precomp[0][3][1], group));
268 	for (i = 1; i < 4; i++) {
269 		MP_CHECKOK(group->
270 				   point_add(&precomp[0][3][0], &precomp[0][3][1],
271 							 &precomp[i][0][0], &precomp[i][0][1],
272 							 &precomp[i][3][0], &precomp[i][3][1], group));
273 	}
274 
275 	d = (mpl_significant_bits(a) + 1) / 2;
276 
277 	/* R = inf */
278 	mp_zero(rx);
279 	mp_zero(ry);
280 
281 	for (i = d - 1; i >= 0; i--) {
282 		ai = MP_GET_BIT(a, 2 * i + 1);
283 		ai <<= 1;
284 		ai |= MP_GET_BIT(a, 2 * i);
285 		bi = MP_GET_BIT(b, 2 * i + 1);
286 		bi <<= 1;
287 		bi |= MP_GET_BIT(b, 2 * i);
288 		/* R = 2^2 * R */
289 		MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group));
290 		MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group));
291 		/* R = R + (ai * A + bi * B) */
292 		MP_CHECKOK(group->
293 				   point_add(rx, ry, &precomp[ai][bi][0],
294 							 &precomp[ai][bi][1], rx, ry, group));
295 	}
296 
297 	if (group->meth->field_dec) {
298 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
299 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
300 	}
301 
302   CLEANUP:
303 	for (i = 0; i < 4; i++) {
304 		for (j = 0; j < 4; j++) {
305 			mp_clear(&precomp[i][j][0]);
306 			mp_clear(&precomp[i][j][1]);
307 		}
308 	}
309 	return res;
310 }
311 
312 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
313  * k2 * P(x, y), where G is the generator (base point) of the group of
314  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
315  * Input and output values are assumed to be NOT field-encoded. */
316 mp_err
ECPoints_mul(const ECGroup * group,const mp_int * k1,const mp_int * k2,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry)317 ECPoints_mul(const ECGroup *group, const mp_int *k1, const mp_int *k2,
318 			 const mp_int *px, const mp_int *py, mp_int *rx, mp_int *ry)
319 {
320 	mp_err res = MP_OKAY;
321 	mp_int k1t, k2t;
322 	const mp_int *k1p, *k2p;
323 
324 	MP_DIGITS(&k1t) = 0;
325 	MP_DIGITS(&k2t) = 0;
326 
327 	ARGCHK(group != NULL, MP_BADARG);
328 
329 	/* want scalar to be less than or equal to group order */
330 	if (k1 != NULL) {
331 		if (mp_cmp(k1, &group->order) >= 0) {
332 			MP_CHECKOK(mp_init(&k1t, FLAG(k1)));
333 			MP_CHECKOK(mp_mod(k1, &group->order, &k1t));
334 			k1p = &k1t;
335 		} else {
336 			k1p = k1;
337 		}
338 	} else {
339 		k1p = k1;
340 	}
341 	if (k2 != NULL) {
342 		if (mp_cmp(k2, &group->order) >= 0) {
343 			MP_CHECKOK(mp_init(&k2t, FLAG(k2)));
344 			MP_CHECKOK(mp_mod(k2, &group->order, &k2t));
345 			k2p = &k2t;
346 		} else {
347 			k2p = k2;
348 		}
349 	} else {
350 		k2p = k2;
351 	}
352 
353 	/* if points_mul is defined, then use it */
354 	if (group->points_mul) {
355 		res = group->points_mul(k1p, k2p, px, py, rx, ry, group);
356 	} else {
357 		res = ec_pts_mul_simul_w2(k1p, k2p, px, py, rx, ry, group);
358 	}
359 
360   CLEANUP:
361 	mp_clear(&k1t);
362 	mp_clear(&k2t);
363 	return res;
364 }
365