Botan 3.11.0
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/bigint.h>
12#include <botan/ec_scalar.h>
13#include <botan/ecc_key.h>
14
15namespace Botan {
16
17/**
18* This class represents SM2 public keys
19*/
20class 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 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 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 EC_PublicKey(alg_id, key_bits) {}
45
46 /**
47 * Get this keys algorithm name.
48 * @result this keys algorithm name
49 */
50 std::string algo_name() const override;
51
52 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
53
57
58 std::optional<size_t> _signature_element_size_for_DER_encoding() const override;
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 */
93 SM2_PrivateKey(const EC_Group& group, const EC_Scalar& x);
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")
109 SM2_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, const BigInt& x);
110
111 bool check_key(RandomNumberGenerator& rng, bool strong) 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 // TODO(Botan4) remove this and the member variable
124 BOTAN_DEPRECATED("Deprecated no replacement") const BigInt& get_da_inv() const { return m_da_inv_legacy; }
125
126 const EC_Scalar& _get_da_inv() const { return m_da_inv; }
127
128 private:
129 EC_Scalar m_da_inv;
130 BigInt m_da_inv_legacy;
131};
132
134
135class HashFunction;
136
137/*
138* This is deprecated because it's not clear what it is useful for
139*
140* Open an issue on GH if you are using this
141*/
142BOTAN_DEPRECATED("Deprecated unclear usage")
143std::vector<uint8_t> BOTAN_PUBLIC_API(3, 7)
144 sm2_compute_za(HashFunction& hash, std::string_view user_id, const EC_Group& group, const EC_AffinePoint& pubkey);
145
146#if defined(BOTAN_HAS_LEGACY_EC_POINT)
147/*
148* This is deprecated because it's not clear what it is useful for
149*
150* Open an issue on GH if you are using this
151*/
152BOTAN_DEPRECATED("Deprecated unclear usage")
153inline std::vector<uint8_t> sm2_compute_za(HashFunction& hash,
154 std::string_view user_id,
155 const EC_Group& group,
156 const EC_Point& pubkey);
157#endif
158
159// For compat with versions 2.2 - 2.7
162
165
166} // namespace Botan
167
168#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
EC_PrivateKey(const EC_PrivateKey &other)=default
EC_PublicKey(const EC_PublicKey &other)=default
std::unique_ptr< Public_Key > public_key() const override
Definition sm2.cpp:27
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2.cpp:278
SM2_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.cpp:52
const BigInt & get_da_inv() const
Definition sm2.h:124
const EC_Scalar & _get_da_inv() const
Definition sm2.h:126
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition sm2.cpp:31
std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2_enc.cpp:209
bool supports_operation(PublicKeyOperation op) const override
Definition sm2.h:54
SM2_PublicKey(const EC_Group &group, const EC_AffinePoint &public_key)
Definition sm2.h:27
SM2_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.h:43
SM2_PublicKey SM2_Encryption_PublicKey
Definition sm2.h:161
SM2_PrivateKey SM2_Signature_PrivateKey
Definition sm2.h:163
SM2_PublicKey SM2_Signature_PublicKey
Definition sm2.h:160
PublicKeyOperation
Definition pk_keys.h:46
SM2_PrivateKey SM2_Encryption_PrivateKey
Definition sm2.h:164
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:82