Coverage Report

Created: 2024-11-21 06:38

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