Botan 3.4.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/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 dom_par the domain parameters associated with this key
23 * @param public_point the public point defining this key
24 */
25 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 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 size_t message_parts() const override { return 2; }
42
43 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
44
45 bool supports_operation(PublicKeyOperation op) const override {
46 return (op == PublicKeyOperation::Signature || op == PublicKeyOperation::Encryption);
47 }
48
49 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 SM2_PublicKey() = default;
60};
61
62/**
63* This class represents SM2 private keys
64*/
65
68
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 const BigInt& get_da_inv() const { return m_da_inv; }
100
101 private:
102 BigInt m_da_inv;
103};
104
106
107class HashFunction;
108
109std::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
115
118
119} // namespace Botan
120
121#endif
const BigInt & get_da_inv() const
Definition sm2.h:99
SM2_PublicKey(const EC_Group &dom_par, const EC_Point &public_point)
Definition sm2.h:25
size_t message_part_size() const override
Definition sm2.h:49
bool supports_operation(PublicKeyOperation op) const override
Definition sm2.h:45
size_t message_parts() const override
Definition sm2.h:41
SM2_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition sm2.h:32
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
PublicKeyOperation
Definition pk_keys.h:45
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, std::string_view user_id, const EC_Group &domain, const EC_Point &pubkey)
Definition sm2.cpp:58