Coverage Report

Created: 2021-02-21 07:20

/src/botan/build/include/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
   {
30
   public:
31
      EC_PublicKey(const EC_PublicKey& other) = default;
32
      EC_PublicKey& operator=(const EC_PublicKey& other) = default;
33
24.8k
      virtual ~EC_PublicKey() = default;
34
35
      /**
36
      * Get the public point of this key.
37
      * @throw Invalid_State is thrown if the
38
      * domain parameters of this point are not set
39
      * @result the public point of this key
40
      */
41
25.7k
      const PointGFp& public_point() const { return m_public_key; }
42
43
      AlgorithmIdentifier algorithm_identifier() const override;
44
45
      std::vector<uint8_t> public_key_bits() const override;
46
47
      bool check_key(RandomNumberGenerator& rng,
48
                     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
37.9k
      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(PointGFp::Compression_Type enc);
69
70
      /**
71
      * Return the DER encoding of this keys domain in whatever format
72
      * is preset for this particular key
73
      */
74
      std::vector<uint8_t> DER_domain() const
75
0
         { return domain().DER_encode(domain_format()); }
76
77
      /**
78
      * Get the domain parameter encoding to be used when encoding this key.
79
      * @result the encoding to use
80
      */
81
      EC_Group_Encoding domain_format() const
82
0
         { return m_domain_encoding; }
83
84
      /**
85
      * Get the point encoding method to be used when encoding this key.
86
      * @result the encoding to use
87
      */
88
      PointGFp::Compression_Type point_encoding() const
89
0
         { return m_point_encoding; }
90
91
      size_t key_length() const override;
92
      size_t estimated_strength() const override;
93
94
   protected:
95
      /**
96
      * Create a public key.
97
      * @param dom_par EC domain parameters
98
      * @param pub_point public point on the curve
99
      */
100
      EC_PublicKey(const EC_Group& dom_par,
101
                   const PointGFp& pub_point);
102
103
      /**
104
      * Load a public key.
105
      * @param alg_id the X.509 algorithm identifier
106
      * @param key_bits DER encoded public key bits
107
      */
108
      EC_PublicKey(const AlgorithmIdentifier& alg_id,
109
                   const std::vector<uint8_t>& key_bits);
110
111
      EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_Group_Encoding::Explicit)
112
22.5k
      {}
113
114
      EC_Group m_domain_params;
115
      PointGFp m_public_key;
116
      EC_Group_Encoding m_domain_encoding;
117
      PointGFp::Compression_Type m_point_encoding = PointGFp::UNCOMPRESSED;
118
   };
119
120
/**
121
* This abstract class represents ECC private keys
122
*/
123
class BOTAN_PUBLIC_API(2,0) EC_PrivateKey : public virtual EC_PublicKey,
124
                                public virtual Private_Key
125
   {
126
   public:
127
      secure_vector<uint8_t> private_key_bits() const override;
128
129
      /**
130
      * Get the private key value of this key object.
131
      * @result the private key value of this key object
132
      */
133
      const BigInt& private_value() const;
134
135
      EC_PrivateKey(const EC_PrivateKey& other) = default;
136
      EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
137
21.4k
      ~EC_PrivateKey() = default;
138
139
   protected:
140
      /*
141
      * If x=0, creates a new private key in the domain
142
      * using the given rng. If with_modular_inverse is set,
143
      * the public key will be calculated by multiplying
144
      * the base point with the modular inverse of
145
      * x (as in ECGDSA and ECKCDSA), otherwise by
146
      * multiplying directly with x (as in ECDSA).
147
      */
148
      EC_PrivateKey(RandomNumberGenerator& rng,
149
                    const EC_Group& domain,
150
                    const BigInt& x,
151
                    bool with_modular_inverse=false);
152
153
      /*
154
      * Creates a new private key object from the
155
      * ECPrivateKey structure given in key_bits.
156
      * If with_modular_inverse is set,
157
      * the public key will be calculated by multiplying
158
      * the base point with the modular inverse of
159
      * x (as in ECGDSA and ECKCDSA), otherwise by
160
      * multiplying directly with x (as in ECDSA).
161
      */
162
      EC_PrivateKey(const AlgorithmIdentifier& alg_id,
163
                    const secure_vector<uint8_t>& key_bits,
164
                    bool with_modular_inverse=false);
165
166
      EC_PrivateKey() = default;
167
168
      BigInt m_private_key;
169
   };
170
171
}
172
173
#endif