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

randombit / botan / 13021208024

28 Jan 2025 11:07PM UTC coverage: 91.241% (-0.02%) from 91.258%
13021208024

push

github

web-flow
Merge pull request #4604 from randombit/jack/support-minimal-curves

Avoid requiring legacy_ec_group in order to run the tests

94137 of 103174 relevant lines covered (91.24%)

11291073.45 hits per line

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

97.62
/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 "test_rng.h"
8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_SM2)
11
   #include "test_pubkey.h"
12
   #include <botan/sm2.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_SM2)
18

19
namespace {
20

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

32
   Botan::EC_Group domain(oid, p, a, b, xG, yG, order);
12✔
33

34
   Botan::Null_RNG null_rng;
12✔
35
   return std::make_unique<Botan::SM2_PrivateKey>(null_rng, domain, x);
24✔
36
}
96✔
37

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

44
      bool skip_this_test(const std::string&, const VarMap&) override {
7✔
45
         return !Botan::EC_Group::supports_application_specific_group();
7✔
46
      }
47

48
      bool clear_between_callbacks() const override { return false; }
7✔
49

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

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

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

63
BOTAN_REGISTER_TEST("pubkey", "sm2_sig", SM2_Signature_KAT_Tests);
64

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

71
      bool skip_this_test(const std::string&, const VarMap&) override {
5✔
72
         return !Botan::EC_Group::supports_application_specific_group();
5✔
73
      }
74

75
      std::string default_padding(const VarMap& vars) const override { return vars.get_opt_str("Hash", "SM3"); }
10✔
76

77
      bool clear_between_callbacks() const override { return false; }
5✔
78

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

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

88
}  // namespace
89

90
BOTAN_REGISTER_TEST("pubkey", "sm2_enc", SM2_Encryption_KAT_Tests);
91

92
class SM2_Keygen_Tests final : public PK_Key_Generation_Test {
×
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
#endif
110

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