diff options
Diffstat (limited to 'src/lib/libcrypto/ec/ec_key.c')
-rw-r--r-- | src/lib/libcrypto/ec/ec_key.c | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c index 522802c07a..bf9fd2dc2c 100644 --- a/src/lib/libcrypto/ec/ec_key.c +++ b/src/lib/libcrypto/ec/ec_key.c | |||
@@ -64,7 +64,9 @@ | |||
64 | #include <string.h> | 64 | #include <string.h> |
65 | #include "ec_lcl.h" | 65 | #include "ec_lcl.h" |
66 | #include <openssl/err.h> | 66 | #include <openssl/err.h> |
67 | #include <string.h> | 67 | #ifdef OPENSSL_FIPS |
68 | #include <openssl/fips.h> | ||
69 | #endif | ||
68 | 70 | ||
69 | EC_KEY *EC_KEY_new(void) | 71 | EC_KEY *EC_KEY_new(void) |
70 | { | 72 | { |
@@ -78,6 +80,7 @@ EC_KEY *EC_KEY_new(void) | |||
78 | } | 80 | } |
79 | 81 | ||
80 | ret->version = 1; | 82 | ret->version = 1; |
83 | ret->flags = 0; | ||
81 | ret->group = NULL; | 84 | ret->group = NULL; |
82 | ret->pub_key = NULL; | 85 | ret->pub_key = NULL; |
83 | ret->priv_key= NULL; | 86 | ret->priv_key= NULL; |
@@ -197,6 +200,7 @@ EC_KEY *EC_KEY_copy(EC_KEY *dest, const EC_KEY *src) | |||
197 | dest->enc_flag = src->enc_flag; | 200 | dest->enc_flag = src->enc_flag; |
198 | dest->conv_form = src->conv_form; | 201 | dest->conv_form = src->conv_form; |
199 | dest->version = src->version; | 202 | dest->version = src->version; |
203 | dest->flags = src->flags; | ||
200 | 204 | ||
201 | return dest; | 205 | return dest; |
202 | } | 206 | } |
@@ -237,6 +241,11 @@ int EC_KEY_generate_key(EC_KEY *eckey) | |||
237 | BIGNUM *priv_key = NULL, *order = NULL; | 241 | BIGNUM *priv_key = NULL, *order = NULL; |
238 | EC_POINT *pub_key = NULL; | 242 | EC_POINT *pub_key = NULL; |
239 | 243 | ||
244 | #ifdef OPENSSL_FIPS | ||
245 | if (FIPS_mode()) | ||
246 | return FIPS_ec_key_generate_key(eckey); | ||
247 | #endif | ||
248 | |||
240 | if (!eckey || !eckey->group) | 249 | if (!eckey || !eckey->group) |
241 | { | 250 | { |
242 | ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER); | 251 | ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER); |
@@ -371,6 +380,82 @@ err: | |||
371 | return(ok); | 380 | return(ok); |
372 | } | 381 | } |
373 | 382 | ||
383 | int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x, BIGNUM *y) | ||
384 | { | ||
385 | BN_CTX *ctx = NULL; | ||
386 | BIGNUM *tx, *ty; | ||
387 | EC_POINT *point = NULL; | ||
388 | int ok = 0, tmp_nid, is_char_two = 0; | ||
389 | |||
390 | if (!key || !key->group || !x || !y) | ||
391 | { | ||
392 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, | ||
393 | ERR_R_PASSED_NULL_PARAMETER); | ||
394 | return 0; | ||
395 | } | ||
396 | ctx = BN_CTX_new(); | ||
397 | if (!ctx) | ||
398 | goto err; | ||
399 | |||
400 | point = EC_POINT_new(key->group); | ||
401 | |||
402 | if (!point) | ||
403 | goto err; | ||
404 | |||
405 | tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(key->group)); | ||
406 | |||
407 | if (tmp_nid == NID_X9_62_characteristic_two_field) | ||
408 | is_char_two = 1; | ||
409 | |||
410 | tx = BN_CTX_get(ctx); | ||
411 | ty = BN_CTX_get(ctx); | ||
412 | #ifndef OPENSSL_NO_EC2M | ||
413 | if (is_char_two) | ||
414 | { | ||
415 | if (!EC_POINT_set_affine_coordinates_GF2m(key->group, point, | ||
416 | x, y, ctx)) | ||
417 | goto err; | ||
418 | if (!EC_POINT_get_affine_coordinates_GF2m(key->group, point, | ||
419 | tx, ty, ctx)) | ||
420 | goto err; | ||
421 | } | ||
422 | else | ||
423 | #endif | ||
424 | { | ||
425 | if (!EC_POINT_set_affine_coordinates_GFp(key->group, point, | ||
426 | x, y, ctx)) | ||
427 | goto err; | ||
428 | if (!EC_POINT_get_affine_coordinates_GFp(key->group, point, | ||
429 | tx, ty, ctx)) | ||
430 | goto err; | ||
431 | } | ||
432 | /* Check if retrieved coordinates match originals: if not values | ||
433 | * are out of range. | ||
434 | */ | ||
435 | if (BN_cmp(x, tx) || BN_cmp(y, ty)) | ||
436 | { | ||
437 | ECerr(EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES, | ||
438 | EC_R_COORDINATES_OUT_OF_RANGE); | ||
439 | goto err; | ||
440 | } | ||
441 | |||
442 | if (!EC_KEY_set_public_key(key, point)) | ||
443 | goto err; | ||
444 | |||
445 | if (EC_KEY_check_key(key) == 0) | ||
446 | goto err; | ||
447 | |||
448 | ok = 1; | ||
449 | |||
450 | err: | ||
451 | if (ctx) | ||
452 | BN_CTX_free(ctx); | ||
453 | if (point) | ||
454 | EC_POINT_free(point); | ||
455 | return ok; | ||
456 | |||
457 | } | ||
458 | |||
374 | const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) | 459 | const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key) |
375 | { | 460 | { |
376 | return key->group; | 461 | return key->group; |
@@ -461,3 +546,18 @@ int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx) | |||
461 | return 0; | 546 | return 0; |
462 | return EC_GROUP_precompute_mult(key->group, ctx); | 547 | return EC_GROUP_precompute_mult(key->group, ctx); |
463 | } | 548 | } |
549 | |||
550 | int EC_KEY_get_flags(const EC_KEY *key) | ||
551 | { | ||
552 | return key->flags; | ||
553 | } | ||
554 | |||
555 | void EC_KEY_set_flags(EC_KEY *key, int flags) | ||
556 | { | ||
557 | key->flags |= flags; | ||
558 | } | ||
559 | |||
560 | void EC_KEY_clear_flags(EC_KEY *key, int flags) | ||
561 | { | ||
562 | key->flags &= ~flags; | ||
563 | } | ||