xref: /illumos-gate/usr/src/common/crypto/ecc/ecl_mult.c (revision 50981ffc7e4c5048d14890df805afee6ec113991)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
46 
47 #include "mpi.h"
48 #include "mplogic.h"
49 #include "ecl.h"
50 #include "ecl-priv.h"
51 #ifndef _KERNEL
52 #include <stdlib.h>
53 #endif
54 
55 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k * P(x,
56  * y).  If x, y = NULL, then P is assumed to be the generator (base point)
57  * of the group of points on the elliptic curve. Input and output values
58  * are assumed to be NOT field-encoded. */
59 mp_err
60 ECPoint_mul(const ECGroup *group, const mp_int *k, const mp_int *px,
61 			const mp_int *py, mp_int *rx, mp_int *ry)
62 {
63 	mp_err res = MP_OKAY;
64 	mp_int kt;
65 
66 	ARGCHK((k != NULL) && (group != NULL), MP_BADARG);
67 	MP_DIGITS(&kt) = 0;
68 
69 	/* want scalar to be less than or equal to group order */
70 	if (mp_cmp(k, &group->order) > 0) {
71 		MP_CHECKOK(mp_init(&kt, FLAG(k)));
72 		MP_CHECKOK(mp_mod(k, &group->order, &kt));
73 	} else {
74 		MP_SIGN(&kt) = MP_ZPOS;
75 		MP_USED(&kt) = MP_USED(k);
76 		MP_ALLOC(&kt) = MP_ALLOC(k);
77 		MP_DIGITS(&kt) = MP_DIGITS(k);
78 	}
79 
80 	if ((px == NULL) || (py == NULL)) {
81 		if (group->base_point_mul) {
82 			MP_CHECKOK(group->base_point_mul(&kt, rx, ry, group));
83 		} else {
84 			MP_CHECKOK(group->
85 					   point_mul(&kt, &group->genx, &group->geny, rx, ry,
86 								 group));
87 		}
88 	} else {
89 		if (group->meth->field_enc) {
90 			MP_CHECKOK(group->meth->field_enc(px, rx, group->meth));
91 			MP_CHECKOK(group->meth->field_enc(py, ry, group->meth));
92 			MP_CHECKOK(group->point_mul(&kt, rx, ry, rx, ry, group));
93 		} else {
94 			MP_CHECKOK(group->point_mul(&kt, px, py, rx, ry, group));
95 		}
96 	}
97 	if (group->meth->field_dec) {
98 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
99 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
100 	}
101 
102   CLEANUP:
103 	if (MP_DIGITS(&kt) != MP_DIGITS(k)) {
104 		mp_clear(&kt);
105 	}
106 	return res;
107 }
108 
109 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
110  * k2 * P(x, y), where G is the generator (base point) of the group of
111  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
112  * Input and output values are assumed to be NOT field-encoded. */
113 mp_err
114 ec_pts_mul_basic(const mp_int *k1, const mp_int *k2, const mp_int *px,
115 				 const mp_int *py, mp_int *rx, mp_int *ry,
116 				 const ECGroup *group)
117 {
118 	mp_err res = MP_OKAY;
119 	mp_int sx, sy;
120 
121 	ARGCHK(group != NULL, MP_BADARG);
122 	ARGCHK(!((k1 == NULL)
123 			 && ((k2 == NULL) || (px == NULL)
124 				 || (py == NULL))), MP_BADARG);
125 
126 	/* if some arguments are not defined used ECPoint_mul */
127 	if (k1 == NULL) {
128 		return ECPoint_mul(group, k2, px, py, rx, ry);
129 	} else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
130 		return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
131 	}
132 
133 	MP_DIGITS(&sx) = 0;
134 	MP_DIGITS(&sy) = 0;
135 	MP_CHECKOK(mp_init(&sx, FLAG(k1)));
136 	MP_CHECKOK(mp_init(&sy, FLAG(k1)));
137 
138 	MP_CHECKOK(ECPoint_mul(group, k1, NULL, NULL, &sx, &sy));
139 	MP_CHECKOK(ECPoint_mul(group, k2, px, py, rx, ry));
140 
141 	if (group->meth->field_enc) {
142 		MP_CHECKOK(group->meth->field_enc(&sx, &sx, group->meth));
143 		MP_CHECKOK(group->meth->field_enc(&sy, &sy, group->meth));
144 		MP_CHECKOK(group->meth->field_enc(rx, rx, group->meth));
145 		MP_CHECKOK(group->meth->field_enc(ry, ry, group->meth));
146 	}
147 
148 	MP_CHECKOK(group->point_add(&sx, &sy, rx, ry, rx, ry, group));
149 
150 	if (group->meth->field_dec) {
151 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
152 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
153 	}
154 
155   CLEANUP:
156 	mp_clear(&sx);
157 	mp_clear(&sy);
158 	return res;
159 }
160 
161 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
162  * k2 * P(x, y), where G is the generator (base point) of the group of
163  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
164  * Input and output values are assumed to be NOT field-encoded. Uses
165  * algorithm 15 (simultaneous multiple point multiplication) from Brown,
166  * Hankerson, Lopez, Menezes. Software Implementation of the NIST
167  * Elliptic Curves over Prime Fields. */
168 mp_err
169 ec_pts_mul_simul_w2(const mp_int *k1, const mp_int *k2, const mp_int *px,
170 					const mp_int *py, mp_int *rx, mp_int *ry,
171 					const ECGroup *group)
172 {
173 	mp_err res = MP_OKAY;
174 	mp_int precomp[4][4][2];
175 	const mp_int *a, *b;
176 	int i, j;
177 	int ai, bi, d;
178 
179 	ARGCHK(group != NULL, MP_BADARG);
180 	ARGCHK(!((k1 == NULL)
181 			 && ((k2 == NULL) || (px == NULL)
182 				 || (py == NULL))), MP_BADARG);
183 
184 	/* if some arguments are not defined used ECPoint_mul */
185 	if (k1 == NULL) {
186 		return ECPoint_mul(group, k2, px, py, rx, ry);
187 	} else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
188 		return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
189 	}
190 
191 	/* initialize precomputation table */
192 	for (i = 0; i < 4; i++) {
193 		for (j = 0; j < 4; j++) {
194 			MP_DIGITS(&precomp[i][j][0]) = 0;
195 			MP_DIGITS(&precomp[i][j][1]) = 0;
196 		}
197 	}
198 	for (i = 0; i < 4; i++) {
199 		for (j = 0; j < 4; j++) {
200 			 MP_CHECKOK( mp_init_size(&precomp[i][j][0],
201 					 ECL_MAX_FIELD_SIZE_DIGITS, FLAG(k1)) );
202 			 MP_CHECKOK( mp_init_size(&precomp[i][j][1],
203 					 ECL_MAX_FIELD_SIZE_DIGITS, FLAG(k1)) );
204 		}
205 	}
206 
207 	/* fill precomputation table */
208 	/* assign {k1, k2} = {a, b} such that len(a) >= len(b) */
209 	if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) {
210 		a = k2;
211 		b = k1;
212 		if (group->meth->field_enc) {
213 			MP_CHECKOK(group->meth->
214 					   field_enc(px, &precomp[1][0][0], group->meth));
215 			MP_CHECKOK(group->meth->
216 					   field_enc(py, &precomp[1][0][1], group->meth));
217 		} else {
218 			MP_CHECKOK(mp_copy(px, &precomp[1][0][0]));
219 			MP_CHECKOK(mp_copy(py, &precomp[1][0][1]));
220 		}
221 		MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0]));
222 		MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1]));
223 	} else {
224 		a = k1;
225 		b = k2;
226 		MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0]));
227 		MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1]));
228 		if (group->meth->field_enc) {
229 			MP_CHECKOK(group->meth->
230 					   field_enc(px, &precomp[0][1][0], group->meth));
231 			MP_CHECKOK(group->meth->
232 					   field_enc(py, &precomp[0][1][1], group->meth));
233 		} else {
234 			MP_CHECKOK(mp_copy(px, &precomp[0][1][0]));
235 			MP_CHECKOK(mp_copy(py, &precomp[0][1][1]));
236 		}
237 	}
238 	/* precompute [*][0][*] */
239 	mp_zero(&precomp[0][0][0]);
240 	mp_zero(&precomp[0][0][1]);
241 	MP_CHECKOK(group->
242 			   point_dbl(&precomp[1][0][0], &precomp[1][0][1],
243 						 &precomp[2][0][0], &precomp[2][0][1], group));
244 	MP_CHECKOK(group->
245 			   point_add(&precomp[1][0][0], &precomp[1][0][1],
246 						 &precomp[2][0][0], &precomp[2][0][1],
247 						 &precomp[3][0][0], &precomp[3][0][1], group));
248 	/* precompute [*][1][*] */
249 	for (i = 1; i < 4; i++) {
250 		MP_CHECKOK(group->
251 				   point_add(&precomp[0][1][0], &precomp[0][1][1],
252 							 &precomp[i][0][0], &precomp[i][0][1],
253 							 &precomp[i][1][0], &precomp[i][1][1], group));
254 	}
255 	/* precompute [*][2][*] */
256 	MP_CHECKOK(group->
257 			   point_dbl(&precomp[0][1][0], &precomp[0][1][1],
258 						 &precomp[0][2][0], &precomp[0][2][1], group));
259 	for (i = 1; i < 4; i++) {
260 		MP_CHECKOK(group->
261 				   point_add(&precomp[0][2][0], &precomp[0][2][1],
262 							 &precomp[i][0][0], &precomp[i][0][1],
263 							 &precomp[i][2][0], &precomp[i][2][1], group));
264 	}
265 	/* precompute [*][3][*] */
266 	MP_CHECKOK(group->
267 			   point_add(&precomp[0][1][0], &precomp[0][1][1],
268 						 &precomp[0][2][0], &precomp[0][2][1],
269 						 &precomp[0][3][0], &precomp[0][3][1], group));
270 	for (i = 1; i < 4; i++) {
271 		MP_CHECKOK(group->
272 				   point_add(&precomp[0][3][0], &precomp[0][3][1],
273 							 &precomp[i][0][0], &precomp[i][0][1],
274 							 &precomp[i][3][0], &precomp[i][3][1], group));
275 	}
276 
277 	d = (mpl_significant_bits(a) + 1) / 2;
278 
279 	/* R = inf */
280 	mp_zero(rx);
281 	mp_zero(ry);
282 
283 	for (i = d - 1; i >= 0; i--) {
284 		ai = MP_GET_BIT(a, 2 * i + 1);
285 		ai <<= 1;
286 		ai |= MP_GET_BIT(a, 2 * i);
287 		bi = MP_GET_BIT(b, 2 * i + 1);
288 		bi <<= 1;
289 		bi |= MP_GET_BIT(b, 2 * i);
290 		/* R = 2^2 * R */
291 		MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group));
292 		MP_CHECKOK(group->point_dbl(rx, ry, rx, ry, group));
293 		/* R = R + (ai * A + bi * B) */
294 		MP_CHECKOK(group->
295 				   point_add(rx, ry, &precomp[ai][bi][0],
296 							 &precomp[ai][bi][1], rx, ry, group));
297 	}
298 
299 	if (group->meth->field_dec) {
300 		MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
301 		MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
302 	}
303 
304   CLEANUP:
305 	for (i = 0; i < 4; i++) {
306 		for (j = 0; j < 4; j++) {
307 			mp_clear(&precomp[i][j][0]);
308 			mp_clear(&precomp[i][j][1]);
309 		}
310 	}
311 	return res;
312 }
313 
314 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
315  * k2 * P(x, y), where G is the generator (base point) of the group of
316  * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
317  * Input and output values are assumed to be NOT field-encoded. */
318 mp_err
319 ECPoints_mul(const ECGroup *group, const mp_int *k1, const mp_int *k2,
320 			 const mp_int *px, const mp_int *py, mp_int *rx, mp_int *ry)
321 {
322 	mp_err res = MP_OKAY;
323 	mp_int k1t, k2t;
324 	const mp_int *k1p, *k2p;
325 
326 	MP_DIGITS(&k1t) = 0;
327 	MP_DIGITS(&k2t) = 0;
328 
329 	ARGCHK(group != NULL, MP_BADARG);
330 
331 	/* want scalar to be less than or equal to group order */
332 	if (k1 != NULL) {
333 		if (mp_cmp(k1, &group->order) >= 0) {
334 			MP_CHECKOK(mp_init(&k1t, FLAG(k1)));
335 			MP_CHECKOK(mp_mod(k1, &group->order, &k1t));
336 			k1p = &k1t;
337 		} else {
338 			k1p = k1;
339 		}
340 	} else {
341 		k1p = k1;
342 	}
343 	if (k2 != NULL) {
344 		if (mp_cmp(k2, &group->order) >= 0) {
345 			MP_CHECKOK(mp_init(&k2t, FLAG(k2)));
346 			MP_CHECKOK(mp_mod(k2, &group->order, &k2t));
347 			k2p = &k2t;
348 		} else {
349 			k2p = k2;
350 		}
351 	} else {
352 		k2p = k2;
353 	}
354 
355 	/* if points_mul is defined, then use it */
356 	if (group->points_mul) {
357 		res = group->points_mul(k1p, k2p, px, py, rx, ry, group);
358 	} else {
359 		res = ec_pts_mul_simul_w2(k1p, k2p, px, py, rx, ry, group);
360 	}
361 
362   CLEANUP:
363 	mp_clear(&k1t);
364 	mp_clear(&k2t);
365 	return res;
366 }
367