Botan 3.6.1
Crypto and TLS for C&
ecc_key.h
Go to the documentation of this file.
1/*
2* ECDSA
3* (C) 2007 Falko Strenzke, FlexSecure GmbH
4* Manuel Hartl, FlexSecure GmbH
5* (C) 2008-2010 Jack Lloyd
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#ifndef BOTAN_ECC_PUBLIC_KEY_BASE_H_
11#define BOTAN_ECC_PUBLIC_KEY_BASE_H_
12
13#include <botan/ec_group.h>
14#include <botan/pk_keys.h>
15#include <memory>
16
17namespace Botan {
18
19class EC_PublicKey_Data;
20class EC_PrivateKey_Data;
21
22/**
23* This class represents abstract ECC public keys. When encoding a key
24* via an encoder that can be accessed via the corresponding member
25* functions, the key will decide upon its internally stored encoding
26* information whether to encode itself with or without domain
27* parameters, or using the domain parameter oid. Furthermore, a public
28* key without domain parameters can be decoded. In that case, it
29* cannot be used for verification until its domain parameters are set
30* by calling the corresponding member function.
31*/
32class BOTAN_PUBLIC_API(2, 0) EC_PublicKey : public virtual Public_Key {
33 public:
34 EC_PublicKey(const EC_PublicKey& other) = default;
35 EC_PublicKey& operator=(const EC_PublicKey& other) = default;
36 EC_PublicKey(EC_PublicKey&& other) = delete;
38 ~EC_PublicKey() override = default;
39
40 /**
41 * Get the public point of this key.
42 * @throw Invalid_State is thrown if the
43 * domain parameters of this point are not set
44 * @result the public point of this key
45 */
46 const EC_Point& public_point() const;
47
48 AlgorithmIdentifier algorithm_identifier() const override;
49
50 std::vector<uint8_t> raw_public_key_bits() const override;
51
52 std::vector<uint8_t> public_key_bits() const override;
53
54 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
55
56 /**
57 * Get the domain parameters of this key.
58 * @throw Invalid_State is thrown if the
59 * domain parameters of this point are not set
60 * @result the domain parameters of this key
61 */
62 const EC_Group& domain() const;
63
64 /**
65 * Set the domain parameter encoding to be used when encoding this key.
66 * @param enc the encoding to use
67 *
68 * This function is deprecated; in a future major release only namedCurve
69 * encoding of domain parameters will be allowed.
70 */
71 BOTAN_DEPRECATED("Support for explicit point encoding is deprecated")
72 void set_parameter_encoding(EC_Group_Encoding enc);
73
74 /**
75 * Set the point encoding method to be used when encoding this key.
76 * @param enc the encoding to use
77 */
78 void set_point_encoding(EC_Point_Format enc);
79
80 /**
81 * Return the DER encoding of this keys domain in whatever format
82 * is preset for this particular key
83 */
84 std::vector<uint8_t> DER_domain() const;
85
86 /**
87 * Get the domain parameter encoding to be used when encoding this key.
88 * @result the encoding to use
89 */
90 EC_Group_Encoding domain_format() const { return m_domain_encoding; }
91
92 /**
93 * Get the point encoding method to be used when encoding this key.
94 * @result the encoding to use
95 */
96 EC_Point_Format point_encoding() const { return m_point_encoding; }
97
98 size_t key_length() const override;
99 size_t estimated_strength() const override;
100
101 const BigInt& get_int_field(std::string_view field) const override;
102
103 const EC_AffinePoint& _public_key() const;
104
105 protected:
106 /**
107 * Load a public key from the point.
108 *
109 * @param group EC domain parameters
110 * @param pub_point public point on the curve
111 */
112 EC_PublicKey(EC_Group group, const EC_Point& pub_point);
113
114 /**
115 * Load a public key from the point.
116 *
117 * @param group EC domain parameters
118 * @param pub_point public point on the curve
119 */
120 EC_PublicKey(EC_Group group, EC_AffinePoint pub_point);
121
122 /**
123 * Load a public key.
124 * @param alg_id the X.509 algorithm identifier
125 * @param key_bits DER encoded public key bits
126 */
127 EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
128
129 EC_PublicKey() = default;
130
131 std::shared_ptr<const EC_PublicKey_Data> m_public_key;
132 EC_Group_Encoding m_domain_encoding = EC_Group_Encoding::NamedCurve;
133 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed;
134};
135
136/**
137* This abstract class represents ECC private keys
138*/
139
142
143class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
144 public virtual Private_Key {
145 public:
146 secure_vector<uint8_t> private_key_bits() const final;
147
148 secure_vector<uint8_t> raw_private_key_bits() const final;
149
150 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
151
152 /**
153 * Get the private key value of this key object.
154 * @result the private key value of this key object
155 */
156 const BigInt& private_value() const;
157
158 EC_PrivateKey(const EC_PrivateKey& other) = default;
159 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
160 EC_PrivateKey(EC_PrivateKey&& other) = delete;
162 ~EC_PrivateKey() override = default;
163
164 const BigInt& get_int_field(std::string_view field) const final;
165
166 const EC_Scalar& _private_key() const;
167
168 protected:
169 /**
170 * If x=0, creates a new private key in the domain
171 * using the given rng. If with_modular_inverse is set,
172 * the public key will be calculated by multiplying
173 * the base point with the modular inverse of
174 * x (as in ECGDSA and ECKCDSA), otherwise by
175 * multiplying directly with x (as in ECDSA).
176 */
177 EC_PrivateKey(RandomNumberGenerator& rng, EC_Group domain, const BigInt& x, bool with_modular_inverse = false);
178
179 /**
180 * Creates a new private key
181 *
182 * If @p with_modular_inverse is set, the public key will be calculated by
183 * multiplying the base point with the modular inverse of x (as in ECGDSA
184 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
185 */
186 EC_PrivateKey(RandomNumberGenerator& rng, EC_Group group, bool with_modular_inverse = false);
187
188 /**
189 * Load a EC private key from the secret scalar
190 *
191 * If @p with_modular_inverse is set, the public key will be calculated by
192 * multiplying the base point with the modular inverse of x (as in ECGDSA
193 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
194 */
195 EC_PrivateKey(EC_Group group, EC_Scalar scalar, bool with_modular_inverse = false);
196
197 /*
198 * Creates a new private key object from the
199 * ECPrivateKey structure given in key_bits.
200 * If with_modular_inverse is set,
201 * the public key will be calculated by multiplying
202 * the base point with the modular inverse of
203 * x (as in ECGDSA and ECKCDSA), otherwise by
204 * multiplying directly with x (as in ECDSA).
205 */
207 std::span<const uint8_t> key_bits,
208 bool with_modular_inverse = false);
209
210 EC_PrivateKey() = default;
211
212 std::shared_ptr<const EC_PrivateKey_Data> m_private_key;
213};
214
216
217} // namespace Botan
218
219#endif
EC_PrivateKey & operator=(EC_PrivateKey &&other)=delete
EC_PrivateKey(EC_PrivateKey &&other)=delete
EC_PrivateKey & operator=(const EC_PrivateKey &other)=default
~EC_PrivateKey() override=default
EC_PrivateKey(const EC_PrivateKey &other)=default
std::shared_ptr< const EC_PrivateKey_Data > m_private_key
Definition ecc_key.h:212
EC_PublicKey(const EC_PublicKey &other)=default
EC_PublicKey(EC_PublicKey &&other)=delete
std::shared_ptr< const EC_PublicKey_Data > m_public_key
Definition ecc_key.h:131
EC_PublicKey()=default
EC_PublicKey & operator=(const EC_PublicKey &other)=default
EC_Point_Format point_encoding() const
Definition ecc_key.h:96
~EC_PublicKey() override=default
EC_PublicKey & operator=(EC_PublicKey &&other)=delete
#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
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
EC_Point_Format
Definition ec_point.h:19
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
EC_Group_Encoding
Definition ec_group.h:26