Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / freebl / ecl / ecp_aff.c
1 /* 
2  * ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the elliptic curve math library for prime field curves.
16  *
17  * The Initial Developer of the Original Code is
18  * Sun Microsystems, Inc.
19  * Portions created by the Initial Developer are Copyright (C) 2003
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
24  *   Stephen Fung <fungstep@hotmail.com>, and
25  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
26  *   Bodo Moeller <moeller@cdc.informatik.tu-darmstadt.de>,
27  *   Nils Larsch <nla@trustcenter.de>, and
28  *   Lenka Fibikova <fibikova@exp-math.uni-essen.de>, the OpenSSL Project
29  *
30  * Alternatively, the contents of this file may be used under the terms of
31  * either the GNU General Public License Version 2 or later (the "GPL"), or
32  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
33  * in which case the provisions of the GPL or the LGPL are applicable instead
34  * of those above. If you wish to allow use of your version of this file only
35  * under the terms of either the GPL or the LGPL, and not to allow others to
36  * use your version of this file under the terms of the MPL, indicate your
37  * decision by deleting the provisions above and replace them with the notice
38  * and other provisions required by the GPL or the LGPL. If you do not delete
39  * the provisions above, a recipient may use your version of this file under
40  * the terms of any one of the MPL, the GPL or the LGPL.
41  *
42  * ***** END LICENSE BLOCK ***** */
43
44 #include "ecp.h"
45 #include "mplogic.h"
46 #include <stdlib.h>
47
48 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
49 mp_err
50 ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
51 {
52
53         if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
54                 return MP_YES;
55         } else {
56                 return MP_NO;
57         }
58
59 }
60
61 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
62 mp_err
63 ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
64 {
65         mp_zero(px);
66         mp_zero(py);
67         return MP_OKAY;
68 }
69
70 /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P, 
71  * Q, and R can all be identical. Uses affine coordinates. Assumes input
72  * is already field-encoded using field_enc, and returns output that is
73  * still field-encoded. */
74 mp_err
75 ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
76                                   const mp_int *qy, mp_int *rx, mp_int *ry,
77                                   const ECGroup *group)
78 {
79         mp_err res = MP_OKAY;
80         mp_int lambda, temp, tempx, tempy;
81
82         MP_DIGITS(&lambda) = 0;
83         MP_DIGITS(&temp) = 0;
84         MP_DIGITS(&tempx) = 0;
85         MP_DIGITS(&tempy) = 0;
86         MP_CHECKOK(mp_init(&lambda));
87         MP_CHECKOK(mp_init(&temp));
88         MP_CHECKOK(mp_init(&tempx));
89         MP_CHECKOK(mp_init(&tempy));
90         /* if P = inf, then R = Q */
91         if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
92                 MP_CHECKOK(mp_copy(qx, rx));
93                 MP_CHECKOK(mp_copy(qy, ry));
94                 res = MP_OKAY;
95                 goto CLEANUP;
96         }
97         /* if Q = inf, then R = P */
98         if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
99                 MP_CHECKOK(mp_copy(px, rx));
100                 MP_CHECKOK(mp_copy(py, ry));
101                 res = MP_OKAY;
102                 goto CLEANUP;
103         }
104         /* if px != qx, then lambda = (py-qy) / (px-qx) */
105         if (mp_cmp(px, qx) != 0) {
106                 MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
107                 MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
108                 MP_CHECKOK(group->meth->
109                                    field_div(&tempy, &tempx, &lambda, group->meth));
110         } else {
111                 /* if py != qy or qy = 0, then R = inf */
112                 if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
113                         mp_zero(rx);
114                         mp_zero(ry);
115                         res = MP_OKAY;
116                         goto CLEANUP;
117                 }
118                 /* lambda = (3qx^2+a) / (2qy) */
119                 MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
120                 MP_CHECKOK(mp_set_int(&temp, 3));
121                 if (group->meth->field_enc) {
122                         MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
123                 }
124                 MP_CHECKOK(group->meth->
125                                    field_mul(&tempx, &temp, &tempx, group->meth));
126                 MP_CHECKOK(group->meth->
127                                    field_add(&tempx, &group->curvea, &tempx, group->meth));
128                 MP_CHECKOK(mp_set_int(&temp, 2));
129                 if (group->meth->field_enc) {
130                         MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
131                 }
132                 MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
133                 MP_CHECKOK(group->meth->
134                                    field_div(&tempx, &tempy, &lambda, group->meth));
135         }
136         /* rx = lambda^2 - px - qx */
137         MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
138         MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
139         MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
140         /* ry = (x1-x2) * lambda - y1 */
141         MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
142         MP_CHECKOK(group->meth->
143                            field_mul(&tempy, &lambda, &tempy, group->meth));
144         MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
145         MP_CHECKOK(mp_copy(&tempx, rx));
146         MP_CHECKOK(mp_copy(&tempy, ry));
147
148   CLEANUP:
149         mp_clear(&lambda);
150         mp_clear(&temp);
151         mp_clear(&tempx);
152         mp_clear(&tempy);
153         return res;
154 }
155
156 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
157  * identical. Uses affine coordinates. Assumes input is already
158  * field-encoded using field_enc, and returns output that is still
159  * field-encoded. */
160 mp_err
161 ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
162                                   const mp_int *qy, mp_int *rx, mp_int *ry,
163                                   const ECGroup *group)
164 {
165         mp_err res = MP_OKAY;
166         mp_int nqy;
167
168         MP_DIGITS(&nqy) = 0;
169         MP_CHECKOK(mp_init(&nqy));
170         /* nqy = -qy */
171         MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
172         res = group->point_add(px, py, qx, &nqy, rx, ry, group);
173   CLEANUP:
174         mp_clear(&nqy);
175         return res;
176 }
177
178 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
179  * affine coordinates. Assumes input is already field-encoded using
180  * field_enc, and returns output that is still field-encoded. */
181 mp_err
182 ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
183                                   mp_int *ry, const ECGroup *group)
184 {
185         return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
186 }
187
188 /* by default, this routine is unused and thus doesn't need to be compiled */
189 #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
190 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and 
191  * R can be identical. Uses affine coordinates. Assumes input is already
192  * field-encoded using field_enc, and returns output that is still
193  * field-encoded. */
194 mp_err
195 ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
196                                   mp_int *rx, mp_int *ry, const ECGroup *group)
197 {
198         mp_err res = MP_OKAY;
199         mp_int k, k3, qx, qy, sx, sy;
200         int b1, b3, i, l;
201
202         MP_DIGITS(&k) = 0;
203         MP_DIGITS(&k3) = 0;
204         MP_DIGITS(&qx) = 0;
205         MP_DIGITS(&qy) = 0;
206         MP_DIGITS(&sx) = 0;
207         MP_DIGITS(&sy) = 0;
208         MP_CHECKOK(mp_init(&k));
209         MP_CHECKOK(mp_init(&k3));
210         MP_CHECKOK(mp_init(&qx));
211         MP_CHECKOK(mp_init(&qy));
212         MP_CHECKOK(mp_init(&sx));
213         MP_CHECKOK(mp_init(&sy));
214
215         /* if n = 0 then r = inf */
216         if (mp_cmp_z(n) == 0) {
217                 mp_zero(rx);
218                 mp_zero(ry);
219                 res = MP_OKAY;
220                 goto CLEANUP;
221         }
222         /* Q = P, k = n */
223         MP_CHECKOK(mp_copy(px, &qx));
224         MP_CHECKOK(mp_copy(py, &qy));
225         MP_CHECKOK(mp_copy(n, &k));
226         /* if n < 0 then Q = -Q, k = -k */
227         if (mp_cmp_z(n) < 0) {
228                 MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
229                 MP_CHECKOK(mp_neg(&k, &k));
230         }
231 #ifdef ECL_DEBUG                                /* basic double and add method */
232         l = mpl_significant_bits(&k) - 1;
233         MP_CHECKOK(mp_copy(&qx, &sx));
234         MP_CHECKOK(mp_copy(&qy, &sy));
235         for (i = l - 1; i >= 0; i--) {
236                 /* S = 2S */
237                 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
238                 /* if k_i = 1, then S = S + Q */
239                 if (mpl_get_bit(&k, i) != 0) {
240                         MP_CHECKOK(group->
241                                            point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
242                 }
243         }
244 #else                                                   /* double and add/subtract method from
245                                                                  * standard */
246         /* k3 = 3 * k */
247         MP_CHECKOK(mp_set_int(&k3, 3));
248         MP_CHECKOK(mp_mul(&k, &k3, &k3));
249         /* S = Q */
250         MP_CHECKOK(mp_copy(&qx, &sx));
251         MP_CHECKOK(mp_copy(&qy, &sy));
252         /* l = index of high order bit in binary representation of 3*k */
253         l = mpl_significant_bits(&k3) - 1;
254         /* for i = l-1 downto 1 */
255         for (i = l - 1; i >= 1; i--) {
256                 /* S = 2S */
257                 MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
258                 b3 = MP_GET_BIT(&k3, i);
259                 b1 = MP_GET_BIT(&k, i);
260                 /* if k3_i = 1 and k_i = 0, then S = S + Q */
261                 if ((b3 == 1) && (b1 == 0)) {
262                         MP_CHECKOK(group->
263                                            point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
264                         /* if k3_i = 0 and k_i = 1, then S = S - Q */
265                 } else if ((b3 == 0) && (b1 == 1)) {
266                         MP_CHECKOK(group->
267                                            point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
268                 }
269         }
270 #endif
271         /* output S */
272         MP_CHECKOK(mp_copy(&sx, rx));
273         MP_CHECKOK(mp_copy(&sy, ry));
274
275   CLEANUP:
276         mp_clear(&k);
277         mp_clear(&k3);
278         mp_clear(&qx);
279         mp_clear(&qy);
280         mp_clear(&sx);
281         mp_clear(&sy);
282         return res;
283 }
284 #endif
285
286 /* Validates a point on a GFp curve. */
287 mp_err 
288 ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
289 {
290         mp_err res = MP_NO;
291         mp_int accl, accr, tmp, pxt, pyt;
292
293         MP_DIGITS(&accl) = 0;
294         MP_DIGITS(&accr) = 0;
295         MP_DIGITS(&tmp) = 0;
296         MP_DIGITS(&pxt) = 0;
297         MP_DIGITS(&pyt) = 0;
298         MP_CHECKOK(mp_init(&accl));
299         MP_CHECKOK(mp_init(&accr));
300         MP_CHECKOK(mp_init(&tmp));
301         MP_CHECKOK(mp_init(&pxt));
302         MP_CHECKOK(mp_init(&pyt));
303
304     /* 1: Verify that publicValue is not the point at infinity */
305         if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
306                 res = MP_NO;
307                 goto CLEANUP;
308         }
309     /* 2: Verify that the coordinates of publicValue are elements 
310      *    of the field.
311      */
312         if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) || 
313                 (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
314                 res = MP_NO;
315                 goto CLEANUP;
316         }
317     /* 3: Verify that publicValue is on the curve. */
318         if (group->meth->field_enc) {
319                 group->meth->field_enc(px, &pxt, group->meth);
320                 group->meth->field_enc(py, &pyt, group->meth);
321         } else {
322                 mp_copy(px, &pxt);
323                 mp_copy(py, &pyt);
324         }
325         /* left-hand side: y^2  */
326         MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
327         /* right-hand side: x^3 + a*x + b */
328         MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
329         MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
330         MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
331         MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
332         MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
333         /* check LHS - RHS == 0 */
334         MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
335         if (mp_cmp_z(&accr) != 0) {
336                 res = MP_NO;
337                 goto CLEANUP;
338         }
339     /* 4: Verify that the order of the curve times the publicValue
340      *    is the point at infinity.
341      */
342         MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
343         if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
344                 res = MP_NO;
345                 goto CLEANUP;
346         }
347
348         res = MP_YES;
349
350 CLEANUP:
351         mp_clear(&accl);
352         mp_clear(&accr);
353         mp_clear(&tmp);
354         mp_clear(&pxt);
355         mp_clear(&pyt);
356         return res;
357 }