diff options
author | Krzysztof Kwiatkowski <[email protected]> | 2017-06-10 17:43:43 +0100 |
---|---|---|
committer | Krzysztof Kwiatkowski <[email protected]> | 2017-06-10 17:43:43 +0100 |
commit | 3619c2f47e5aaa1eb76f72e9751c6a98b8f2ca46 (patch) | |
tree | 5adaaa805c26cb8a376c08955811a7b6abba1dfe /src | |
parent | 30568fc980e64d8692e1bb747d5b26f6ab62d1bb (diff) |
FFI: Add functions specific to ECDSA and ECDH
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/ffi/ffi.cpp | 71 | ||||
-rw-r--r-- | src/lib/ffi/ffi.h | 16 | ||||
-rw-r--r-- | src/tests/test_ffi.cpp | 4 |
3 files changed, 76 insertions, 15 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index f72ba180a..9940c380e 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -1699,9 +1699,14 @@ int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, #endif } -int botan_privkey_load_ec(botan_privkey_t* key, - const botan_mp_t scalar, - const char* curve_name) +namespace { +/* Those functions can't be defined in namespace {} that's located higher in this file + * as they use botan_privkey_struct/botan_pubkey_struct structs. + */ +int privkey_load_ec(botan_privkey_t* key, + const botan_mp_t scalar, + const char* curve_name, + const std::string& algo_name) { #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) *key = nullptr; @@ -1709,7 +1714,14 @@ int botan_privkey_load_ec(botan_privkey_t* key, { Botan::Null_RNG null_rng; Botan::EC_Group grp(curve_name); - *key = new botan_privkey_struct(new Botan::ECDSA_PrivateKey(null_rng, grp, safe_get(scalar))); + if (algo_name.compare("ECDSA") == 0) { + *key = new botan_privkey_struct(new Botan::ECDSA_PrivateKey(null_rng, grp, safe_get(scalar))); + } else if (algo_name.compare("ECDH") == 0) { + *key = new botan_privkey_struct(new Botan::ECDH_PrivateKey(null_rng, grp, safe_get(scalar))); + } else { + return -1; + } + return 0; } catch(std::exception& e) @@ -1723,19 +1735,29 @@ 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) +int pubkey_load_ec(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, + const char* curve_name, + const std::string& algo_name) { #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) + if(key == nullptr || curve_name == nullptr) + return -1; *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)); + if (algo_name.compare("ECDSA") == 0) { + *key = new botan_pubkey_struct(new Botan::ECDSA_PublicKey(grp, uncompressed_point)); + } else if (algo_name.compare("ECDH") == 0) { + *key = new botan_pubkey_struct(new Botan::ECDH_PublicKey(grp, uncompressed_point)); + } else { + return -1; + } + return 0; } catch(std::exception& e) @@ -1748,6 +1770,37 @@ BOTAN_DLL int botan_pubkey_load_ec(botan_pubkey_t* key, return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; #endif } +} // namespace {} + +int botan_pubkey_load_ecdsa(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, + const char* curve_name) + { + return pubkey_load_ec(key, public_x, public_y, curve_name, "ECDSA"); + } + +int botan_pubkey_load_ecdh(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, + const char* curve_name) + { + return pubkey_load_ec(key, public_x, public_y, curve_name, "ECDH"); + } + +int botan_privkey_load_ecdsa(botan_privkey_t* key, + const botan_mp_t scalar, + const char* curve_name) + { + return privkey_load_ec(key, scalar, curve_name, "ECDSA"); + } + +int botan_privkey_load_ecdh(botan_privkey_t* key, + const botan_mp_t scalar, + const char* curve_name) + { + return privkey_load_ec(key, scalar, curve_name, "ECDH"); + } int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index e5a399bbd..a179e5fa4 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -864,16 +864,24 @@ BOTAN_DLL int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, /* * Algorithm specific key operations: ECDSA and ECDH */ -BOTAN_DLL int botan_privkey_load_ec(botan_privkey_t* key, +BOTAN_DLL int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name); +BOTAN_DLL int botan_pubkey_load_ecdsa(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, + 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, +BOTAN_DLL int botan_pubkey_load_ecdh(botan_pubkey_t* key, + const botan_mp_t public_x, + const botan_mp_t public_y, const char* curve_name); +BOTAN_DLL int botan_privkey_load_ecdh(botan_privkey_t* key, + const botan_mp_t scalar, + const char* curve_name); + /* * Public Key Encryption */ diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp index e1cff32d9..dc6b25a48 100644 --- a/src/tests/test_ffi.cpp +++ b/src/tests/test_ffi.cpp @@ -1215,8 +1215,8 @@ class FFI_Unit_Tests : public Test 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_load_ecdsa, (&loaded_privkey, private_scalar, kCurve)); + TEST_FFI_OK(botan_pubkey_load_ecdsa, (&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)); |