Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/ecc_key.h
Line
Count
Source
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
17
namespace Botan {
18
19
class EC_AffinePoint;
20
class EC_Point;
21
class EC_Group;
22
class EC_Scalar;
23
class EC_PublicKey_Data;
24
class EC_PrivateKey_Data;
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
*/
36
class 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;
41
      EC_PublicKey& operator=(EC_PublicKey&& other) = delete;
42
167k
      ~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
54
      AlgorithmIdentifier algorithm_identifier() const override;
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")
78
      void set_parameter_encoding(EC_Group_Encoding enc);
79
80
      /**
81
      * Set the point encoding method to be used when encoding this key.
82
      * @param enc the encoding to use
83
      */
84
      void set_point_encoding(EC_Point_Format enc);
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
      */
96
0
      EC_Group_Encoding domain_format() const { return m_domain_encoding; }
97
98
      /**
99
      * Get the point encoding method to be used when encoding this key.
100
      * @result the encoding to use
101
      */
102
0
      EC_Point_Format point_encoding() const { return m_point_encoding; }
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
0
      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
151
BOTAN_DIAGNOSTIC_PUSH
152
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
153
154
class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
155
                                             public virtual Private_Key {
156
   public:
157
      secure_vector<uint8_t> private_key_bits() const final;
158
159
      secure_vector<uint8_t> raw_private_key_bits() const final;
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;
172
      EC_PrivateKey& operator=(EC_PrivateKey&& other) = delete;
173
0
      ~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
      */
191
      EC_PrivateKey(RandomNumberGenerator& rng,
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
      */
223
      EC_PrivateKey(const AlgorithmIdentifier& alg_id,
224
                    std::span<const uint8_t> key_bits,
225
                    bool with_modular_inverse = false);
226
227
0
      EC_PrivateKey() : m_with_modular_inverse(false) {}
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
233
BOTAN_DIAGNOSTIC_POP
234
235
}  // namespace Botan
236
237
#endif