Botan 3.14.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_point_format.h>
14#include <botan/pk_keys.h>
15#include <memory>
16
17namespace Botan {
18
19class EC_AffinePoint;
20class EC_Point;
21class EC_Group;
22class EC_Scalar;
25
26/**
27* This class represents abstract ECC public keys. When encoding a key
28* via an encoder that can be accessed via the corresponding member
29* functions, the key will decide upon its internally stored encoding
30* information whether to encode itself with or without domain
31* parameters, or using the domain parameter oid. Furthermore, a public
32* key without domain parameters can be decoded. In that case, it
33* cannot be used for verification until its domain parameters are set
34* by calling the corresponding member function.
35*/
36class BOTAN_PUBLIC_API(2, 0) EC_PublicKey : public virtual Public_Key {
37 public:
38 EC_PublicKey(const EC_PublicKey& other) = default;
39 EC_PublicKey& operator=(const EC_PublicKey& other) = default;
40 EC_PublicKey(EC_PublicKey&& other) = delete;
42 ~EC_PublicKey() override = default;
43
44#if defined(BOTAN_HAS_LEGACY_EC_POINT)
45 /**
46 * Get the public point of this key.
47 * @throw Invalid_State is thrown if the
48 * domain parameters of this point are not set
49 * @result the public point of this key
50 */
51 BOTAN_DEPRECATED("Avoid accessing the point directly") const EC_Point& public_point() const;
52#endif
53
55
56 std::vector<uint8_t> raw_public_key_bits() const override;
57
58 std::vector<uint8_t> public_key_bits() const override;
59
60 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
61
62 /**
63 * Get the domain parameters of this key.
64 * @throw Invalid_State is thrown if the
65 * domain parameters of this point are not set
66 * @result the domain parameters of this key
67 */
68 const EC_Group& domain() const;
69
70 /**
71 * Set the domain parameter encoding to be used when encoding this key.
72 * @param enc the encoding to use
73 *
74 * This function is deprecated; in a future major release only namedCurve
75 * encoding of domain parameters will be allowed.
76 */
77 BOTAN_DEPRECATED("Support for explicit point encoding is deprecated")
79
80 /**
81 * Set the point encoding method to be used when encoding this key.
82 * @param enc the encoding to use
83 */
85
86 /**
87 * Return the DER encoding of this keys domain in whatever format
88 * is preset for this particular key
89 */
90 std::vector<uint8_t> DER_domain() const;
91
92 /**
93 * Get the domain parameter encoding to be used when encoding this key.
94 * @result the encoding to use
95 */
97
98 /**
99 * Get the point encoding method to be used when encoding this key.
100 * @result the encoding to use
101 */
103
104 size_t key_length() const override;
105 size_t estimated_strength() const override;
106
107 const BigInt& get_int_field(std::string_view field) const override;
108
109 /**
110 * Return the public point of this key
111 */
112 const EC_AffinePoint& public_ec_point() const;
113
114 // TODO(Botan4) remove this
116
117 protected:
118#if defined(BOTAN_HAS_LEGACY_EC_POINT)
119 /**
120 * Load a public key from the point.
121 *
122 * @param group EC domain parameters
123 * @param pub_point public point on the curve
124 */
125 EC_PublicKey(const EC_Group& group, const EC_Point& pub_point);
126#endif
127
128 /**
129 * Load a public key from the point.
130 *
131 * @param group EC domain parameters
132 * @param public_key public point on the curve
133 */
134 EC_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key);
135
136 /**
137 * Load a public key.
138 * @param alg_id the X.509 algorithm identifier
139 * @param key_bits DER encoded public key bits
140 */
141 EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
142
143 static const AlgorithmIdentifier& assert_algorithm_identifier(const AlgorithmIdentifier& alg_id,
144 std::string_view alg_name);
145
146 EC_PublicKey() = default;
147
148 std::shared_ptr<const EC_PublicKey_Data> m_public_key; // NOLINT(*non-private-member-variable*)
149 EC_Group_Encoding m_domain_encoding = EC_Group_Encoding::NamedCurve; // NOLINT(*non-private-member-variable*)
150 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed; // NOLINT(*non-private-member-variable*)
151};
152
153/**
154* This abstract class represents ECC private keys
155*/
156
159
160class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
161 public virtual Private_Key {
162 public:
164
166
167 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
168
169 /**
170 * Get the private key value of this key object.
171 * @result the private key value of this key object
172 */
173 const BigInt& private_value() const;
174
175 EC_PrivateKey(const EC_PrivateKey& other) = default;
176 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
177 EC_PrivateKey(EC_PrivateKey&& other) = delete;
179 ~EC_PrivateKey() override = default;
180
181 const BigInt& get_int_field(std::string_view field) const final;
182
183 const EC_Scalar& _private_key() const;
184
185 protected:
186 /**
187 * If x=0, creates a new private key in the domain
188 * using the given rng. If with_modular_inverse is set,
189 * the public key will be calculated by multiplying
190 * the base point with the modular inverse of
191 * x (as in ECGDSA and ECKCDSA), otherwise by
192 * multiplying directly with x (as in ECDSA).
193 *
194 * TODO: Remove, once the respective deprecated constructors of the
195 * concrete ECC algorithms is removed.
196 */
198 const EC_Group& group,
199 const BigInt& x,
200 bool with_modular_inverse = false);
201
202 /**
203 * Creates a new private key
204 *
205 * If @p with_modular_inverse is set, the public key will be calculated by
206 * multiplying the base point with the modular inverse of x (as in ECGDSA
207 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
208 */
209 EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, bool with_modular_inverse = false);
210
211 /**
212 * Load a EC private key from the secret scalar
213 *
214 * If @p with_modular_inverse is set, the public key will be calculated by
215 * multiplying the base point with the modular inverse of x (as in ECGDSA
216 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
217 */
218 EC_PrivateKey(const EC_Group& group, const EC_Scalar& scalar, bool with_modular_inverse = false);
219
220 /*
221 * Creates a new private key object from the
222 * ECPrivateKey structure given in key_bits.
223 * If with_modular_inverse is set,
224 * the public key will be calculated by multiplying
225 * the base point with the modular inverse of
226 * x (as in ECGDSA and ECKCDSA), otherwise by
227 * multiplying directly with x (as in ECDSA).
228 */
230 std::span<const uint8_t> key_bits,
231 bool with_modular_inverse = false);
232
234
235 std::shared_ptr<const EC_PrivateKey_Data> m_private_key; // NOLINT(*non-private-member-variable*)
236 bool m_with_modular_inverse; // NOLINT(*non-private-member-variable*)
237};
238
240
241} // namespace Botan
242
243#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 & operator=(EC_PrivateKey &&other)=delete
const EC_Scalar & _private_key() const
Definition ecc_key.cpp:135
bool m_with_modular_inverse
Definition ecc_key.h:236
const BigInt & private_value() const
Definition ecc_key.cpp:130
EC_PrivateKey(EC_PrivateKey &&other)=delete
secure_vector< uint8_t > raw_private_key_bits() const final
Definition ecc_key.cpp:169
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:235
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:237
const BigInt & get_int_field(std::string_view field) const final
Definition ecc_key.cpp:274
secure_vector< uint8_t > private_key_bits() const final
Definition ecc_key.cpp:174
const EC_Group & domain() const
Definition ecc_key.cpp:76
std::vector< uint8_t > DER_domain() const
Definition ecc_key.cpp:110
const EC_AffinePoint & public_ec_point() const
Definition ecc_key.cpp:88
EC_PublicKey(const EC_PublicKey &other)=default
void set_parameter_encoding(EC_Group_Encoding enc)
Definition ecc_key.cpp:122
EC_Point_Format m_point_encoding
Definition ecc_key.h:150
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:149
EC_PublicKey(EC_PublicKey &&other)=delete
AlgorithmIdentifier algorithm_identifier() const override
Definition ecc_key.cpp:98
EC_Group_Encoding domain_format() const
Definition ecc_key.h:96
std::shared_ptr< const EC_PublicKey_Data > m_public_key
Definition ecc_key.h:148
std::vector< uint8_t > raw_public_key_bits() const override
Definition ecc_key.cpp:102
void set_point_encoding(EC_Point_Format enc)
Definition ecc_key.cpp:114
EC_PublicKey()=default
EC_PublicKey & operator=(const EC_PublicKey &other)=default
EC_Point_Format point_encoding() const
Definition ecc_key.h:102
~EC_PublicKey() override=default
const EC_AffinePoint & _public_ec_point() const
Definition ecc_key.h:115
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:93
EC_PublicKey & operator=(EC_PublicKey &&other)=delete
std::vector< uint8_t > public_key_bits() const override
Definition ecc_key.cpp:106
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128