Botan 3.13.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
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
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 SM2_PublicKey() = default;
68};
69
70/**
71* This class represents SM2 private keys
72*/
73
76
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 */
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 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#endif
157
158// For compat with versions 2.2 - 2.7
161
164
165} // namespace Botan
166
167#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:128
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:125
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:127
#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:45
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition sm2.cpp:331
SM2_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.cpp:70
const BigInt & get_da_inv() const
Definition sm2.h:123
const EC_Scalar & _get_da_inv() const
Definition sm2.h:125
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition sm2.cpp:49
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:222
bool supports_operation(PublicKeyOperation op) const override
Definition sm2.h:53
SM2_PublicKey(const EC_Group &group, const EC_AffinePoint &public_key)
Definition sm2.h:27
SM2_PublicKey SM2_Encryption_PublicKey
Definition sm2.h:160
SM2_PrivateKey SM2_Signature_PrivateKey
Definition sm2.h:162
SM2_PublicKey SM2_Signature_PublicKey
Definition sm2.h:159
PublicKeyOperation
Definition pk_keys.h:46
SM2_PrivateKey SM2_Encryption_PrivateKey
Definition sm2.h:163
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:124