From 729ee64431748d898a2a53baa8f8e17f2925e16e Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Wed, 6 Sep 2017 13:30:30 -0400 Subject: Add support for computing SM2 ZA field to FFI This is a contribution from Ribose Inc. --- src/lib/ffi/ffi.h | 6 ++++++ src/lib/ffi/ffi_pkey_algs.cpp | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src/lib/ffi') diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 565d5ce7b..005d32eee 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -961,6 +961,12 @@ BOTAN_DLL int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name); +BOTAN_DLL int botan_pubkey_sm2_compute_za(uint8_t out[], + size_t* out_len, + const char* ident, + const char* hash_algo, + const botan_pubkey_t key); + /* * Public Key Encryption */ diff --git a/src/lib/ffi/ffi_pkey_algs.cpp b/src/lib/ffi/ffi_pkey_algs.cpp index b06fd113c..83ee51768 100644 --- a/src/lib/ffi/ffi_pkey_algs.cpp +++ b/src/lib/ffi/ffi_pkey_algs.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -570,6 +571,41 @@ int botan_privkey_load_ecdh(botan_privkey_t* key, /* SM2 specific operations */ +int botan_pubkey_sm2_compute_za(uint8_t out[], + size_t* out_len, + const char* ident, + const char* hash_algo, + const botan_pubkey_t key) + { + if(out == nullptr || out_len == nullptr) + return BOTAN_FFI_ERROR_NULL_POINTER; + if(ident == nullptr || hash_algo == nullptr || key == nullptr) + return BOTAN_FFI_ERROR_NULL_POINTER; + +#if defined(BOTAN_HAS_SM2) + return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() { + const Botan::Public_Key& pub_key = safe_get(key); + const Botan::EC_PublicKey* ec_key = dynamic_cast(&pub_key); + if(key == nullptr) + return BOTAN_FFI_ERROR_BAD_PARAMETER; + + if(ec_key->algo_name() != "SM2_Sig" && ec_key->algo_name() != "SM2_Enc") + return BOTAN_FFI_ERROR_BAD_PARAMETER; + + const std::string ident_str(ident); + std::unique_ptr hash = + Botan::HashFunction::create_or_throw(hash_algo); + + const std::vector za = + Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), ec_key->public_point()); + + return write_vec_output(out, out_len, za); + }); +#else + return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; +#endif + } + int botan_pubkey_load_sm2(botan_pubkey_t* key, const botan_mp_t public_x, const botan_mp_t public_y, -- cgit v1.2.3