Botan 3.11.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 EC_PublicKey() = default;
138
139 std::shared_ptr<const EC_PublicKey_Data> m_public_key; // NOLINT(*non-private-member-variable*)
140 EC_Group_Encoding m_domain_encoding = EC_Group_Encoding::NamedCurve; // NOLINT(*non-private-member-variable*)
141 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed; // NOLINT(*non-private-member-variable*)
142};
143
144/**
145* This abstract class represents ECC private keys
146*/
147
150
151class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
152 public virtual Private_Key {
153 public:
155
157
158 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
159
160 /**
161 * Get the private key value of this key object.
162 * @result the private key value of this key object
163 */
164 const BigInt& private_value() const;
165
166 EC_PrivateKey(const EC_PrivateKey& other) = default;
167 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
168 EC_PrivateKey(EC_PrivateKey&& other) = delete;
170 ~EC_PrivateKey() override = default;
171
172 const BigInt& get_int_field(std::string_view field) const final;
173
174 const EC_Scalar& _private_key() const;
175
176 protected:
177 /**
178 * If x=0, creates a new private key in the domain
179 * using the given rng. If with_modular_inverse is set,
180 * the public key will be calculated by multiplying
181 * the base point with the modular inverse of
182 * x (as in ECGDSA and ECKCDSA), otherwise by
183 * multiplying directly with x (as in ECDSA).
184 *
185 * TODO: Remove, once the respective deprecated constructors of the
186 * concrete ECC algorithms is removed.
187 */
189 const EC_Group& group,
190 const BigInt& x,
191 bool with_modular_inverse = false);
192
193 /**
194 * Creates a new private key
195 *
196 * If @p with_modular_inverse is set, the public key will be calculated by
197 * multiplying the base point with the modular inverse of x (as in ECGDSA
198 * and ECKCDSA), otherwise by multiplying directly with x (as in ECDSA).
199 */
200 EC_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, bool with_modular_inverse = false);
201
202 /**
203 * Load a EC private key from the secret scalar
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(const EC_Group& group, const EC_Scalar& scalar, bool with_modular_inverse = false);
210
211 /*
212 * Creates a new private key object from the
213 * ECPrivateKey structure given in key_bits.
214 * If with_modular_inverse is set,
215 * the public key will be calculated by multiplying
216 * the base point with the modular inverse of
217 * x (as in ECGDSA and ECKCDSA), otherwise by
218 * multiplying directly with x (as in ECDSA).
219 */
221 std::span<const uint8_t> key_bits,
222 bool with_modular_inverse = false);
223
224 EC_PrivateKey() = default;
225
226 std::shared_ptr<const EC_PrivateKey_Data> m_private_key; // NOLINT(*non-private-member-variable*)
227};
228
230
231} // namespace Botan
232
233#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#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:123
const BigInt & private_value() const
Definition ecc_key.cpp:118
EC_PrivateKey(EC_PrivateKey &&other)=delete
secure_vector< uint8_t > raw_private_key_bits() const final
Definition ecc_key.cpp:154
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:226
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:201
const BigInt & get_int_field(std::string_view field) const final
Definition ecc_key.cpp:231
secure_vector< uint8_t > private_key_bits() const final
Definition ecc_key.cpp:159
const EC_Group & domain() const
Definition ecc_key.cpp:64
std::vector< uint8_t > DER_domain() const
Definition ecc_key.cpp:98
EC_PublicKey(const EC_PublicKey &other)=default
void set_parameter_encoding(EC_Group_Encoding enc)
Definition ecc_key.cpp:110
EC_Point_Format m_point_encoding
Definition ecc_key.h:141
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:140
EC_PublicKey(EC_PublicKey &&other)=delete
AlgorithmIdentifier algorithm_identifier() const override
Definition ecc_key.cpp:86
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:139
std::vector< uint8_t > raw_public_key_bits() const override
Definition ecc_key.cpp:90
void set_point_encoding(EC_Point_Format enc)
Definition ecc_key.cpp:102
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:81
EC_PublicKey & operator=(EC_PublicKey &&other)=delete
std::vector< uint8_t > public_key_bits() const override
Definition ecc_key.cpp:94
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68