Coverage Report

Created: 2020-11-21 08:34

/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
      /**
32
      * Create a public key.
33
      * @param dom_par EC domain parameters
34
      * @param pub_point public point on the curve
35
      */
36
      EC_PublicKey(const EC_Group& dom_par,
37
                   const PointGFp& pub_point);
38
39
      /**
40
      * Load a public key.
41
      * @param alg_id the X.509 algorithm identifier
42
      * @param key_bits DER encoded public key bits
43
      */
44
      EC_PublicKey(const AlgorithmIdentifier& alg_id,
45
                   const std::vector<uint8_t>& key_bits);
46
47
      EC_PublicKey(const EC_PublicKey& other) = default;
48
      EC_PublicKey& operator=(const EC_PublicKey& other) = default;
49
22.2k
      virtual ~EC_PublicKey() = default;
50
51
      /**
52
      * Get the public point of this key.
53
      * @throw Invalid_State is thrown if the
54
      * domain parameters of this point are not set
55
      * @result the public point of this key
56
      */
57
21.9k
      const PointGFp& public_point() const { return m_public_key; }
58
59
      AlgorithmIdentifier algorithm_identifier() const override;
60
61
      std::vector<uint8_t> public_key_bits() const override;
62
63
      bool check_key(RandomNumberGenerator& rng,
64
                     bool strong) const override;
65
66
      /**
67
      * Get the domain parameters of this key.
68
      * @throw Invalid_State is thrown if the
69
      * domain parameters of this point are not set
70
      * @result the domain parameters of this key
71
      */
72
40.0k
      const EC_Group& domain() const { return m_domain_params; }
73
74
      /**
75
      * Set the domain parameter encoding to be used when encoding this key.
76
      * @param enc the encoding to use
77
      */
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(PointGFp::Compression_Type 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
0
         { return domain().DER_encode(domain_format()); }
92
93
      /**
94
      * Get the domain parameter encoding to be used when encoding this key.
95
      * @result the encoding to use
96
      */
97
      EC_Group_Encoding domain_format() const
98
0
         { return m_domain_encoding; }
99
100
      /**
101
      * Get the point encoding method to be used when encoding this key.
102
      * @result the encoding to use
103
      */
104
      PointGFp::Compression_Type point_encoding() const
105
0
         { return m_point_encoding; }
106
107
      size_t key_length() const override;
108
      size_t estimated_strength() const override;
109
110
   protected:
111
      EC_PublicKey() : m_domain_params{}, m_public_key{}, m_domain_encoding(EC_DOMPAR_ENC_EXPLICIT)
112
20.2k
      {}
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
      /*
128
      * If x=0, creates a new private key in the domain
129
      * using the given rng. If with_modular_inverse is set,
130
      * the public key will be calculated by multiplying
131
      * the base point with the modular inverse of
132
      * x (as in ECGDSA and ECKCDSA), otherwise by
133
      * multiplying directly with x (as in ECDSA).
134
      */
135
      EC_PrivateKey(RandomNumberGenerator& rng,
136
                    const EC_Group& domain,
137
                    const BigInt& x,
138
                    bool with_modular_inverse=false);
139
140
      /*
141
      * Creates a new private key object from the
142
      * ECPrivateKey structure given in key_bits.
143
      * If with_modular_inverse is set,
144
      * the public key will be calculated by multiplying
145
      * the base point with the modular inverse of
146
      * x (as in ECGDSA and ECKCDSA), otherwise by
147
      * multiplying directly with x (as in ECDSA).
148
      */
149
      EC_PrivateKey(const AlgorithmIdentifier& alg_id,
150
                    const secure_vector<uint8_t>& key_bits,
151
                    bool with_modular_inverse=false);
152
153
      secure_vector<uint8_t> private_key_bits() const override;
154
155
      /**
156
      * Get the private key value of this key object.
157
      * @result the private key value of this key object
158
      */
159
      const BigInt& private_value() const;
160
161
      EC_PrivateKey(const EC_PrivateKey& other) = default;
162
      EC_PrivateKey& operator=(const EC_PrivateKey& other) = default;
163
19.0k
      ~EC_PrivateKey() = default;
164
   protected:
165
      EC_PrivateKey() = default;
166
167
      BigInt m_private_key;
168
   };
169
170
}
171
172
#endif