|
1 /* |
|
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * Use is subject to license terms. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2.1 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public License |
|
16 * along with this library; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* ********************************************************************* |
|
25 * |
|
26 * The Original Code is the elliptic curve math library for binary polynomial field curves. |
|
27 * |
|
28 * The Initial Developer of the Original Code is |
|
29 * Sun Microsystems, Inc. |
|
30 * Portions created by the Initial Developer are Copyright (C) 2003 |
|
31 * the Initial Developer. All Rights Reserved. |
|
32 * |
|
33 * Contributor(s): |
|
34 * Sheueling Chang-Shantz <sheueling.chang@sun.com>, |
|
35 * Stephen Fung <fungstep@hotmail.com>, and |
|
36 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories. |
|
37 * |
|
38 *********************************************************************** */ |
|
39 |
|
40 #include "ec2.h" |
|
41 #include "mplogic.h" |
|
42 #include "mp_gf2m.h" |
|
43 #ifndef _KERNEL |
|
44 #include <stdlib.h> |
|
45 #endif |
|
46 |
|
47 /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery |
|
48 * projective coordinates. Uses algorithm Mdouble in appendix of Lopez, J. |
|
49 * and Dahab, R. "Fast multiplication on elliptic curves over GF(2^m) |
|
50 * without precomputation". modified to not require precomputation of |
|
51 * c=b^{2^{m-1}}. */ |
|
52 static mp_err |
|
53 gf2m_Mdouble(mp_int *x, mp_int *z, const ECGroup *group, int kmflag) |
|
54 { |
|
55 mp_err res = MP_OKAY; |
|
56 mp_int t1; |
|
57 |
|
58 MP_DIGITS(&t1) = 0; |
|
59 MP_CHECKOK(mp_init(&t1, kmflag)); |
|
60 |
|
61 MP_CHECKOK(group->meth->field_sqr(x, x, group->meth)); |
|
62 MP_CHECKOK(group->meth->field_sqr(z, &t1, group->meth)); |
|
63 MP_CHECKOK(group->meth->field_mul(x, &t1, z, group->meth)); |
|
64 MP_CHECKOK(group->meth->field_sqr(x, x, group->meth)); |
|
65 MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth)); |
|
66 MP_CHECKOK(group->meth-> |
|
67 field_mul(&group->curveb, &t1, &t1, group->meth)); |
|
68 MP_CHECKOK(group->meth->field_add(x, &t1, x, group->meth)); |
|
69 |
|
70 CLEANUP: |
|
71 mp_clear(&t1); |
|
72 return res; |
|
73 } |
|
74 |
|
75 /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in |
|
76 * Montgomery projective coordinates. Uses algorithm Madd in appendix of |
|
77 * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over |
|
78 * GF(2^m) without precomputation". */ |
|
79 static mp_err |
|
80 gf2m_Madd(const mp_int *x, mp_int *x1, mp_int *z1, mp_int *x2, mp_int *z2, |
|
81 const ECGroup *group, int kmflag) |
|
82 { |
|
83 mp_err res = MP_OKAY; |
|
84 mp_int t1, t2; |
|
85 |
|
86 MP_DIGITS(&t1) = 0; |
|
87 MP_DIGITS(&t2) = 0; |
|
88 MP_CHECKOK(mp_init(&t1, kmflag)); |
|
89 MP_CHECKOK(mp_init(&t2, kmflag)); |
|
90 |
|
91 MP_CHECKOK(mp_copy(x, &t1)); |
|
92 MP_CHECKOK(group->meth->field_mul(x1, z2, x1, group->meth)); |
|
93 MP_CHECKOK(group->meth->field_mul(z1, x2, z1, group->meth)); |
|
94 MP_CHECKOK(group->meth->field_mul(x1, z1, &t2, group->meth)); |
|
95 MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth)); |
|
96 MP_CHECKOK(group->meth->field_sqr(z1, z1, group->meth)); |
|
97 MP_CHECKOK(group->meth->field_mul(z1, &t1, x1, group->meth)); |
|
98 MP_CHECKOK(group->meth->field_add(x1, &t2, x1, group->meth)); |
|
99 |
|
100 CLEANUP: |
|
101 mp_clear(&t1); |
|
102 mp_clear(&t2); |
|
103 return res; |
|
104 } |
|
105 |
|
106 /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2) |
|
107 * using Montgomery point multiplication algorithm Mxy() in appendix of |
|
108 * Lopex, J. and Dahab, R. "Fast multiplication on elliptic curves over |
|
109 * GF(2^m) without precomputation". Returns: 0 on error 1 if return value |
|
110 * should be the point at infinity 2 otherwise */ |
|
111 static int |
|
112 gf2m_Mxy(const mp_int *x, const mp_int *y, mp_int *x1, mp_int *z1, |
|
113 mp_int *x2, mp_int *z2, const ECGroup *group) |
|
114 { |
|
115 mp_err res = MP_OKAY; |
|
116 int ret = 0; |
|
117 mp_int t3, t4, t5; |
|
118 |
|
119 MP_DIGITS(&t3) = 0; |
|
120 MP_DIGITS(&t4) = 0; |
|
121 MP_DIGITS(&t5) = 0; |
|
122 MP_CHECKOK(mp_init(&t3, FLAG(x2))); |
|
123 MP_CHECKOK(mp_init(&t4, FLAG(x2))); |
|
124 MP_CHECKOK(mp_init(&t5, FLAG(x2))); |
|
125 |
|
126 if (mp_cmp_z(z1) == 0) { |
|
127 mp_zero(x2); |
|
128 mp_zero(z2); |
|
129 ret = 1; |
|
130 goto CLEANUP; |
|
131 } |
|
132 |
|
133 if (mp_cmp_z(z2) == 0) { |
|
134 MP_CHECKOK(mp_copy(x, x2)); |
|
135 MP_CHECKOK(group->meth->field_add(x, y, z2, group->meth)); |
|
136 ret = 2; |
|
137 goto CLEANUP; |
|
138 } |
|
139 |
|
140 MP_CHECKOK(mp_set_int(&t5, 1)); |
|
141 if (group->meth->field_enc) { |
|
142 MP_CHECKOK(group->meth->field_enc(&t5, &t5, group->meth)); |
|
143 } |
|
144 |
|
145 MP_CHECKOK(group->meth->field_mul(z1, z2, &t3, group->meth)); |
|
146 |
|
147 MP_CHECKOK(group->meth->field_mul(z1, x, z1, group->meth)); |
|
148 MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth)); |
|
149 MP_CHECKOK(group->meth->field_mul(z2, x, z2, group->meth)); |
|
150 MP_CHECKOK(group->meth->field_mul(z2, x1, x1, group->meth)); |
|
151 MP_CHECKOK(group->meth->field_add(z2, x2, z2, group->meth)); |
|
152 |
|
153 MP_CHECKOK(group->meth->field_mul(z2, z1, z2, group->meth)); |
|
154 MP_CHECKOK(group->meth->field_sqr(x, &t4, group->meth)); |
|
155 MP_CHECKOK(group->meth->field_add(&t4, y, &t4, group->meth)); |
|
156 MP_CHECKOK(group->meth->field_mul(&t4, &t3, &t4, group->meth)); |
|
157 MP_CHECKOK(group->meth->field_add(&t4, z2, &t4, group->meth)); |
|
158 |
|
159 MP_CHECKOK(group->meth->field_mul(&t3, x, &t3, group->meth)); |
|
160 MP_CHECKOK(group->meth->field_div(&t5, &t3, &t3, group->meth)); |
|
161 MP_CHECKOK(group->meth->field_mul(&t3, &t4, &t4, group->meth)); |
|
162 MP_CHECKOK(group->meth->field_mul(x1, &t3, x2, group->meth)); |
|
163 MP_CHECKOK(group->meth->field_add(x2, x, z2, group->meth)); |
|
164 |
|
165 MP_CHECKOK(group->meth->field_mul(z2, &t4, z2, group->meth)); |
|
166 MP_CHECKOK(group->meth->field_add(z2, y, z2, group->meth)); |
|
167 |
|
168 ret = 2; |
|
169 |
|
170 CLEANUP: |
|
171 mp_clear(&t3); |
|
172 mp_clear(&t4); |
|
173 mp_clear(&t5); |
|
174 if (res == MP_OKAY) { |
|
175 return ret; |
|
176 } else { |
|
177 return 0; |
|
178 } |
|
179 } |
|
180 |
|
181 /* Computes R = nP based on algorithm 2P of Lopex, J. and Dahab, R. "Fast |
|
182 * multiplication on elliptic curves over GF(2^m) without |
|
183 * precomputation". Elliptic curve points P and R can be identical. Uses |
|
184 * Montgomery projective coordinates. */ |
|
185 mp_err |
|
186 ec_GF2m_pt_mul_mont(const mp_int *n, const mp_int *px, const mp_int *py, |
|
187 mp_int *rx, mp_int *ry, const ECGroup *group) |
|
188 { |
|
189 mp_err res = MP_OKAY; |
|
190 mp_int x1, x2, z1, z2; |
|
191 int i, j; |
|
192 mp_digit top_bit, mask; |
|
193 |
|
194 MP_DIGITS(&x1) = 0; |
|
195 MP_DIGITS(&x2) = 0; |
|
196 MP_DIGITS(&z1) = 0; |
|
197 MP_DIGITS(&z2) = 0; |
|
198 MP_CHECKOK(mp_init(&x1, FLAG(n))); |
|
199 MP_CHECKOK(mp_init(&x2, FLAG(n))); |
|
200 MP_CHECKOK(mp_init(&z1, FLAG(n))); |
|
201 MP_CHECKOK(mp_init(&z2, FLAG(n))); |
|
202 |
|
203 /* if result should be point at infinity */ |
|
204 if ((mp_cmp_z(n) == 0) || (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES)) { |
|
205 MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry)); |
|
206 goto CLEANUP; |
|
207 } |
|
208 |
|
209 MP_CHECKOK(mp_copy(px, &x1)); /* x1 = px */ |
|
210 MP_CHECKOK(mp_set_int(&z1, 1)); /* z1 = 1 */ |
|
211 MP_CHECKOK(group->meth->field_sqr(&x1, &z2, group->meth)); /* z2 = |
|
212 * x1^2 = |
|
213 * px^2 */ |
|
214 MP_CHECKOK(group->meth->field_sqr(&z2, &x2, group->meth)); |
|
215 MP_CHECKOK(group->meth->field_add(&x2, &group->curveb, &x2, group->meth)); /* x2 |
|
216 * = |
|
217 * px^4 |
|
218 * + |
|
219 * b |
|
220 */ |
|
221 |
|
222 /* find top-most bit and go one past it */ |
|
223 i = MP_USED(n) - 1; |
|
224 j = MP_DIGIT_BIT - 1; |
|
225 top_bit = 1; |
|
226 top_bit <<= MP_DIGIT_BIT - 1; |
|
227 mask = top_bit; |
|
228 while (!(MP_DIGITS(n)[i] & mask)) { |
|
229 mask >>= 1; |
|
230 j--; |
|
231 } |
|
232 mask >>= 1; |
|
233 j--; |
|
234 |
|
235 /* if top most bit was at word break, go to next word */ |
|
236 if (!mask) { |
|
237 i--; |
|
238 j = MP_DIGIT_BIT - 1; |
|
239 mask = top_bit; |
|
240 } |
|
241 |
|
242 for (; i >= 0; i--) { |
|
243 for (; j >= 0; j--) { |
|
244 if (MP_DIGITS(n)[i] & mask) { |
|
245 MP_CHECKOK(gf2m_Madd(px, &x1, &z1, &x2, &z2, group, FLAG(n))); |
|
246 MP_CHECKOK(gf2m_Mdouble(&x2, &z2, group, FLAG(n))); |
|
247 } else { |
|
248 MP_CHECKOK(gf2m_Madd(px, &x2, &z2, &x1, &z1, group, FLAG(n))); |
|
249 MP_CHECKOK(gf2m_Mdouble(&x1, &z1, group, FLAG(n))); |
|
250 } |
|
251 mask >>= 1; |
|
252 } |
|
253 j = MP_DIGIT_BIT - 1; |
|
254 mask = top_bit; |
|
255 } |
|
256 |
|
257 /* convert out of "projective" coordinates */ |
|
258 i = gf2m_Mxy(px, py, &x1, &z1, &x2, &z2, group); |
|
259 if (i == 0) { |
|
260 res = MP_BADARG; |
|
261 goto CLEANUP; |
|
262 } else if (i == 1) { |
|
263 MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry)); |
|
264 } else { |
|
265 MP_CHECKOK(mp_copy(&x2, rx)); |
|
266 MP_CHECKOK(mp_copy(&z2, ry)); |
|
267 } |
|
268 |
|
269 CLEANUP: |
|
270 mp_clear(&x1); |
|
271 mp_clear(&x2); |
|
272 mp_clear(&z1); |
|
273 mp_clear(&z2); |
|
274 return res; |
|
275 } |