|
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 163-bit curve. Assumes reduction |
|
71 * polynomial with terms {163, 7, 6, 3, 0}. */ |
|
72 mp_err |
|
73 ec_GF2m_163_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) < 6) { |
|
83 MP_CHECKOK(s_mp_pad(r, 6)); |
|
84 } |
|
85 u = MP_DIGITS(r); |
|
86 MP_USED(r) = 6; |
|
87 |
|
88 /* u[5] only has 6 significant bits */ |
|
89 z = u[5]; |
|
90 u[2] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29); |
|
91 z = u[4]; |
|
92 u[2] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35); |
|
93 u[1] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29); |
|
94 z = u[3]; |
|
95 u[1] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35); |
|
96 u[0] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29); |
|
97 z = u[2] >> 35; /* z only has 29 significant bits */ |
|
98 u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z; |
|
99 /* clear bits above 163 */ |
|
100 u[5] = u[4] = u[3] = 0; |
|
101 u[2] ^= z << 35; |
|
102 #else |
|
103 if (MP_USED(r) < 11) { |
|
104 MP_CHECKOK(s_mp_pad(r, 11)); |
|
105 } |
|
106 u = MP_DIGITS(r); |
|
107 MP_USED(r) = 11; |
|
108 |
|
109 /* u[11] only has 6 significant bits */ |
|
110 z = u[10]; |
|
111 u[5] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3); |
|
112 u[4] ^= (z << 29); |
|
113 z = u[9]; |
|
114 u[5] ^= (z >> 28) ^ (z >> 29); |
|
115 u[4] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3); |
|
116 u[3] ^= (z << 29); |
|
117 z = u[8]; |
|
118 u[4] ^= (z >> 28) ^ (z >> 29); |
|
119 u[3] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3); |
|
120 u[2] ^= (z << 29); |
|
121 z = u[7]; |
|
122 u[3] ^= (z >> 28) ^ (z >> 29); |
|
123 u[2] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3); |
|
124 u[1] ^= (z << 29); |
|
125 z = u[6]; |
|
126 u[2] ^= (z >> 28) ^ (z >> 29); |
|
127 u[1] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3); |
|
128 u[0] ^= (z << 29); |
|
129 z = u[5] >> 3; /* z only has 29 significant bits */ |
|
130 u[1] ^= (z >> 25) ^ (z >> 26); |
|
131 u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z; |
|
132 /* clear bits above 163 */ |
|
133 u[11] = u[10] = u[9] = u[8] = u[7] = u[6] = 0; |
|
134 u[5] ^= z << 3; |
|
135 #endif |
|
136 s_mp_clamp(r); |
|
137 |
|
138 CLEANUP: |
|
139 return res; |
|
140 } |
|
141 |
|
142 /* Fast squaring for polynomials over a 163-bit curve. Assumes reduction |
|
143 * polynomial with terms {163, 7, 6, 3, 0}. */ |
|
144 mp_err |
|
145 ec_GF2m_163_sqr(const mp_int *a, mp_int *r, const GFMethod *meth) |
|
146 { |
|
147 mp_err res = MP_OKAY; |
|
148 mp_digit *u, *v; |
|
149 |
|
150 v = MP_DIGITS(a); |
|
151 |
|
152 #ifdef ECL_SIXTY_FOUR_BIT |
|
153 if (MP_USED(a) < 3) { |
|
154 return mp_bsqrmod(a, meth->irr_arr, r); |
|
155 } |
|
156 if (MP_USED(r) < 6) { |
|
157 MP_CHECKOK(s_mp_pad(r, 6)); |
|
158 } |
|
159 MP_USED(r) = 6; |
|
160 #else |
|
161 if (MP_USED(a) < 6) { |
|
162 return mp_bsqrmod(a, meth->irr_arr, r); |
|
163 } |
|
164 if (MP_USED(r) < 12) { |
|
165 MP_CHECKOK(s_mp_pad(r, 12)); |
|
166 } |
|
167 MP_USED(r) = 12; |
|
168 #endif |
|
169 u = MP_DIGITS(r); |
|
170 |
|
171 #ifdef ECL_THIRTY_TWO_BIT |
|
172 u[11] = gf2m_SQR1(v[5]); |
|
173 u[10] = gf2m_SQR0(v[5]); |
|
174 u[9] = gf2m_SQR1(v[4]); |
|
175 u[8] = gf2m_SQR0(v[4]); |
|
176 u[7] = gf2m_SQR1(v[3]); |
|
177 u[6] = gf2m_SQR0(v[3]); |
|
178 #endif |
|
179 u[5] = gf2m_SQR1(v[2]); |
|
180 u[4] = gf2m_SQR0(v[2]); |
|
181 u[3] = gf2m_SQR1(v[1]); |
|
182 u[2] = gf2m_SQR0(v[1]); |
|
183 u[1] = gf2m_SQR1(v[0]); |
|
184 u[0] = gf2m_SQR0(v[0]); |
|
185 return ec_GF2m_163_mod(r, r, meth); |
|
186 |
|
187 CLEANUP: |
|
188 return res; |
|
189 } |
|
190 |
|
191 /* Fast multiplication for polynomials over a 163-bit curve. Assumes |
|
192 * reduction polynomial with terms {163, 7, 6, 3, 0}. */ |
|
193 mp_err |
|
194 ec_GF2m_163_mul(const mp_int *a, const mp_int *b, mp_int *r, |
|
195 const GFMethod *meth) |
|
196 { |
|
197 mp_err res = MP_OKAY; |
|
198 mp_digit a2 = 0, a1 = 0, a0, b2 = 0, b1 = 0, b0; |
|
199 |
|
200 #ifdef ECL_THIRTY_TWO_BIT |
|
201 mp_digit a5 = 0, a4 = 0, a3 = 0, b5 = 0, b4 = 0, b3 = 0; |
|
202 mp_digit rm[6]; |
|
203 #endif |
|
204 |
|
205 if (a == b) { |
|
206 return ec_GF2m_163_sqr(a, r, meth); |
|
207 } else { |
|
208 switch (MP_USED(a)) { |
|
209 #ifdef ECL_THIRTY_TWO_BIT |
|
210 case 6: |
|
211 a5 = MP_DIGIT(a, 5); |
|
212 case 5: |
|
213 a4 = MP_DIGIT(a, 4); |
|
214 case 4: |
|
215 a3 = MP_DIGIT(a, 3); |
|
216 #endif |
|
217 case 3: |
|
218 a2 = MP_DIGIT(a, 2); |
|
219 case 2: |
|
220 a1 = MP_DIGIT(a, 1); |
|
221 default: |
|
222 a0 = MP_DIGIT(a, 0); |
|
223 } |
|
224 switch (MP_USED(b)) { |
|
225 #ifdef ECL_THIRTY_TWO_BIT |
|
226 case 6: |
|
227 b5 = MP_DIGIT(b, 5); |
|
228 case 5: |
|
229 b4 = MP_DIGIT(b, 4); |
|
230 case 4: |
|
231 b3 = MP_DIGIT(b, 3); |
|
232 #endif |
|
233 case 3: |
|
234 b2 = MP_DIGIT(b, 2); |
|
235 case 2: |
|
236 b1 = MP_DIGIT(b, 1); |
|
237 default: |
|
238 b0 = MP_DIGIT(b, 0); |
|
239 } |
|
240 #ifdef ECL_SIXTY_FOUR_BIT |
|
241 MP_CHECKOK(s_mp_pad(r, 6)); |
|
242 s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0); |
|
243 MP_USED(r) = 6; |
|
244 s_mp_clamp(r); |
|
245 #else |
|
246 MP_CHECKOK(s_mp_pad(r, 12)); |
|
247 s_bmul_3x3(MP_DIGITS(r) + 6, a5, a4, a3, b5, b4, b3); |
|
248 s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0); |
|
249 s_bmul_3x3(rm, a5 ^ a2, a4 ^ a1, a3 ^ a0, b5 ^ b2, b4 ^ b1, |
|
250 b3 ^ b0); |
|
251 rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 11); |
|
252 rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 10); |
|
253 rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 9); |
|
254 rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 8); |
|
255 rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 7); |
|
256 rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 6); |
|
257 MP_DIGIT(r, 8) ^= rm[5]; |
|
258 MP_DIGIT(r, 7) ^= rm[4]; |
|
259 MP_DIGIT(r, 6) ^= rm[3]; |
|
260 MP_DIGIT(r, 5) ^= rm[2]; |
|
261 MP_DIGIT(r, 4) ^= rm[1]; |
|
262 MP_DIGIT(r, 3) ^= rm[0]; |
|
263 MP_USED(r) = 12; |
|
264 s_mp_clamp(r); |
|
265 #endif |
|
266 return ec_GF2m_163_mod(r, r, meth); |
|
267 } |
|
268 |
|
269 CLEANUP: |
|
270 return res; |
|
271 } |
|
272 |
|
273 /* Wire in fast field arithmetic for 163-bit curves. */ |
|
274 mp_err |
|
275 ec_group_set_gf2m163(ECGroup *group, ECCurveName name) |
|
276 { |
|
277 group->meth->field_mod = &ec_GF2m_163_mod; |
|
278 group->meth->field_mul = &ec_GF2m_163_mul; |
|
279 group->meth->field_sqr = &ec_GF2m_163_sqr; |
|
280 return MP_OKAY; |
|
281 } |