1 /*
2 * (C) 2017 Ribose Inc
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include "tests.h"
8 #include "test_rng.h"
9
10 #if defined(BOTAN_HAS_SM2)
11 #include <botan/sm2.h>
12 #include "test_pubkey.h"
13 #endif
14
15 namespace Botan_Tests {
16
17 #if defined(BOTAN_HAS_SM2)
18
19 namespace {
20
load_sm2_private_key(const VarMap & vars)21 std::unique_ptr<Botan::Private_Key> load_sm2_private_key(const VarMap& vars)
22 {
23 // group params
24 const BigInt p = vars.get_req_bn("P");
25 const BigInt a = vars.get_req_bn("A");
26 const BigInt b = vars.get_req_bn("B");
27 const BigInt xG = vars.get_req_bn("xG");
28 const BigInt yG = vars.get_req_bn("yG");
29 const BigInt order = vars.get_req_bn("Order");
30 const BigInt cofactor = vars.get_req_bn("Cofactor");
31 const BigInt x = vars.get_req_bn("x");
32
33 Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor);
34
35 Botan::Null_RNG null_rng;
36 return std::unique_ptr<Botan::Private_Key>(new Botan::SM2_PrivateKey(null_rng, domain, x));
37 }
38
39 class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test
40 {
41 public:
SM2_Signature_KAT_Tests()42 SM2_Signature_KAT_Tests()
43 : PK_Signature_Generation_Test(
44 "SM2",
45 "pubkey/sm2_sig.vec",
46 "P,A,B,xG,yG,Order,Cofactor,Ident,Msg,x,Nonce,Signature",
47 "Hash") {}
48
clear_between_callbacks() const49 bool clear_between_callbacks() const override { return false; }
50
default_padding(const VarMap & vars) const51 std::string default_padding(const VarMap& vars) const override
52 {
53 return vars.get_req_str("Ident") + "," + vars.get_opt_str("Hash", "SM3");
54 }
55
test_rng(const std::vector<uint8_t> & nonce) const56 Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
57 {
58 return new Fixed_Output_Position_RNG(nonce, 1);
59 }
60
load_private_key(const VarMap & vars)61 std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
62 {
63 return load_sm2_private_key(vars);
64 }
65 };
66
67 BOTAN_REGISTER_TEST("pubkey", "sm2_sig", SM2_Signature_KAT_Tests);
68
69 class SM2_Encryption_KAT_Tests final : public PK_Encryption_Decryption_Test
70 {
71 public:
SM2_Encryption_KAT_Tests()72 SM2_Encryption_KAT_Tests()
73 : PK_Encryption_Decryption_Test(
74 "SM2",
75 "pubkey/sm2_enc.vec",
76 "P,A,B,xG,yG,Order,Cofactor,Msg,x,Nonce,Ciphertext",
77 "Hash") {}
78
default_padding(const VarMap & vars) const79 std::string default_padding(const VarMap& vars) const override
80 {
81 return vars.get_opt_str("Hash", "SM3");
82 }
83
clear_between_callbacks() const84 bool clear_between_callbacks() const override { return false; }
85
test_rng(const std::vector<uint8_t> & nonce) const86 Botan::RandomNumberGenerator* test_rng(const std::vector<uint8_t>& nonce) const override
87 {
88 return new Fixed_Output_Position_RNG(nonce, 1);
89 }
90
load_private_key(const VarMap & vars)91 std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
92 {
93 return load_sm2_private_key(vars);
94 }
95 };
96
97 }
98
99 BOTAN_REGISTER_TEST("pubkey", "sm2_enc", SM2_Encryption_KAT_Tests);
100
101 class SM2_Keygen_Tests final : public PK_Key_Generation_Test
102 {
103 public:
keygen_params() const104 std::vector<std::string> keygen_params() const override
105 {
106 return { "secp256r1", "sm2p256v1" };
107 }
108
algo_name() const109 std::string algo_name() const override
110 {
111 return "SM2";
112 }
113 };
114
115 BOTAN_REGISTER_TEST("pubkey", "sm2_keygen", SM2_Keygen_Tests);
116
117
118 #endif
119
120 }
121