Botan 3.0.0-alpha0
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 {
20 public:
21
22 /**
23 * Create a public key from a given public point.
24 * @param dom_par the domain parameters associated with this key
25 * @param public_point the public point defining this key
26 */
27 SM2_PublicKey(const EC_Group& dom_par,
28 const PointGFp& public_point) :
29 EC_PublicKey(dom_par, public_point) {}
30
31 /**
32 * Load a public key.
33 * @param alg_id the X.509 algorithm identifier
34 * @param key_bits DER encoded public key bits
35 */
37 const std::vector<uint8_t>& key_bits) :
38 EC_PublicKey(alg_id, key_bits) {}
39
40 /**
41 * Get this keys algorithm name.
42 * @result this keys algorithm name
43 */
44 std::string algo_name() const override;
45
46 size_t message_parts() const override { return 2; }
47
48 size_t message_part_size() const override
49 { return domain().get_order().bytes(); }
50
51 std::unique_ptr<PK_Ops::Verification>
52 create_verification_op(const std::string& params,
53 const std::string& provider) const override;
54
55 std::unique_ptr<PK_Ops::Encryption>
56 create_encryption_op(RandomNumberGenerator& rng,
57 const std::string& params,
58 const std::string& provider) const override;
59
60 protected:
61 SM2_PublicKey() = default;
62 };
63
64/**
65* This class represents SM2 private keys
66*/
68 public SM2_PublicKey, public EC_PrivateKey
69 {
70 public:
71
72 /**
73 * Load a private key
74 * @param alg_id the X.509 algorithm identifier
75 * @param key_bits ECPrivateKey bits
76 */
78 const secure_vector<uint8_t>& key_bits);
79
80 /**
81 * Create a private key.
82 * @param rng a random number generator
83 * @param domain parameters to used for this key
84 * @param x the private key (if zero, generate a new random key)
85 */
87 const EC_Group& domain,
88 const BigInt& x = BigInt::zero());
89
90 bool check_key(RandomNumberGenerator& rng, bool) const override;
91
92 std::unique_ptr<Public_Key> public_key() const override;
93
94 std::unique_ptr<PK_Ops::Signature>
95 create_signature_op(RandomNumberGenerator& rng,
96 const std::string& params,
97 const std::string& provider) const override;
98
99 std::unique_ptr<PK_Ops::Decryption>
100 create_decryption_op(RandomNumberGenerator& rng,
101 const std::string& params,
102 const std::string& provider) const override;
103
104 const BigInt& get_da_inv() const { return m_da_inv; }
105 private:
106 BigInt m_da_inv;
107 };
108
109class HashFunction;
110
111std::vector<uint8_t>
112BOTAN_PUBLIC_API(2,5) sm2_compute_za(HashFunction& hash,
113 const std::string& user_id,
114 const EC_Group& domain,
115 const PointGFp& pubkey);
116
117// For compat with versions 2.2 - 2.7
120
123
124}
125
126#endif
static BigInt zero()
Definition: bigint.h:45
const BigInt & get_da_inv() const
Definition: sm2.h:104
size_t message_part_size() const override
Definition: sm2.h:48
SM2_PublicKey(const AlgorithmIdentifier &alg_id, const std::vector< uint8_t > &key_bits)
Definition: sm2.h:36
size_t message_parts() const override
Definition: sm2.h:46
SM2_PublicKey(const EC_Group &dom_par, const PointGFp &public_point)
Definition: sm2.h:27
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
std::vector< uint8_t > sm2_compute_za(HashFunction &hash, const std::string &user_id, const EC_Group &domain, const PointGFp &pubkey)
Definition: sm2.cpp:57
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
Definition: bigint.h:1077
MechanismType hash