Coverage Report

Created: 2024-07-10 06:19

/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
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
30.3k
      ~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
34.2k
      const EC_Point& public_point() const { return m_public_key; }
41
42
      AlgorithmIdentifier algorithm_identifier() const override;
43
44
      std::vector<uint8_t> raw_public_key_bits() const override;
45
46
      std::vector<uint8_t> public_key_bits() const override;
47
48
      bool check_key(RandomNumberGenerator& rng, 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
39.7k
      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
0
      std::vector<uint8_t> DER_domain() const { return domain().DER_encode(domain_format()); }
75
76
      /**
77
      * Get the domain parameter encoding to be used when encoding this key.
78
      * @result the encoding to use
79
      */
80
0
      EC_Group_Encoding domain_format() const { return m_domain_encoding; }
81
82
      /**
83
      * Get the point encoding method to be used when encoding this key.
84
      * @result the encoding to use
85
      */
86
0
      EC_Point_Format point_encoding() const { return m_point_encoding; }
87
88
      size_t key_length() const override;
89
      size_t estimated_strength() const override;
90
91
      const BigInt& get_int_field(std::string_view field) const override;
92
93
   protected:
94
      /**
95
      * Create a public key.
96
      * @param dom_par EC domain parameters
97
      * @param pub_point public point on the curve
98
      */
99
      EC_PublicKey(const EC_Group& dom_par, const EC_Point& pub_point);
100
101
      /**
102
      * Load a public key.
103
      * @param alg_id the X.509 algorithm identifier
104
      * @param key_bits DER encoded public key bits
105
      */
106
      EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
107
108
21.8k
      EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_Group_Encoding::Explicit) {}
109
110
      EC_Group m_domain_params;
111
      EC_Point m_public_key;
112
      EC_Group_Encoding m_domain_encoding;
113
      EC_Point_Format m_point_encoding = EC_Point_Format::Uncompressed;
114
};
115
116
/**
117
* This abstract class represents ECC private keys
118
*/
119
120
BOTAN_DIAGNOSTIC_PUSH
121
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
122
123
class BOTAN_PUBLIC_API(2, 0) EC_PrivateKey : public virtual EC_PublicKey,
124
                                             public virtual Private_Key {
125
   public:
126
      secure_vector<uint8_t> private_key_bits() const final;
127
128
      secure_vector<uint8_t> raw_private_key_bits() const final;
129
130
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
131
132
      /**
133
      * Get the private key value of this key object.
134
      * @result the private key value of this key object
135
      */
136
      const BigInt& private_value() const;
137
138
      EC_PrivateKey(const EC_PrivateKey& other) = default;
139
      EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
140
19.7k
      ~EC_PrivateKey() override = default;
141
142
      const BigInt& get_int_field(std::string_view field) const final;
143
144
   protected:
145
      /*
146
      * If x=0, creates a new private key in the domain
147
      * using the given rng. If with_modular_inverse is set,
148
      * the public key will be calculated by multiplying
149
      * the base point with the modular inverse of
150
      * x (as in ECGDSA and ECKCDSA), otherwise by
151
      * multiplying directly with x (as in ECDSA).
152
      */
153
      EC_PrivateKey(RandomNumberGenerator& rng,
154
                    const EC_Group& domain,
155
                    const BigInt& x,
156
                    bool with_modular_inverse = false);
157
158
      /*
159
      * Creates a new private key object from the
160
      * ECPrivateKey structure given in key_bits.
161
      * If with_modular_inverse is set,
162
      * the public key will be calculated by multiplying
163
      * the base point with the modular inverse of
164
      * x (as in ECGDSA and ECKCDSA), otherwise by
165
      * multiplying directly with x (as in ECDSA).
166
      */
167
      EC_PrivateKey(const AlgorithmIdentifier& alg_id,
168
                    std::span<const uint8_t> key_bits,
169
                    bool with_modular_inverse = false);
170
171
      EC_PrivateKey() = default;
172
173
      BigInt m_private_key;
174
};
175
176
BOTAN_DIAGNOSTIC_POP
177
178
}  // namespace Botan
179
180
#endif