Botan 3.13.0
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 const EC_AffinePoint& _public_ec_point() const;
110
111 protected:
112#if defined(BOTAN_HAS_LEGACY_EC_POINT)
113 /**
114 * Load a public key from the point.
115 *
116 * @param group EC domain parameters
117 * @param pub_point public point on the curve
118 */
119 EC_PublicKey(const EC_Group& group, const EC_Point& pub_point);
120#endif
121
122 /**
123 * Load a public key from the point.
124 *
125 * @param group EC domain parameters
126 * @param public_key public point on the curve
127 */
128 EC_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key);
129
130 /**
131 * Load a public key.
132 * @param alg_id the X.509 algorithm identifier
133 * @param key_bits DER encoded public key bits
134 */
135 EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
136
137 static const AlgorithmIdentifier& assert_algorithm_identifier(const AlgorithmIdentifier& alg_id,
138 std::string_view alg_name);
139
140 EC_PublicKey() = default;
141
142 std::shared_ptr<const EC_PublicKey_Data> m_public_key; // NOLINT(*non-private-member-variable*)
143 EC_Group_Encoding m_domain_encoding = EC_Group_Encoding::NamedCurve; // NOLINT(*non-private-member-variable*)
144 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed; // NOLINT(*non-private-member-variable*)
145};
146
147/**
148* This abstract class represents ECC private keys
149*/
150
153
154class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
155 public virtual Private_Key {
156 public:
158
160
161 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
162
163 /**
164 * Get the private key value of this key object.
165 * @result the private key value of this key object
166 */
167 const BigInt& private_value() const;
168
169 EC_PrivateKey(const EC_PrivateKey& other) = default;
170 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
171 EC_PrivateKey(EC_PrivateKey&& other) = delete;
173 ~EC_PrivateKey() override = default;
174
175 const BigInt& get_int_field(std::string_view field) const final;
176
177 const EC_Scalar& _private_key() const;
178
179 protected:
180 /**
181 * If x=0, creates a new private key in the domain
182 * using the given rng. If with_modular_inverse is set,
183 * the public key will be calculated by multiplying
184 * the base point with the modular inverse of
185 * x (as in ECGDSA and ECKCDSA), otherwise by
186 * multiplying directly with x (as in ECDSA).
187 *
188 * TODO: Remove, once the respective deprecated constructors of the
189 * concrete ECC algorithms is removed.
190 */
192 const EC_Group& group,
193 const BigInt& x,
194 bool with_modular_inverse = false);
195
196 /**
197 * Creates a new private key
198 *
199 * If @p with_modular_inverse is set, the public key will be calculated by
200 * multiplying the base point with the modular inverse of x (as in ECGDSA
201 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
202 */
203 EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, bool with_modular_inverse = false);
204
205 /**
206 * Load a EC private key from the secret scalar
207 *
208 * If @p with_modular_inverse is set, the public key will be calculated by
209 * multiplying the base point with the modular inverse of x (as in ECGDSA
210 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
211 */
212 EC_PrivateKey(const EC_Group& group, const EC_Scalar& scalar, bool with_modular_inverse = false);
213
214 /*
215 * Creates a new private key object from the
216 * ECPrivateKey structure given in key_bits.
217 * If with_modular_inverse is set,
218 * the public key will be calculated by multiplying
219 * the base point with the modular inverse of
220 * x (as in ECGDSA and ECKCDSA), otherwise by
221 * multiplying directly with x (as in ECDSA).
222 */
224 std::span<const uint8_t> key_bits,
225 bool with_modular_inverse = false);
226
228
229 std::shared_ptr<const EC_PrivateKey_Data> m_private_key; // NOLINT(*non-private-member-variable*)
230 bool m_with_modular_inverse; // NOLINT(*non-private-member-variable*)
231};
232
234
235} // namespace Botan
236
237#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:230
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:229
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:235
const BigInt & get_int_field(std::string_view field) const final
Definition ecc_key.cpp:272
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
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:144
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:143
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:142
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
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