Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/build/include/public/botan/sm2.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* SM2
3
* (C) 2017 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_SM2_KEY_H_
9
#define BOTAN_SM2_KEY_H_
10
11
#include <botan/ecc_key.h>
12
13
namespace Botan {
14
15
/**
16
* This class represents SM2 public keys
17
*/
18
class BOTAN_PUBLIC_API(2, 2) SM2_PublicKey : public virtual EC_PublicKey {
19
   public:
20
      /**
21
      * Create a public key from a given public point.
22
      * @param dom_par the domain parameters associated with this key
23
      * @param public_point the public point defining this key
24
      */
25
15.1k
      SM2_PublicKey(const EC_Group& dom_par, const EC_Point& public_point) : EC_PublicKey(dom_par, public_point) {}
26
27
      /**
28
      * Load a public key.
29
      * @param alg_id the X.509 algorithm identifier
30
      * @param key_bits DER encoded public key bits
31
      */
32
      SM2_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
33
0
            EC_PublicKey(alg_id, key_bits) {}
34
35
      /**
36
      * Get this keys algorithm name.
37
      * @result this keys algorithm name
38
      */
39
      std::string algo_name() const override;
40
41
7.45k
      size_t message_parts() const override { return 2; }
42
43
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
44
45
0
      bool supports_operation(PublicKeyOperation op) const override {
46
0
         return (op == PublicKeyOperation::Signature || op == PublicKeyOperation::Encryption);
47
0
      }
48
49
7.45k
      size_t message_part_size() const override { return domain().get_order().bytes(); }
50
51
      std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
52
                                                                   std::string_view provider) const override;
53
54
      std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
55
                                                               std::string_view params,
56
                                                               std::string_view provider) const override;
57
58
   protected:
59
0
      SM2_PublicKey() = default;
60
};
61
62
/**
63
* This class represents SM2 private keys
64
*/
65
66
BOTAN_DIAGNOSTIC_PUSH
67
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
68
69
class BOTAN_PUBLIC_API(2, 2) SM2_PrivateKey final : public SM2_PublicKey,
70
                                                    public EC_PrivateKey {
71
   public:
72
      /**
73
      * Load a private key
74
      * @param alg_id the X.509 algorithm identifier
75
      * @param key_bits ECPrivateKey bits
76
      */
77
      SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
78
79
      /**
80
      * Create a private key.
81
      * @param rng a random number generator
82
      * @param domain parameters to used for this key
83
      * @param x the private key (if zero, generate a new random key)
84
      */
85
      SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& domain, const BigInt& x = BigInt::zero());
86
87
      bool check_key(RandomNumberGenerator& rng, bool) const override;
88
89
      std::unique_ptr<Public_Key> public_key() const override;
90
91
      std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
92
                                                             std::string_view params,
93
                                                             std::string_view provider) const override;
94
95
      std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
96
                                                               std::string_view params,
97
                                                               std::string_view provider) const override;
98
99
0
      const BigInt& get_da_inv() const { return m_da_inv; }
100
101
   private:
102
      BigInt m_da_inv;
103
};
104
105
BOTAN_DIAGNOSTIC_POP
106
107
class HashFunction;
108
109
std::vector<uint8_t> BOTAN_PUBLIC_API(2, 5)
110
   sm2_compute_za(HashFunction& hash, std::string_view user_id, const EC_Group& domain, const EC_Point& pubkey);
111
112
// For compat with versions 2.2 - 2.7
113
typedef SM2_PublicKey SM2_Signature_PublicKey;
114
typedef SM2_PublicKey SM2_Encryption_PublicKey;
115
116
typedef SM2_PrivateKey SM2_Signature_PrivateKey;
117
typedef SM2_PrivateKey SM2_Encryption_PrivateKey;
118
119
}  // namespace Botan
120
121
#endif