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 for prime field curves.
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  *   Stephen Fung <fungstep@hotmail.com>, Sun Microsystems Laboratories
35  *
36  * Last Modified Date from the Original Code: May 2017
37  *********************************************************************** */
38 
39 #include "ecp.h"
40 #include "ecl-priv.h"
41 #include "mplogic.h"
42 #ifndef _KERNEL
43 #include <stdlib.h>
44 #endif
45 
46 #define MAX_SCRATCH 6
47 
48 /* Computes R = 2P.  Elliptic curve points P and R can be identical.  Uses
49  * Modified Jacobian coordinates.
50  *
51  * Assumes input is already field-encoded using field_enc, and returns
52  * output that is still field-encoded.
53  *
54  */
55 mp_err
ec_GFp_pt_dbl_jm(const mp_int * px,const mp_int * py,const mp_int * pz,const mp_int * paz4,mp_int * rx,mp_int * ry,mp_int * rz,mp_int * raz4,mp_int scratch[],const ECGroup * group)56 ec_GFp_pt_dbl_jm(const mp_int *px, const mp_int *py, const mp_int *pz,
57                                  const mp_int *paz4, mp_int *rx, mp_int *ry, mp_int *rz,
58                                  mp_int *raz4, mp_int scratch[], const ECGroup *group)
59 {
60         mp_err res = MP_OKAY;
61         mp_int *t0, *t1, *M, *S;
62 
63         t0 = &scratch[0];
64         t1 = &scratch[1];
65         M = &scratch[2];
66         S = &scratch[3];
67 
68 #if MAX_SCRATCH < 4
69 #error "Scratch array defined too small "
70 #endif
71 
72         /* Check for point at infinity */
73         if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
74                 /* Set r = pt at infinity by setting rz = 0 */
75 
76                 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
77                 goto CLEANUP;
78         }
79 
80         /* M = 3 (px^2) + a*(pz^4) */
81         MP_CHECKOK(group->meth->field_sqr(px, t0, group->meth));
82         MP_CHECKOK(group->meth->field_add(t0, t0, M, group->meth));
83         MP_CHECKOK(group->meth->field_add(t0, M, t0, group->meth));
84         MP_CHECKOK(group->meth->field_add(t0, paz4, M, group->meth));
85 
86         /* rz = 2 * py * pz */
87         MP_CHECKOK(group->meth->field_mul(py, pz, S, group->meth));
88         MP_CHECKOK(group->meth->field_add(S, S, rz, group->meth));
89 
90         /* t0 = 2y^2 , t1 = 8y^4 */
91         MP_CHECKOK(group->meth->field_sqr(py, t0, group->meth));
92         MP_CHECKOK(group->meth->field_add(t0, t0, t0, group->meth));
93         MP_CHECKOK(group->meth->field_sqr(t0, t1, group->meth));
94         MP_CHECKOK(group->meth->field_add(t1, t1, t1, group->meth));
95 
96         /* S = 4 * px * py^2 = 2 * px * t0 */
97         MP_CHECKOK(group->meth->field_mul(px, t0, S, group->meth));
98         MP_CHECKOK(group->meth->field_add(S, S, S, group->meth));
99 
100 
101         /* rx = M^2 - 2S */
102         MP_CHECKOK(group->meth->field_sqr(M, rx, group->meth));
103         MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth));
104         MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth));
105 
106         /* ry = M * (S - rx) - t1 */
107         MP_CHECKOK(group->meth->field_sub(S, rx, S, group->meth));
108         MP_CHECKOK(group->meth->field_mul(S, M, ry, group->meth));
109         MP_CHECKOK(group->meth->field_sub(ry, t1, ry, group->meth));
110 
111         /* ra*z^4 = 2*t1*(apz4) */
112         MP_CHECKOK(group->meth->field_mul(paz4, t1, raz4, group->meth));
113         MP_CHECKOK(group->meth->field_add(raz4, raz4, raz4, group->meth));
114 
115 
116   CLEANUP:
117         return res;
118 }
119 
120 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
121  * (qx, qy, 1).  Elliptic curve points P, Q, and R can all be identical.
122  * Uses mixed Modified_Jacobian-affine coordinates. Assumes input is
123  * already field-encoded using field_enc, and returns output that is still
124  * field-encoded. */
125 mp_err
ec_GFp_pt_add_jm_aff(const mp_int * px,const mp_int * py,const mp_int * pz,const mp_int * paz4,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,mp_int * rz,mp_int * raz4,mp_int scratch[],const ECGroup * group)126 ec_GFp_pt_add_jm_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
127                                          const mp_int *paz4, const mp_int *qx,
128                                          const mp_int *qy, mp_int *rx, mp_int *ry, mp_int *rz,
129                                          mp_int *raz4, mp_int scratch[], const ECGroup *group)
130 {
131         mp_err res = MP_OKAY;
132         mp_int *A, *B, *C, *D, *C2, *C3;
133 
134         A = &scratch[0];
135         B = &scratch[1];
136         C = &scratch[2];
137         D = &scratch[3];
138         C2 = &scratch[4];
139         C3 = &scratch[5];
140 
141 #if MAX_SCRATCH < 6
142 #error "Scratch array defined too small "
143 #endif
144 
145         /* If either P or Q is the point at infinity, then return the other
146          * point */
147         if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
148                 MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
149                 MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
150                 MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
151                 MP_CHECKOK(group->meth->
152                                    field_mul(raz4, &group->curvea, raz4, group->meth));
153                 goto CLEANUP;
154         }
155         if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
156                 MP_CHECKOK(mp_copy(px, rx));
157                 MP_CHECKOK(mp_copy(py, ry));
158                 MP_CHECKOK(mp_copy(pz, rz));
159                 MP_CHECKOK(mp_copy(paz4, raz4));
160                 goto CLEANUP;
161         }
162 
163         /* A = qx * pz^2, B = qy * pz^3 */
164         MP_CHECKOK(group->meth->field_sqr(pz, A, group->meth));
165         MP_CHECKOK(group->meth->field_mul(A, pz, B, group->meth));
166         MP_CHECKOK(group->meth->field_mul(A, qx, A, group->meth));
167         MP_CHECKOK(group->meth->field_mul(B, qy, B, group->meth));
168 
169         /*
170          * Additional checks for point equality and point at infinity
171          */
172         if (mp_cmp(px, A) == 0 && mp_cmp(py, B) == 0) {
173             /* POINT_DOUBLE(P) */
174             MP_CHECKOK(ec_GFp_pt_dbl_jm(px, py, pz, paz4, rx, ry, rz, raz4,
175                                         scratch, group));
176             goto CLEANUP;
177         }
178 
179         /* C = A - px, D = B - py */
180         MP_CHECKOK(group->meth->field_sub(A, px, C, group->meth));
181         MP_CHECKOK(group->meth->field_sub(B, py, D, group->meth));
182 
183         /* C2 = C^2, C3 = C^3 */
184         MP_CHECKOK(group->meth->field_sqr(C, C2, group->meth));
185         MP_CHECKOK(group->meth->field_mul(C, C2, C3, group->meth));
186 
187         /* rz = pz * C */
188         MP_CHECKOK(group->meth->field_mul(pz, C, rz, group->meth));
189 
190         /* C = px * C^2 */
191         MP_CHECKOK(group->meth->field_mul(px, C2, C, group->meth));
192         /* A = D^2 */
193         MP_CHECKOK(group->meth->field_sqr(D, A, group->meth));
194 
195         /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
196         MP_CHECKOK(group->meth->field_add(C, C, rx, group->meth));
197         MP_CHECKOK(group->meth->field_add(C3, rx, rx, group->meth));
198         MP_CHECKOK(group->meth->field_sub(A, rx, rx, group->meth));
199 
200         /* C3 = py * C^3 */
201         MP_CHECKOK(group->meth->field_mul(py, C3, C3, group->meth));
202 
203         /* ry = D * (px * C^2 - rx) - py * C^3 */
204         MP_CHECKOK(group->meth->field_sub(C, rx, ry, group->meth));
205         MP_CHECKOK(group->meth->field_mul(D, ry, ry, group->meth));
206         MP_CHECKOK(group->meth->field_sub(ry, C3, ry, group->meth));
207 
208         /* raz4 = a * rz^4 */
209         MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
210         MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
211         MP_CHECKOK(group->meth->
212                            field_mul(raz4, &group->curvea, raz4, group->meth));
213 CLEANUP:
214         return res;
215 }
216 
217 /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic
218  * curve points P and R can be identical. Uses mixed Modified-Jacobian
219  * co-ordinates for doubling and Chudnovsky Jacobian coordinates for
220  * additions. Assumes input is already field-encoded using field_enc, and
221  * returns output that is still field-encoded. Uses 5-bit window NAF
222  * method (algorithm 11) for scalar-point multiplication from Brown,
223  * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic
224  * Curves Over Prime Fields. */
225 mp_err
ec_GFp_pt_mul_jm_wNAF(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group,int timing)226 ec_GFp_pt_mul_jm_wNAF(const mp_int *n, const mp_int *px, const mp_int *py,
227                                           mp_int *rx, mp_int *ry, const ECGroup *group,
228                                           int timing)
229 {
230         mp_err res = MP_OKAY;
231         mp_int precomp[16][2], rz, tpx, tpy, tpz;
232         mp_int raz4, tpaz4;
233         mp_int scratch[MAX_SCRATCH];
234         signed char *naf = NULL;
235         int i, orderBitSize;
236         int numDoubles, numAdds, extraDoubles, extraAdds;
237 
238         MP_DIGITS(&rz) = 0;
239         MP_DIGITS(&raz4) = 0;
240         MP_DIGITS(&tpx) = 0;
241         MP_DIGITS(&tpy) = 0;
242         MP_DIGITS(&tpz) = 0;
243         MP_DIGITS(&tpaz4) = 0;
244         for (i = 0; i < 16; i++) {
245                 MP_DIGITS(&precomp[i][0]) = 0;
246                 MP_DIGITS(&precomp[i][1]) = 0;
247         }
248         for (i = 0; i < MAX_SCRATCH; i++) {
249                 MP_DIGITS(&scratch[i]) = 0;
250         }
251 
252         ARGCHK(group != NULL, MP_BADARG);
253         ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
254 
255         /* initialize precomputation table */
256         MP_CHECKOK(mp_init(&tpx, FLAG(n)));
257         MP_CHECKOK(mp_init(&tpy, FLAG(n)));
258         MP_CHECKOK(mp_init(&tpz, FLAG(n)));
259         MP_CHECKOK(mp_init(&tpaz4, FLAG(n)));
260         MP_CHECKOK(mp_init(&rz, FLAG(n)));
261         MP_CHECKOK(mp_init(&raz4, FLAG(n)));
262 
263         for (i = 0; i < 16; i++) {
264                 MP_CHECKOK(mp_init(&precomp[i][0], FLAG(n)));
265                 MP_CHECKOK(mp_init(&precomp[i][1], FLAG(n)));
266         }
267         for (i = 0; i < MAX_SCRATCH; i++) {
268                 MP_CHECKOK(mp_init(&scratch[i], FLAG(n)));
269         }
270 
271         /* Set out[8] = P */
272         MP_CHECKOK(mp_copy(px, &precomp[8][0]));
273         MP_CHECKOK(mp_copy(py, &precomp[8][1]));
274 
275         /* Set (tpx, tpy) = 2P */
276         MP_CHECKOK(group->
277                            point_dbl(&precomp[8][0], &precomp[8][1], &tpx, &tpy,
278                                                  group));
279 
280         /* Set 3P, 5P, ..., 15P */
281         for (i = 8; i < 15; i++) {
282                 MP_CHECKOK(group->
283                                    point_add(&precomp[i][0], &precomp[i][1], &tpx, &tpy,
284                                                          &precomp[i + 1][0], &precomp[i + 1][1],
285                                                          group));
286         }
287 
288         /* Set -15P, -13P, ..., -P */
289         for (i = 0; i < 8; i++) {
290                 MP_CHECKOK(mp_copy(&precomp[15 - i][0], &precomp[i][0]));
291                 MP_CHECKOK(group->meth->
292                                    field_neg(&precomp[15 - i][1], &precomp[i][1],
293                                                          group->meth));
294         }
295 
296         /* R = inf */
297         MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
298 
299         orderBitSize = mpl_significant_bits(&group->order);
300 
301         /* Allocate memory for NAF */
302 #ifdef _KERNEL
303         naf = (signed char *) kmem_alloc((orderBitSize + 1), FLAG(n));
304 #else
305         naf = (signed char *) malloc(sizeof(signed char) * (orderBitSize + 1));
306         if (naf == NULL) {
307                 res = MP_MEM;
308                 goto CLEANUP;
309         }
310 #endif
311 
312         /* Compute 5NAF */
313         ec_compute_wNAF(naf, orderBitSize, n, 5);
314 
315         numAdds = 0;
316         numDoubles = orderBitSize;
317         /* wNAF method */
318         for (i = orderBitSize; i >= 0; i--) {
319 
320                 if (ec_GFp_pt_is_inf_jac(rx, ry, &rz) == MP_YES) {
321                   numDoubles--;
322                 }
323 
324                 /* R = 2R */
325                 ec_GFp_pt_dbl_jm(rx, ry, &rz, &raz4, rx, ry, &rz,
326                                              &raz4, scratch, group);
327 
328                 if (naf[i] != 0) {
329                         ec_GFp_pt_add_jm_aff(rx, ry, &rz, &raz4,
330                                                                  &precomp[(naf[i] + 15) / 2][0],
331                                                                  &precomp[(naf[i] + 15) / 2][1], rx, ry,
332                                                                  &rz, &raz4, scratch, group);
333                         numAdds++;
334                 }
335         }
336 
337         /* extra operations to make timing less dependent on secrets */
338         if (timing) {
339                 /* low-order bit of timing argument contains no entropy */
340                 timing >>= 1;
341 
342                 MP_CHECKOK(ec_GFp_pt_set_inf_jac(&tpx, &tpy, &tpz));
343                 mp_zero(&tpaz4);
344 
345                 /* Set the temp value to a non-infinite point */
346                 ec_GFp_pt_add_jm_aff(&tpx, &tpy, &tpz, &tpaz4,
347                                                                  &precomp[8][0],
348                                                                  &precomp[8][1], &tpx, &tpy,
349                                                                  &tpz, &tpaz4, scratch, group);
350 
351                 /* two bits of extra adds */
352                 extraAdds = timing & 0x3;
353                 timing >>= 2;
354                 /* Window size is 5, so the maximum number of additions is ceil(orderBitSize/5) */
355                 /* This is the same as (orderBitSize + 4) / 5 */
356                 for(i = numAdds; i <= (orderBitSize + 4) / 5 + extraAdds; i++) {
357                         ec_GFp_pt_add_jm_aff(&tpx, &tpy, &tpz, &tpaz4,
358                                                                  &precomp[9 + (i % 3)][0],
359                                                                  &precomp[9 + (i % 3)][1], &tpx, &tpy,
360                                                                  &tpz, &tpaz4, scratch, group);
361                 }
362 
363                 /* two bits of extra doubles */
364                 extraDoubles = timing & 0x3;
365                 timing >>= 2;
366                 for(i = numDoubles; i <= orderBitSize + extraDoubles; i++) {
367                         ec_GFp_pt_dbl_jm(&tpx, &tpy, &tpz, &tpaz4, &tpx, &tpy, &tpz,
368                                              &tpaz4, scratch, group);
369                 }
370 
371         }
372 
373         /* convert result S to affine coordinates */
374         MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
375 
376   CLEANUP:
377         for (i = 0; i < MAX_SCRATCH; i++) {
378                 mp_clear(&scratch[i]);
379         }
380         for (i = 0; i < 16; i++) {
381                 mp_clear(&precomp[i][0]);
382                 mp_clear(&precomp[i][1]);
383         }
384         mp_clear(&tpx);
385         mp_clear(&tpy);
386         mp_clear(&tpz);
387         mp_clear(&tpaz4);
388         mp_clear(&rz);
389         mp_clear(&raz4);
390 #ifdef _KERNEL
391         kmem_free(naf, (orderBitSize + 1));
392 #else
393         free(naf);
394 #endif
395         return res;
396 }
397