Coverage Report

Created: 2025-12-31 06:08

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