Botan 3.4.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_group.h>
14#include <botan/pk_keys.h>
15
16namespace Botan {
17
18/**
19* This class represents abstract ECC public keys. When encoding a key
20* via an encoder that can be accessed via the corresponding member
21* functions, the key will decide upon its internally stored encoding
22* information whether to encode itself with or without domain
23* parameters, or using the domain parameter oid. Furthermore, a public
24* key without domain parameters can be decoded. In that case, it
25* cannot be used for verification until its domain parameters are set
26* by calling the corresponding member function.
27*/
28class BOTAN_PUBLIC_API(2, 0) EC_PublicKey : public virtual Public_Key {
29 public:
30 EC_PublicKey(const EC_PublicKey& other) = default;
31 EC_PublicKey& operator=(const EC_PublicKey& other) = default;
32 ~EC_PublicKey() override = default;
33
34 /**
35 * Get the public point of this key.
36 * @throw Invalid_State is thrown if the
37 * domain parameters of this point are not set
38 * @result the public point of this key
39 */
40 const EC_Point& public_point() const { return m_public_key; }
41
42 AlgorithmIdentifier algorithm_identifier() const override;
43
44 std::vector<uint8_t> public_key_bits() const override;
45
46 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
47
48 /**
49 * Get the domain parameters of this key.
50 * @throw Invalid_State is thrown if the
51 * domain parameters of this point are not set
52 * @result the domain parameters of this key
53 */
54 const EC_Group& domain() const { return m_domain_params; }
55
56 /**
57 * Set the domain parameter encoding to be used when encoding this key.
58 * @param enc the encoding to use
59 */
60 void set_parameter_encoding(EC_Group_Encoding enc);
61
62 /**
63 * Set the point encoding method to be used when encoding this key.
64 * @param enc the encoding to use
65 */
66 void set_point_encoding(EC_Point_Format enc);
67
68 /**
69 * Return the DER encoding of this keys domain in whatever format
70 * is preset for this particular key
71 */
72 std::vector<uint8_t> DER_domain() const { return domain().DER_encode(domain_format()); }
73
74 /**
75 * Get the domain parameter encoding to be used when encoding this key.
76 * @result the encoding to use
77 */
78 EC_Group_Encoding domain_format() const { return m_domain_encoding; }
79
80 /**
81 * Get the point encoding method to be used when encoding this key.
82 * @result the encoding to use
83 */
84 EC_Point_Format point_encoding() const { return m_point_encoding; }
85
86 size_t key_length() const override;
87 size_t estimated_strength() const override;
88
89 const BigInt& get_int_field(std::string_view field) const override;
90
91 protected:
92 /**
93 * Create a public key.
94 * @param dom_par EC domain parameters
95 * @param pub_point public point on the curve
96 */
97 EC_PublicKey(const EC_Group& dom_par, const EC_Point& pub_point);
98
99 /**
100 * Load a public key.
101 * @param alg_id the X.509 algorithm identifier
102 * @param key_bits DER encoded public key bits
103 */
104 EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
105
106 EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_Group_Encoding::Explicit) {}
107
111 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed;
112};
113
114/**
115* This abstract class represents ECC private keys
116*/
117
120
121class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
122 public virtual Private_Key {
123 public:
124 secure_vector<uint8_t> private_key_bits() const final;
125
126 secure_vector<uint8_t> raw_private_key_bits() const final;
127
128 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
129
130 /**
131 * Get the private key value of this key object.
132 * @result the private key value of this key object
133 */
134 const BigInt& private_value() const;
135
136 EC_PrivateKey(const EC_PrivateKey& other) = default;
137 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
138 ~EC_PrivateKey() override = default;
139
140 const BigInt& get_int_field(std::string_view field) const final;
141
142 protected:
143 /*
144 * If x=0, creates a new private key in the domain
145 * using the given rng. If with_modular_inverse is set,
146 * the public key will be calculated by multiplying
147 * the base point with the modular inverse of
148 * x (as in ECGDSA and ECKCDSA), otherwise by
149 * multiplying directly with x (as in ECDSA).
150 */
152 const EC_Group& domain,
153 const BigInt& x,
154 bool with_modular_inverse = false);
155
156 /*
157 * Creates a new private key object from the
158 * ECPrivateKey structure given in key_bits.
159 * If with_modular_inverse is set,
160 * the public key will be calculated by multiplying
161 * the base point with the modular inverse of
162 * x (as in ECGDSA and ECKCDSA), otherwise by
163 * multiplying directly with x (as in ECDSA).
164 */
166 std::span<const uint8_t> key_bits,
167 bool with_modular_inverse = false);
168
169 EC_PrivateKey() = default;
170
172};
173
175
176} // namespace Botan
177
178#endif
EC_PrivateKey & operator=(const EC_PrivateKey &other)=default
~EC_PrivateKey() override=default
EC_PrivateKey(const EC_PrivateKey &other)=default
const EC_Group & domain() const
Definition ecc_key.h:54
std::vector< uint8_t > DER_domain() const
Definition ecc_key.h:72
EC_PublicKey(const EC_PublicKey &other)=default
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:110
EC_Group_Encoding domain_format() const
Definition ecc_key.h:78
EC_Group m_domain_params
Definition ecc_key.h:108
EC_PublicKey & operator=(const EC_PublicKey &other)=default
EC_Point_Format point_encoding() const
Definition ecc_key.h:84
EC_Point m_public_key
Definition ecc_key.h:109
~EC_PublicKey() override=default
const EC_Point & public_point() const
Definition ecc_key.h:40
#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
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:24