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 "mplogic.h"
7 #include <stdlib.h>
8 
9 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
10 mp_err
ec_GFp_pt_is_inf_aff(const mp_int * px,const mp_int * py)11 ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
12 {
13 
14     if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
15         return MP_YES;
16     } else {
17         return MP_NO;
18     }
19 }
20 
21 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
22 mp_err
ec_GFp_pt_set_inf_aff(mp_int * px,mp_int * py)23 ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
24 {
25     mp_zero(px);
26     mp_zero(py);
27     return MP_OKAY;
28 }
29 
30 /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P,
31  * Q, and R can all be identical. Uses affine coordinates. Assumes input
32  * is already field-encoded using field_enc, and returns output that is
33  * still field-encoded. */
34 mp_err
ec_GFp_pt_add_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)35 ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
36                   const mp_int *qy, mp_int *rx, mp_int *ry,
37                   const ECGroup *group)
38 {
39     mp_err res = MP_OKAY;
40     mp_int lambda, temp, tempx, tempy;
41 
42     MP_DIGITS(&lambda) = 0;
43     MP_DIGITS(&temp) = 0;
44     MP_DIGITS(&tempx) = 0;
45     MP_DIGITS(&tempy) = 0;
46     MP_CHECKOK(mp_init(&lambda));
47     MP_CHECKOK(mp_init(&temp));
48     MP_CHECKOK(mp_init(&tempx));
49     MP_CHECKOK(mp_init(&tempy));
50     /* if P = inf, then R = Q */
51     if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
52         MP_CHECKOK(mp_copy(qx, rx));
53         MP_CHECKOK(mp_copy(qy, ry));
54         res = MP_OKAY;
55         goto CLEANUP;
56     }
57     /* if Q = inf, then R = P */
58     if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
59         MP_CHECKOK(mp_copy(px, rx));
60         MP_CHECKOK(mp_copy(py, ry));
61         res = MP_OKAY;
62         goto CLEANUP;
63     }
64     /* if px != qx, then lambda = (py-qy) / (px-qx) */
65     if (mp_cmp(px, qx) != 0) {
66         MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
67         MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
68         MP_CHECKOK(group->meth->field_div(&tempy, &tempx, &lambda, group->meth));
69     } else {
70         /* if py != qy or qy = 0, then R = inf */
71         if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
72             mp_zero(rx);
73             mp_zero(ry);
74             res = MP_OKAY;
75             goto CLEANUP;
76         }
77         /* lambda = (3qx^2+a) / (2qy) */
78         MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
79         MP_CHECKOK(mp_set_int(&temp, 3));
80         if (group->meth->field_enc) {
81             MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
82         }
83         MP_CHECKOK(group->meth->field_mul(&tempx, &temp, &tempx, group->meth));
84         MP_CHECKOK(group->meth->field_add(&tempx, &group->curvea, &tempx, group->meth));
85         MP_CHECKOK(mp_set_int(&temp, 2));
86         if (group->meth->field_enc) {
87             MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
88         }
89         MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
90         MP_CHECKOK(group->meth->field_div(&tempx, &tempy, &lambda, group->meth));
91     }
92     /* rx = lambda^2 - px - qx */
93     MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
94     MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
95     MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
96     /* ry = (x1-x2) * lambda - y1 */
97     MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
98     MP_CHECKOK(group->meth->field_mul(&tempy, &lambda, &tempy, group->meth));
99     MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
100     MP_CHECKOK(mp_copy(&tempx, rx));
101     MP_CHECKOK(mp_copy(&tempy, ry));
102 
103 CLEANUP:
104     mp_clear(&lambda);
105     mp_clear(&temp);
106     mp_clear(&tempx);
107     mp_clear(&tempy);
108     return res;
109 }
110 
111 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
112  * identical. Uses affine coordinates. Assumes input is already
113  * field-encoded using field_enc, and returns output that is still
114  * field-encoded. */
115 mp_err
ec_GFp_pt_sub_aff(const mp_int * px,const mp_int * py,const mp_int * qx,const mp_int * qy,mp_int * rx,mp_int * ry,const ECGroup * group)116 ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
117                   const mp_int *qy, mp_int *rx, mp_int *ry,
118                   const ECGroup *group)
119 {
120     mp_err res = MP_OKAY;
121     mp_int nqy;
122 
123     MP_DIGITS(&nqy) = 0;
124     MP_CHECKOK(mp_init(&nqy));
125     /* nqy = -qy */
126     MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
127     res = group->point_add(px, py, qx, &nqy, rx, ry, group);
128 CLEANUP:
129     mp_clear(&nqy);
130     return res;
131 }
132 
133 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
134  * affine coordinates. Assumes input is already field-encoded using
135  * field_enc, and returns output that is still field-encoded. */
136 mp_err
ec_GFp_pt_dbl_aff(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)137 ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
138                   mp_int *ry, const ECGroup *group)
139 {
140     return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
141 }
142 
143 /* by default, this routine is unused and thus doesn't need to be compiled */
144 #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
145 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
146  * R can be identical. Uses affine coordinates. Assumes input is already
147  * field-encoded using field_enc, and returns output that is still
148  * field-encoded. */
149 mp_err
ec_GFp_pt_mul_aff(const mp_int * n,const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)150 ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
151                   mp_int *rx, mp_int *ry, const ECGroup *group)
152 {
153     mp_err res = MP_OKAY;
154     mp_int k, k3, qx, qy, sx, sy;
155     int b1, b3, i, l;
156 
157     MP_DIGITS(&k) = 0;
158     MP_DIGITS(&k3) = 0;
159     MP_DIGITS(&qx) = 0;
160     MP_DIGITS(&qy) = 0;
161     MP_DIGITS(&sx) = 0;
162     MP_DIGITS(&sy) = 0;
163     MP_CHECKOK(mp_init(&k));
164     MP_CHECKOK(mp_init(&k3));
165     MP_CHECKOK(mp_init(&qx));
166     MP_CHECKOK(mp_init(&qy));
167     MP_CHECKOK(mp_init(&sx));
168     MP_CHECKOK(mp_init(&sy));
169 
170     /* if n = 0 then r = inf */
171     if (mp_cmp_z(n) == 0) {
172         mp_zero(rx);
173         mp_zero(ry);
174         res = MP_OKAY;
175         goto CLEANUP;
176     }
177     /* Q = P, k = n */
178     MP_CHECKOK(mp_copy(px, &qx));
179     MP_CHECKOK(mp_copy(py, &qy));
180     MP_CHECKOK(mp_copy(n, &k));
181     /* if n < 0 then Q = -Q, k = -k */
182     if (mp_cmp_z(n) < 0) {
183         MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
184         MP_CHECKOK(mp_neg(&k, &k));
185     }
186 #ifdef ECL_DEBUG /* basic double and add method */
187     l = mpl_significant_bits(&k) - 1;
188     MP_CHECKOK(mp_copy(&qx, &sx));
189     MP_CHECKOK(mp_copy(&qy, &sy));
190     for (i = l - 1; i >= 0; i--) {
191         /* S = 2S */
192         MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
193         /* if k_i = 1, then S = S + Q */
194         if (mpl_get_bit(&k, i) != 0) {
195             MP_CHECKOK(group->point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
196         }
197     }
198 #else /* double and add/subtract method from \
199                * standard */
200     /* k3 = 3 * k */
201     MP_CHECKOK(mp_set_int(&k3, 3));
202     MP_CHECKOK(mp_mul(&k, &k3, &k3));
203     /* S = Q */
204     MP_CHECKOK(mp_copy(&qx, &sx));
205     MP_CHECKOK(mp_copy(&qy, &sy));
206     /* l = index of high order bit in binary representation of 3*k */
207     l = mpl_significant_bits(&k3) - 1;
208     /* for i = l-1 downto 1 */
209     for (i = l - 1; i >= 1; i--) {
210         /* S = 2S */
211         MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
212         b3 = MP_GET_BIT(&k3, i);
213         b1 = MP_GET_BIT(&k, i);
214         /* if k3_i = 1 and k_i = 0, then S = S + Q */
215         if ((b3 == 1) && (b1 == 0)) {
216             MP_CHECKOK(group->point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
217             /* if k3_i = 0 and k_i = 1, then S = S - Q */
218         } else if ((b3 == 0) && (b1 == 1)) {
219             MP_CHECKOK(group->point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
220         }
221     }
222 #endif
223     /* output S */
224     MP_CHECKOK(mp_copy(&sx, rx));
225     MP_CHECKOK(mp_copy(&sy, ry));
226 
227 CLEANUP:
228     mp_clear(&k);
229     mp_clear(&k3);
230     mp_clear(&qx);
231     mp_clear(&qy);
232     mp_clear(&sx);
233     mp_clear(&sy);
234     return res;
235 }
236 #endif
237 
238 /* Validates a point on a GFp curve. */
239 mp_err
ec_GFp_validate_point(const mp_int * px,const mp_int * py,const ECGroup * group)240 ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
241 {
242     mp_err res = MP_NO;
243     mp_int accl, accr, tmp, pxt, pyt;
244 
245     MP_DIGITS(&accl) = 0;
246     MP_DIGITS(&accr) = 0;
247     MP_DIGITS(&tmp) = 0;
248     MP_DIGITS(&pxt) = 0;
249     MP_DIGITS(&pyt) = 0;
250     MP_CHECKOK(mp_init(&accl));
251     MP_CHECKOK(mp_init(&accr));
252     MP_CHECKOK(mp_init(&tmp));
253     MP_CHECKOK(mp_init(&pxt));
254     MP_CHECKOK(mp_init(&pyt));
255 
256     /* 1: Verify that publicValue is not the point at infinity */
257     if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
258         res = MP_NO;
259         goto CLEANUP;
260     }
261     /* 2: Verify that the coordinates of publicValue are elements
262      *    of the field.
263      */
264     if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
265         (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
266         res = MP_NO;
267         goto CLEANUP;
268     }
269     /* 3: Verify that publicValue is on the curve. */
270     if (group->meth->field_enc) {
271         group->meth->field_enc(px, &pxt, group->meth);
272         group->meth->field_enc(py, &pyt, group->meth);
273     } else {
274         MP_CHECKOK(mp_copy(px, &pxt));
275         MP_CHECKOK(mp_copy(py, &pyt));
276     }
277     /* left-hand side: y^2  */
278     MP_CHECKOK(group->meth->field_sqr(&pyt, &accl, group->meth));
279     /* right-hand side: x^3 + a*x + b = (x^2 + a)*x + b by Horner's rule */
280     MP_CHECKOK(group->meth->field_sqr(&pxt, &tmp, group->meth));
281     MP_CHECKOK(group->meth->field_add(&tmp, &group->curvea, &tmp, group->meth));
282     MP_CHECKOK(group->meth->field_mul(&tmp, &pxt, &accr, group->meth));
283     MP_CHECKOK(group->meth->field_add(&accr, &group->curveb, &accr, group->meth));
284     /* check LHS - RHS == 0 */
285     MP_CHECKOK(group->meth->field_sub(&accl, &accr, &accr, group->meth));
286     if (mp_cmp_z(&accr) != 0) {
287         res = MP_NO;
288         goto CLEANUP;
289     }
290     /* 4: Verify that the order of the curve times the publicValue
291      *    is the point at infinity.
292      */
293     MP_CHECKOK(ECPoint_mul(group, &group->order, px, py, &pxt, &pyt));
294     if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
295         res = MP_NO;
296         goto CLEANUP;
297     }
298 
299     res = MP_YES;
300 
301 CLEANUP:
302     mp_clear(&accl);
303     mp_clear(&accr);
304     mp_clear(&tmp);
305     mp_clear(&pxt);
306     mp_clear(&pyt);
307     return res;
308 }
309