2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is the elliptic curve math library for prime field curves.
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.
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
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.
42 * ***** END LICENSE BLOCK ***** */
48 /* Checks if point P(px, py) is at infinity. Uses affine coordinates. */
50 ec_GFp_pt_is_inf_aff(const mp_int
*px
, const mp_int
*py
)
53 if ((mp_cmp_z(px
) == 0) && (mp_cmp_z(py
) == 0)) {
61 /* Sets P(px, py) to be the point at infinity. Uses affine coordinates. */
63 ec_GFp_pt_set_inf_aff(mp_int
*px
, mp_int
*py
)
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. */
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
,
80 mp_int lambda
, temp
, tempx
, tempy
;
82 MP_DIGITS(&lambda
) = 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
));
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
));
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
));
111 /* if py != qy or qy = 0, then R = inf */
112 if (((mp_cmp(py
, qy
) != 0)) || (mp_cmp_z(qy
) == 0)) {
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
));
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
));
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
));
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
));
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
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
)
165 mp_err res
= MP_OKAY
;
169 MP_CHECKOK(mp_init(&nqy
));
171 MP_CHECKOK(group
->meth
->field_neg(qy
, &nqy
, group
->meth
));
172 res
= group
->point_add(px
, py
, qx
, &nqy
, rx
, ry
, group
);
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. */
182 ec_GFp_pt_dbl_aff(const mp_int
*px
, const mp_int
*py
, mp_int
*rx
,
183 mp_int
*ry
, const ECGroup
*group
)
185 return ec_GFp_pt_add_aff(px
, py
, px
, py
, rx
, ry
, group
);
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
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
)
198 mp_err res
= MP_OKAY
;
199 mp_int k
, k3
, qx
, qy
, sx
, sy
;
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
));
215 /* if n = 0 then r = inf */
216 if (mp_cmp_z(n
) == 0) {
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
));
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
--) {
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) {
241 point_add(&sx
, &sy
, &qx
, &qy
, &sx
, &sy
, group
));
244 #else /* double and add/subtract method from
247 MP_CHECKOK(mp_set_int(&k3
, 3));
248 MP_CHECKOK(mp_mul(&k
, &k3
, &k3
));
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
--) {
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)) {
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)) {
267 point_sub(&sx
, &sy
, &qx
, &qy
, &sx
, &sy
, group
));
272 MP_CHECKOK(mp_copy(&sx
, rx
));
273 MP_CHECKOK(mp_copy(&sy
, ry
));
286 /* Validates a point on a GFp curve. */
288 ec_GFp_validate_point(const mp_int
*px
, const mp_int
*py
, const ECGroup
*group
)
291 mp_int accl
, accr
, tmp
, pxt
, pyt
;
293 MP_DIGITS(&accl
) = 0;
294 MP_DIGITS(&accr
) = 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
));
304 /* 1: Verify that publicValue is not the point at infinity */
305 if (ec_GFp_pt_is_inf_aff(px
, py
) == MP_YES
) {
309 /* 2: Verify that the coordinates of publicValue are elements
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)) {
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
);
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) {
339 /* 4: Verify that the order of the curve times the publicValue
340 * is the point at infinity.
342 MP_CHECKOK( ECPoint_mul(group
, &group
->order
, px
, py
, &pxt
, &pyt
) );
343 if (ec_GFp_pt_is_inf_aff(&pxt
, &pyt
) != MP_YES
) {