xref: /illumos-gate/usr/src/common/crypto/ecc/ecp_224.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
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  *   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 "ecp.h"
46 #include "mpi.h"
47 #include "mplogic.h"
48 #include "mpi-priv.h"
49 #ifndef _KERNEL
50 #include <stdlib.h>
51 #endif
52 
53 #define ECP224_DIGITS ECL_CURVE_DIGITS(224)
54 
55 /* Fast modular reduction for p224 = 2^224 - 2^96 + 1.  a can be r. Uses
56  * algorithm 7 from Brown, Hankerson, Lopez, Menezes. Software
57  * Implementation of the NIST Elliptic Curves over Prime Fields. */
58 mp_err
59 ec_GFp_nistp224_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60 {
61 	mp_err res = MP_OKAY;
62 	mp_size a_used = MP_USED(a);
63 
64 	int    r3b;
65 	mp_digit carry;
66 #ifdef ECL_THIRTY_TWO_BIT
67         mp_digit a6a = 0, a6b = 0,
68                 a5a = 0, a5b = 0, a4a = 0, a4b = 0, a3a = 0, a3b = 0;
69         mp_digit r0a, r0b, r1a, r1b, r2a, r2b, r3a;
70 #else
71 	mp_digit a6 = 0, a5 = 0, a4 = 0, a3b = 0, a5a = 0;
72         mp_digit a6b = 0, a6a_a5b = 0, a5b = 0, a5a_a4b = 0, a4a_a3b = 0;
73         mp_digit r0, r1, r2, r3;
74 #endif
75 
76 	/* reduction not needed if a is not larger than field size */
77 	if (a_used < ECP224_DIGITS) {
78 		if (a == r) return MP_OKAY;
79 		return mp_copy(a, r);
80 	}
81 	/* for polynomials larger than twice the field size, use regular
82 	 * reduction */
83 	if (a_used > ECL_CURVE_DIGITS(224*2)) {
84 		MP_CHECKOK(mp_mod(a, &meth->irr, r));
85 	} else {
86 #ifdef ECL_THIRTY_TWO_BIT
87 		/* copy out upper words of a */
88 		switch (a_used) {
89 		case 14:
90 			a6b = MP_DIGIT(a, 13);
91 			/* FALLTHROUGH */
92 		case 13:
93 			a6a = MP_DIGIT(a, 12);
94 			/* FALLTHROUGH */
95 		case 12:
96 			a5b = MP_DIGIT(a, 11);
97 			/* FALLTHROUGH */
98 		case 11:
99 			a5a = MP_DIGIT(a, 10);
100 			/* FALLTHROUGH */
101 		case 10:
102 			a4b = MP_DIGIT(a, 9);
103 			/* FALLTHROUGH */
104 		case 9:
105 			a4a = MP_DIGIT(a, 8);
106 			/* FALLTHROUGH */
107 		case 8:
108 			a3b = MP_DIGIT(a, 7);
109 		}
110 		r3a = MP_DIGIT(a, 6);
111 		r2b= MP_DIGIT(a, 5);
112 		r2a= MP_DIGIT(a, 4);
113 		r1b = MP_DIGIT(a, 3);
114 		r1a = MP_DIGIT(a, 2);
115 		r0b = MP_DIGIT(a, 1);
116 		r0a = MP_DIGIT(a, 0);
117 
118 
119 		/* implement r = (a3a,a2,a1,a0)
120 			+(a5a, a4,a3b,  0)
121 			+(  0, a6,a5b,  0)
122 			-(  0	 0,    0|a6b, a6a|a5b )
123 			-(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
124 		MP_ADD_CARRY (r1b, a3b, r1b, 0,     carry);
125 		MP_ADD_CARRY (r2a, a4a, r2a, carry, carry);
126 		MP_ADD_CARRY (r2b, a4b, r2b, carry, carry);
127 		MP_ADD_CARRY (r3a, a5a, r3a, carry, carry);
128 		r3b = carry;
129 		MP_ADD_CARRY (r1b, a5b, r1b, 0,     carry);
130 		MP_ADD_CARRY (r2a, a6a, r2a, carry, carry);
131 		MP_ADD_CARRY (r2b, a6b, r2b, carry, carry);
132 		MP_ADD_CARRY (r3a,   0, r3a, carry, carry);
133 		r3b += carry;
134 		MP_SUB_BORROW(r0a, a3b, r0a, 0,     carry);
135 		MP_SUB_BORROW(r0b, a4a, r0b, carry, carry);
136 		MP_SUB_BORROW(r1a, a4b, r1a, carry, carry);
137 		MP_SUB_BORROW(r1b, a5a, r1b, carry, carry);
138 		MP_SUB_BORROW(r2a, a5b, r2a, carry, carry);
139 		MP_SUB_BORROW(r2b, a6a, r2b, carry, carry);
140 		MP_SUB_BORROW(r3a, a6b, r3a, carry, carry);
141 		r3b -= carry;
142 		MP_SUB_BORROW(r0a, a5b, r0a, 0,     carry);
143 		MP_SUB_BORROW(r0b, a6a, r0b, carry, carry);
144 		MP_SUB_BORROW(r1a, a6b, r1a, carry, carry);
145 		if (carry) {
146 			MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
147 			MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
148 			MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
149 			MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
150 			r3b -= carry;
151 		}
152 
153 		while (r3b > 0) {
154 			int tmp;
155 			MP_ADD_CARRY(r1b, r3b, r1b, 0,     carry);
156 			if (carry) {
157 				MP_ADD_CARRY(r2a,  0, r2a, carry, carry);
158 				MP_ADD_CARRY(r2b,  0, r2b, carry, carry);
159 				MP_ADD_CARRY(r3a,  0, r3a, carry, carry);
160 			}
161 			tmp = carry;
162 			MP_SUB_BORROW(r0a, r3b, r0a, 0,     carry);
163 			if (carry) {
164 				MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
165 				MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
166 				MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
167 				MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
168 				MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
169 				MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
170 				tmp -= carry;
171 			}
172 			r3b = tmp;
173 		}
174 
175 		while (r3b < 0) {
176 			mp_digit maxInt = MP_DIGIT_MAX;
177                 	MP_ADD_CARRY (r0a, 1, r0a, 0,     carry);
178                 	MP_ADD_CARRY (r0b, 0, r0b, carry, carry);
179                 	MP_ADD_CARRY (r1a, 0, r1a, carry, carry);
180                 	MP_ADD_CARRY (r1b, maxInt, r1b, carry, carry);
181                 	MP_ADD_CARRY (r2a, maxInt, r2a, carry, carry);
182                 	MP_ADD_CARRY (r2b, maxInt, r2b, carry, carry);
183                 	MP_ADD_CARRY (r3a, maxInt, r3a, carry, carry);
184 			r3b += carry;
185 		}
186 		/* check for final reduction */
187 		/* now the only way we are over is if the top 4 words are all ones */
188 		if ((r3a == MP_DIGIT_MAX) && (r2b == MP_DIGIT_MAX)
189 			&& (r2a == MP_DIGIT_MAX) && (r1b == MP_DIGIT_MAX) &&
190 			 ((r1a != 0) || (r0b != 0) || (r0a != 0)) ) {
191 			/* one last subraction */
192 			MP_SUB_BORROW(r0a, 1, r0a, 0,     carry);
193 			MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
194 			MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
195 			r1b = r2a = r2b = r3a = 0;
196 		}
197 
198 
199 		if (a != r) {
200 			MP_CHECKOK(s_mp_pad(r, 7));
201 		}
202 		/* set the lower words of r */
203 		MP_SIGN(r) = MP_ZPOS;
204 		MP_USED(r) = 7;
205 		MP_DIGIT(r, 6) = r3a;
206 		MP_DIGIT(r, 5) = r2b;
207 		MP_DIGIT(r, 4) = r2a;
208 		MP_DIGIT(r, 3) = r1b;
209 		MP_DIGIT(r, 2) = r1a;
210 		MP_DIGIT(r, 1) = r0b;
211 		MP_DIGIT(r, 0) = r0a;
212 #else
213 		/* copy out upper words of a */
214 		switch (a_used) {
215 		case 7:
216 			a6 = MP_DIGIT(a, 6);
217 			a6b = a6 >> 32;
218 			a6a_a5b = a6 << 32;
219 			/* FALLTHROUGH */
220 		case 6:
221 			a5 = MP_DIGIT(a, 5);
222 			a5b = a5 >> 32;
223 			a6a_a5b |= a5b;
224 			a5b = a5b << 32;
225 			a5a_a4b = a5 << 32;
226 			a5a = a5 & 0xffffffff;
227 			/* FALLTHROUGH */
228 		case 5:
229 			a4 = MP_DIGIT(a, 4);
230 			a5a_a4b |= a4 >> 32;
231 			a4a_a3b = a4 << 32;
232 			/* FALLTHROUGH */
233 		case 4:
234 			a3b = MP_DIGIT(a, 3) >> 32;
235 			a4a_a3b |= a3b;
236 			a3b = a3b << 32;
237 		}
238 
239 		r3 = MP_DIGIT(a, 3) & 0xffffffff;
240 		r2 = MP_DIGIT(a, 2);
241 		r1 = MP_DIGIT(a, 1);
242 		r0 = MP_DIGIT(a, 0);
243 
244 		/* implement r = (a3a,a2,a1,a0)
245 			+(a5a, a4,a3b,  0)
246 			+(  0, a6,a5b,  0)
247 			-(  0	 0,    0|a6b, a6a|a5b )
248 			-(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
249 		MP_ADD_CARRY (r1, a3b, r1, 0,     carry);
250 		MP_ADD_CARRY (r2, a4 , r2, carry, carry);
251 		MP_ADD_CARRY (r3, a5a, r3, carry, carry);
252 		MP_ADD_CARRY (r1, a5b, r1, 0,     carry);
253 		MP_ADD_CARRY (r2, a6 , r2, carry, carry);
254 		MP_ADD_CARRY (r3,   0, r3, carry, carry);
255 
256 		MP_SUB_BORROW(r0, a4a_a3b, r0, 0,     carry);
257 		MP_SUB_BORROW(r1, a5a_a4b, r1, carry, carry);
258 		MP_SUB_BORROW(r2, a6a_a5b, r2, carry, carry);
259 		MP_SUB_BORROW(r3, a6b    , r3, carry, carry);
260 		MP_SUB_BORROW(r0, a6a_a5b, r0, 0,     carry);
261 		MP_SUB_BORROW(r1, a6b    , r1, carry, carry);
262 		if (carry) {
263 			MP_SUB_BORROW(r2, 0, r2, carry, carry);
264 			MP_SUB_BORROW(r3, 0, r3, carry, carry);
265 		}
266 
267 
268 		/* if the value is negative, r3 has a 2's complement
269 		 * high value */
270 		r3b = (int)(r3 >>32);
271 		while (r3b > 0) {
272 			r3 &= 0xffffffff;
273 			MP_ADD_CARRY(r1,((mp_digit)r3b) << 32, r1, 0, carry);
274 			if (carry) {
275 				MP_ADD_CARRY(r2,  0, r2, carry, carry);
276 				MP_ADD_CARRY(r3,  0, r3, carry, carry);
277 			}
278 			MP_SUB_BORROW(r0, r3b, r0, 0, carry);
279 			if (carry) {
280 				MP_SUB_BORROW(r1, 0, r1, carry, carry);
281 				MP_SUB_BORROW(r2, 0, r2, carry, carry);
282 				MP_SUB_BORROW(r3, 0, r3, carry, carry);
283 			}
284 			r3b = (int)(r3 >>32);
285 		}
286 
287 		while (r3b < 0) {
288                 	MP_ADD_CARRY (r0, 1, r0, 0,     carry);
289                 	MP_ADD_CARRY (r1, MP_DIGIT_MAX <<32, r1, carry, carry);
290                 	MP_ADD_CARRY (r2, MP_DIGIT_MAX, r2, carry, carry);
291                 	MP_ADD_CARRY (r3, MP_DIGIT_MAX >> 32, r3, carry, carry);
292 			r3b = (int)(r3 >>32);
293 		}
294 		/* check for final reduction */
295 		/* now the only way we are over is if the top 4 words are all ones */
296 		if ((r3 == (MP_DIGIT_MAX >> 32)) && (r2 == MP_DIGIT_MAX)
297 			&& ((r1 & MP_DIGIT_MAX << 32)== MP_DIGIT_MAX << 32) &&
298 			 ((r1 != MP_DIGIT_MAX << 32 ) || (r0 != 0)) ) {
299 			/* one last subraction */
300 			MP_SUB_BORROW(r0, 1, r0, 0,     carry);
301 			MP_SUB_BORROW(r1, 0, r1, carry, carry);
302 			r2 = r3 = 0;
303 		}
304 
305 
306 		if (a != r) {
307 			MP_CHECKOK(s_mp_pad(r, 4));
308 		}
309 		/* set the lower words of r */
310 		MP_SIGN(r) = MP_ZPOS;
311 		MP_USED(r) = 4;
312 		MP_DIGIT(r, 3) = r3;
313 		MP_DIGIT(r, 2) = r2;
314 		MP_DIGIT(r, 1) = r1;
315 		MP_DIGIT(r, 0) = r0;
316 #endif
317 	}
318 
319   CLEANUP:
320 	return res;
321 }
322 
323 /* Compute the square of polynomial a, reduce modulo p224. Store the
324  * result in r.  r could be a.  Uses optimized modular reduction for p224.
325  */
326 mp_err
327 ec_GFp_nistp224_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
328 {
329 	mp_err res = MP_OKAY;
330 
331 	MP_CHECKOK(mp_sqr(a, r));
332 	MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
333   CLEANUP:
334 	return res;
335 }
336 
337 /* Compute the product of two polynomials a and b, reduce modulo p224.
338  * Store the result in r.  r could be a or b; a could be b.  Uses
339  * optimized modular reduction for p224. */
340 mp_err
341 ec_GFp_nistp224_mul(const mp_int *a, const mp_int *b, mp_int *r,
342 					const GFMethod *meth)
343 {
344 	mp_err res = MP_OKAY;
345 
346 	MP_CHECKOK(mp_mul(a, b, r));
347 	MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
348   CLEANUP:
349 	return res;
350 }
351 
352 /* Divides two field elements. If a is NULL, then returns the inverse of
353  * b. */
354 mp_err
355 ec_GFp_nistp224_div(const mp_int *a, const mp_int *b, mp_int *r,
356 		   const GFMethod *meth)
357 {
358 	mp_err res = MP_OKAY;
359 	mp_int t;
360 
361 	/* If a is NULL, then return the inverse of b, otherwise return a/b. */
362 	if (a == NULL) {
363 		return  mp_invmod(b, &meth->irr, r);
364 	} else {
365 		/* MPI doesn't support divmod, so we implement it using invmod and
366 		 * mulmod. */
367 		MP_CHECKOK(mp_init(&t, FLAG(b)));
368 		MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
369 		MP_CHECKOK(mp_mul(a, &t, r));
370 		MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
371 	  CLEANUP:
372 		mp_clear(&t);
373 		return res;
374 	}
375 }
376 
377 /* Wire in fast field arithmetic and precomputation of base point for
378  * named curves. */
379 mp_err
380 ec_group_set_gfp224(ECGroup *group, ECCurveName name)
381 {
382 	if (name == ECCurve_NIST_P224) {
383 		group->meth->field_mod = &ec_GFp_nistp224_mod;
384 		group->meth->field_mul = &ec_GFp_nistp224_mul;
385 		group->meth->field_sqr = &ec_GFp_nistp224_sqr;
386 		group->meth->field_div = &ec_GFp_nistp224_div;
387 	}
388 	return MP_OKAY;
389 }
390