diff options
author | Jorge Ramirez-Ortiz <jorge@foundries.io> | 2023-03-20 12:51:55 +0100 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2023-03-27 12:06:21 +0200 |
commit | 1ffb6e19eeee95784456831da329cbccaa59fbcf (patch) | |
tree | 26d8a7b6357ded3e166ecba4a84cfe288a75a41b /test/evp_extra_test.c | |
parent | 9adbce74933b87dd4fe776b70fef55f2f468f5f7 (diff) |
test: evp_extra: EC, read affine coordinates
Add a test to read the EC X,Y coordinates.
Support legacy keys.
Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20535)
Diffstat (limited to 'test/evp_extra_test.c')
-rw-r--r-- | test/evp_extra_test.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/test/evp_extra_test.c b/test/evp_extra_test.c index a829a1f7ef..1141d52666 100644 --- a/test/evp_extra_test.c +++ b/test/evp_extra_test.c @@ -1032,6 +1032,65 @@ static int test_EC_priv_pub(void) return ret; } +/* Also test that we can read the EC PUB affine coordinates */ +static int test_evp_get_ec_pub(void) +{ + OSSL_PARAM_BLD *bld = NULL; + OSSL_PARAM *params = NULL; + unsigned char *pad = NULL; + EVP_PKEY *keypair = NULL; + BIGNUM *priv = NULL; + BIGNUM *x = NULL; + BIGNUM *y = NULL; + int ret = 0; + + if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) + goto err; + + if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) + || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, + OSSL_PKEY_PARAM_GROUP_NAME, + "P-256", 0)) + || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, + OSSL_PKEY_PARAM_PUB_KEY, + ec_pub, sizeof(ec_pub))) + || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, + priv))) + goto err; + + if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)) + || !TEST_ptr(keypair = make_key_fromdata("EC", params))) + goto err; + + if (!test_selection(keypair, EVP_PKEY_KEYPAIR)) + goto err; + + if (!EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_X, &x) + || !EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_Y, &y)) + goto err; + + if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub)))) + goto err; + + pad[0] = ec_pub[0]; + BN_bn2bin(x, &pad[1]); + BN_bn2bin(y, &pad[33]); + if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0)) + goto err; + + ret = 1; + +err: + OSSL_PARAM_free(params); + OSSL_PARAM_BLD_free(bld); + EVP_PKEY_free(keypair); + OPENSSL_free(pad); + BN_free(priv); + BN_free(x); + BN_free(y); + return ret; +} + /* Test that using a legacy EC key with only a private key in it works */ # ifndef OPENSSL_NO_DEPRECATED_3_0 static int test_EC_priv_only_legacy(void) @@ -1095,6 +1154,76 @@ static int test_EC_priv_only_legacy(void) return ret; } + +static int test_evp_get_ec_pub_legacy(void) +{ + OSSL_LIB_CTX *libctx = NULL; + unsigned char *pad = NULL; + EVP_PKEY *pkey = NULL; + EC_KEY *eckey = NULL; + BIGNUM *priv = NULL; + BIGNUM *x = NULL; + BIGNUM *y = NULL; + int ret = 0; + + if (!TEST_ptr(libctx = OSSL_LIB_CTX_new())) + goto err; + + /* Create the legacy key */ + if (!TEST_ptr(eckey = EC_KEY_new_by_curve_name_ex(libctx, NULL, + NID_X9_62_prime256v1))) + goto err; + + if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL))) + goto err; + + if (!TEST_true(EC_KEY_set_private_key(eckey, priv))) + goto err; + + if (!TEST_ptr(x = BN_bin2bn(&ec_pub[1], 32, NULL))) + goto err; + + if (!TEST_ptr(y = BN_bin2bn(&ec_pub[33], 32, NULL))) + goto err; + + if (!TEST_true(EC_KEY_set_public_key_affine_coordinates(eckey, x, y))) + goto err; + + if (!TEST_ptr(pkey = EVP_PKEY_new())) + goto err; + + /* Transfer the legacy key */ + if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))) + goto err; + eckey = NULL; + + if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &x)) + || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &y))) + goto err; + + if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub)))) + goto err; + + pad[0] = ec_pub[0]; + BN_bn2bin(x, &pad[1]); + BN_bn2bin(y, &pad[33]); + + if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0)) + goto err; + + ret = 1; + +err: + OSSL_LIB_CTX_free(libctx); + EVP_PKEY_free(pkey); + EC_KEY_free(eckey); + OPENSSL_free(pad); + BN_free(priv); + BN_free(x); + BN_free(y); + + return ret; +} # endif /* OPENSSL_NO_DEPRECATED_3_0 */ #endif /* OPENSSL_NO_EC */ @@ -4865,8 +4994,10 @@ int setup_tests(void) #endif #ifndef OPENSSL_NO_EC ADD_TEST(test_EC_priv_pub); + ADD_TEST(test_evp_get_ec_pub); # ifndef OPENSSL_NO_DEPRECATED_3_0 ADD_TEST(test_EC_priv_only_legacy); + ADD_TEST(test_evp_get_ec_pub_legacy); # endif #endif ADD_ALL_TESTS(test_keygen_with_empty_template, 2); |