summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ec/ec_lib.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/ec_lib.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/ec_lib.c953
1 files changed, 484 insertions, 469 deletions
diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c
index 546fd08e38..b37efac246 100644
--- a/src/lib/libcrypto/ec/ec_lib.c
+++ b/src/lib/libcrypto/ec/ec_lib.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
@@ -57,7 +57,7 @@
57 */ 57 */
58/* ==================================================================== 58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * Binary polynomial ECC support in OpenSSL originally developed by 60 * Binary polynomial ECC support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */ 62 */
63 63
@@ -73,28 +73,24 @@ static const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
73 73
74/* functions for EC_GROUP objects */ 74/* functions for EC_GROUP objects */
75 75
76EC_GROUP *EC_GROUP_new(const EC_METHOD *meth) 76EC_GROUP *
77 { 77EC_GROUP_new(const EC_METHOD * meth)
78{
78 EC_GROUP *ret; 79 EC_GROUP *ret;
79 80
80 if (meth == NULL) 81 if (meth == NULL) {
81 {
82 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL); 82 ECerr(EC_F_EC_GROUP_NEW, EC_R_SLOT_FULL);
83 return NULL; 83 return NULL;
84 } 84 }
85 if (meth->group_init == 0) 85 if (meth->group_init == 0) {
86 {
87 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 86 ECerr(EC_F_EC_GROUP_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
88 return NULL; 87 return NULL;
89 } 88 }
90
91 ret = malloc(sizeof *ret); 89 ret = malloc(sizeof *ret);
92 if (ret == NULL) 90 if (ret == NULL) {
93 {
94 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE); 91 ECerr(EC_F_EC_GROUP_NEW, ERR_R_MALLOC_FAILURE);
95 return NULL; 92 return NULL;
96 } 93 }
97
98 ret->meth = meth; 94 ret->meth = meth;
99 95
100 ret->extra_data = NULL; 96 ret->extra_data = NULL;
@@ -103,26 +99,26 @@ EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
103 BN_init(&ret->order); 99 BN_init(&ret->order);
104 BN_init(&ret->cofactor); 100 BN_init(&ret->cofactor);
105 101
106 ret->curve_name = 0; 102 ret->curve_name = 0;
107 ret->asn1_flag = 0; 103 ret->asn1_flag = 0;
108 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED; 104 ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;
109 105
110 ret->seed = NULL; 106 ret->seed = NULL;
111 ret->seed_len = 0; 107 ret->seed_len = 0;
112 108
113 if (!meth->group_init(ret)) 109 if (!meth->group_init(ret)) {
114 {
115 free(ret); 110 free(ret);
116 return NULL; 111 return NULL;
117 }
118
119 return ret;
120 } 112 }
113 return ret;
114}
121 115
122 116
123void EC_GROUP_free(EC_GROUP *group) 117void
124 { 118EC_GROUP_free(EC_GROUP * group)
125 if (!group) return; 119{
120 if (!group)
121 return;
126 122
127 if (group->meth->group_finish != 0) 123 if (group->meth->group_finish != 0)
128 group->meth->group_finish(group); 124 group->meth->group_finish(group);
@@ -138,12 +134,14 @@ void EC_GROUP_free(EC_GROUP *group)
138 free(group->seed); 134 free(group->seed);
139 135
140 free(group); 136 free(group);
141 } 137}
142 138
143 139
144void EC_GROUP_clear_free(EC_GROUP *group) 140void
145 { 141EC_GROUP_clear_free(EC_GROUP * group)
146 if (!group) return; 142{
143 if (!group)
144 return;
147 145
148 if (group->meth->group_clear_finish != 0) 146 if (group->meth->group_clear_finish != 0)
149 group->meth->group_clear_finish(group); 147 group->meth->group_clear_finish(group);
@@ -157,74 +155,69 @@ void EC_GROUP_clear_free(EC_GROUP *group)
157 BN_clear_free(&group->order); 155 BN_clear_free(&group->order);
158 BN_clear_free(&group->cofactor); 156 BN_clear_free(&group->cofactor);
159 157
160 if (group->seed) 158 if (group->seed) {
161 {
162 OPENSSL_cleanse(group->seed, group->seed_len); 159 OPENSSL_cleanse(group->seed, group->seed_len);
163 free(group->seed); 160 free(group->seed);
164 } 161 }
165
166 OPENSSL_cleanse(group, sizeof *group); 162 OPENSSL_cleanse(group, sizeof *group);
167 free(group); 163 free(group);
168 } 164}
169 165
170 166
171int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src) 167int
172 { 168EC_GROUP_copy(EC_GROUP * dest, const EC_GROUP * src)
169{
173 EC_EXTRA_DATA *d; 170 EC_EXTRA_DATA *d;
174 171
175 if (dest->meth->group_copy == 0) 172 if (dest->meth->group_copy == 0) {
176 {
177 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 173 ECerr(EC_F_EC_GROUP_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
178 return 0; 174 return 0;
179 } 175 }
180 if (dest->meth != src->meth) 176 if (dest->meth != src->meth) {
181 {
182 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS); 177 ECerr(EC_F_EC_GROUP_COPY, EC_R_INCOMPATIBLE_OBJECTS);
183 return 0; 178 return 0;
184 } 179 }
185 if (dest == src) 180 if (dest == src)
186 return 1; 181 return 1;
187 182
188 EC_EX_DATA_free_all_data(&dest->extra_data); 183 EC_EX_DATA_free_all_data(&dest->extra_data);
189 184
190 for (d = src->extra_data; d != NULL; d = d->next) 185 for (d = src->extra_data; d != NULL; d = d->next) {
191 {
192 void *t = d->dup_func(d->data); 186 void *t = d->dup_func(d->data);
193 187
194 if (t == NULL) 188 if (t == NULL)
195 return 0; 189 return 0;
196 if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func, d->free_func, d->clear_free_func)) 190 if (!EC_EX_DATA_set_data(&dest->extra_data, t, d->dup_func,
191 d->free_func, d->clear_free_func))
197 return 0; 192 return 0;
198 } 193 }
199 194
200 if (src->generator != NULL) 195 if (src->generator != NULL) {
201 { 196 if (dest->generator == NULL) {
202 if (dest->generator == NULL)
203 {
204 dest->generator = EC_POINT_new(dest); 197 dest->generator = EC_POINT_new(dest);
205 if (dest->generator == NULL) return 0; 198 if (dest->generator == NULL)
206 } 199 return 0;
207 if (!EC_POINT_copy(dest->generator, src->generator)) return 0;
208 } 200 }
209 else 201 if (!EC_POINT_copy(dest->generator, src->generator))
210 { 202 return 0;
203 } else {
211 /* src->generator == NULL */ 204 /* src->generator == NULL */
212 if (dest->generator != NULL) 205 if (dest->generator != NULL) {
213 {
214 EC_POINT_clear_free(dest->generator); 206 EC_POINT_clear_free(dest->generator);
215 dest->generator = NULL; 207 dest->generator = NULL;
216 }
217 } 208 }
209 }
218 210
219 if (!BN_copy(&dest->order, &src->order)) return 0; 211 if (!BN_copy(&dest->order, &src->order))
220 if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0; 212 return 0;
213 if (!BN_copy(&dest->cofactor, &src->cofactor))
214 return 0;
221 215
222 dest->curve_name = src->curve_name; 216 dest->curve_name = src->curve_name;
223 dest->asn1_flag = src->asn1_flag; 217 dest->asn1_flag = src->asn1_flag;
224 dest->asn1_form = src->asn1_form; 218 dest->asn1_form = src->asn1_form;
225 219
226 if (src->seed) 220 if (src->seed) {
227 {
228 if (dest->seed) 221 if (dest->seed)
229 free(dest->seed); 222 free(dest->seed);
230 dest->seed = malloc(src->seed_len); 223 dest->seed = malloc(src->seed_len);
@@ -233,153 +226,168 @@ int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
233 if (!memcpy(dest->seed, src->seed, src->seed_len)) 226 if (!memcpy(dest->seed, src->seed, src->seed_len))
234 return 0; 227 return 0;
235 dest->seed_len = src->seed_len; 228 dest->seed_len = src->seed_len;
236 } 229 } else {
237 else
238 {
239 if (dest->seed) 230 if (dest->seed)
240 free(dest->seed); 231 free(dest->seed);
241 dest->seed = NULL; 232 dest->seed = NULL;
242 dest->seed_len = 0; 233 dest->seed_len = 0;
243 } 234 }
244 235
245 236
246 return dest->meth->group_copy(dest, src); 237 return dest->meth->group_copy(dest, src);
247 } 238}
248 239
249 240
250EC_GROUP *EC_GROUP_dup(const EC_GROUP *a) 241EC_GROUP *
251 { 242EC_GROUP_dup(const EC_GROUP * a)
243{
252 EC_GROUP *t = NULL; 244 EC_GROUP *t = NULL;
253 int ok = 0; 245 int ok = 0;
254 246
255 if (a == NULL) return NULL; 247 if (a == NULL)
248 return NULL;
256 249
257 if ((t = EC_GROUP_new(a->meth)) == NULL) return(NULL); 250 if ((t = EC_GROUP_new(a->meth)) == NULL)
258 if (!EC_GROUP_copy(t, a)) goto err; 251 return (NULL);
252 if (!EC_GROUP_copy(t, a))
253 goto err;
259 254
260 ok = 1; 255 ok = 1;
261 256
262 err: 257err:
263 if (!ok) 258 if (!ok) {
264 { 259 if (t)
265 if (t) EC_GROUP_free(t); 260 EC_GROUP_free(t);
266 return NULL; 261 return NULL;
267 } 262 } else
268 else return t; 263 return t;
269 } 264}
270 265
271 266
272const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group) 267const EC_METHOD *
273 { 268EC_GROUP_method_of(const EC_GROUP *group)
269{
274 return group->meth; 270 return group->meth;
275 } 271}
276 272
277 273
278int EC_METHOD_get_field_type(const EC_METHOD *meth) 274int
279 { 275EC_METHOD_get_field_type(const EC_METHOD *meth)
280 return meth->field_type; 276{
281 } 277 return meth->field_type;
278}
282 279
283 280
284int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor) 281int
285 { 282EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
286 if (generator == NULL) 283 const BIGNUM *order, const BIGNUM *cofactor)
287 { 284{
285 if (generator == NULL) {
288 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER); 286 ECerr(EC_F_EC_GROUP_SET_GENERATOR, ERR_R_PASSED_NULL_PARAMETER);
289 return 0 ; 287 return 0;
290 } 288 }
291 289 if (group->generator == NULL) {
292 if (group->generator == NULL)
293 {
294 group->generator = EC_POINT_new(group); 290 group->generator = EC_POINT_new(group);
295 if (group->generator == NULL) return 0; 291 if (group->generator == NULL)
296 } 292 return 0;
297 if (!EC_POINT_copy(group->generator, generator)) return 0; 293 }
294 if (!EC_POINT_copy(group->generator, generator))
295 return 0;
298 296
299 if (order != NULL) 297 if (order != NULL) {
300 { if (!BN_copy(&group->order, order)) return 0; } 298 if (!BN_copy(&group->order, order))
301 else 299 return 0;
300 } else
302 BN_zero(&group->order); 301 BN_zero(&group->order);
303 302
304 if (cofactor != NULL) 303 if (cofactor != NULL) {
305 { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 304 if (!BN_copy(&group->cofactor, cofactor))
306 else 305 return 0;
306 } else
307 BN_zero(&group->cofactor); 307 BN_zero(&group->cofactor);
308 308
309 return 1; 309 return 1;
310 } 310}
311 311
312 312
313const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group) 313const EC_POINT *
314 { 314EC_GROUP_get0_generator(const EC_GROUP *group)
315{
315 return group->generator; 316 return group->generator;
316 } 317}
317 318
318 319
319int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx) 320int
320 { 321EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
322{
321 if (!BN_copy(order, &group->order)) 323 if (!BN_copy(order, &group->order))
322 return 0; 324 return 0;
323 325
324 return !BN_is_zero(order); 326 return !BN_is_zero(order);
325 } 327}
326 328
327 329
328int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx) 330int
329 { 331EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
332{
330 if (!BN_copy(cofactor, &group->cofactor)) 333 if (!BN_copy(cofactor, &group->cofactor))
331 return 0; 334 return 0;
332 335
333 return !BN_is_zero(&group->cofactor); 336 return !BN_is_zero(&group->cofactor);
334 } 337}
335 338
336 339
337void EC_GROUP_set_curve_name(EC_GROUP *group, int nid) 340void
338 { 341EC_GROUP_set_curve_name(EC_GROUP * group, int nid)
342{
339 group->curve_name = nid; 343 group->curve_name = nid;
340 } 344}
341 345
342 346
343int EC_GROUP_get_curve_name(const EC_GROUP *group) 347int
344 { 348EC_GROUP_get_curve_name(const EC_GROUP * group)
349{
345 return group->curve_name; 350 return group->curve_name;
346 } 351}
347 352
348 353
349void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag) 354void
350 { 355EC_GROUP_set_asn1_flag(EC_GROUP * group, int flag)
356{
351 group->asn1_flag = flag; 357 group->asn1_flag = flag;
352 } 358}
353 359
354 360
355int EC_GROUP_get_asn1_flag(const EC_GROUP *group) 361int
356 { 362EC_GROUP_get_asn1_flag(const EC_GROUP * group)
363{
357 return group->asn1_flag; 364 return group->asn1_flag;
358 } 365}
359 366
360 367
361void EC_GROUP_set_point_conversion_form(EC_GROUP *group, 368void
362 point_conversion_form_t form) 369EC_GROUP_set_point_conversion_form(EC_GROUP * group,
363 { 370 point_conversion_form_t form)
371{
364 group->asn1_form = form; 372 group->asn1_form = form;
365 } 373}
366 374
367 375
368point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *group) 376point_conversion_form_t
369 { 377EC_GROUP_get_point_conversion_form(const EC_GROUP * group)
378{
370 return group->asn1_form; 379 return group->asn1_form;
371 } 380}
372 381
373 382
374size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len) 383size_t
375 { 384EC_GROUP_set_seed(EC_GROUP * group, const unsigned char *p, size_t len)
376 if (group->seed) 385{
377 { 386 if (group->seed) {
378 free(group->seed); 387 free(group->seed);
379 group->seed = NULL; 388 group->seed = NULL;
380 group->seed_len = 0; 389 group->seed_len = 0;
381 } 390 }
382
383 if (!len || !p) 391 if (!len || !p)
384 return 1; 392 return 1;
385 393
@@ -389,94 +397,101 @@ size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)
389 group->seed_len = len; 397 group->seed_len = len;
390 398
391 return len; 399 return len;
392 } 400}
393 401
394 402
395unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group) 403unsigned char *
396 { 404EC_GROUP_get0_seed(const EC_GROUP * group)
405{
397 return group->seed; 406 return group->seed;
398 } 407}
399 408
400 409
401size_t EC_GROUP_get_seed_len(const EC_GROUP *group) 410size_t
402 { 411EC_GROUP_get_seed_len(const EC_GROUP * group)
412{
403 return group->seed_len; 413 return group->seed_len;
404 } 414}
405 415
406 416
407int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 417int
408 { 418EC_GROUP_set_curve_GFp(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
409 if (group->meth->group_set_curve == 0) 419 const BIGNUM * b, BN_CTX * ctx)
410 { 420{
421 if (group->meth->group_set_curve == 0) {
411 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 422 ECerr(EC_F_EC_GROUP_SET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
412 return 0; 423 return 0;
413 }
414 return group->meth->group_set_curve(group, p, a, b, ctx);
415 } 424 }
425 return group->meth->group_set_curve(group, p, a, b, ctx);
426}
416 427
417 428
418int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) 429int
419 { 430EC_GROUP_get_curve_GFp(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
420 if (group->meth->group_get_curve == 0) 431 BIGNUM * b, BN_CTX * ctx)
421 { 432{
433 if (group->meth->group_get_curve == 0) {
422 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 434 ECerr(EC_F_EC_GROUP_GET_CURVE_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
423 return 0; 435 return 0;
424 }
425 return group->meth->group_get_curve(group, p, a, b, ctx);
426 } 436 }
437 return group->meth->group_get_curve(group, p, a, b, ctx);
438}
427 439
428#ifndef OPENSSL_NO_EC2M 440#ifndef OPENSSL_NO_EC2M
429int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) 441int
430 { 442EC_GROUP_set_curve_GF2m(EC_GROUP * group, const BIGNUM * p, const BIGNUM * a,
431 if (group->meth->group_set_curve == 0) 443 const BIGNUM * b, BN_CTX * ctx)
432 { 444{
445 if (group->meth->group_set_curve == 0) {
433 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 446 ECerr(EC_F_EC_GROUP_SET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
434 return 0; 447 return 0;
435 }
436 return group->meth->group_set_curve(group, p, a, b, ctx);
437 } 448 }
449 return group->meth->group_set_curve(group, p, a, b, ctx);
450}
438 451
439 452
440int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx) 453int
441 { 454EC_GROUP_get_curve_GF2m(const EC_GROUP * group, BIGNUM * p, BIGNUM * a,
442 if (group->meth->group_get_curve == 0) 455 BIGNUM * b, BN_CTX * ctx)
443 { 456{
457 if (group->meth->group_get_curve == 0) {
444 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 458 ECerr(EC_F_EC_GROUP_GET_CURVE_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
445 return 0; 459 return 0;
446 }
447 return group->meth->group_get_curve(group, p, a, b, ctx);
448 } 460 }
461 return group->meth->group_get_curve(group, p, a, b, ctx);
462}
449#endif 463#endif
450 464
451int EC_GROUP_get_degree(const EC_GROUP *group) 465int
452 { 466EC_GROUP_get_degree(const EC_GROUP * group)
453 if (group->meth->group_get_degree == 0) 467{
454 { 468 if (group->meth->group_get_degree == 0) {
455 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 469 ECerr(EC_F_EC_GROUP_GET_DEGREE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
456 return 0; 470 return 0;
457 }
458 return group->meth->group_get_degree(group);
459 } 471 }
472 return group->meth->group_get_degree(group);
473}
460 474
461 475
462int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx) 476int
463 { 477EC_GROUP_check_discriminant(const EC_GROUP * group, BN_CTX * ctx)
464 if (group->meth->group_check_discriminant == 0) 478{
465 { 479 if (group->meth->group_check_discriminant == 0) {
466 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 480 ECerr(EC_F_EC_GROUP_CHECK_DISCRIMINANT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
467 return 0; 481 return 0;
468 }
469 return group->meth->group_check_discriminant(group, ctx);
470 } 482 }
483 return group->meth->group_check_discriminant(group, ctx);
484}
471 485
472 486
473int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) 487int
474 { 488EC_GROUP_cmp(const EC_GROUP * a, const EC_GROUP * b, BN_CTX * ctx)
475 int r = 0; 489{
490 int r = 0;
476 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3; 491 BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;
477 BN_CTX *ctx_new = NULL; 492 BN_CTX *ctx_new = NULL;
478 493
479 /* compare the field types*/ 494 /* compare the field types */
480 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) != 495 if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) !=
481 EC_METHOD_get_field_type(EC_GROUP_method_of(b))) 496 EC_METHOD_get_field_type(EC_GROUP_method_of(b)))
482 return 1; 497 return 1;
@@ -489,7 +504,7 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
489 ctx_new = ctx = BN_CTX_new(); 504 ctx_new = ctx = BN_CTX_new();
490 if (!ctx) 505 if (!ctx)
491 return -1; 506 return -1;
492 507
493 BN_CTX_start(ctx); 508 BN_CTX_start(ctx);
494 a1 = BN_CTX_get(ctx); 509 a1 = BN_CTX_get(ctx);
495 a2 = BN_CTX_get(ctx); 510 a2 = BN_CTX_get(ctx);
@@ -497,16 +512,15 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
497 b1 = BN_CTX_get(ctx); 512 b1 = BN_CTX_get(ctx);
498 b2 = BN_CTX_get(ctx); 513 b2 = BN_CTX_get(ctx);
499 b3 = BN_CTX_get(ctx); 514 b3 = BN_CTX_get(ctx);
500 if (!b3) 515 if (!b3) {
501 {
502 BN_CTX_end(ctx); 516 BN_CTX_end(ctx);
503 if (ctx_new) 517 if (ctx_new)
504 BN_CTX_free(ctx); 518 BN_CTX_free(ctx);
505 return -1; 519 return -1;
506 } 520 }
507 521 /*
508 /* XXX This approach assumes that the external representation 522 * XXX This approach assumes that the external representation of
509 * of curves over the same field type is the same. 523 * curves over the same field type is the same.
510 */ 524 */
511 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) || 525 if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||
512 !b->meth->group_get_curve(b, b1, b2, b3, ctx)) 526 !b->meth->group_get_curve(b, b1, b2, b3, ctx))
@@ -517,51 +531,50 @@ int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)
517 531
518 /* XXX EC_POINT_cmp() assumes that the methods are equal */ 532 /* XXX EC_POINT_cmp() assumes that the methods are equal */
519 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a), 533 if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),
520 EC_GROUP_get0_generator(b), ctx)) 534 EC_GROUP_get0_generator(b), ctx))
521 r = 1; 535 r = 1;
522 536
523 if (!r) 537 if (!r) {
524 {
525 /* compare the order and cofactor */ 538 /* compare the order and cofactor */
526 if (!EC_GROUP_get_order(a, a1, ctx) || 539 if (!EC_GROUP_get_order(a, a1, ctx) ||
527 !EC_GROUP_get_order(b, b1, ctx) || 540 !EC_GROUP_get_order(b, b1, ctx) ||
528 !EC_GROUP_get_cofactor(a, a2, ctx) || 541 !EC_GROUP_get_cofactor(a, a2, ctx) ||
529 !EC_GROUP_get_cofactor(b, b2, ctx)) 542 !EC_GROUP_get_cofactor(b, b2, ctx)) {
530 {
531 BN_CTX_end(ctx); 543 BN_CTX_end(ctx);
532 if (ctx_new) 544 if (ctx_new)
533 BN_CTX_free(ctx); 545 BN_CTX_free(ctx);
534 return -1; 546 return -1;
535 } 547 }
536 if (BN_cmp(a1, b1) || BN_cmp(a2, b2)) 548 if (BN_cmp(a1, b1) || BN_cmp(a2, b2))
537 r = 1; 549 r = 1;
538 } 550 }
539
540 BN_CTX_end(ctx); 551 BN_CTX_end(ctx);
541 if (ctx_new) 552 if (ctx_new)
542 BN_CTX_free(ctx); 553 BN_CTX_free(ctx);
543 554
544 return r; 555 return r;
545 } 556}
546 557
547 558
548/* this has 'package' visibility */ 559/* this has 'package' visibility */
549int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data, 560int
550 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) 561EC_EX_DATA_set_data(EC_EXTRA_DATA ** ex_data, void *data,
551 { 562 void *(*dup_func) (void *),
563 void (*free_func) (void *),
564 void (*clear_free_func) (void *))
565{
552 EC_EXTRA_DATA *d; 566 EC_EXTRA_DATA *d;
553 567
554 if (ex_data == NULL) 568 if (ex_data == NULL)
555 return 0; 569 return 0;
556 570
557 for (d = *ex_data; d != NULL; d = d->next) 571 for (d = *ex_data; d != NULL; d = d->next) {
558 { 572 if (d->dup_func == dup_func && d->free_func == free_func &&
559 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func) 573 d->clear_free_func == clear_free_func) {
560 {
561 ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL); 574 ECerr(EC_F_EC_EX_DATA_SET_DATA, EC_R_SLOT_FULL);
562 return 0; 575 return 0;
563 }
564 } 576 }
577 }
565 578
566 if (data == NULL) 579 if (data == NULL)
567 /* no explicit entry needed */ 580 /* no explicit entry needed */
@@ -580,163 +593,169 @@ int EC_EX_DATA_set_data(EC_EXTRA_DATA **ex_data, void *data,
580 *ex_data = d; 593 *ex_data = d;
581 594
582 return 1; 595 return 1;
583 } 596}
584 597
585/* this has 'package' visibility */ 598/* this has 'package' visibility */
586void *EC_EX_DATA_get_data(const EC_EXTRA_DATA *ex_data, 599void *
587 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) 600EC_EX_DATA_get_data(const EC_EXTRA_DATA * ex_data,
588 { 601 void *(*dup_func) (void *),
602 void (*free_func) (void *),
603 void (*clear_free_func) (void *))
604{
589 const EC_EXTRA_DATA *d; 605 const EC_EXTRA_DATA *d;
590 606
591 for (d = ex_data; d != NULL; d = d->next) 607 for (d = ex_data; d != NULL; d = d->next) {
592 {
593 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func) 608 if (d->dup_func == dup_func && d->free_func == free_func && d->clear_free_func == clear_free_func)
594 return d->data; 609 return d->data;
595 }
596
597 return NULL;
598 } 610 }
599 611
612 return NULL;
613}
614
600/* this has 'package' visibility */ 615/* this has 'package' visibility */
601void EC_EX_DATA_free_data(EC_EXTRA_DATA **ex_data, 616void
602 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) 617EC_EX_DATA_free_data(EC_EXTRA_DATA ** ex_data,
603 { 618 void *(*dup_func) (void *),
619 void (*free_func) (void *),
620 void (*clear_free_func) (void *))
621{
604 EC_EXTRA_DATA **p; 622 EC_EXTRA_DATA **p;
605 623
606 if (ex_data == NULL) 624 if (ex_data == NULL)
607 return; 625 return;
608 626
609 for (p = ex_data; *p != NULL; p = &((*p)->next)) 627 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
610 { 628 if ((*p)->dup_func == dup_func &&
611 if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func) 629 (*p)->free_func == free_func &&
612 { 630 (*p)->clear_free_func == clear_free_func) {
613 EC_EXTRA_DATA *next = (*p)->next; 631 EC_EXTRA_DATA *next = (*p)->next;
614 632
615 (*p)->free_func((*p)->data); 633 (*p)->free_func((*p)->data);
616 free(*p); 634 free(*p);
617 635
618 *p = next; 636 *p = next;
619 return; 637 return;
620 }
621 } 638 }
622 } 639 }
640}
623 641
624/* this has 'package' visibility */ 642/* this has 'package' visibility */
625void EC_EX_DATA_clear_free_data(EC_EXTRA_DATA **ex_data, 643void
626 void *(*dup_func)(void *), void (*free_func)(void *), void (*clear_free_func)(void *)) 644EC_EX_DATA_clear_free_data(EC_EXTRA_DATA ** ex_data,
627 { 645 void *(*dup_func) (void *),
646 void (*free_func) (void *),
647 void (*clear_free_func) (void *))
648{
628 EC_EXTRA_DATA **p; 649 EC_EXTRA_DATA **p;
629 650
630 if (ex_data == NULL) 651 if (ex_data == NULL)
631 return; 652 return;
632 653
633 for (p = ex_data; *p != NULL; p = &((*p)->next)) 654 for (p = ex_data; *p != NULL; p = &((*p)->next)) {
634 { 655 if ((*p)->dup_func == dup_func &&
635 if ((*p)->dup_func == dup_func && (*p)->free_func == free_func && (*p)->clear_free_func == clear_free_func) 656 (*p)->free_func == free_func &&
636 { 657 (*p)->clear_free_func == clear_free_func) {
637 EC_EXTRA_DATA *next = (*p)->next; 658 EC_EXTRA_DATA *next = (*p)->next;
638 659
639 (*p)->clear_free_func((*p)->data); 660 (*p)->clear_free_func((*p)->data);
640 free(*p); 661 free(*p);
641 662
642 *p = next; 663 *p = next;
643 return; 664 return;
644 }
645 } 665 }
646 } 666 }
667}
647 668
648/* this has 'package' visibility */ 669/* this has 'package' visibility */
649void EC_EX_DATA_free_all_data(EC_EXTRA_DATA **ex_data) 670void
650 { 671EC_EX_DATA_free_all_data(EC_EXTRA_DATA ** ex_data)
672{
651 EC_EXTRA_DATA *d; 673 EC_EXTRA_DATA *d;
652 674
653 if (ex_data == NULL) 675 if (ex_data == NULL)
654 return; 676 return;
655 677
656 d = *ex_data; 678 d = *ex_data;
657 while (d) 679 while (d) {
658 {
659 EC_EXTRA_DATA *next = d->next; 680 EC_EXTRA_DATA *next = d->next;
660 681
661 d->free_func(d->data); 682 d->free_func(d->data);
662 free(d); 683 free(d);
663 684
664 d = next; 685 d = next;
665 }
666 *ex_data = NULL;
667 } 686 }
687 *ex_data = NULL;
688}
668 689
669/* this has 'package' visibility */ 690/* this has 'package' visibility */
670void EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA **ex_data) 691void
671 { 692EC_EX_DATA_clear_free_all_data(EC_EXTRA_DATA ** ex_data)
693{
672 EC_EXTRA_DATA *d; 694 EC_EXTRA_DATA *d;
673 695
674 if (ex_data == NULL) 696 if (ex_data == NULL)
675 return; 697 return;
676 698
677 d = *ex_data; 699 d = *ex_data;
678 while (d) 700 while (d) {
679 {
680 EC_EXTRA_DATA *next = d->next; 701 EC_EXTRA_DATA *next = d->next;
681 702
682 d->clear_free_func(d->data); 703 d->clear_free_func(d->data);
683 free(d); 704 free(d);
684 705
685 d = next; 706 d = next;
686 }
687 *ex_data = NULL;
688 } 707 }
708 *ex_data = NULL;
709}
689 710
690 711
691/* functions for EC_POINT objects */ 712/* functions for EC_POINT objects */
692 713
693EC_POINT *EC_POINT_new(const EC_GROUP *group) 714EC_POINT *
694 { 715EC_POINT_new(const EC_GROUP * group)
716{
695 EC_POINT *ret; 717 EC_POINT *ret;
696 718
697 if (group == NULL) 719 if (group == NULL) {
698 {
699 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER); 720 ECerr(EC_F_EC_POINT_NEW, ERR_R_PASSED_NULL_PARAMETER);
700 return NULL; 721 return NULL;
701 } 722 }
702 if (group->meth->point_init == 0) 723 if (group->meth->point_init == 0) {
703 {
704 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 724 ECerr(EC_F_EC_POINT_NEW, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
705 return NULL; 725 return NULL;
706 } 726 }
707
708 ret = malloc(sizeof *ret); 727 ret = malloc(sizeof *ret);
709 if (ret == NULL) 728 if (ret == NULL) {
710 {
711 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE); 729 ECerr(EC_F_EC_POINT_NEW, ERR_R_MALLOC_FAILURE);
712 return NULL; 730 return NULL;
713 } 731 }
714
715 ret->meth = group->meth; 732 ret->meth = group->meth;
716 733
717 if (!ret->meth->point_init(ret)) 734 if (!ret->meth->point_init(ret)) {
718 {
719 free(ret); 735 free(ret);
720 return NULL; 736 return NULL;
721 }
722
723 return ret;
724 } 737 }
738 return ret;
739}
725 740
726 741
727void EC_POINT_free(EC_POINT *point) 742void
728 { 743EC_POINT_free(EC_POINT * point)
729 if (!point) return; 744{
745 if (!point)
746 return;
730 747
731 if (point->meth->point_finish != 0) 748 if (point->meth->point_finish != 0)
732 point->meth->point_finish(point); 749 point->meth->point_finish(point);
733 free(point); 750 free(point);
734 } 751}
735
736 752
737void EC_POINT_clear_free(EC_POINT *point) 753
738 { 754void
739 if (!point) return; 755EC_POINT_clear_free(EC_POINT * point)
756{
757 if (!point)
758 return;
740 759
741 if (point->meth->point_clear_finish != 0) 760 if (point->meth->point_clear_finish != 0)
742 point->meth->point_clear_finish(point); 761 point->meth->point_clear_finish(point);
@@ -744,301 +763,290 @@ void EC_POINT_clear_free(EC_POINT *point)
744 point->meth->point_finish(point); 763 point->meth->point_finish(point);
745 OPENSSL_cleanse(point, sizeof *point); 764 OPENSSL_cleanse(point, sizeof *point);
746 free(point); 765 free(point);
747 } 766}
748 767
749 768
750int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) 769int
751 { 770EC_POINT_copy(EC_POINT * dest, const EC_POINT * src)
752 if (dest->meth->point_copy == 0) 771{
753 { 772 if (dest->meth->point_copy == 0) {
754 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 773 ECerr(EC_F_EC_POINT_COPY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
755 return 0; 774 return 0;
756 } 775 }
757 if (dest->meth != src->meth) 776 if (dest->meth != src->meth) {
758 {
759 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS); 777 ECerr(EC_F_EC_POINT_COPY, EC_R_INCOMPATIBLE_OBJECTS);
760 return 0; 778 return 0;
761 } 779 }
762 if (dest == src) 780 if (dest == src)
763 return 1; 781 return 1;
764 return dest->meth->point_copy(dest, src); 782 return dest->meth->point_copy(dest, src);
765 } 783}
766 784
767 785
768EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) 786EC_POINT *
769 { 787EC_POINT_dup(const EC_POINT * a, const EC_GROUP * group)
788{
770 EC_POINT *t; 789 EC_POINT *t;
771 int r; 790 int r;
772 791
773 if (a == NULL) return NULL; 792 if (a == NULL)
793 return NULL;
774 794
775 t = EC_POINT_new(group); 795 t = EC_POINT_new(group);
776 if (t == NULL) return(NULL); 796 if (t == NULL)
797 return (NULL);
777 r = EC_POINT_copy(t, a); 798 r = EC_POINT_copy(t, a);
778 if (!r) 799 if (!r) {
779 {
780 EC_POINT_free(t); 800 EC_POINT_free(t);
781 return NULL; 801 return NULL;
782 } 802 } else
783 else return t; 803 return t;
784 } 804}
785 805
786 806
787const EC_METHOD *EC_POINT_method_of(const EC_POINT *point) 807const EC_METHOD *
788 { 808EC_POINT_method_of(const EC_POINT * point)
809{
789 return point->meth; 810 return point->meth;
790 } 811}
791 812
792 813
793int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point) 814int
794 { 815EC_POINT_set_to_infinity(const EC_GROUP * group, EC_POINT * point)
795 if (group->meth->point_set_to_infinity == 0) 816{
796 { 817 if (group->meth->point_set_to_infinity == 0) {
797 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 818 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
798 return 0; 819 return 0;
799 } 820 }
800 if (group->meth != point->meth) 821 if (group->meth != point->meth) {
801 {
802 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); 822 ECerr(EC_F_EC_POINT_SET_TO_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
803 return 0; 823 return 0;
804 }
805 return group->meth->point_set_to_infinity(group, point);
806 } 824 }
825 return group->meth->point_set_to_infinity(group, point);
826}
807 827
808 828
809int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, 829int
810 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx) 830EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
811 { 831 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
812 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) 832{
813 { 833 if (group->meth->point_set_Jprojective_coordinates_GFp == 0) {
814 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 834 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
815 return 0; 835 return 0;
816 } 836 }
817 if (group->meth != point->meth) 837 if (group->meth != point->meth) {
818 {
819 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS); 838 ECerr(EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
820 return 0; 839 return 0;
821 }
822 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
823 } 840 }
841 return group->meth->point_set_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
842}
824 843
825 844
826int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point, 845int
827 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx) 846EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
828 { 847 const EC_POINT *point, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
829 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) 848{
830 { 849 if (group->meth->point_get_Jprojective_coordinates_GFp == 0) {
831 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 850 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
832 return 0; 851 return 0;
833 } 852 }
834 if (group->meth != point->meth) 853 if (group->meth != point->meth) {
835 {
836 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS); 854 ECerr(EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
837 return 0; 855 return 0;
838 }
839 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
840 } 856 }
857 return group->meth->point_get_Jprojective_coordinates_GFp(group, point, x, y, z, ctx);
858}
841 859
842 860
843int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point, 861int
844 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) 862EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
845 { 863 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
846 if (group->meth->point_set_affine_coordinates == 0) 864{
847 { 865 if (group->meth->point_set_affine_coordinates == 0) {
848 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 866 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
849 return 0; 867 return 0;
850 } 868 }
851 if (group->meth != point->meth) 869 if (group->meth != point->meth) {
852 {
853 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS); 870 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
854 return 0; 871 return 0;
855 }
856 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
857 } 872 }
873 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
874}
858 875
859#ifndef OPENSSL_NO_EC2M 876#ifndef OPENSSL_NO_EC2M
860int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point, 877int
861 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) 878EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *point,
862 { 879 const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
863 if (group->meth->point_set_affine_coordinates == 0) 880{
864 { 881 if (group->meth->point_set_affine_coordinates == 0) {
865 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 882 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
866 return 0; 883 return 0;
867 } 884 }
868 if (group->meth != point->meth) 885 if (group->meth != point->meth) {
869 {
870 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS); 886 ECerr(EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
871 return 0; 887 return 0;
872 }
873 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
874 } 888 }
889 return group->meth->point_set_affine_coordinates(group, point, x, y, ctx);
890}
875#endif 891#endif
876 892
877int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point, 893int
878 BIGNUM *x, BIGNUM *y, BN_CTX *ctx) 894EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
879 { 895 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
880 if (group->meth->point_get_affine_coordinates == 0) 896{
881 { 897 if (group->meth->point_get_affine_coordinates == 0) {
882 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 898 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
883 return 0; 899 return 0;
884 } 900 }
885 if (group->meth != point->meth) 901 if (group->meth != point->meth) {
886 {
887 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS); 902 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP, EC_R_INCOMPATIBLE_OBJECTS);
888 return 0; 903 return 0;
889 }
890 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
891 } 904 }
905 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
906}
892 907
893#ifndef OPENSSL_NO_EC2M 908#ifndef OPENSSL_NO_EC2M
894int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point, 909int
895 BIGNUM *x, BIGNUM *y, BN_CTX *ctx) 910EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *point,
896 { 911 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
897 if (group->meth->point_get_affine_coordinates == 0) 912{
898 { 913 if (group->meth->point_get_affine_coordinates == 0) {
899 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 914 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
900 return 0; 915 return 0;
901 } 916 }
902 if (group->meth != point->meth) 917 if (group->meth != point->meth) {
903 {
904 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS); 918 ECerr(EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M, EC_R_INCOMPATIBLE_OBJECTS);
905 return 0; 919 return 0;
906 }
907 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
908 } 920 }
921 return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);
922}
909#endif 923#endif
910 924
911int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx) 925int
912 { 926EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
913 if (group->meth->add == 0) 927 const EC_POINT *b, BN_CTX *ctx)
914 { 928{
929 if (group->meth->add == 0) {
915 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 930 ECerr(EC_F_EC_POINT_ADD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
916 return 0; 931 return 0;
917 } 932 }
918 if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth)) 933 if ((group->meth != r->meth) || (r->meth != a->meth) || (a->meth != b->meth)) {
919 {
920 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS); 934 ECerr(EC_F_EC_POINT_ADD, EC_R_INCOMPATIBLE_OBJECTS);
921 return 0; 935 return 0;
922 }
923 return group->meth->add(group, r, a, b, ctx);
924 } 936 }
937 return group->meth->add(group, r, a, b, ctx);
938}
925 939
926 940
927int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx) 941int
928 { 942EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
929 if (group->meth->dbl == 0) 943{
930 { 944 if (group->meth->dbl == 0) {
931 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 945 ECerr(EC_F_EC_POINT_DBL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
932 return 0; 946 return 0;
933 } 947 }
934 if ((group->meth != r->meth) || (r->meth != a->meth)) 948 if ((group->meth != r->meth) || (r->meth != a->meth)) {
935 {
936 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS); 949 ECerr(EC_F_EC_POINT_DBL, EC_R_INCOMPATIBLE_OBJECTS);
937 return 0; 950 return 0;
938 }
939 return group->meth->dbl(group, r, a, ctx);
940 } 951 }
952 return group->meth->dbl(group, r, a, ctx);
953}
941 954
942 955
943int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx) 956int
944 { 957EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
945 if (group->meth->invert == 0) 958{
946 { 959 if (group->meth->invert == 0) {
947 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 960 ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
948 return 0; 961 return 0;
949 } 962 }
950 if (group->meth != a->meth) 963 if (group->meth != a->meth) {
951 {
952 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS); 964 ECerr(EC_F_EC_POINT_INVERT, EC_R_INCOMPATIBLE_OBJECTS);
953 return 0; 965 return 0;
954 }
955 return group->meth->invert(group, a, ctx);
956 } 966 }
967 return group->meth->invert(group, a, ctx);
968}
957 969
958 970
959int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point) 971int
960 { 972EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
961 if (group->meth->is_at_infinity == 0) 973{
962 { 974 if (group->meth->is_at_infinity == 0) {
963 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 975 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
964 return 0; 976 return 0;
965 } 977 }
966 if (group->meth != point->meth) 978 if (group->meth != point->meth) {
967 {
968 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS); 979 ECerr(EC_F_EC_POINT_IS_AT_INFINITY, EC_R_INCOMPATIBLE_OBJECTS);
969 return 0; 980 return 0;
970 }
971 return group->meth->is_at_infinity(group, point);
972 } 981 }
982 return group->meth->is_at_infinity(group, point);
983}
973 984
974 985
975int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx) 986int
976 { 987EC_POINT_is_on_curve(const EC_GROUP * group, const EC_POINT * point, BN_CTX * ctx)
977 if (group->meth->is_on_curve == 0) 988{
978 { 989 if (group->meth->is_on_curve == 0) {
979 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 990 ECerr(EC_F_EC_POINT_IS_ON_CURVE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
980 return 0; 991 return 0;
981 } 992 }
982 if (group->meth != point->meth) 993 if (group->meth != point->meth) {
983 {
984 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS); 994 ECerr(EC_F_EC_POINT_IS_ON_CURVE, EC_R_INCOMPATIBLE_OBJECTS);
985 return 0; 995 return 0;
986 }
987 return group->meth->is_on_curve(group, point, ctx);
988 } 996 }
997 return group->meth->is_on_curve(group, point, ctx);
998}
989 999
990 1000
991int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx) 1001int
992 { 1002EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
993 if (group->meth->point_cmp == 0) 1003 BN_CTX * ctx)
994 { 1004{
1005 if (group->meth->point_cmp == 0) {
995 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1006 ECerr(EC_F_EC_POINT_CMP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
996 return -1; 1007 return -1;
997 } 1008 }
998 if ((group->meth != a->meth) || (a->meth != b->meth)) 1009 if ((group->meth != a->meth) || (a->meth != b->meth)) {
999 {
1000 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS); 1010 ECerr(EC_F_EC_POINT_CMP, EC_R_INCOMPATIBLE_OBJECTS);
1001 return -1; 1011 return -1;
1002 }
1003 return group->meth->point_cmp(group, a, b, ctx);
1004 } 1012 }
1013 return group->meth->point_cmp(group, a, b, ctx);
1014}
1005 1015
1006 1016
1007int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx) 1017int
1008 { 1018EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1009 if (group->meth->make_affine == 0) 1019{
1010 { 1020 if (group->meth->make_affine == 0) {
1011 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1021 ECerr(EC_F_EC_POINT_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1012 return 0; 1022 return 0;
1013 } 1023 }
1014 if (group->meth != point->meth) 1024 if (group->meth != point->meth) {
1015 {
1016 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); 1025 ECerr(EC_F_EC_POINT_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1017 return 0; 1026 return 0;
1018 }
1019 return group->meth->make_affine(group, point, ctx);
1020 } 1027 }
1028 return group->meth->make_affine(group, point, ctx);
1029}
1021 1030
1022 1031
1023int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx) 1032int
1024 { 1033EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
1034 BN_CTX *ctx)
1035{
1025 size_t i; 1036 size_t i;
1026 1037
1027 if (group->meth->points_make_affine == 0) 1038 if (group->meth->points_make_affine == 0) {
1028 {
1029 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 1039 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1030 return 0; 1040 return 0;
1031 } 1041 }
1032 for (i = 0; i < num; i++) 1042 for (i = 0; i < num; i++) {
1033 { 1043 if (group->meth != points[i]->meth) {
1034 if (group->meth != points[i]->meth)
1035 {
1036 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS); 1044 ECerr(EC_F_EC_POINTS_MAKE_AFFINE, EC_R_INCOMPATIBLE_OBJECTS);
1037 return 0; 1045 return 0;
1038 }
1039 } 1046 }
1040 return group->meth->points_make_affine(group, num, points, ctx);
1041 } 1047 }
1048 return group->meth->points_make_affine(group, num, points, ctx);
1049}
1042 1050
1043 1051
1044/* Functions for point multiplication. 1052/* Functions for point multiplication.
@@ -1047,19 +1055,21 @@ int EC_POINTs_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[],
1047 * otherwise we dispatch through methods. 1055 * otherwise we dispatch through methods.
1048 */ 1056 */
1049 1057
1050int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, 1058int
1051 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx) 1059EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
1052 { 1060 size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
1061{
1053 if (group->meth->mul == 0) 1062 if (group->meth->mul == 0)
1054 /* use default */ 1063 /* use default */
1055 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx); 1064 return ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
1056 1065
1057 return group->meth->mul(group, r, scalar, num, points, scalars, ctx); 1066 return group->meth->mul(group, r, scalar, num, points, scalars, ctx);
1058 } 1067}
1059 1068
1060int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar, 1069int
1061 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx) 1070EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1062 { 1071 const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)
1072{
1063 /* just a convenient interface to EC_POINTs_mul() */ 1073 /* just a convenient interface to EC_POINTs_mul() */
1064 1074
1065 const EC_POINT *points[1]; 1075 const EC_POINT *points[1];
@@ -1068,11 +1078,14 @@ int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
1068 points[0] = point; 1078 points[0] = point;
1069 scalars[0] = p_scalar; 1079 scalars[0] = p_scalar;
1070 1080
1071 return EC_POINTs_mul(group, r, g_scalar, (point != NULL && p_scalar != NULL), points, scalars, ctx); 1081 return EC_POINTs_mul(group, r, g_scalar,
1072 } 1082 (point != NULL && p_scalar != NULL),
1083 points, scalars, ctx);
1084}
1073 1085
1074int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx) 1086int
1075 { 1087EC_GROUP_precompute_mult(EC_GROUP * group, BN_CTX * ctx)
1088{
1076 if (group->meth->mul == 0) 1089 if (group->meth->mul == 0)
1077 /* use default */ 1090 /* use default */
1078 return ec_wNAF_precompute_mult(group, ctx); 1091 return ec_wNAF_precompute_mult(group, ctx);
@@ -1080,11 +1093,12 @@ int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
1080 if (group->meth->precompute_mult != 0) 1093 if (group->meth->precompute_mult != 0)
1081 return group->meth->precompute_mult(group, ctx); 1094 return group->meth->precompute_mult(group, ctx);
1082 else 1095 else
1083 return 1; /* nothing to do, so report success */ 1096 return 1; /* nothing to do, so report success */
1084 } 1097}
1085 1098
1086int EC_GROUP_have_precompute_mult(const EC_GROUP *group) 1099int
1087 { 1100EC_GROUP_have_precompute_mult(const EC_GROUP * group)
1101{
1088 if (group->meth->mul == 0) 1102 if (group->meth->mul == 0)
1089 /* use default */ 1103 /* use default */
1090 return ec_wNAF_have_precompute_mult(group); 1104 return ec_wNAF_have_precompute_mult(group);
@@ -1092,5 +1106,6 @@ int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
1092 if (group->meth->have_precompute_mult != 0) 1106 if (group->meth->have_precompute_mult != 0)
1093 return group->meth->have_precompute_mult(group); 1107 return group->meth->have_precompute_mult(group);
1094 else 1108 else
1095 return 0; /* cannot tell whether precomputation has been performed */ 1109 return 0; /* cannot tell whether precomputation has
1096 } 1110 * been performed */
1111}