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 #pragma ident "%Z%%M% %I% %E% SMI"
48
49 #include "ec2.h"
50 #include "mp_gf2m.h"
51 #include "mp_gf2m-priv.h"
52 #include "mpi.h"
53 #include "mpi-priv.h"
54 #ifndef _KERNEL
55 #include <stdlib.h>
56 #endif
57
58 /* Fast reduction for polynomials over a 233-bit curve. Assumes reduction
59 * polynomial with terms {233, 74, 0}. */
60 mp_err
ec_GF2m_233_mod(const mp_int * a,mp_int * r,const GFMethod * meth)61 ec_GF2m_233_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
62 {
63 mp_err res = MP_OKAY;
64 mp_digit *u, z;
65
66 if (a != r) {
67 MP_CHECKOK(mp_copy(a, r));
68 }
69 #ifdef ECL_SIXTY_FOUR_BIT
70 if (MP_USED(r) < 8) {
71 MP_CHECKOK(s_mp_pad(r, 8));
72 }
73 u = MP_DIGITS(r);
74 MP_USED(r) = 8;
75
76 /* u[7] only has 18 significant bits */
77 z = u[7];
78 u[4] ^= (z << 33) ^ (z >> 41);
79 u[3] ^= (z << 23);
80 z = u[6];
81 u[4] ^= (z >> 31);
82 u[3] ^= (z << 33) ^ (z >> 41);
83 u[2] ^= (z << 23);
84 z = u[5];
85 u[3] ^= (z >> 31);
86 u[2] ^= (z << 33) ^ (z >> 41);
87 u[1] ^= (z << 23);
88 z = u[4];
89 u[2] ^= (z >> 31);
90 u[1] ^= (z << 33) ^ (z >> 41);
91 u[0] ^= (z << 23);
92 z = u[3] >> 41; /* z only has 23 significant bits */
93 u[1] ^= (z << 10);
94 u[0] ^= z;
95 /* clear bits above 233 */
96 u[7] = u[6] = u[5] = u[4] = 0;
97 u[3] ^= z << 41;
98 #else
99 if (MP_USED(r) < 15) {
100 MP_CHECKOK(s_mp_pad(r, 15));
101 }
102 u = MP_DIGITS(r);
103 MP_USED(r) = 15;
104
105 /* u[14] only has 18 significant bits */
106 z = u[14];
107 u[9] ^= (z << 1);
108 u[7] ^= (z >> 9);
109 u[6] ^= (z << 23);
110 z = u[13];
111 u[9] ^= (z >> 31);
112 u[8] ^= (z << 1);
113 u[6] ^= (z >> 9);
114 u[5] ^= (z << 23);
115 z = u[12];
116 u[8] ^= (z >> 31);
117 u[7] ^= (z << 1);
118 u[5] ^= (z >> 9);
119 u[4] ^= (z << 23);
120 z = u[11];
121 u[7] ^= (z >> 31);
122 u[6] ^= (z << 1);
123 u[4] ^= (z >> 9);
124 u[3] ^= (z << 23);
125 z = u[10];
126 u[6] ^= (z >> 31);
127 u[5] ^= (z << 1);
128 u[3] ^= (z >> 9);
129 u[2] ^= (z << 23);
130 z = u[9];
131 u[5] ^= (z >> 31);
132 u[4] ^= (z << 1);
133 u[2] ^= (z >> 9);
134 u[1] ^= (z << 23);
135 z = u[8];
136 u[4] ^= (z >> 31);
137 u[3] ^= (z << 1);
138 u[1] ^= (z >> 9);
139 u[0] ^= (z << 23);
140 z = u[7] >> 9; /* z only has 23 significant bits */
141 u[3] ^= (z >> 22);
142 u[2] ^= (z << 10);
143 u[0] ^= z;
144 /* clear bits above 233 */
145 u[14] = u[13] = u[12] = u[11] = u[10] = u[9] = u[8] = 0;
146 u[7] ^= z << 9;
147 #endif
148 s_mp_clamp(r);
149
150 CLEANUP:
151 return res;
152 }
153
154 /* Fast squaring for polynomials over a 233-bit curve. Assumes reduction
155 * polynomial with terms {233, 74, 0}. */
156 mp_err
ec_GF2m_233_sqr(const mp_int * a,mp_int * r,const GFMethod * meth)157 ec_GF2m_233_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
158 {
159 mp_err res = MP_OKAY;
160 mp_digit *u, *v;
161
162 v = MP_DIGITS(a);
163
164 #ifdef ECL_SIXTY_FOUR_BIT
165 if (MP_USED(a) < 4) {
166 return mp_bsqrmod(a, meth->irr_arr, r);
167 }
168 if (MP_USED(r) < 8) {
169 MP_CHECKOK(s_mp_pad(r, 8));
170 }
171 MP_USED(r) = 8;
172 #else
173 if (MP_USED(a) < 8) {
174 return mp_bsqrmod(a, meth->irr_arr, r);
175 }
176 if (MP_USED(r) < 15) {
177 MP_CHECKOK(s_mp_pad(r, 15));
178 }
179 MP_USED(r) = 15;
180 #endif
181 u = MP_DIGITS(r);
182
183 #ifdef ECL_THIRTY_TWO_BIT
184 u[14] = gf2m_SQR0(v[7]);
185 u[13] = gf2m_SQR1(v[6]);
186 u[12] = gf2m_SQR0(v[6]);
187 u[11] = gf2m_SQR1(v[5]);
188 u[10] = gf2m_SQR0(v[5]);
189 u[9] = gf2m_SQR1(v[4]);
190 u[8] = gf2m_SQR0(v[4]);
191 #endif
192 u[7] = gf2m_SQR1(v[3]);
193 u[6] = gf2m_SQR0(v[3]);
194 u[5] = gf2m_SQR1(v[2]);
195 u[4] = gf2m_SQR0(v[2]);
196 u[3] = gf2m_SQR1(v[1]);
197 u[2] = gf2m_SQR0(v[1]);
198 u[1] = gf2m_SQR1(v[0]);
199 u[0] = gf2m_SQR0(v[0]);
200 return ec_GF2m_233_mod(r, r, meth);
201
202 CLEANUP:
203 return res;
204 }
205
206 /* Fast multiplication for polynomials over a 233-bit curve. Assumes
207 * reduction polynomial with terms {233, 74, 0}. */
208 mp_err
ec_GF2m_233_mul(const mp_int * a,const mp_int * b,mp_int * r,const GFMethod * meth)209 ec_GF2m_233_mul(const mp_int *a, const mp_int *b, mp_int *r,
210 const GFMethod *meth)
211 {
212 mp_err res = MP_OKAY;
213 mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
214
215 #ifdef ECL_THIRTY_TWO_BIT
216 mp_digit a7 = 0, a6 = 0, a5 = 0, a4 = 0, b7 = 0, b6 = 0, b5 = 0, b4 =
217 0;
218 mp_digit rm[8];
219 #endif
220
221 if (a == b) {
222 return ec_GF2m_233_sqr(a, r, meth);
223 } else {
224 switch (MP_USED(a)) {
225 #ifdef ECL_THIRTY_TWO_BIT
226 case 8:
227 a7 = MP_DIGIT(a, 7);
228 case 7:
229 a6 = MP_DIGIT(a, 6);
230 case 6:
231 a5 = MP_DIGIT(a, 5);
232 case 5:
233 a4 = MP_DIGIT(a, 4);
234 #endif
235 case 4:
236 a3 = MP_DIGIT(a, 3);
237 case 3:
238 a2 = MP_DIGIT(a, 2);
239 case 2:
240 a1 = MP_DIGIT(a, 1);
241 default:
242 a0 = MP_DIGIT(a, 0);
243 }
244 switch (MP_USED(b)) {
245 #ifdef ECL_THIRTY_TWO_BIT
246 case 8:
247 b7 = MP_DIGIT(b, 7);
248 case 7:
249 b6 = MP_DIGIT(b, 6);
250 case 6:
251 b5 = MP_DIGIT(b, 5);
252 case 5:
253 b4 = MP_DIGIT(b, 4);
254 #endif
255 case 4:
256 b3 = MP_DIGIT(b, 3);
257 case 3:
258 b2 = MP_DIGIT(b, 2);
259 case 2:
260 b1 = MP_DIGIT(b, 1);
261 default:
262 b0 = MP_DIGIT(b, 0);
263 }
264 #ifdef ECL_SIXTY_FOUR_BIT
265 MP_CHECKOK(s_mp_pad(r, 8));
266 s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
267 MP_USED(r) = 8;
268 s_mp_clamp(r);
269 #else
270 MP_CHECKOK(s_mp_pad(r, 16));
271 s_bmul_4x4(MP_DIGITS(r) + 8, a7, a6, a5, a4, b7, b6, b5, b4);
272 s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
273 s_bmul_4x4(rm, a7 ^ a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b7 ^ b3,
274 b6 ^ b2, b5 ^ b1, b4 ^ b0);
275 rm[7] ^= MP_DIGIT(r, 7) ^ MP_DIGIT(r, 15);
276 rm[6] ^= MP_DIGIT(r, 6) ^ MP_DIGIT(r, 14);
277 rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
278 rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
279 rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
280 rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
281 rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
282 rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
283 MP_DIGIT(r, 11) ^= rm[7];
284 MP_DIGIT(r, 10) ^= rm[6];
285 MP_DIGIT(r, 9) ^= rm[5];
286 MP_DIGIT(r, 8) ^= rm[4];
287 MP_DIGIT(r, 7) ^= rm[3];
288 MP_DIGIT(r, 6) ^= rm[2];
289 MP_DIGIT(r, 5) ^= rm[1];
290 MP_DIGIT(r, 4) ^= rm[0];
291 MP_USED(r) = 16;
292 s_mp_clamp(r);
293 #endif
294 return ec_GF2m_233_mod(r, r, meth);
295 }
296
297 CLEANUP:
298 return res;
299 }
300
301 /* Wire in fast field arithmetic for 233-bit curves. */
302 mp_err
ec_group_set_gf2m233(ECGroup * group,ECCurveName name)303 ec_group_set_gf2m233(ECGroup *group, ECCurveName name)
304 {
305 group->meth->field_mod = &ec_GF2m_233_mod;
306 group->meth->field_mul = &ec_GF2m_233_mul;
307 group->meth->field_sqr = &ec_GF2m_233_sqr;
308 return MP_OKAY;
309 }
310