xref: /illumos-gate/usr/src/common/crypto/ecc/ecp_aff.c (revision c40a6cd7)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
45  * Use is subject to license terms.
46  *
47  * Sun elects to use this software under the MPL license.
48  */
49 
50 #include "ecp.h"
51 #include "mplogic.h"
52 #ifndef _KERNEL
53 #include <stdlib.h>
54 #endif
55 
56 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
57 mp_err
ec_GFp_pt_is_inf_aff(const mp_int * px,const mp_int * py)58 ec_GFp_pt_is_inf_aff(const mp_int *px, const mp_int *py)
59 {
60 
61 	if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
62 		return MP_YES;
63 	} else {
64 		return MP_NO;
65 	}
66 
67 }
68 
69 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
70 mp_err
ec_GFp_pt_set_inf_aff(mp_int * px,mp_int * py)71 ec_GFp_pt_set_inf_aff(mp_int *px, mp_int *py)
72 {
73 	mp_zero(px);
74 	mp_zero(py);
75 	return MP_OKAY;
76 }
77 
78 /* Computes R = P + Q based on IEEE P1363 A.10.1. Elliptic curve points P,
79  * Q, and R can all be identical. Uses affine coordinates. Assumes input
80  * is already field-encoded using field_enc, and returns output that is
81  * still field-encoded. */
82 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)83 ec_GFp_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
84 				  const mp_int *qy, mp_int *rx, mp_int *ry,
85 				  const ECGroup *group)
86 {
87 	mp_err res = MP_OKAY;
88 	mp_int lambda, temp, tempx, tempy;
89 
90 	MP_DIGITS(&lambda) = 0;
91 	MP_DIGITS(&temp) = 0;
92 	MP_DIGITS(&tempx) = 0;
93 	MP_DIGITS(&tempy) = 0;
94 	MP_CHECKOK(mp_init(&lambda, FLAG(px)));
95 	MP_CHECKOK(mp_init(&temp, FLAG(px)));
96 	MP_CHECKOK(mp_init(&tempx, FLAG(px)));
97 	MP_CHECKOK(mp_init(&tempy, FLAG(px)));
98 	/* if P = inf, then R = Q */
99 	if (ec_GFp_pt_is_inf_aff(px, py) == 0) {
100 		MP_CHECKOK(mp_copy(qx, rx));
101 		MP_CHECKOK(mp_copy(qy, ry));
102 		res = MP_OKAY;
103 		goto CLEANUP;
104 	}
105 	/* if Q = inf, then R = P */
106 	if (ec_GFp_pt_is_inf_aff(qx, qy) == 0) {
107 		MP_CHECKOK(mp_copy(px, rx));
108 		MP_CHECKOK(mp_copy(py, ry));
109 		res = MP_OKAY;
110 		goto CLEANUP;
111 	}
112 	/* if px != qx, then lambda = (py-qy) / (px-qx) */
113 	if (mp_cmp(px, qx) != 0) {
114 		MP_CHECKOK(group->meth->field_sub(py, qy, &tempy, group->meth));
115 		MP_CHECKOK(group->meth->field_sub(px, qx, &tempx, group->meth));
116 		MP_CHECKOK(group->meth->
117 				   field_div(&tempy, &tempx, &lambda, group->meth));
118 	} else {
119 		/* if py != qy or qy = 0, then R = inf */
120 		if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qy) == 0)) {
121 			mp_zero(rx);
122 			mp_zero(ry);
123 			res = MP_OKAY;
124 			goto CLEANUP;
125 		}
126 		/* lambda = (3qx^2+a) / (2qy) */
127 		MP_CHECKOK(group->meth->field_sqr(qx, &tempx, group->meth));
128 		MP_CHECKOK(mp_set_int(&temp, 3));
129 		if (group->meth->field_enc) {
130 			MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
131 		}
132 		MP_CHECKOK(group->meth->
133 				   field_mul(&tempx, &temp, &tempx, group->meth));
134 		MP_CHECKOK(group->meth->
135 				   field_add(&tempx, &group->curvea, &tempx, group->meth));
136 		MP_CHECKOK(mp_set_int(&temp, 2));
137 		if (group->meth->field_enc) {
138 			MP_CHECKOK(group->meth->field_enc(&temp, &temp, group->meth));
139 		}
140 		MP_CHECKOK(group->meth->field_mul(qy, &temp, &tempy, group->meth));
141 		MP_CHECKOK(group->meth->
142 				   field_div(&tempx, &tempy, &lambda, group->meth));
143 	}
144 	/* rx = lambda^2 - px - qx */
145 	MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
146 	MP_CHECKOK(group->meth->field_sub(&tempx, px, &tempx, group->meth));
147 	MP_CHECKOK(group->meth->field_sub(&tempx, qx, &tempx, group->meth));
148 	/* ry = (x1-x2) * lambda - y1 */
149 	MP_CHECKOK(group->meth->field_sub(qx, &tempx, &tempy, group->meth));
150 	MP_CHECKOK(group->meth->
151 			   field_mul(&tempy, &lambda, &tempy, group->meth));
152 	MP_CHECKOK(group->meth->field_sub(&tempy, qy, &tempy, group->meth));
153 	MP_CHECKOK(mp_copy(&tempx, rx));
154 	MP_CHECKOK(mp_copy(&tempy, ry));
155 
156   CLEANUP:
157 	mp_clear(&lambda);
158 	mp_clear(&temp);
159 	mp_clear(&tempx);
160 	mp_clear(&tempy);
161 	return res;
162 }
163 
164 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
165  * identical. Uses affine coordinates. Assumes input is already
166  * field-encoded using field_enc, and returns output that is still
167  * field-encoded. */
168 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)169 ec_GFp_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
170 				  const mp_int *qy, mp_int *rx, mp_int *ry,
171 				  const ECGroup *group)
172 {
173 	mp_err res = MP_OKAY;
174 	mp_int nqy;
175 
176 	MP_DIGITS(&nqy) = 0;
177 	MP_CHECKOK(mp_init(&nqy, FLAG(px)));
178 	/* nqy = -qy */
179 	MP_CHECKOK(group->meth->field_neg(qy, &nqy, group->meth));
180 	res = group->point_add(px, py, qx, &nqy, rx, ry, group);
181   CLEANUP:
182 	mp_clear(&nqy);
183 	return res;
184 }
185 
186 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
187  * affine coordinates. Assumes input is already field-encoded using
188  * field_enc, and returns output that is still field-encoded. */
189 mp_err
ec_GFp_pt_dbl_aff(const mp_int * px,const mp_int * py,mp_int * rx,mp_int * ry,const ECGroup * group)190 ec_GFp_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
191 				  mp_int *ry, const ECGroup *group)
192 {
193 	return ec_GFp_pt_add_aff(px, py, px, py, rx, ry, group);
194 }
195 
196 /* by default, this routine is unused and thus doesn't need to be compiled */
197 #ifdef ECL_ENABLE_GFP_PT_MUL_AFF
198 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
199  * R can be identical. Uses affine coordinates. Assumes input is already
200  * field-encoded using field_enc, and returns output that is still
201  * field-encoded. */
202 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)203 ec_GFp_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
204 				  mp_int *rx, mp_int *ry, const ECGroup *group)
205 {
206 	mp_err res = MP_OKAY;
207 	mp_int k, k3, qx, qy, sx, sy;
208 	int b1, b3, i, l;
209 
210 	MP_DIGITS(&k) = 0;
211 	MP_DIGITS(&k3) = 0;
212 	MP_DIGITS(&qx) = 0;
213 	MP_DIGITS(&qy) = 0;
214 	MP_DIGITS(&sx) = 0;
215 	MP_DIGITS(&sy) = 0;
216 	MP_CHECKOK(mp_init(&k));
217 	MP_CHECKOK(mp_init(&k3));
218 	MP_CHECKOK(mp_init(&qx));
219 	MP_CHECKOK(mp_init(&qy));
220 	MP_CHECKOK(mp_init(&sx));
221 	MP_CHECKOK(mp_init(&sy));
222 
223 	/* if n = 0 then r = inf */
224 	if (mp_cmp_z(n) == 0) {
225 		mp_zero(rx);
226 		mp_zero(ry);
227 		res = MP_OKAY;
228 		goto CLEANUP;
229 	}
230 	/* Q = P, k = n */
231 	MP_CHECKOK(mp_copy(px, &qx));
232 	MP_CHECKOK(mp_copy(py, &qy));
233 	MP_CHECKOK(mp_copy(n, &k));
234 	/* if n < 0 then Q = -Q, k = -k */
235 	if (mp_cmp_z(n) < 0) {
236 		MP_CHECKOK(group->meth->field_neg(&qy, &qy, group->meth));
237 		MP_CHECKOK(mp_neg(&k, &k));
238 	}
239 #ifdef ECL_DEBUG				/* basic double and add method */
240 	l = mpl_significant_bits(&k) - 1;
241 	MP_CHECKOK(mp_copy(&qx, &sx));
242 	MP_CHECKOK(mp_copy(&qy, &sy));
243 	for (i = l - 1; i >= 0; i--) {
244 		/* S = 2S */
245 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
246 		/* if k_i = 1, then S = S + Q */
247 		if (mpl_get_bit(&k, i) != 0) {
248 			MP_CHECKOK(group->
249 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
250 		}
251 	}
252 #else							/* double and add/subtract method from
253 								 * standard */
254 	/* k3 = 3 * k */
255 	MP_CHECKOK(mp_set_int(&k3, 3));
256 	MP_CHECKOK(mp_mul(&k, &k3, &k3));
257 	/* S = Q */
258 	MP_CHECKOK(mp_copy(&qx, &sx));
259 	MP_CHECKOK(mp_copy(&qy, &sy));
260 	/* l = index of high order bit in binary representation of 3*k */
261 	l = mpl_significant_bits(&k3) - 1;
262 	/* for i = l-1 downto 1 */
263 	for (i = l - 1; i >= 1; i--) {
264 		/* S = 2S */
265 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
266 		b3 = MP_GET_BIT(&k3, i);
267 		b1 = MP_GET_BIT(&k, i);
268 		/* if k3_i = 1 and k_i = 0, then S = S + Q */
269 		if ((b3 == 1) && (b1 == 0)) {
270 			MP_CHECKOK(group->
271 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
272 			/* if k3_i = 0 and k_i = 1, then S = S - Q */
273 		} else if ((b3 == 0) && (b1 == 1)) {
274 			MP_CHECKOK(group->
275 					   point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
276 		}
277 	}
278 #endif
279 	/* output S */
280 	MP_CHECKOK(mp_copy(&sx, rx));
281 	MP_CHECKOK(mp_copy(&sy, ry));
282 
283   CLEANUP:
284 	mp_clear(&k);
285 	mp_clear(&k3);
286 	mp_clear(&qx);
287 	mp_clear(&qy);
288 	mp_clear(&sx);
289 	mp_clear(&sy);
290 	return res;
291 }
292 #endif
293 
294 /* Validates a point on a GFp curve. */
295 mp_err
ec_GFp_validate_point(const mp_int * px,const mp_int * py,const ECGroup * group)296 ec_GFp_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
297 {
298 	mp_err res = MP_NO;
299 	mp_int accl, accr, tmp, pxt, pyt;
300 
301 	MP_DIGITS(&accl) = 0;
302 	MP_DIGITS(&accr) = 0;
303 	MP_DIGITS(&tmp) = 0;
304 	MP_DIGITS(&pxt) = 0;
305 	MP_DIGITS(&pyt) = 0;
306 	MP_CHECKOK(mp_init(&accl, FLAG(px)));
307 	MP_CHECKOK(mp_init(&accr, FLAG(px)));
308 	MP_CHECKOK(mp_init(&tmp, FLAG(px)));
309 	MP_CHECKOK(mp_init(&pxt, FLAG(px)));
310 	MP_CHECKOK(mp_init(&pyt, FLAG(px)));
311 
312     /* 1: Verify that publicValue is not the point at infinity */
313 	if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
314 		res = MP_NO;
315 		goto CLEANUP;
316 	}
317     /* 2: Verify that the coordinates of publicValue are elements
318      *    of the field.
319      */
320 	if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
321 		(MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
322 		res = MP_NO;
323 		goto CLEANUP;
324 	}
325     /* 3: Verify that publicValue is on the curve. */
326 	if (group->meth->field_enc) {
327 		group->meth->field_enc(px, &pxt, group->meth);
328 		group->meth->field_enc(py, &pyt, group->meth);
329 	} else {
330 		mp_copy(px, &pxt);
331 		mp_copy(py, &pyt);
332 	}
333 	/* left-hand side: y^2  */
334 	MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
335 	/* right-hand side: x^3 + a*x + b */
336 	MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
337 	MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
338 	MP_CHECKOK( group->meth->field_mul(&group->curvea, &pxt, &tmp, group->meth) );
339 	MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
340 	MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
341 	/* check LHS - RHS == 0 */
342 	MP_CHECKOK( group->meth->field_sub(&accl, &accr, &accr, group->meth) );
343 	if (mp_cmp_z(&accr) != 0) {
344 		res = MP_NO;
345 		goto CLEANUP;
346 	}
347     /* 4: Verify that the order of the curve times the publicValue
348      *    is the point at infinity.
349      */
350 	MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
351 	if (ec_GFp_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
352 		res = MP_NO;
353 		goto CLEANUP;
354 	}
355 
356 	res = MP_YES;
357 
358 CLEANUP:
359 	mp_clear(&accl);
360 	mp_clear(&accr);
361 	mp_clear(&tmp);
362 	mp_clear(&pxt);
363 	mp_clear(&pyt);
364 	return res;
365 }
366