Botan 3.7.1
Crypto and TLS for C&
sm2.h
Go to the documentation of this file.
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
13namespace Botan {
14
15/**
16* This class represents SM2 public keys
17*/
18class 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 group the domain parameters associated with this key
23 * @param public_key the public point defining this key
24 */
25 SM2_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key) : EC_PublicKey(group, public_key) {}
26
27#if defined(BOTAN_HAS_LEGACY_EC_POINT)
28 /**
29 * Create a public key from a given public point.
30 * @param group the domain parameters associated with this key
31 * @param public_point the public point defining this key
32 */
33 SM2_PublicKey(const EC_Group& group, const EC_Point& public_point) : EC_PublicKey(group, public_point) {}
34#endif
35
36 /**
37 * Load a public key.
38 * @param alg_id the X.509 algorithm identifier
39 * @param key_bits DER encoded public key bits
40 */
41 SM2_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
42 EC_PublicKey(alg_id, key_bits) {}
43
44 /**
45 * Get this keys algorithm name.
46 * @result this keys algorithm name
47 */
48 std::string algo_name() const override;
49
50 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
51
52 bool supports_operation(PublicKeyOperation op) const override {
53 return (op == PublicKeyOperation::Signature || op == PublicKeyOperation::Encryption);
54 }
55
56 std::optional<size_t> _signature_element_size_for_DER_encoding() const override {
57 return domain().get_order_bytes();
58 }
59
60 std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
61 std::string_view provider) const override;
62
63 std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
64 std::string_view params,
65 std::string_view provider) const override;
66
67 protected:
68 SM2_PublicKey() = default;
69};
70
71/**
72* This class represents SM2 private keys
73*/
74
77
79 public EC_PrivateKey {
80 public:
81 /**
82 * Load a private key
83 * @param alg_id the X.509 algorithm identifier
84 * @param key_bits ECPrivateKey bits
85 */
86 SM2_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
87
88 /**
89 * Create a private key from a given secret @p x
90 * @param group curve parameters to bu used for this key
91 * @param x the private key
92 */
94
95 /**
96 * Create a new private key
97 * @param rng a random number generator
98 * @param group parameters to used for this key
99 */
101
102 /**
103 * Create a private key.
104 * @param rng a random number generator
105 * @param group parameters to used for this key
106 * @param x the private key (if zero, generate a new random key)
107 */
108 BOTAN_DEPRECATED("Use one of the other constructors")
110
111 bool check_key(RandomNumberGenerator& rng, bool) const override;
112
113 std::unique_ptr<Public_Key> public_key() const override;
114
115 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
116 std::string_view params,
117 std::string_view provider) const override;
118
119 std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
120 std::string_view params,
121 std::string_view provider) const override;
122
123 BOTAN_DEPRECATED("Deprecated no replacement") const BigInt& get_da_inv() const { return m_da_inv_legacy; }
124
125 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
133
134class 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*/
141BOTAN_DEPRECATED("Deprecated unclear usage")
142std::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*/
151BOTAN_DEPRECATED("Deprecated unclear usage")
152inline 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 auto apoint = EC_AffinePoint(group, pubkey);
157 return sm2_compute_za(hash, user_id, group, apoint);
158}
159#endif
160
161// For compat with versions 2.2 - 2.7
164
167
168} // namespace Botan
169
170#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:108
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:105
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:107
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
#define BOTAN_DEPRECATED(msg)
Definition api.h:59
const EC_Scalar & _get_da_inv() const
Definition sm2.h:125
bool supports_operation(PublicKeyOperation op) const override
Definition sm2.h:52
SM2_PublicKey(const EC_Group &group, const EC_AffinePoint &public_key)
Definition sm2.h:25
std::optional< size_t > _signature_element_size_for_DER_encoding() const override
Definition sm2.h:56
SM2_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.h:41
int(* final)(unsigned char *, CTX *)
PublicKeyOperation
Definition pk_keys.h:45
SM2_PublicKey SM2_Encryption_PublicKey
Definition sm2.h:163
SM2_PrivateKey SM2_Signature_PrivateKey
Definition sm2.h:165
SM2_PublicKey SM2_Signature_PublicKey
Definition sm2.h:162
SM2_PrivateKey SM2_Encryption_PrivateKey
Definition sm2.h:166
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &group, const EC_AffinePoint &pubkey)
Definition sm2.cpp:68