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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 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
namespace {
20

21
#if defined(BOTAN_HAS_SM2)
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
BOTAN_REGISTER_TEST("pubkey", "sm2_enc", SM2_Encryption_KAT_Tests);
91

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

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

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

107
BOTAN_REGISTER_TEST("pubkey", "sm2_keygen", SM2_Keygen_Tests);
108

109
class SM2_Invalid_Ciphertexts : public Text_Based_Test {
×
110
   public:
111
      SM2_Invalid_Ciphertexts() : Text_Based_Test("pubkey/sm2_invalid.vec", "Key,Ctext") {}
2✔
112

113
      bool clear_between_callbacks() const override { return false; }
19✔
114

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

118
         const auto key = vars.get_req_bin("Key");
19✔
119
         const auto ctext = vars.get_req_bin("Ctext");
19✔
120

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

124
         Botan::PK_Decryptor_EME dec(pkey, rng(), "SM3");
19✔
125

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

129
         return result;
19✔
130
      }
19✔
131
};
132

133
BOTAN_REGISTER_TEST("pubkey", "sm2_invalid_ctext", SM2_Invalid_Ciphertexts);
134

135
#endif
136

137
}  // namespace
138

139
}  // 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