diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/ffi/ffi.cpp | 28 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 6 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 24 |
3 files changed, 50 insertions, 8 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 3803e5dd6..f72ba180a 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -1703,7 +1703,7 @@ int botan_privkey_load_ec(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) { -#if defined(BOTAN_HAS_ECDSA) +#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) *key = nullptr; try { @@ -1723,6 +1723,32 @@ int botan_privkey_load_ec(botan_privkey_t* key, #endif } +BOTAN_DLL int botan_pubkey_load_ec(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, + const char* curve_name) + { +#if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) + *key = nullptr; + try + { + Botan::Null_RNG null_rng; + Botan::EC_Group grp(curve_name); + Botan::PointGFp uncompressed_point(grp.get_curve(), safe_get(public_x), safe_get(public_y)); + *key = new botan_pubkey_struct(new Botan::ECDSA_PublicKey(grp, uncompressed_point)); + return 0; + } + catch(std::exception& e) + { + log_exception(BOTAN_CURRENT_FUNCTION, e.what()); + } + return -1; +#else + BOTAN_UNUSED(key, public_x, public_y, curve_name); + return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; +#endif + } + int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 206b749b2..e5a399bbd 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -868,6 +868,12 @@ BOTAN_DLL int botan_privkey_load_ec(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name); + +BOTAN_DLL int botan_pubkey_load_ec(botan_pubkey_t* key, + const botan_mp_t x, + const botan_mp_t y, + const char* curve_name); + /* * Public Key Encryption */ diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index 8411af941..e1cff32d9 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -1198,20 +1198,27 @@ class FFI_Unit_Tests : public Test Test::Result result("FFI ECDSA"); static const char* kCurve = "secp384r1"; botan_privkey_t priv; - botan_privkey_t loaded_privkey; botan_pubkey_t pub; + botan_privkey_t loaded_privkey; + botan_pubkey_t loaded_pubkey; REQUIRE_FFI_OK(botan_privkey_create_ecdsa, (&priv, rng, kCurve)); TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv)); ffi_test_pubkey_export(result, pub, priv, rng); // Check key load functions - botan_mp_t x; - botan_mp_init(&x); - - TEST_FFI_OK(botan_privkey_get_field, (x, priv, "x")); - TEST_FFI_OK(botan_privkey_load_ec, (&loaded_privkey, x, kCurve)); + botan_mp_t private_scalar, public_x, public_y; + botan_mp_init(&private_scalar); + botan_mp_init(&public_x); + botan_mp_init(&public_y); + + TEST_FFI_OK(botan_privkey_get_field, (private_scalar, priv, "x")); + TEST_FFI_OK(botan_pubkey_get_field, (public_x, pub, "public_x")); + TEST_FFI_OK(botan_pubkey_get_field, (public_y, pub, "public_y")); + TEST_FFI_OK(botan_privkey_load_ec, (&loaded_privkey, private_scalar, kCurve)); + TEST_FFI_OK(botan_pubkey_load_ec, (&loaded_pubkey, public_x, public_y, kCurve)); TEST_FFI_OK(botan_privkey_check_key, (loaded_privkey, rng, 0)); + TEST_FFI_OK(botan_pubkey_check_key, (loaded_pubkey, rng, 0)); char namebuf[32] = { 0 }; size_t name_len = sizeof(namebuf); @@ -1262,10 +1269,13 @@ class FFI_Unit_Tests : public Test TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier)); } - botan_mp_destroy(x); + botan_mp_destroy(private_scalar); + botan_mp_destroy(public_x); + botan_mp_destroy(public_y); TEST_FFI_OK(botan_pubkey_destroy, (pub)); TEST_FFI_OK(botan_privkey_destroy, (priv)); TEST_FFI_OK(botan_privkey_destroy, (loaded_privkey)); + TEST_FFI_OK(botan_pubkey_destroy, (loaded_pubkey)); return result; } |