Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/sm2.h
Line
Count
Source
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/bigint.h>
12
#include <botan/ec_scalar.h>
13
#include <botan/ecc_key.h>
14
15
namespace Botan {
16
17
/**
18
* This class represents SM2 public keys
19
*/
20
class BOTAN_PUBLIC_API(2, 2) SM2_PublicKey : public virtual EC_PublicKey {
21
   public:
22
      /**
23
      * Create a public key from a given public point.
24
      * @param group the domain parameters associated with this key
25
      * @param public_key the public point defining this key
26
      */
27
46.2k
      SM2_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key) : EC_PublicKey(group, public_key) {}
28
29
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
30
      /**
31
      * Create a public key from a given public point.
32
      * @param group the domain parameters associated with this key
33
      * @param public_point the public point defining this key
34
      */
35
0
      SM2_PublicKey(const EC_Group& group, const EC_Point& public_point) : EC_PublicKey(group, public_point) {}
36
#endif
37
38
      /**
39
      * Load a public key.
40
      * @param alg_id the X.509 algorithm identifier
41
      * @param key_bits DER encoded public key bits
42
      */
43
      SM2_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
44
45
      /**
46
      * Get this keys algorithm name.
47
      * @result this keys algorithm name
48
      */
49
      std::string algo_name() const override;
50
51
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
52
53
0
      bool supports_operation(PublicKeyOperation op) const override {
54
0
         return (op == PublicKeyOperation::Signature || op == PublicKeyOperation::Encryption);
55
0
      }
56
57
      std::optional<size_t> _signature_element_size_for_DER_encoding() const override;
58
59
      std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
60
                                                                   std::string_view provider) const override;
61
62
      std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
63
                                                               std::string_view params,
64
                                                               std::string_view provider) const override;
65
66
   protected:
67
0
      SM2_PublicKey() = default;
68
};
69
70
/**
71
* This class represents SM2 private keys
72
*/
73
74
BOTAN_DIAGNOSTIC_PUSH
75
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
76
77
class BOTAN_PUBLIC_API(2, 2) SM2_PrivateKey final : public SM2_PublicKey,
78
                                                    public EC_PrivateKey {
79
   public:
80
      /**
81
      * Load a private key
82
      * @param alg_id the X.509 algorithm identifier
83
      * @param key_bits ECPrivateKey bits
84
      */
85
      SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
86
87
      /**
88
      * Create a private key from a given secret @p x
89
      * @param group curve parameters to bu used for this key
90
      * @param x      the private key
91
      */
92
      SM2_PrivateKey(const EC_Group& group, const EC_Scalar& x);
93
94
      /**
95
      * Create a new private key
96
      * @param rng a random number generator
97
      * @param group parameters to used for this key
98
      */
99
      SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group);
100
101
      /**
102
      * Create a private key.
103
      * @param rng a random number generator
104
      * @param group parameters to used for this key
105
      * @param x the private key (if zero, generate a new random key)
106
      */
107
      BOTAN_DEPRECATED("Use one of the other constructors")
108
      SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, const BigInt& x);
109
110
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
111
112
      std::unique_ptr<Public_Key> public_key() const override;
113
114
      std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
115
                                                             std::string_view params,
116
                                                             std::string_view provider) const override;
117
118
      std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
119
                                                               std::string_view params,
120
                                                               std::string_view provider) const override;
121
122
      // TODO(Botan4) remove this and the member variable
123
0
      BOTAN_DEPRECATED("Deprecated no replacement") const BigInt& get_da_inv() const { return m_da_inv_legacy; }
124
125
0
      const EC_Scalar& _get_da_inv() const { return m_da_inv; }
126
127
   private:
128
      EC_Scalar m_da_inv;
129
      BigInt m_da_inv_legacy;
130
};
131
132
BOTAN_DIAGNOSTIC_POP
133
134
class HashFunction;
135
136
/*
137
* This is deprecated because it's not clear what it is useful for
138
*
139
* Open an issue on GH if you are using this
140
*/
141
BOTAN_DEPRECATED("Deprecated unclear usage")
142
std::vector<uint8_t> BOTAN_PUBLIC_API(3, 7)
143
   sm2_compute_za(HashFunction& hash, std::string_view user_id, const EC_Group& group, const EC_AffinePoint& pubkey);
144
145
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
146
/*
147
* This is deprecated because it's not clear what it is useful for
148
*
149
* Open an issue on GH if you are using this
150
*/
151
BOTAN_DEPRECATED("Deprecated unclear usage")
152
inline std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
153
                                           std::string_view user_id,
154
                                           const EC_Group& group,
155
                                           const EC_Point& pubkey);
156
#endif
157
158
// For compat with versions 2.2 - 2.7
159
typedef SM2_PublicKey SM2_Signature_PublicKey;
160
typedef SM2_PublicKey SM2_Encryption_PublicKey;
161
162
typedef SM2_PrivateKey SM2_Signature_PrivateKey;
163
typedef SM2_PrivateKey SM2_Encryption_PrivateKey;
164
165
}  // namespace Botan
166
167
#endif