xref: /illumos-gate/usr/src/common/crypto/ecc/ec2_aff.c (revision d227ea68051b5404410ee1880d0bba1ebb33a4cc)
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 binary polynomial 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  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 /*
39  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
40  * Use is subject to license terms.
41  *
42  * Sun elects to use this software under the MPL license.
43  */
44 
45 #include "ec2.h"
46 #include "mplogic.h"
47 #include "mp_gf2m.h"
48 #ifndef _KERNEL
49 #include <stdlib.h>
50 #endif
51 
52 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
53 mp_err
54 ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
55 {
56 
57 	if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
58 		return MP_YES;
59 	} else {
60 		return MP_NO;
61 	}
62 
63 }
64 
65 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
66 mp_err
67 ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
68 {
69 	mp_zero(px);
70 	mp_zero(py);
71 	return MP_OKAY;
72 }
73 
74 /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P,
75  * Q, and R can all be identical. Uses affine coordinates. */
76 mp_err
77 ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
78 				   const mp_int *qy, mp_int *rx, mp_int *ry,
79 				   const ECGroup *group)
80 {
81 	mp_err res = MP_OKAY;
82 	mp_int lambda, tempx, tempy;
83 
84 	MP_DIGITS(&lambda) = 0;
85 	MP_DIGITS(&tempx) = 0;
86 	MP_DIGITS(&tempy) = 0;
87 	MP_CHECKOK(mp_init(&lambda, FLAG(px)));
88 	MP_CHECKOK(mp_init(&tempx, FLAG(px)));
89 	MP_CHECKOK(mp_init(&tempy, FLAG(px)));
90 	/* if P = inf, then R = Q */
91 	if (ec_GF2m_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_GF2m_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), tempx = a + lambda^2
105 	 * + lambda + px + qx */
106 	if (mp_cmp(px, qx) != 0) {
107 		MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
108 		MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
109 		MP_CHECKOK(group->meth->
110 				   field_div(&tempy, &tempx, &lambda, group->meth));
111 		MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
112 		MP_CHECKOK(group->meth->
113 				   field_add(&tempx, &lambda, &tempx, group->meth));
114 		MP_CHECKOK(group->meth->
115 				   field_add(&tempx, &group->curvea, &tempx, group->meth));
116 		MP_CHECKOK(group->meth->
117 				   field_add(&tempx, px, &tempx, group->meth));
118 		MP_CHECKOK(group->meth->
119 				   field_add(&tempx, qx, &tempx, group->meth));
120 	} else {
121 		/* if py != qy or qx = 0, then R = inf */
122 		if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
123 			mp_zero(rx);
124 			mp_zero(ry);
125 			res = MP_OKAY;
126 			goto CLEANUP;
127 		}
128 		/* lambda = qx + qy / qx */
129 		MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
130 		MP_CHECKOK(group->meth->
131 				   field_add(&lambda, qx, &lambda, group->meth));
132 		/* tempx = a + lambda^2 + lambda */
133 		MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
134 		MP_CHECKOK(group->meth->
135 				   field_add(&tempx, &lambda, &tempx, group->meth));
136 		MP_CHECKOK(group->meth->
137 				   field_add(&tempx, &group->curvea, &tempx, group->meth));
138 	}
139 	/* ry = (qx + tempx) * lambda + tempx + qy */
140 	MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
141 	MP_CHECKOK(group->meth->
142 			   field_mul(&tempy, &lambda, &tempy, group->meth));
143 	MP_CHECKOK(group->meth->
144 			   field_add(&tempy, &tempx, &tempy, group->meth));
145 	MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
146 	/* rx = tempx */
147 	MP_CHECKOK(mp_copy(&tempx, rx));
148 
149   CLEANUP:
150 	mp_clear(&lambda);
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. */
158 mp_err
159 ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
160 				   const mp_int *qy, mp_int *rx, mp_int *ry,
161 				   const ECGroup *group)
162 {
163 	mp_err res = MP_OKAY;
164 	mp_int nqy;
165 
166 	MP_DIGITS(&nqy) = 0;
167 	MP_CHECKOK(mp_init(&nqy, FLAG(px)));
168 	/* nqy = qx+qy */
169 	MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
170 	MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
171   CLEANUP:
172 	mp_clear(&nqy);
173 	return res;
174 }
175 
176 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
177  * affine coordinates. */
178 mp_err
179 ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
180 				   mp_int *ry, const ECGroup *group)
181 {
182 	return group->point_add(px, py, px, py, rx, ry, group);
183 }
184 
185 /* by default, this routine is unused and thus doesn't need to be compiled */
186 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
187 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and
188  * R can be identical. Uses affine coordinates. */
189 mp_err
190 ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
191 				   mp_int *rx, mp_int *ry, const ECGroup *group)
192 {
193 	mp_err res = MP_OKAY;
194 	mp_int k, k3, qx, qy, sx, sy;
195 	int b1, b3, i, l;
196 
197 	MP_DIGITS(&k) = 0;
198 	MP_DIGITS(&k3) = 0;
199 	MP_DIGITS(&qx) = 0;
200 	MP_DIGITS(&qy) = 0;
201 	MP_DIGITS(&sx) = 0;
202 	MP_DIGITS(&sy) = 0;
203 	MP_CHECKOK(mp_init(&k));
204 	MP_CHECKOK(mp_init(&k3));
205 	MP_CHECKOK(mp_init(&qx));
206 	MP_CHECKOK(mp_init(&qy));
207 	MP_CHECKOK(mp_init(&sx));
208 	MP_CHECKOK(mp_init(&sy));
209 
210 	/* if n = 0 then r = inf */
211 	if (mp_cmp_z(n) == 0) {
212 		mp_zero(rx);
213 		mp_zero(ry);
214 		res = MP_OKAY;
215 		goto CLEANUP;
216 	}
217 	/* Q = P, k = n */
218 	MP_CHECKOK(mp_copy(px, &qx));
219 	MP_CHECKOK(mp_copy(py, &qy));
220 	MP_CHECKOK(mp_copy(n, &k));
221 	/* if n < 0 then Q = -Q, k = -k */
222 	if (mp_cmp_z(n) < 0) {
223 		MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
224 		MP_CHECKOK(mp_neg(&k, &k));
225 	}
226 #ifdef ECL_DEBUG				/* basic double and add method */
227 	l = mpl_significant_bits(&k) - 1;
228 	MP_CHECKOK(mp_copy(&qx, &sx));
229 	MP_CHECKOK(mp_copy(&qy, &sy));
230 	for (i = l - 1; i >= 0; i--) {
231 		/* S = 2S */
232 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
233 		/* if k_i = 1, then S = S + Q */
234 		if (mpl_get_bit(&k, i) != 0) {
235 			MP_CHECKOK(group->
236 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
237 		}
238 	}
239 #else							/* double and add/subtract method from
240 								 * standard */
241 	/* k3 = 3 * k */
242 	MP_CHECKOK(mp_set_int(&k3, 3));
243 	MP_CHECKOK(mp_mul(&k, &k3, &k3));
244 	/* S = Q */
245 	MP_CHECKOK(mp_copy(&qx, &sx));
246 	MP_CHECKOK(mp_copy(&qy, &sy));
247 	/* l = index of high order bit in binary representation of 3*k */
248 	l = mpl_significant_bits(&k3) - 1;
249 	/* for i = l-1 downto 1 */
250 	for (i = l - 1; i >= 1; i--) {
251 		/* S = 2S */
252 		MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
253 		b3 = MP_GET_BIT(&k3, i);
254 		b1 = MP_GET_BIT(&k, i);
255 		/* if k3_i = 1 and k_i = 0, then S = S + Q */
256 		if ((b3 == 1) && (b1 == 0)) {
257 			MP_CHECKOK(group->
258 					   point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
259 			/* if k3_i = 0 and k_i = 1, then S = S - Q */
260 		} else if ((b3 == 0) && (b1 == 1)) {
261 			MP_CHECKOK(group->
262 					   point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
263 		}
264 	}
265 #endif
266 	/* output S */
267 	MP_CHECKOK(mp_copy(&sx, rx));
268 	MP_CHECKOK(mp_copy(&sy, ry));
269 
270   CLEANUP:
271 	mp_clear(&k);
272 	mp_clear(&k3);
273 	mp_clear(&qx);
274 	mp_clear(&qy);
275 	mp_clear(&sx);
276 	mp_clear(&sy);
277 	return res;
278 }
279 #endif
280 
281 /* Validates a point on a GF2m curve. */
282 mp_err
283 ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
284 {
285 	mp_err res = MP_NO;
286 	mp_int accl, accr, tmp, pxt, pyt;
287 
288 	MP_DIGITS(&accl) = 0;
289 	MP_DIGITS(&accr) = 0;
290 	MP_DIGITS(&tmp) = 0;
291 	MP_DIGITS(&pxt) = 0;
292 	MP_DIGITS(&pyt) = 0;
293 	MP_CHECKOK(mp_init(&accl, FLAG(px)));
294 	MP_CHECKOK(mp_init(&accr, FLAG(px)));
295 	MP_CHECKOK(mp_init(&tmp, FLAG(px)));
296 	MP_CHECKOK(mp_init(&pxt, FLAG(px)));
297 	MP_CHECKOK(mp_init(&pyt, FLAG(px)));
298 
299     /* 1: Verify that publicValue is not the point at infinity */
300 	if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
301 		res = MP_NO;
302 		goto CLEANUP;
303 	}
304     /* 2: Verify that the coordinates of publicValue are elements
305      *    of the field.
306      */
307 	if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) ||
308 		(MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
309 		res = MP_NO;
310 		goto CLEANUP;
311 	}
312     /* 3: Verify that publicValue is on the curve. */
313 	if (group->meth->field_enc) {
314 		group->meth->field_enc(px, &pxt, group->meth);
315 		group->meth->field_enc(py, &pyt, group->meth);
316 	} else {
317 		mp_copy(px, &pxt);
318 		mp_copy(py, &pyt);
319 	}
320 	/* left-hand side: y^2 + x*y  */
321 	MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
322 	MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
323 	MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
324 	/* right-hand side: x^3 + a*x^2 + b */
325 	MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
326 	MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
327 	MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
328 	MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
329 	MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
330 	/* check LHS - RHS == 0 */
331 	MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
332 	if (mp_cmp_z(&accr) != 0) {
333 		res = MP_NO;
334 		goto CLEANUP;
335 	}
336     /* 4: Verify that the order of the curve times the publicValue
337      *    is the point at infinity.
338      */
339 	MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
340 	if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
341 		res = MP_NO;
342 		goto CLEANUP;
343 	}
344 
345 	res = MP_YES;
346 
347 CLEANUP:
348 	mp_clear(&accl);
349 	mp_clear(&accr);
350 	mp_clear(&tmp);
351 	mp_clear(&pxt);
352 	mp_clear(&pyt);
353 	return res;
354 }
355