aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-06-12 15:32:08 -0400
committerJack Lloyd <[email protected]>2017-06-12 15:32:08 -0400
commit40feca3a3235d9ca49e7443619451205ccb3fd06 (patch)
tree653d281ad9417c7186a8a881687a61dc01a37d9a /src/lib
parent6149a8a7ac9dd940c34af7f41e10610f75e8995b (diff)
parent5ba835fd0469f1b52d2dedfc64bbd63fe0f8e5b8 (diff)
Merge GH #1074 Add ability to load ECDSA/ECDH via FFI
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ffi/ffi.cpp155
-rw-r--r--src/lib/ffi/ffi.h21
2 files changed, 175 insertions, 1 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp
index d7f164b86..f46d7ec11 100644
--- a/src/lib/ffi/ffi.cpp
+++ b/src/lib/ffi/ffi.cpp
@@ -330,7 +330,59 @@ Botan::BigInt privkey_get_field(const Botan::Private_Key& key,
throw Botan::Exception("Unsupported algorithm type for botan_privkey_get_field");
}
-}
+#if defined(BOTAN_HAS_ECDSA) || defined(BOTAN_HAS_ECDH)
+template<class ECPrivateKey_t>
+int privkey_load_ec(std::unique_ptr<ECPrivateKey_t>& key,
+ const Botan::BigInt& scalar,
+ const char* curve_name)
+ {
+
+ if(curve_name == nullptr)
+ return -1;
+
+ try
+ {
+ Botan::Null_RNG null_rng;
+ Botan::EC_Group grp(curve_name);
+ key.reset(new ECPrivateKey_t(null_rng, grp, scalar));
+ return 0;
+ }
+ catch(std::exception& e)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, e.what());
+ }
+ return -1;
+ }
+#endif
+
+#if defined(BOTAN_HAS_ECDSA) || defined(BOTAN_HAS_ECDH)
+template<class ECPublicKey_t>
+int pubkey_load_ec( std::unique_ptr<ECPublicKey_t>& key,
+ const Botan::BigInt& public_x,
+ const Botan::BigInt& public_y,
+ const char* curve_name)
+ {
+
+ if(curve_name == nullptr)
+ return -1;
+
+ try
+ {
+ Botan::Null_RNG null_rng;
+ Botan::EC_Group grp(curve_name);
+ Botan::PointGFp uncompressed_point(grp.get_curve(), public_x, public_y);
+ key.reset(new ECPublicKey_t(grp, uncompressed_point));
+ return 0;
+ }
+ catch(std::exception& e)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, e.what());
+ }
+ return -1;
+ }
+#endif
+
+} // closes anonymous namespace
extern "C" {
@@ -1699,6 +1751,107 @@ int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key,
#endif
}
+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)
+ {
+#if defined(BOTAN_HAS_ECDSA)
+ std::unique_ptr<Botan::ECDSA_PublicKey> p_key;
+ try
+ {
+ if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name))
+ {
+ *key = new botan_pubkey_struct(p_key.release());
+ return 0;
+ }
+ }
+ catch(std::exception& exn)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
+ }
+ return -1;
+#else
+ BOTAN_UNUSED(key, public_x, public_y, curve_name);
+ return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
+#endif
+ }
+
+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)
+ {
+#if defined(BOTAN_HAS_ECDH)
+ std::unique_ptr<Botan::ECDH_PublicKey> p_key;
+ try
+ {
+ if(!pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name))
+ {
+ *key = new botan_pubkey_struct(p_key.release());
+ return 0;
+ }
+ }
+ catch(std::exception& exn)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
+ }
+ return -1;
+#else
+ BOTAN_UNUSED(key, public_x, public_y, curve_name);
+ return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
+#endif
+ }
+
+int botan_privkey_load_ecdsa(botan_privkey_t* key,
+ const botan_mp_t scalar,
+ const char* curve_name)
+ {
+#if defined(BOTAN_HAS_ECDSA)
+ std::unique_ptr<Botan::ECDSA_PrivateKey> p_key;
+ try
+ {
+ if(!privkey_load_ec(p_key, safe_get(scalar), curve_name))
+ {
+ *key = new botan_privkey_struct(p_key.release());
+ return 0;
+ }
+ }
+ catch(std::exception& exn)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
+ }
+ return -1;
+#else
+ BOTAN_UNUSED(key, scalar, curve_name);
+ return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
+#endif
+ }
+int botan_privkey_load_ecdh(botan_privkey_t* key,
+ const botan_mp_t scalar,
+ const char* curve_name)
+ {
+#if defined(BOTAN_HAS_ECDH)
+ std::unique_ptr<Botan::ECDH_PrivateKey> p_key;
+ try
+ {
+ if(!privkey_load_ec(p_key, safe_get(scalar), curve_name))
+ {
+ *key = new botan_privkey_struct(p_key.release());
+ return 0;
+ }
+ }
+ catch(std::exception& exn)
+ {
+ log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
+ }
+ return -1;
+#else
+ BOTAN_UNUSED(key, scalar, 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 3e64c69cd..a179e5fa4 100644
--- a/src/lib/ffi/ffi.h
+++ b/src/lib/ffi/ffi.h
@@ -862,6 +862,27 @@ BOTAN_DLL int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key,
uint8_t pubkey[32]);
/*
+* Algorithm specific key operations: ECDSA and ECDH
+*/
+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_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
*/
typedef struct botan_pk_op_encrypt_struct* botan_pk_op_encrypt_t;