xref: /illumos-gate/usr/src/common/crypto/ecc/ec2_233.c (revision 30165b7f6753bc3d48c52319bed7ec7b3ea36b3c)
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  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
24  *   Stephen Fung <fungstep@hotmail.com>, and
25  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 /*
41  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
42  * Use is subject to license terms.
43  *
44  * Sun elects to use this software under the MPL license.
45  */
46 
47 #include "ec2.h"
48 #include "mp_gf2m.h"
49 #include "mp_gf2m-priv.h"
50 #include "mpi.h"
51 #include "mpi-priv.h"
52 #ifndef _KERNEL
53 #include <stdlib.h>
54 #endif
55 
56 /* Fast reduction for polynomials over a 233-bit curve. Assumes reduction
57  * polynomial with terms {233, 74, 0}. */
58 mp_err
59 ec_GF2m_233_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
60 {
61 	mp_err res = MP_OKAY;
62 	mp_digit *u, z;
63 
64 	if (a != r) {
65 		MP_CHECKOK(mp_copy(a, r));
66 	}
67 #ifdef ECL_SIXTY_FOUR_BIT
68 	if (MP_USED(r) < 8) {
69 		MP_CHECKOK(s_mp_pad(r, 8));
70 	}
71 	u = MP_DIGITS(r);
72 	MP_USED(r) = 8;
73 
74 	/* u[7] only has 18 significant bits */
75 	z = u[7];
76 	u[4] ^= (z << 33) ^ (z >> 41);
77 	u[3] ^= (z << 23);
78 	z = u[6];
79 	u[4] ^= (z >> 31);
80 	u[3] ^= (z << 33) ^ (z >> 41);
81 	u[2] ^= (z << 23);
82 	z = u[5];
83 	u[3] ^= (z >> 31);
84 	u[2] ^= (z << 33) ^ (z >> 41);
85 	u[1] ^= (z << 23);
86 	z = u[4];
87 	u[2] ^= (z >> 31);
88 	u[1] ^= (z << 33) ^ (z >> 41);
89 	u[0] ^= (z << 23);
90 	z = u[3] >> 41;				/* z only has 23 significant bits */
91 	u[1] ^= (z << 10);
92 	u[0] ^= z;
93 	/* clear bits above 233 */
94 	u[7] = u[6] = u[5] = u[4] = 0;
95 	u[3] ^= z << 41;
96 #else
97 	if (MP_USED(r) < 15) {
98 		MP_CHECKOK(s_mp_pad(r, 15));
99 	}
100 	u = MP_DIGITS(r);
101 	MP_USED(r) = 15;
102 
103 	/* u[14] only has 18 significant bits */
104 	z = u[14];
105 	u[9] ^= (z << 1);
106 	u[7] ^= (z >> 9);
107 	u[6] ^= (z << 23);
108 	z = u[13];
109 	u[9] ^= (z >> 31);
110 	u[8] ^= (z << 1);
111 	u[6] ^= (z >> 9);
112 	u[5] ^= (z << 23);
113 	z = u[12];
114 	u[8] ^= (z >> 31);
115 	u[7] ^= (z << 1);
116 	u[5] ^= (z >> 9);
117 	u[4] ^= (z << 23);
118 	z = u[11];
119 	u[7] ^= (z >> 31);
120 	u[6] ^= (z << 1);
121 	u[4] ^= (z >> 9);
122 	u[3] ^= (z << 23);
123 	z = u[10];
124 	u[6] ^= (z >> 31);
125 	u[5] ^= (z << 1);
126 	u[3] ^= (z >> 9);
127 	u[2] ^= (z << 23);
128 	z = u[9];
129 	u[5] ^= (z >> 31);
130 	u[4] ^= (z << 1);
131 	u[2] ^= (z >> 9);
132 	u[1] ^= (z << 23);
133 	z = u[8];
134 	u[4] ^= (z >> 31);
135 	u[3] ^= (z << 1);
136 	u[1] ^= (z >> 9);
137 	u[0] ^= (z << 23);
138 	z = u[7] >> 9;				/* z only has 23 significant bits */
139 	u[3] ^= (z >> 22);
140 	u[2] ^= (z << 10);
141 	u[0] ^= z;
142 	/* clear bits above 233 */
143 	u[14] = u[13] = u[12] = u[11] = u[10] = u[9] = u[8] = 0;
144 	u[7] ^= z << 9;
145 #endif
146 	s_mp_clamp(r);
147 
148   CLEANUP:
149 	return res;
150 }
151 
152 /* Fast squaring for polynomials over a 233-bit curve. Assumes reduction
153  * polynomial with terms {233, 74, 0}. */
154 mp_err
155 ec_GF2m_233_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
156 {
157 	mp_err res = MP_OKAY;
158 	mp_digit *u, *v;
159 
160 	v = MP_DIGITS(a);
161 
162 #ifdef ECL_SIXTY_FOUR_BIT
163 	if (MP_USED(a) < 4) {
164 		return mp_bsqrmod(a, meth->irr_arr, r);
165 	}
166 	if (MP_USED(r) < 8) {
167 		MP_CHECKOK(s_mp_pad(r, 8));
168 	}
169 	MP_USED(r) = 8;
170 #else
171 	if (MP_USED(a) < 8) {
172 		return mp_bsqrmod(a, meth->irr_arr, r);
173 	}
174 	if (MP_USED(r) < 15) {
175 		MP_CHECKOK(s_mp_pad(r, 15));
176 	}
177 	MP_USED(r) = 15;
178 #endif
179 	u = MP_DIGITS(r);
180 
181 #ifdef ECL_THIRTY_TWO_BIT
182 	u[14] = gf2m_SQR0(v[7]);
183 	u[13] = gf2m_SQR1(v[6]);
184 	u[12] = gf2m_SQR0(v[6]);
185 	u[11] = gf2m_SQR1(v[5]);
186 	u[10] = gf2m_SQR0(v[5]);
187 	u[9] = gf2m_SQR1(v[4]);
188 	u[8] = gf2m_SQR0(v[4]);
189 #endif
190 	u[7] = gf2m_SQR1(v[3]);
191 	u[6] = gf2m_SQR0(v[3]);
192 	u[5] = gf2m_SQR1(v[2]);
193 	u[4] = gf2m_SQR0(v[2]);
194 	u[3] = gf2m_SQR1(v[1]);
195 	u[2] = gf2m_SQR0(v[1]);
196 	u[1] = gf2m_SQR1(v[0]);
197 	u[0] = gf2m_SQR0(v[0]);
198 	return ec_GF2m_233_mod(r, r, meth);
199 
200   CLEANUP:
201 	return res;
202 }
203 
204 /* Fast multiplication for polynomials over a 233-bit curve. Assumes
205  * reduction polynomial with terms {233, 74, 0}. */
206 mp_err
207 ec_GF2m_233_mul(const mp_int *a, const mp_int *b, mp_int *r,
208 				const GFMethod *meth)
209 {
210 	mp_err res = MP_OKAY;
211 	mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
212 
213 #ifdef ECL_THIRTY_TWO_BIT
214 	mp_digit a7 = 0, a6 = 0, a5 = 0, a4 = 0, b7 = 0, b6 = 0, b5 = 0, b4 =
215 		0;
216 	mp_digit rm[8];
217 #endif
218 
219 	if (a == b) {
220 		return ec_GF2m_233_sqr(a, r, meth);
221 	} else {
222 		switch (MP_USED(a)) {
223 #ifdef ECL_THIRTY_TWO_BIT
224 		case 8:
225 			a7 = MP_DIGIT(a, 7);
226 			/* FALLTHROUGH */
227 		case 7:
228 			a6 = MP_DIGIT(a, 6);
229 			/* FALLTHROUGH */
230 		case 6:
231 			a5 = MP_DIGIT(a, 5);
232 			/* FALLTHROUGH */
233 		case 5:
234 			a4 = MP_DIGIT(a, 4);
235 #endif
236 			/* FALLTHROUGH */
237 		case 4:
238 			a3 = MP_DIGIT(a, 3);
239 			/* FALLTHROUGH */
240 		case 3:
241 			a2 = MP_DIGIT(a, 2);
242 			/* FALLTHROUGH */
243 		case 2:
244 			a1 = MP_DIGIT(a, 1);
245 			/* FALLTHROUGH */
246 		default:
247 			a0 = MP_DIGIT(a, 0);
248 		}
249 		switch (MP_USED(b)) {
250 #ifdef ECL_THIRTY_TWO_BIT
251 		case 8:
252 			b7 = MP_DIGIT(b, 7);
253 			/* FALLTHROUGH */
254 		case 7:
255 			b6 = MP_DIGIT(b, 6);
256 			/* FALLTHROUGH */
257 		case 6:
258 			b5 = MP_DIGIT(b, 5);
259 			/* FALLTHROUGH */
260 		case 5:
261 			b4 = MP_DIGIT(b, 4);
262 #endif
263 			/* FALLTHROUGH */
264 		case 4:
265 			b3 = MP_DIGIT(b, 3);
266 			/* FALLTHROUGH */
267 		case 3:
268 			b2 = MP_DIGIT(b, 2);
269 			/* FALLTHROUGH */
270 		case 2:
271 			b1 = MP_DIGIT(b, 1);
272 			/* FALLTHROUGH */
273 		default:
274 			b0 = MP_DIGIT(b, 0);
275 		}
276 #ifdef ECL_SIXTY_FOUR_BIT
277 		MP_CHECKOK(s_mp_pad(r, 8));
278 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
279 		MP_USED(r) = 8;
280 		s_mp_clamp(r);
281 #else
282 		MP_CHECKOK(s_mp_pad(r, 16));
283 		s_bmul_4x4(MP_DIGITS(r) + 8, a7, a6, a5, a4, b7, b6, b5, b4);
284 		s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
285 		s_bmul_4x4(rm, a7 ^ a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b7 ^ b3,
286 				   b6 ^ b2, b5 ^ b1, b4 ^ b0);
287 		rm[7] ^= MP_DIGIT(r, 7) ^ MP_DIGIT(r, 15);
288 		rm[6] ^= MP_DIGIT(r, 6) ^ MP_DIGIT(r, 14);
289 		rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
290 		rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
291 		rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
292 		rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
293 		rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
294 		rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
295 		MP_DIGIT(r, 11) ^= rm[7];
296 		MP_DIGIT(r, 10) ^= rm[6];
297 		MP_DIGIT(r, 9) ^= rm[5];
298 		MP_DIGIT(r, 8) ^= rm[4];
299 		MP_DIGIT(r, 7) ^= rm[3];
300 		MP_DIGIT(r, 6) ^= rm[2];
301 		MP_DIGIT(r, 5) ^= rm[1];
302 		MP_DIGIT(r, 4) ^= rm[0];
303 		MP_USED(r) = 16;
304 		s_mp_clamp(r);
305 #endif
306 		return ec_GF2m_233_mod(r, r, meth);
307 	}
308 
309   CLEANUP:
310 	return res;
311 }
312 
313 /* Wire in fast field arithmetic for 233-bit curves. */
314 mp_err
315 ec_group_set_gf2m233(ECGroup *group, ECCurveName name)
316 {
317 	group->meth->field_mod = &ec_GF2m_233_mod;
318 	group->meth->field_mul = &ec_GF2m_233_mul;
319 	group->meth->field_sqr = &ec_GF2m_233_sqr;
320 	return MP_OKAY;
321 }
322