aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/data/hash/sm3.vec5
-rw-r--r--src/tests/data/pubkey/sm2_sig.vec14
-rw-r--r--src/tests/test_ffi.cpp92
-rw-r--r--src/tests/test_sm2.cpp68
4 files changed, 179 insertions, 0 deletions
diff --git a/src/tests/data/hash/sm3.vec b/src/tests/data/hash/sm3.vec
index 2dd4710bc..f5dee3ccb 100644
--- a/src/tests/data/hash/sm3.vec
+++ b/src/tests/data/hash/sm3.vec
@@ -1,5 +1,10 @@
[SM3]
+# From ZA computation in https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02
+
+In = 0090414C494345313233405941484F4F2E434F4D787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E49863E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A20AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857
+Out = F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A
+
# https://tools.ietf.org/html/draft-shen-sm3-hash-01
In = 616263
Out = 66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0
diff --git a/src/tests/data/pubkey/sm2_sig.vec b/src/tests/data/pubkey/sm2_sig.vec
new file mode 100644
index 000000000..d3356cceb
--- /dev/null
+++ b/src/tests/data/pubkey/sm2_sig.vec
@@ -0,0 +1,14 @@
+
+P = 0x8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3
+A = 0x787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498
+B = 0x63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A
+xG = 0x421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D
+yG = 0x0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2
+Order = 0x8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7
+Cofactor = 1
+
+Msg = 6D65737361676520646967657374
+x = 0x128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263
+Nonce = 6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F
+Signature = 40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D16FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7
diff --git a/src/tests/test_ffi.cpp b/src/tests/test_ffi.cpp
index e9d9b6e20..bf5fc0e9c 100644
--- a/src/tests/test_ffi.cpp
+++ b/src/tests/test_ffi.cpp
@@ -383,6 +383,10 @@ class FFI_Unit_Tests : public Test
results.push_back(ffi_test_ecdh(rng));
#endif
+#if defined(BOTAN_HAS_SM2)
+ results.push_back(ffi_test_sm2(rng));
+#endif
+
#if defined(BOTAN_HAS_MCELIECE)
results.push_back(ffi_test_mceliece(rng));
#endif
@@ -1280,6 +1284,94 @@ class FFI_Unit_Tests : public Test
return result;
}
+ Test::Result ffi_test_sm2(botan_rng_t rng)
+ {
+ Test::Result result("FFI SM2");
+ static const char* kCurve = "sm2p256v1";
+ const std::string sm2_ident = "SM2 Ident Field";
+ botan_privkey_t priv;
+ botan_pubkey_t pub;
+ botan_privkey_t loaded_privkey;
+ botan_pubkey_t loaded_pubkey;
+
+ REQUIRE_FFI_OK(botan_privkey_create, (&priv, "SM2_Sig", kCurve, rng));
+ TEST_FFI_OK(botan_privkey_export_pubkey, (&pub, priv));
+ ffi_test_pubkey_export(result, pub, priv, rng);
+
+ // Check key load functions
+ 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_sm2, (&loaded_privkey, private_scalar, kCurve));
+ TEST_FFI_OK(botan_pubkey_load_sm2, (&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);
+
+ TEST_FFI_OK(botan_pubkey_algo_name, (pub, &namebuf[0], &name_len));
+ result.test_eq(namebuf, namebuf, "SM2_Sig");
+
+ std::vector<uint8_t> message(1280), signature;
+ TEST_FFI_OK(botan_rng_get, (rng, message.data(), message.size()));
+ botan_pk_op_sign_t signer;
+ if(TEST_FFI_OK(botan_pk_op_sign_create, (&signer, loaded_privkey, sm2_ident.c_str(), 0)))
+ {
+ // TODO: break input into multiple calls to update
+ TEST_FFI_OK(botan_pk_op_sign_update, (signer, message.data(), message.size()));
+
+ signature.resize(96); // TODO: no way to derive this from API
+ size_t sig_len = signature.size();
+ TEST_FFI_OK(botan_pk_op_sign_finish, (signer, rng, signature.data(), &sig_len));
+ signature.resize(sig_len);
+
+ TEST_FFI_OK(botan_pk_op_sign_destroy, (signer));
+ }
+
+ botan_pk_op_verify_t verifier;
+
+ if(signature.size() > 0 && TEST_FFI_OK(botan_pk_op_verify_create, (&verifier, pub, sm2_ident.c_str(), 0)))
+ {
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ // TODO: randomize this
+ signature[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ message[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ signature[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_FAIL("bad signature", botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ message[0] ^= 1;
+ TEST_FFI_OK(botan_pk_op_verify_update, (verifier, message.data(), message.size()));
+ TEST_FFI_OK(botan_pk_op_verify_finish, (verifier, signature.data(), signature.size()));
+
+ TEST_FFI_OK(botan_pk_op_verify_destroy, (verifier));
+ }
+
+ TEST_FFI_OK(botan_mp_destroy, (private_scalar));
+ TEST_FFI_OK(botan_mp_destroy, (public_x));
+ TEST_FFI_OK(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;
+ }
+
Test::Result ffi_test_ecdh(botan_rng_t rng)
{
Test::Result result("FFI ECDH");
diff --git a/src/tests/test_sm2.cpp b/src/tests/test_sm2.cpp
new file mode 100644
index 000000000..0361ea3a3
--- /dev/null
+++ b/src/tests/test_sm2.cpp
@@ -0,0 +1,68 @@
+/*
+* (C) 2017 Ribose Inc
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include "tests.h"
+#include "test_rng.h"
+
+#if defined(BOTAN_HAS_SM2)
+ #include <botan/sm2.h>
+ #include "test_pubkey.h"
+#endif
+
+namespace Botan_Tests {
+
+#if defined(BOTAN_HAS_SM2)
+
+namespace {
+
+class SM2_Signature_KAT_Tests : public PK_Signature_Generation_Test
+ {
+ public:
+ SM2_Signature_KAT_Tests()
+ : PK_Signature_Generation_Test(
+ "SM2",
+ "pubkey/sm2_sig.vec",
+ "P,A,B,xG,yG,Order,Cofactor,Ident,Msg,x,Nonce,Signature",
+ "") {}
+
+ virtual std::string default_padding(const VarMap& vars) const
+ {
+ return get_req_str(vars, "Ident");
+ }
+
+ Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
+ {
+ return new Fixed_Output_Position_RNG(nonce, 1);
+ }
+
+ std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
+ {
+ // group params
+ const BigInt p = get_req_bn(vars, "P");
+ const BigInt a = get_req_bn(vars, "A");
+ const BigInt b = get_req_bn(vars, "B");
+ const BigInt xG = get_req_bn(vars, "xG");
+ const BigInt yG = get_req_bn(vars, "yG");
+ const BigInt order = get_req_bn(vars, "Order");
+ const BigInt cofactor = get_req_bn(vars, "Cofactor");
+ const BigInt x = get_req_bn(vars, "x");
+
+ Botan::CurveGFp curve(p, a, b);
+ Botan::PointGFp base_point(curve, xG, yG);
+ Botan::EC_Group domain(curve, base_point, order, cofactor);
+
+ Botan::Null_RNG null_rng;
+ std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Signature_PrivateKey(null_rng, domain, x));
+ return key;
+ }
+ };
+
+}
+
+BOTAN_REGISTER_TEST("sm2_sig", SM2_Signature_KAT_Tests);
+#endif
+
+}