|
1 /* ********************************************************************* |
|
2 * |
|
3 * Sun elects to have this file available under and governed by the |
|
4 * Mozilla Public License Version 1.1 ("MPL") (see |
|
5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance |
|
6 * of doubt and subject to the following, Sun also elects to allow |
|
7 * licensees to use this file under the MPL, the GNU General Public |
|
8 * License version 2 only or the Lesser General Public License version |
|
9 * 2.1 only. Any references to the "GNU General Public License version 2 |
|
10 * or later" or "GPL" in the following shall be construed to mean the |
|
11 * GNU General Public License version 2 only. Any references to the "GNU |
|
12 * Lesser General Public License version 2.1 or later" or "LGPL" in the |
|
13 * following shall be construed to mean the GNU Lesser General Public |
|
14 * License version 2.1 only. However, the following notice accompanied |
|
15 * the original version of this file: |
|
16 * |
|
17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
18 * |
|
19 * The contents of this file are subject to the Mozilla Public License Version |
|
20 * 1.1 (the "License"); you may not use this file except in compliance with |
|
21 * the License. You may obtain a copy of the License at |
|
22 * http://www.mozilla.org/MPL/ |
|
23 * |
|
24 * Software distributed under the License is distributed on an "AS IS" basis, |
|
25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|
26 * for the specific language governing rights and limitations under the |
|
27 * License. |
|
28 * |
|
29 * The Original Code is the elliptic curve math library for binary polynomial field curves. |
|
30 * |
|
31 * The Initial Developer of the Original Code is |
|
32 * Sun Microsystems, Inc. |
|
33 * Portions created by the Initial Developer are Copyright (C) 2003 |
|
34 * the Initial Developer. All Rights Reserved. |
|
35 * |
|
36 * Contributor(s): |
|
37 * Sheueling Chang-Shantz <sheueling.chang@sun.com>, |
|
38 * Stephen Fung <fungstep@hotmail.com>, and |
|
39 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories. |
|
40 * |
|
41 * Alternatively, the contents of this file may be used under the terms of |
|
42 * either the GNU General Public License Version 2 or later (the "GPL"), or |
|
43 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|
44 * in which case the provisions of the GPL or the LGPL are applicable instead |
|
45 * of those above. If you wish to allow use of your version of this file only |
|
46 * under the terms of either the GPL or the LGPL, and not to allow others to |
|
47 * use your version of this file under the terms of the MPL, indicate your |
|
48 * decision by deleting the provisions above and replace them with the notice |
|
49 * and other provisions required by the GPL or the LGPL. If you do not delete |
|
50 * the provisions above, a recipient may use your version of this file under |
|
51 * the terms of any one of the MPL, the GPL or the LGPL. |
|
52 * |
|
53 *********************************************************************** */ |
|
54 /* |
|
55 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
|
56 * Use is subject to license terms. |
|
57 */ |
|
58 |
|
59 #pragma ident "%Z%%M% %I% %E% SMI" |
|
60 |
|
61 #include "ec2.h" |
|
62 #include "mp_gf2m.h" |
|
63 #include "mp_gf2m-priv.h" |
|
64 #include "mpi.h" |
|
65 #include "mpi-priv.h" |
|
66 #ifndef _KERNEL |
|
67 #include <stdlib.h> |
|
68 #endif |
|
69 |
|
70 /* Fast reduction for polynomials over a 193-bit curve. Assumes reduction |
|
71 * polynomial with terms {193, 15, 0}. */ |
|
72 mp_err |
|
73 ec_GF2m_193_mod(const mp_int *a, mp_int *r, const GFMethod *meth) |
|
74 { |
|
75 mp_err res = MP_OKAY; |
|
76 mp_digit *u, z; |
|
77 |
|
78 if (a != r) { |
|
79 MP_CHECKOK(mp_copy(a, r)); |
|
80 } |
|
81 #ifdef ECL_SIXTY_FOUR_BIT |
|
82 if (MP_USED(r) < 7) { |
|
83 MP_CHECKOK(s_mp_pad(r, 7)); |
|
84 } |
|
85 u = MP_DIGITS(r); |
|
86 MP_USED(r) = 7; |
|
87 |
|
88 /* u[6] only has 2 significant bits */ |
|
89 z = u[6]; |
|
90 u[3] ^= (z << 14) ^ (z >> 1); |
|
91 u[2] ^= (z << 63); |
|
92 z = u[5]; |
|
93 u[3] ^= (z >> 50); |
|
94 u[2] ^= (z << 14) ^ (z >> 1); |
|
95 u[1] ^= (z << 63); |
|
96 z = u[4]; |
|
97 u[2] ^= (z >> 50); |
|
98 u[1] ^= (z << 14) ^ (z >> 1); |
|
99 u[0] ^= (z << 63); |
|
100 z = u[3] >> 1; /* z only has 63 significant bits */ |
|
101 u[1] ^= (z >> 49); |
|
102 u[0] ^= (z << 15) ^ z; |
|
103 /* clear bits above 193 */ |
|
104 u[6] = u[5] = u[4] = 0; |
|
105 u[3] ^= z << 1; |
|
106 #else |
|
107 if (MP_USED(r) < 13) { |
|
108 MP_CHECKOK(s_mp_pad(r, 13)); |
|
109 } |
|
110 u = MP_DIGITS(r); |
|
111 MP_USED(r) = 13; |
|
112 |
|
113 /* u[12] only has 2 significant bits */ |
|
114 z = u[12]; |
|
115 u[6] ^= (z << 14) ^ (z >> 1); |
|
116 u[5] ^= (z << 31); |
|
117 z = u[11]; |
|
118 u[6] ^= (z >> 18); |
|
119 u[5] ^= (z << 14) ^ (z >> 1); |
|
120 u[4] ^= (z << 31); |
|
121 z = u[10]; |
|
122 u[5] ^= (z >> 18); |
|
123 u[4] ^= (z << 14) ^ (z >> 1); |
|
124 u[3] ^= (z << 31); |
|
125 z = u[9]; |
|
126 u[4] ^= (z >> 18); |
|
127 u[3] ^= (z << 14) ^ (z >> 1); |
|
128 u[2] ^= (z << 31); |
|
129 z = u[8]; |
|
130 u[3] ^= (z >> 18); |
|
131 u[2] ^= (z << 14) ^ (z >> 1); |
|
132 u[1] ^= (z << 31); |
|
133 z = u[7]; |
|
134 u[2] ^= (z >> 18); |
|
135 u[1] ^= (z << 14) ^ (z >> 1); |
|
136 u[0] ^= (z << 31); |
|
137 z = u[6] >> 1; /* z only has 31 significant bits */ |
|
138 u[1] ^= (z >> 17); |
|
139 u[0] ^= (z << 15) ^ z; |
|
140 /* clear bits above 193 */ |
|
141 u[12] = u[11] = u[10] = u[9] = u[8] = u[7] = 0; |
|
142 u[6] ^= z << 1; |
|
143 #endif |
|
144 s_mp_clamp(r); |
|
145 |
|
146 CLEANUP: |
|
147 return res; |
|
148 } |
|
149 |
|
150 /* Fast squaring for polynomials over a 193-bit curve. Assumes reduction |
|
151 * polynomial with terms {193, 15, 0}. */ |
|
152 mp_err |
|
153 ec_GF2m_193_sqr(const mp_int *a, mp_int *r, const GFMethod *meth) |
|
154 { |
|
155 mp_err res = MP_OKAY; |
|
156 mp_digit *u, *v; |
|
157 |
|
158 v = MP_DIGITS(a); |
|
159 |
|
160 #ifdef ECL_SIXTY_FOUR_BIT |
|
161 if (MP_USED(a) < 4) { |
|
162 return mp_bsqrmod(a, meth->irr_arr, r); |
|
163 } |
|
164 if (MP_USED(r) < 7) { |
|
165 MP_CHECKOK(s_mp_pad(r, 7)); |
|
166 } |
|
167 MP_USED(r) = 7; |
|
168 #else |
|
169 if (MP_USED(a) < 7) { |
|
170 return mp_bsqrmod(a, meth->irr_arr, r); |
|
171 } |
|
172 if (MP_USED(r) < 13) { |
|
173 MP_CHECKOK(s_mp_pad(r, 13)); |
|
174 } |
|
175 MP_USED(r) = 13; |
|
176 #endif |
|
177 u = MP_DIGITS(r); |
|
178 |
|
179 #ifdef ECL_THIRTY_TWO_BIT |
|
180 u[12] = gf2m_SQR0(v[6]); |
|
181 u[11] = gf2m_SQR1(v[5]); |
|
182 u[10] = gf2m_SQR0(v[5]); |
|
183 u[9] = gf2m_SQR1(v[4]); |
|
184 u[8] = gf2m_SQR0(v[4]); |
|
185 u[7] = gf2m_SQR1(v[3]); |
|
186 #endif |
|
187 u[6] = gf2m_SQR0(v[3]); |
|
188 u[5] = gf2m_SQR1(v[2]); |
|
189 u[4] = gf2m_SQR0(v[2]); |
|
190 u[3] = gf2m_SQR1(v[1]); |
|
191 u[2] = gf2m_SQR0(v[1]); |
|
192 u[1] = gf2m_SQR1(v[0]); |
|
193 u[0] = gf2m_SQR0(v[0]); |
|
194 return ec_GF2m_193_mod(r, r, meth); |
|
195 |
|
196 CLEANUP: |
|
197 return res; |
|
198 } |
|
199 |
|
200 /* Fast multiplication for polynomials over a 193-bit curve. Assumes |
|
201 * reduction polynomial with terms {193, 15, 0}. */ |
|
202 mp_err |
|
203 ec_GF2m_193_mul(const mp_int *a, const mp_int *b, mp_int *r, |
|
204 const GFMethod *meth) |
|
205 { |
|
206 mp_err res = MP_OKAY; |
|
207 mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0; |
|
208 |
|
209 #ifdef ECL_THIRTY_TWO_BIT |
|
210 mp_digit a6 = 0, a5 = 0, a4 = 0, b6 = 0, b5 = 0, b4 = 0; |
|
211 mp_digit rm[8]; |
|
212 #endif |
|
213 |
|
214 if (a == b) { |
|
215 return ec_GF2m_193_sqr(a, r, meth); |
|
216 } else { |
|
217 switch (MP_USED(a)) { |
|
218 #ifdef ECL_THIRTY_TWO_BIT |
|
219 case 7: |
|
220 a6 = MP_DIGIT(a, 6); |
|
221 case 6: |
|
222 a5 = MP_DIGIT(a, 5); |
|
223 case 5: |
|
224 a4 = MP_DIGIT(a, 4); |
|
225 #endif |
|
226 case 4: |
|
227 a3 = MP_DIGIT(a, 3); |
|
228 case 3: |
|
229 a2 = MP_DIGIT(a, 2); |
|
230 case 2: |
|
231 a1 = MP_DIGIT(a, 1); |
|
232 default: |
|
233 a0 = MP_DIGIT(a, 0); |
|
234 } |
|
235 switch (MP_USED(b)) { |
|
236 #ifdef ECL_THIRTY_TWO_BIT |
|
237 case 7: |
|
238 b6 = MP_DIGIT(b, 6); |
|
239 case 6: |
|
240 b5 = MP_DIGIT(b, 5); |
|
241 case 5: |
|
242 b4 = MP_DIGIT(b, 4); |
|
243 #endif |
|
244 case 4: |
|
245 b3 = MP_DIGIT(b, 3); |
|
246 case 3: |
|
247 b2 = MP_DIGIT(b, 2); |
|
248 case 2: |
|
249 b1 = MP_DIGIT(b, 1); |
|
250 default: |
|
251 b0 = MP_DIGIT(b, 0); |
|
252 } |
|
253 #ifdef ECL_SIXTY_FOUR_BIT |
|
254 MP_CHECKOK(s_mp_pad(r, 8)); |
|
255 s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0); |
|
256 MP_USED(r) = 8; |
|
257 s_mp_clamp(r); |
|
258 #else |
|
259 MP_CHECKOK(s_mp_pad(r, 14)); |
|
260 s_bmul_3x3(MP_DIGITS(r) + 8, a6, a5, a4, b6, b5, b4); |
|
261 s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0); |
|
262 s_bmul_4x4(rm, a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b3, b6 ^ b2, b5 ^ b1, |
|
263 b4 ^ b0); |
|
264 rm[7] ^= MP_DIGIT(r, 7); |
|
265 rm[6] ^= MP_DIGIT(r, 6); |
|
266 rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13); |
|
267 rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12); |
|
268 rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11); |
|
269 rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10); |
|
270 rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9); |
|
271 rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8); |
|
272 MP_DIGIT(r, 11) ^= rm[7]; |
|
273 MP_DIGIT(r, 10) ^= rm[6]; |
|
274 MP_DIGIT(r, 9) ^= rm[5]; |
|
275 MP_DIGIT(r, 8) ^= rm[4]; |
|
276 MP_DIGIT(r, 7) ^= rm[3]; |
|
277 MP_DIGIT(r, 6) ^= rm[2]; |
|
278 MP_DIGIT(r, 5) ^= rm[1]; |
|
279 MP_DIGIT(r, 4) ^= rm[0]; |
|
280 MP_USED(r) = 14; |
|
281 s_mp_clamp(r); |
|
282 #endif |
|
283 return ec_GF2m_193_mod(r, r, meth); |
|
284 } |
|
285 |
|
286 CLEANUP: |
|
287 return res; |
|
288 } |
|
289 |
|
290 /* Wire in fast field arithmetic for 193-bit curves. */ |
|
291 mp_err |
|
292 ec_group_set_gf2m193(ECGroup *group, ECCurveName name) |
|
293 { |
|
294 group->meth->field_mod = &ec_GF2m_193_mod; |
|
295 group->meth->field_mul = &ec_GF2m_193_mul; |
|
296 group->meth->field_sqr = &ec_GF2m_193_sqr; |
|
297 return MP_OKAY; |
|
298 } |