• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 23116397659

15 Mar 2026 06:19PM UTC coverage: 89.753% (+0.01%) from 89.74%
23116397659

push

github

web-flow
Merge pull request #5450 from randombit/jack/sm2-mac-length-check

In SM2 verify the C3 field is of the required length

104421 of 116342 relevant lines covered (89.75%)

11437774.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.21
/src/tests/test_sm2.cpp
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

9
#if defined(BOTAN_HAS_SM2)
10
   #include "test_pubkey.h"
11
   #include "test_rng.h"
12
   #include <botan/ec_group.h>
13
   #include <botan/pubkey.h>
14
   #include <botan/sm2.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_SM2)
20

21
namespace {
22

23
std::unique_ptr<Botan::Private_Key> load_sm2_private_key(const VarMap& vars) {
12✔
24
   // group params
25
   const BigInt p = vars.get_req_bn("P");
12✔
26
   const BigInt a = vars.get_req_bn("A");
12✔
27
   const BigInt b = vars.get_req_bn("B");
12✔
28
   const BigInt xG = vars.get_req_bn("xG");
12✔
29
   const BigInt yG = vars.get_req_bn("yG");
12✔
30
   const BigInt order = vars.get_req_bn("Order");
12✔
31
   const BigInt x = vars.get_req_bn("x");
12✔
32
   const Botan::OID oid = Botan::OID(vars.get_req_str("Oid"));
12✔
33

34
   const Botan::EC_Group domain(oid, p, a, b, xG, yG, order);
12✔
35

36
   Botan::Null_RNG null_rng;
12✔
37
   return std::make_unique<Botan::SM2_PrivateKey>(null_rng, domain, x);
36✔
38
}
12✔
39

40
class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test {
41
   public:
42
      SM2_Signature_KAT_Tests() :
1✔
43
            PK_Signature_Generation_Test(
44
               "SM2", "pubkey/sm2_sig.vec", "P,A,B,xG,yG,Order,Oid,Ident,Msg,x,Nonce,Signature", "Hash") {}
2✔
45

46
      bool skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) override {
7✔
47
         return !Botan::EC_Group::supports_application_specific_group();
7✔
48
      }
49

50
      bool clear_between_callbacks() const override { return false; }
7✔
51

52
      std::string default_padding(const VarMap& vars) const override {
7✔
53
         return vars.get_req_str("Ident") + "," + vars.get_opt_str("Hash", "SM3");
21✔
54
      }
55

56
      std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const override {
7✔
57
         return std::make_unique<Fixed_Output_Position_RNG>(nonce, 1, this->rng());
7✔
58
      }
59

60
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
7✔
61
         return load_sm2_private_key(vars);
7✔
62
      }
63
};
64

65
BOTAN_REGISTER_TEST("pubkey", "sm2_sig", SM2_Signature_KAT_Tests);
66

67
class SM2_Encryption_KAT_Tests final : public PK_Encryption_Decryption_Test {
68
   public:
69
      SM2_Encryption_KAT_Tests() :
1✔
70
            PK_Encryption_Decryption_Test(
71
               "SM2", "pubkey/sm2_enc.vec", "P,A,B,xG,yG,Order,Oid,Msg,x,Nonce,Ciphertext", "Hash") {}
2✔
72

73
      bool skip_this_test(const std::string& /*header*/, const VarMap& /*vars*/) override {
5✔
74
         return !Botan::EC_Group::supports_application_specific_group();
5✔
75
      }
76

77
      std::string default_padding(const VarMap& vars) const override { return vars.get_opt_str("Hash", "SM3"); }
5✔
78

79
      bool clear_between_callbacks() const override { return false; }
5✔
80

81
      std::unique_ptr<Botan::RandomNumberGenerator> test_rng(const std::vector<uint8_t>& nonce) const override {
5✔
82
         return std::make_unique<Fixed_Output_Position_RNG>(nonce, 1, this->rng());
5✔
83
      }
84

85
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
5✔
86
         return load_sm2_private_key(vars);
5✔
87
      }
88
};
89

90
}  // namespace
91

92
BOTAN_REGISTER_TEST("pubkey", "sm2_enc", SM2_Encryption_KAT_Tests);
93

94
class SM2_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
95
   public:
96
      std::vector<std::string> keygen_params() const override { return {"secp256r1", "sm2p256v1"}; }
1✔
97

98
      std::string algo_name() const override { return "SM2"; }
2✔
99

100
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
2✔
101
                                                             std::string_view /* provider */,
102
                                                             std::span<const uint8_t> raw_pk) const override {
103
         const auto group = Botan::EC_Group(keygen_params);
2✔
104
         const auto public_key = Botan::EC_AffinePoint(group, raw_pk);
2✔
105
         return std::make_unique<Botan::SM2_PublicKey>(group, public_key);
6✔
106
      }
2✔
107
};
108

109
BOTAN_REGISTER_TEST("pubkey", "sm2_keygen", SM2_Keygen_Tests);
110

111
namespace {
112

113
class SM2_Invalid_Ciphertexts : public Text_Based_Test {
×
114
   public:
115
      SM2_Invalid_Ciphertexts() : Text_Based_Test("pubkey/sm2_invalid.vec", "Key,Ctext") {}
2✔
116

117
      bool clear_between_callbacks() const override { return false; }
19✔
118

119
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
19✔
120
         Test::Result result("SM2 invalid ciphertext");
19✔
121

122
         const auto key = vars.get_req_bin("Key");
19✔
123
         const auto ctext = vars.get_req_bin("Ctext");
19✔
124

125
         const auto group = Botan::EC_Group::from_name("sm2p256v1");
19✔
126
         const auto pkey = Botan::SM2_PrivateKey(group, Botan::EC_Scalar::deserialize(group, key).value());
38✔
127

128
         Botan::PK_Decryptor_EME dec(pkey, rng(), "SM3");
19✔
129

130
         result.test_throws<Botan::Exception>("Decryption should fail for invalid ciphertext",
19✔
131
                                              [&] { dec.decrypt(ctext); });
38✔
132

133
         return result;
19✔
134
      }
19✔
135
};
136

137
BOTAN_REGISTER_TEST("pubkey", "sm2_invalid_ctext", SM2_Invalid_Ciphertexts);
138

139
}  // namespace
140

141
#endif
142

143
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc