1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include "ecp.h"
6 #include "ecl-priv.h"
7 #include "mplogic.h"
8 #include <stdlib.h>
9 
10 #define MAX_SCRATCH 6
11 
12 /* Computes R = 2P.  Elliptic curve points P and R can be identical.  Uses
13  * Modified Jacobian coordinates.
14  *
15  * Assumes input is already field-encoded using field_enc, and returns
16  * output that is still field-encoded.
17  *
18  */
19 static 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)20 ec_GFp_pt_dbl_jm(const mp_int *px, const mp_int *py, const mp_int *pz,
21                  const mp_int *paz4, mp_int *rx, mp_int *ry, mp_int *rz,
22                  mp_int *raz4, mp_int scratch[], const ECGroup *group)
23 {
24     mp_err res = MP_OKAY;
25     mp_int *t0, *t1, *M, *S;
26 
27     t0 = &scratch[0];
28     t1 = &scratch[1];
29     M = &scratch[2];
30     S = &scratch[3];
31 
32 #if MAX_SCRATCH < 4
33 #error "Scratch array defined too small "
34 #endif
35 
36     /* Check for point at infinity */
37     if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
38         /* Set r = pt at infinity by setting rz = 0 */
39 
40         MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
41         goto CLEANUP;
42     }
43 
44     /* M = 3 (px^2) + a*(pz^4) */
45     MP_CHECKOK(group->meth->field_sqr(px, t0, group->meth));
46     MP_CHECKOK(group->meth->field_add(t0, t0, M, group->meth));
47     MP_CHECKOK(group->meth->field_add(t0, M, t0, group->meth));
48     MP_CHECKOK(group->meth->field_add(t0, paz4, M, group->meth));
49 
50     /* rz = 2 * py * pz */
51     MP_CHECKOK(group->meth->field_mul(py, pz, S, group->meth));
52     MP_CHECKOK(group->meth->field_add(S, S, rz, group->meth));
53 
54     /* t0 = 2y^2 , t1 = 8y^4 */
55     MP_CHECKOK(group->meth->field_sqr(py, t0, group->meth));
56     MP_CHECKOK(group->meth->field_add(t0, t0, t0, group->meth));
57     MP_CHECKOK(group->meth->field_sqr(t0, t1, group->meth));
58     MP_CHECKOK(group->meth->field_add(t1, t1, t1, group->meth));
59 
60     /* S = 4 * px * py^2 = 2 * px * t0 */
61     MP_CHECKOK(group->meth->field_mul(px, t0, S, group->meth));
62     MP_CHECKOK(group->meth->field_add(S, S, S, group->meth));
63 
64     /* rx = M^2 - 2S */
65     MP_CHECKOK(group->meth->field_sqr(M, rx, group->meth));
66     MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth));
67     MP_CHECKOK(group->meth->field_sub(rx, S, rx, group->meth));
68 
69     /* ry = M * (S - rx) - t1 */
70     MP_CHECKOK(group->meth->field_sub(S, rx, S, group->meth));
71     MP_CHECKOK(group->meth->field_mul(S, M, ry, group->meth));
72     MP_CHECKOK(group->meth->field_sub(ry, t1, ry, group->meth));
73 
74     /* ra*z^4 = 2*t1*(apz4) */
75     MP_CHECKOK(group->meth->field_mul(paz4, t1, raz4, group->meth));
76     MP_CHECKOK(group->meth->field_add(raz4, raz4, raz4, group->meth));
77 
78 CLEANUP:
79     return res;
80 }
81 
82 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
83  * (qx, qy, 1).  Elliptic curve points P, Q, and R can all be identical.
84  * Uses mixed Modified_Jacobian-affine coordinates. Assumes input is
85  * already field-encoded using field_enc, and returns output that is still
86  * field-encoded. */
87 static 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)88 ec_GFp_pt_add_jm_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
89                      const mp_int *paz4, const mp_int *qx,
90                      const mp_int *qy, mp_int *rx, mp_int *ry, mp_int *rz,
91                      mp_int *raz4, mp_int scratch[], const ECGroup *group)
92 {
93     mp_err res = MP_OKAY;
94     mp_int *A, *B, *C, *D, *C2, *C3;
95 
96     A = &scratch[0];
97     B = &scratch[1];
98     C = &scratch[2];
99     D = &scratch[3];
100     C2 = &scratch[4];
101     C3 = &scratch[5];
102 
103 #if MAX_SCRATCH < 6
104 #error "Scratch array defined too small "
105 #endif
106 
107     /* If either P or Q is the point at infinity, then return the other
108      * point */
109     if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
110         MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
111         MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
112         MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
113         MP_CHECKOK(group->meth->field_mul(raz4, &group->curvea, raz4, group->meth));
114         goto CLEANUP;
115     }
116     if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
117         MP_CHECKOK(mp_copy(px, rx));
118         MP_CHECKOK(mp_copy(py, ry));
119         MP_CHECKOK(mp_copy(pz, rz));
120         MP_CHECKOK(mp_copy(paz4, raz4));
121         goto CLEANUP;
122     }
123 
124     /* A = qx * pz^2, B = qy * pz^3 */
125     MP_CHECKOK(group->meth->field_sqr(pz, A, group->meth));
126     MP_CHECKOK(group->meth->field_mul(A, pz, B, group->meth));
127     MP_CHECKOK(group->meth->field_mul(A, qx, A, group->meth));
128     MP_CHECKOK(group->meth->field_mul(B, qy, B, group->meth));
129 
130     /* Check P == Q */
131     if (mp_cmp(A, px) == 0) {
132         if (mp_cmp(B, py) == 0) {
133             /* If Px == Qx && Py == Qy, double P. */
134             return ec_GFp_pt_dbl_jm(px, py, pz, paz4, rx, ry, rz, raz4,
135                                     scratch, group);
136         }
137         /* If Px == Qx && Py != Qy, return point at infinity. */
138         return ec_GFp_pt_set_inf_jac(rx, ry, rz);
139     }
140 
141     /* C = A - px, D = B - py */
142     MP_CHECKOK(group->meth->field_sub(A, px, C, group->meth));
143     MP_CHECKOK(group->meth->field_sub(B, py, D, group->meth));
144 
145     /* C2 = C^2, C3 = C^3 */
146     MP_CHECKOK(group->meth->field_sqr(C, C2, group->meth));
147     MP_CHECKOK(group->meth->field_mul(C, C2, C3, group->meth));
148 
149     /* rz = pz * C */
150     MP_CHECKOK(group->meth->field_mul(pz, C, rz, group->meth));
151 
152     /* C = px * C^2 */
153     MP_CHECKOK(group->meth->field_mul(px, C2, C, group->meth));
154     /* A = D^2 */
155     MP_CHECKOK(group->meth->field_sqr(D, A, group->meth));
156 
157     /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
158     MP_CHECKOK(group->meth->field_add(C, C, rx, group->meth));
159     MP_CHECKOK(group->meth->field_add(C3, rx, rx, group->meth));
160     MP_CHECKOK(group->meth->field_sub(A, rx, rx, group->meth));
161 
162     /* C3 = py * C^3 */
163     MP_CHECKOK(group->meth->field_mul(py, C3, C3, group->meth));
164 
165     /* ry = D * (px * C^2 - rx) - py * C^3 */
166     MP_CHECKOK(group->meth->field_sub(C, rx, ry, group->meth));
167     MP_CHECKOK(group->meth->field_mul(D, ry, ry, group->meth));
168     MP_CHECKOK(group->meth->field_sub(ry, C3, ry, group->meth));
169 
170     /* raz4 = a * rz^4 */
171     MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
172     MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
173     MP_CHECKOK(group->meth->field_mul(raz4, &group->curvea, raz4, group->meth));
174 CLEANUP:
175     return res;
176 }
177 
178 /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic
179  * curve points P and R can be identical. Uses mixed Modified-Jacobian
180  * co-ordinates for doubling and Chudnovsky Jacobian coordinates for
181  * additions. Assumes input is already field-encoded using field_enc, and
182  * returns output that is still field-encoded. Uses 5-bit window NAF
183  * method (algorithm 11) for scalar-point multiplication from Brown,
184  * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic
185  * Curves Over Prime Fields. */
186 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)187 ec_GFp_pt_mul_jm_wNAF(const mp_int *n, const mp_int *px, const mp_int *py,
188                       mp_int *rx, mp_int *ry, const ECGroup *group)
189 {
190     mp_err res = MP_OKAY;
191     mp_int precomp[16][2], rz, tpx, tpy;
192     mp_int raz4;
193     mp_int scratch[MAX_SCRATCH];
194     signed char *naf = NULL;
195     int i, orderBitSize;
196 
197     MP_DIGITS(&rz) = 0;
198     MP_DIGITS(&raz4) = 0;
199     MP_DIGITS(&tpx) = 0;
200     MP_DIGITS(&tpy) = 0;
201     for (i = 0; i < 16; i++) {
202         MP_DIGITS(&precomp[i][0]) = 0;
203         MP_DIGITS(&precomp[i][1]) = 0;
204     }
205     for (i = 0; i < MAX_SCRATCH; i++) {
206         MP_DIGITS(&scratch[i]) = 0;
207     }
208 
209     ARGCHK(group != NULL, MP_BADARG);
210     ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
211 
212     /* initialize precomputation table */
213     MP_CHECKOK(mp_init(&tpx));
214     MP_CHECKOK(mp_init(&tpy));
215     ;
216     MP_CHECKOK(mp_init(&rz));
217     MP_CHECKOK(mp_init(&raz4));
218 
219     for (i = 0; i < 16; i++) {
220         MP_CHECKOK(mp_init(&precomp[i][0]));
221         MP_CHECKOK(mp_init(&precomp[i][1]));
222     }
223     for (i = 0; i < MAX_SCRATCH; i++) {
224         MP_CHECKOK(mp_init(&scratch[i]));
225     }
226 
227     /* Set out[8] = P */
228     MP_CHECKOK(mp_copy(px, &precomp[8][0]));
229     MP_CHECKOK(mp_copy(py, &precomp[8][1]));
230 
231     /* Set (tpx, tpy) = 2P */
232     MP_CHECKOK(group->point_dbl(&precomp[8][0], &precomp[8][1], &tpx, &tpy,
233                                 group));
234 
235     /* Set 3P, 5P, ..., 15P */
236     for (i = 8; i < 15; i++) {
237         MP_CHECKOK(group->point_add(&precomp[i][0], &precomp[i][1], &tpx, &tpy,
238                                     &precomp[i + 1][0], &precomp[i + 1][1],
239                                     group));
240     }
241 
242     /* Set -15P, -13P, ..., -P */
243     for (i = 0; i < 8; i++) {
244         MP_CHECKOK(mp_copy(&precomp[15 - i][0], &precomp[i][0]));
245         MP_CHECKOK(group->meth->field_neg(&precomp[15 - i][1], &precomp[i][1],
246                                           group->meth));
247     }
248 
249     /* R = inf */
250     MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
251 
252     orderBitSize = mpl_significant_bits(&group->order);
253 
254     /* Allocate memory for NAF */
255     naf = (signed char *)malloc(sizeof(signed char) * (orderBitSize + 1));
256     if (naf == NULL) {
257         res = MP_MEM;
258         goto CLEANUP;
259     }
260 
261     /* Compute 5NAF */
262     ec_compute_wNAF(naf, orderBitSize, n, 5);
263 
264     /* wNAF method */
265     for (i = orderBitSize; i >= 0; i--) {
266         /* R = 2R */
267         ec_GFp_pt_dbl_jm(rx, ry, &rz, &raz4, rx, ry, &rz,
268                          &raz4, scratch, group);
269         if (naf[i] != 0) {
270             ec_GFp_pt_add_jm_aff(rx, ry, &rz, &raz4,
271                                  &precomp[(naf[i] + 15) / 2][0],
272                                  &precomp[(naf[i] + 15) / 2][1], rx, ry,
273                                  &rz, &raz4, scratch, group);
274         }
275     }
276 
277     /* convert result S to affine coordinates */
278     MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
279 
280 CLEANUP:
281     for (i = 0; i < MAX_SCRATCH; i++) {
282         mp_clear(&scratch[i]);
283     }
284     for (i = 0; i < 16; i++) {
285         mp_clear(&precomp[i][0]);
286         mp_clear(&precomp[i][1]);
287     }
288     mp_clear(&tpx);
289     mp_clear(&tpy);
290     mp_clear(&rz);
291     mp_clear(&raz4);
292     free(naf);
293     return res;
294 }
295