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