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