Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/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
16
namespace 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
*/
28
class 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
73.4k
      ~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
73.4k
      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
139k
      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
0
      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
0
      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
0
      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
0
      EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_Group_Encoding::Explicit) {}
107
108
      EC_Group m_domain_params;
109
      EC_Point m_public_key;
110
      EC_Group_Encoding m_domain_encoding;
111
      EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed;
112
};
113
114
/**
115
* This abstract class represents ECC private keys
116
*/
117
118
BOTAN_DIAGNOSTIC_PUSH
119
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
120
121
class 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
0
      ~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
      */
151
      EC_PrivateKey(RandomNumberGenerator& rng,
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
      */
165
      EC_PrivateKey(const AlgorithmIdentifier& alg_id,
166
                    std::span<const uint8_t> key_bits,
167
                    bool with_modular_inverse = false);
168
169
      EC_PrivateKey() = default;
170
171
      BigInt m_private_key;
172
};
173
174
BOTAN_DIAGNOSTIC_POP
175
176
}  // namespace Botan
177
178
#endif