aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-09-06 13:30:30 -0400
committerJack Lloyd <[email protected]>2017-09-06 13:31:12 -0400
commit729ee64431748d898a2a53baa8f8e17f2925e16e (patch)
tree6cb50725d51f1d1ac6fb0bd4730a7ab6cfd084df
parent5a2db384a328dde3d5059fcdc2ebc1ff9a6039a7 (diff)
Add support for computing SM2 ZA field to FFI
This is a contribution from Ribose Inc.
-rw-r--r--src/lib/ffi/ffi.h6
-rw-r--r--src/lib/ffi/ffi_pkey_algs.cpp36
-rw-r--r--src/tests/test_ffi.cpp8
3 files changed, 50 insertions, 0 deletions
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 <botan/ffi.h>
+#include <botan/hash.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/ffi_pkey.h>
#include <botan/internal/ffi_rng.h>
@@ -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<const Botan::EC_PublicKey*>(&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<Botan::HashFunction> hash =
+ Botan::HashFunction::create_or_throw(hash_algo);
+
+ const std::vector<uint8_t> 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,
diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp
index 41fa3a044..4a8e7b317 100644
--- a/src/tests/test_ffi.cpp
+++ b/src/tests/test_ffi.cpp
@@ -1408,6 +1408,10 @@ class FFI_Unit_Tests : public Test
TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
ffi_test_pubkey_export(result, pub, priv, rng);
+ uint8_t za[32];
+ size_t sizeof_za = sizeof(za);
+ TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
+
// Check key load functions
botan_mp_t private_scalar, public_x, public_y;
botan_mp_init(&private_scalar);
@@ -1495,6 +1499,10 @@ class FFI_Unit_Tests : public Test
TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
ffi_test_pubkey_export(result, pub, priv, rng);
+ uint8_t za[32];
+ size_t sizeof_za = sizeof(za);
+ TEST_FFI_OK(botan_pubkey_sm2_compute_za, (za, &sizeof_za, "Ident", "SM3", pub));
+
// Check key load functions
botan_mp_t private_scalar, public_x, public_y;
botan_mp_init(&private_scalar);