Botan 3.0.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 {
30 public:
31 EC_PublicKey(const EC_PublicKey& other) = default;
32 EC_PublicKey& operator=(const EC_PublicKey& other) = default;
33 virtual ~EC_PublicKey() = default;
34
35 /**
36 * Get the public point of this key.
37 * @throw Invalid_State is thrown if the
38 * domain parameters of this point are not set
39 * @result the public point of this key
40 */
41 const EC_Point& public_point() const { return m_public_key; }
42
43 AlgorithmIdentifier algorithm_identifier() const override;
44
45 std::vector<uint8_t> public_key_bits() const override;
46
47 bool check_key(RandomNumberGenerator& rng,
48 bool strong) const override;
49
50 /**
51 * Get the domain parameters of this key.
52 * @throw Invalid_State is thrown if the
53 * domain parameters of this point are not set
54 * @result the domain parameters of this key
55 */
56 const EC_Group& domain() const { return m_domain_params; }
57
58 /**
59 * Set the domain parameter encoding to be used when encoding this key.
60 * @param enc the encoding to use
61 */
62 void set_parameter_encoding(EC_Group_Encoding enc);
63
64 /**
65 * Set the point encoding method to be used when encoding this key.
66 * @param enc the encoding to use
67 */
68 void set_point_encoding(EC_Point_Format enc);
69
70 /**
71 * Return the DER encoding of this keys domain in whatever format
72 * is preset for this particular key
73 */
74 std::vector<uint8_t> DER_domain() const
75 { return domain().DER_encode(domain_format()); }
76
77 /**
78 * Get the domain parameter encoding to be used when encoding this key.
79 * @result the encoding to use
80 */
82 { return m_domain_encoding; }
83
84 /**
85 * Get the point encoding method to be used when encoding this key.
86 * @result the encoding to use
87 */
89 { return m_point_encoding; }
90
91 size_t key_length() const override;
92 size_t estimated_strength() const override;
93
94 const BigInt& get_int_field(std::string_view field) const override;
95
96 protected:
97 /**
98 * Create a public key.
99 * @param dom_par EC domain parameters
100 * @param pub_point public point on the curve
101 */
102 EC_PublicKey(const EC_Group& dom_par,
103 const EC_Point& pub_point);
104
105 /**
106 * Load a public key.
107 * @param alg_id the X.509 algorithm identifier
108 * @param key_bits DER encoded public key bits
109 */
110 EC_PublicKey(const AlgorithmIdentifier& alg_id,
111 std::span<const uint8_t> key_bits);
112
113 EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_Group_Encoding::Explicit)
114 {}
115
119 EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed;
120 };
121
122/**
123* This abstract class represents ECC private keys
124*/
125
128
129class BOTAN_PUBLIC_API(2,0) EC_PrivateKey : public virtual EC_PublicKey,
130 public virtual Private_Key
131 {
132 public:
133 secure_vector<uint8_t> private_key_bits() const override final;
134
135 secure_vector<uint8_t> raw_private_key_bits() const override final;
136
137 /**
138 * Get the private key value of this key object.
139 * @result the private key value of this key object
140 */
141 const BigInt& private_value() const;
142
143 EC_PrivateKey(const EC_PrivateKey& other) = default;
144 EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
145 ~EC_PrivateKey() = default;
146
147 const BigInt& get_int_field(std::string_view field) const override final;
148
149 protected:
150 /*
151 * If x=0, creates a new private key in the domain
152 * using the given rng. If with_modular_inverse is set,
153 * the public key will be calculated by multiplying
154 * the base point with the modular inverse of
155 * x (as in ECGDSA and ECKCDSA), otherwise by
156 * multiplying directly with x (as in ECDSA).
157 */
159 const EC_Group& domain,
160 const BigInt& x,
161 bool with_modular_inverse=false);
162
163 /*
164 * Creates a new private key object from the
165 * ECPrivateKey structure given in key_bits.
166 * If with_modular_inverse is set,
167 * the public key will be calculated by multiplying
168 * the base point with the modular inverse of
169 * x (as in ECGDSA and ECKCDSA), otherwise by
170 * multiplying directly with x (as in ECDSA).
171 */
173 std::span<const uint8_t> key_bits,
174 bool with_modular_inverse=false);
175
176 EC_PrivateKey() = default;
177
179 };
180
182
183}
184
185#endif
EC_PrivateKey & operator=(const EC_PrivateKey &other)=default
BigInt m_private_key
Definition: ecc_key.h:178
EC_PrivateKey(const EC_PrivateKey &other)=default
~EC_PrivateKey()=default
const EC_Group & domain() const
Definition: ecc_key.h:56
std::vector< uint8_t > DER_domain() const
Definition: ecc_key.h:74
EC_PublicKey(const EC_PublicKey &other)=default
EC_Group_Encoding m_domain_encoding
Definition: ecc_key.h:118
virtual ~EC_PublicKey()=default
EC_Group_Encoding domain_format() const
Definition: ecc_key.h:81
EC_Group m_domain_params
Definition: ecc_key.h:116
EC_PublicKey & operator=(const EC_PublicKey &other)=default
EC_Point_Format point_encoding() const
Definition: ecc_key.h:88
EC_Point m_public_key
Definition: ecc_key.h:117
const EC_Point & public_point() const
Definition: ecc_key.h:41
#define BOTAN_DIAGNOSTIC_POP
Definition: compiler.h:204
#define BOTAN_DIAGNOSTIC_PUSH
Definition: compiler.h:201
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition: compiler.h:203
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:12
EC_Point_Format
Definition: ec_point.h:19
EC_Group_Encoding
Definition: ec_group.h:24
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64