summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ecp_mont.c
diff options
context:
space:
mode:
authortedu <>2014-05-06 03:56:27 +0000
committertedu <>2014-05-06 03:56:27 +0000
commit2518b24aa4315d557b967bff48dfc9efed909569 (patch)
treece2ee4fdddbbe61dd0ccb045a1604a3d92a86a00 /src/lib/libcrypto/ec/ecp_mont.c
parent0539604f5771dae2c3ecffa8122b5651ff283719 (diff)
downloadopenbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.gz
openbsd-2518b24aa4315d557b967bff48dfc9efed909569.tar.bz2
openbsd-2518b24aa4315d557b967bff48dfc9efed909569.zip
knf approximation
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/ec/ecp_mont.c220
1 files changed, 107 insertions, 113 deletions
diff --git a/src/lib/libcrypto/ec/ecp_mont.c b/src/lib/libcrypto/ec/ecp_mont.c
index 6b5b856344..ececbe56dd 100644
--- a/src/lib/libcrypto/ec/ecp_mont.c
+++ b/src/lib/libcrypto/ec/ecp_mont.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -80,20 +80,20 @@ EC_GFp_mont_method(void)
80 .group_get_curve = ec_GFp_simple_group_get_curve, 80 .group_get_curve = ec_GFp_simple_group_get_curve,
81 .group_get_degree = ec_GFp_simple_group_get_degree, 81 .group_get_degree = ec_GFp_simple_group_get_degree,
82 .group_check_discriminant = 82 .group_check_discriminant =
83 ec_GFp_simple_group_check_discriminant, 83 ec_GFp_simple_group_check_discriminant,
84 .point_init = ec_GFp_simple_point_init, 84 .point_init = ec_GFp_simple_point_init,
85 .point_finish = ec_GFp_simple_point_finish, 85 .point_finish = ec_GFp_simple_point_finish,
86 .point_clear_finish = ec_GFp_simple_point_clear_finish, 86 .point_clear_finish = ec_GFp_simple_point_clear_finish,
87 .point_copy = ec_GFp_simple_point_copy, 87 .point_copy = ec_GFp_simple_point_copy,
88 .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, 88 .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity,
89 .point_set_Jprojective_coordinates_GFp = 89 .point_set_Jprojective_coordinates_GFp =
90 ec_GFp_simple_set_Jprojective_coordinates_GFp, 90 ec_GFp_simple_set_Jprojective_coordinates_GFp,
91 .point_get_Jprojective_coordinates_GFp = 91 .point_get_Jprojective_coordinates_GFp =
92 ec_GFp_simple_get_Jprojective_coordinates_GFp, 92 ec_GFp_simple_get_Jprojective_coordinates_GFp,
93 .point_set_affine_coordinates = 93 .point_set_affine_coordinates =
94 ec_GFp_simple_point_set_affine_coordinates, 94 ec_GFp_simple_point_set_affine_coordinates,
95 .point_get_affine_coordinates = 95 .point_get_affine_coordinates =
96 ec_GFp_simple_point_get_affine_coordinates, 96 ec_GFp_simple_point_get_affine_coordinates,
97 .add = ec_GFp_simple_add, 97 .add = ec_GFp_simple_add,
98 .dbl = ec_GFp_simple_dbl, 98 .dbl = ec_GFp_simple_dbl,
99 .invert = ec_GFp_simple_invert, 99 .invert = ec_GFp_simple_invert,
@@ -113,123 +113,119 @@ EC_GFp_mont_method(void)
113} 113}
114 114
115 115
116int ec_GFp_mont_group_init(EC_GROUP *group) 116int
117 { 117ec_GFp_mont_group_init(EC_GROUP * group)
118{
118 int ok; 119 int ok;
119 120
120 ok = ec_GFp_simple_group_init(group); 121 ok = ec_GFp_simple_group_init(group);
121 group->field_data1 = NULL; 122 group->field_data1 = NULL;
122 group->field_data2 = NULL; 123 group->field_data2 = NULL;
123 return ok; 124 return ok;
124 } 125}
125 126
126 127
127void ec_GFp_mont_group_finish(EC_GROUP *group) 128void
128 { 129ec_GFp_mont_group_finish(EC_GROUP * group)
129 if (group->field_data1 != NULL) 130{
130 { 131 if (group->field_data1 != NULL) {
131 BN_MONT_CTX_free(group->field_data1); 132 BN_MONT_CTX_free(group->field_data1);
132 group->field_data1 = NULL; 133 group->field_data1 = NULL;
133 } 134 }
134 if (group->field_data2 != NULL) 135 if (group->field_data2 != NULL) {
135 {
136 BN_free(group->field_data2); 136 BN_free(group->field_data2);
137 group->field_data2 = NULL; 137 group->field_data2 = NULL;
138 }
139 ec_GFp_simple_group_finish(group);
140 } 138 }
139 ec_GFp_simple_group_finish(group);
140}
141 141
142 142
143void ec_GFp_mont_group_clear_finish(EC_GROUP *group) 143void
144 { 144ec_GFp_mont_group_clear_finish(EC_GROUP * group)
145 if (group->field_data1 != NULL) 145{
146 { 146 if (group->field_data1 != NULL) {
147 BN_MONT_CTX_free(group->field_data1); 147 BN_MONT_CTX_free(group->field_data1);
148 group->field_data1 = NULL; 148 group->field_data1 = NULL;
149 } 149 }
150 if (group->field_data2 != NULL) 150 if (group->field_data2 != NULL) {
151 {
152 BN_clear_free(group->field_data2); 151 BN_clear_free(group->field_data2);
153 group->field_data2 = NULL; 152 group->field_data2 = NULL;
154 }
155 ec_GFp_simple_group_clear_finish(group);
156 } 153 }
154 ec_GFp_simple_group_clear_finish(group);
155}
157 156
158 157
159int ec_GFp_mont_group_copy(EC_GROUP *dest, const EC_GROUP *src) 158int
160 { 159ec_GFp_mont_group_copy(EC_GROUP * dest, const EC_GROUP * src)
161 if (dest->field_data1 != NULL) 160{
162 { 161 if (dest->field_data1 != NULL) {
163 BN_MONT_CTX_free(dest->field_data1); 162 BN_MONT_CTX_free(dest->field_data1);
164 dest->field_data1 = NULL; 163 dest->field_data1 = NULL;
165 } 164 }
166 if (dest->field_data2 != NULL) 165 if (dest->field_data2 != NULL) {
167 {
168 BN_clear_free(dest->field_data2); 166 BN_clear_free(dest->field_data2);
169 dest->field_data2 = NULL; 167 dest->field_data2 = NULL;
170 } 168 }
171 169 if (!ec_GFp_simple_group_copy(dest, src))
172 if (!ec_GFp_simple_group_copy(dest, src)) return 0; 170 return 0;
173 171
174 if (src->field_data1 != NULL) 172 if (src->field_data1 != NULL) {
175 {
176 dest->field_data1 = BN_MONT_CTX_new(); 173 dest->field_data1 = BN_MONT_CTX_new();
177 if (dest->field_data1 == NULL) return 0; 174 if (dest->field_data1 == NULL)
178 if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1)) goto err; 175 return 0;
179 } 176 if (!BN_MONT_CTX_copy(dest->field_data1, src->field_data1))
180 if (src->field_data2 != NULL) 177 goto err;
181 { 178 }
179 if (src->field_data2 != NULL) {
182 dest->field_data2 = BN_dup(src->field_data2); 180 dest->field_data2 = BN_dup(src->field_data2);
183 if (dest->field_data2 == NULL) goto err; 181 if (dest->field_data2 == NULL)
184 } 182 goto err;
185 183 }
186 return 1; 184 return 1;
187 185
188 err: 186err:
189 if (dest->field_data1 != NULL) 187 if (dest->field_data1 != NULL) {
190 {
191 BN_MONT_CTX_free(dest->field_data1); 188 BN_MONT_CTX_free(dest->field_data1);
192 dest->field_data1 = NULL; 189 dest->field_data1 = NULL;
193 }
194 return 0;
195 } 190 }
191 return 0;
192}
196 193
197 194
198int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 195int
199 { 196ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
197 const BIGNUM *b, BN_CTX *ctx)
198{
200 BN_CTX *new_ctx = NULL; 199 BN_CTX *new_ctx = NULL;
201 BN_MONT_CTX *mont = NULL; 200 BN_MONT_CTX *mont = NULL;
202 BIGNUM *one = NULL; 201 BIGNUM *one = NULL;
203 int ret = 0; 202 int ret = 0;
204 203
205 if (group->field_data1 != NULL) 204 if (group->field_data1 != NULL) {
206 {
207 BN_MONT_CTX_free(group->field_data1); 205 BN_MONT_CTX_free(group->field_data1);
208 group->field_data1 = NULL; 206 group->field_data1 = NULL;
209 } 207 }
210 if (group->field_data2 != NULL) 208 if (group->field_data2 != NULL) {
211 {
212 BN_free(group->field_data2); 209 BN_free(group->field_data2);
213 group->field_data2 = NULL; 210 group->field_data2 = NULL;
214 } 211 }
215 212 if (ctx == NULL) {
216 if (ctx == NULL)
217 {
218 ctx = new_ctx = BN_CTX_new(); 213 ctx = new_ctx = BN_CTX_new();
219 if (ctx == NULL) 214 if (ctx == NULL)
220 return 0; 215 return 0;
221 } 216 }
222
223 mont = BN_MONT_CTX_new(); 217 mont = BN_MONT_CTX_new();
224 if (mont == NULL) goto err; 218 if (mont == NULL)
225 if (!BN_MONT_CTX_set(mont, p, ctx)) 219 goto err;
226 { 220 if (!BN_MONT_CTX_set(mont, p, ctx)) {
227 ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB); 221 ECerr(EC_F_EC_GFP_MONT_GROUP_SET_CURVE, ERR_R_BN_LIB);
228 goto err; 222 goto err;
229 } 223 }
230 one = BN_new(); 224 one = BN_new();
231 if (one == NULL) goto err; 225 if (one == NULL)
232 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx)) goto err; 226 goto err;
227 if (!BN_to_montgomery(one, BN_value_one(), mont, ctx))
228 goto err;
233 229
234 group->field_data1 = mont; 230 group->field_data1 = mont;
235 mont = NULL; 231 mont = NULL;
@@ -238,79 +234,77 @@ int ec_GFp_mont_group_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *
238 234
239 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx); 235 ret = ec_GFp_simple_group_set_curve(group, p, a, b, ctx);
240 236
241 if (!ret) 237 if (!ret) {
242 {
243 BN_MONT_CTX_free(group->field_data1); 238 BN_MONT_CTX_free(group->field_data1);
244 group->field_data1 = NULL; 239 group->field_data1 = NULL;
245 BN_free(group->field_data2); 240 BN_free(group->field_data2);
246 group->field_data2 = NULL; 241 group->field_data2 = NULL;
247 } 242 }
248 243err:
249 err:
250 if (new_ctx != NULL) 244 if (new_ctx != NULL)
251 BN_CTX_free(new_ctx); 245 BN_CTX_free(new_ctx);
252 if (mont != NULL) 246 if (mont != NULL)
253 BN_MONT_CTX_free(mont); 247 BN_MONT_CTX_free(mont);
254 return ret; 248 return ret;
255 } 249}
256 250
257 251
258int ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 252int
259 { 253ec_GFp_mont_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
260 if (group->field_data1 == NULL) 254 const BIGNUM *b, BN_CTX *ctx)
261 { 255{
256 if (group->field_data1 == NULL) {
262 ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED); 257 ECerr(EC_F_EC_GFP_MONT_FIELD_MUL, EC_R_NOT_INITIALIZED);
263 return 0; 258 return 0;
264 }
265
266 return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
267 } 259 }
260 return BN_mod_mul_montgomery(r, a, b, group->field_data1, ctx);
261}
268 262
269 263
270int ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) 264int
271 { 265ec_GFp_mont_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
272 if (group->field_data1 == NULL) 266 BN_CTX *ctx)
273 { 267{
268 if (group->field_data1 == NULL) {
274 ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED); 269 ECerr(EC_F_EC_GFP_MONT_FIELD_SQR, EC_R_NOT_INITIALIZED);
275 return 0; 270 return 0;
276 }
277
278 return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
279 } 271 }
272 return BN_mod_mul_montgomery(r, a, a, group->field_data1, ctx);
273}
280 274
281 275
282int ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) 276int
283 { 277ec_GFp_mont_field_encode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
284 if (group->field_data1 == NULL) 278 BN_CTX *ctx)
285 { 279{
280 if (group->field_data1 == NULL) {
286 ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED); 281 ECerr(EC_F_EC_GFP_MONT_FIELD_ENCODE, EC_R_NOT_INITIALIZED);
287 return 0; 282 return 0;
288 }
289
290 return BN_to_montgomery(r, a, (BN_MONT_CTX *)group->field_data1, ctx);
291 } 283 }
284 return BN_to_montgomery(r, a, (BN_MONT_CTX *) group->field_data1, ctx);
285}
292 286
293 287
294int ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx) 288int
295 { 289ec_GFp_mont_field_decode(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a,
296 if (group->field_data1 == NULL) 290 BN_CTX *ctx)
297 { 291{
292 if (group->field_data1 == NULL) {
298 ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED); 293 ECerr(EC_F_EC_GFP_MONT_FIELD_DECODE, EC_R_NOT_INITIALIZED);
299 return 0; 294 return 0;
300 }
301
302 return BN_from_montgomery(r, a, group->field_data1, ctx);
303 } 295 }
296 return BN_from_montgomery(r, a, group->field_data1, ctx);
297}
304 298
305 299
306int ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx) 300int
307 { 301ec_GFp_mont_field_set_to_one(const EC_GROUP *group, BIGNUM *r, BN_CTX *ctx)
308 if (group->field_data2 == NULL) 302{
309 { 303 if (group->field_data2 == NULL) {
310 ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED); 304 ECerr(EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE, EC_R_NOT_INITIALIZED);
311 return 0; 305 return 0;
312 }
313
314 if (!BN_copy(r, group->field_data2)) return 0;
315 return 1;
316 } 306 }
307 if (!BN_copy(r, group->field_data2))
308 return 0;
309 return 1;
310}