aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/ecdsa/ecc_key.cpp
blob: 1beba96c585b4bf361d52cb56a102f9cb3fbca12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*************************************************
* ECDSA Header File                              *
* (C) 2007 Falko Strenzke, FlexSecure GmbH       *
*          Manuel hartl, FlexSecure GmbH         *
* (C) 2008 Jack Lloyd                            *
*************************************************/

#ifndef BOTAN_ECC_KEY_H__
#define BOTAN_ECC_KEY_H__

#include <botan/bigint.h>
#include <botan/curve_gfp.h>
#include <botan/pk_keys.h>
#include <botan/ec_dompar.h>
#include <botan/ecc_core.h>
#include <botan/x509_key.h>
#include <botan/pkcs8.h>

namespace Botan {

/**
* This class represents abstract EC Public Keys. When encoding a key
* via an encoder that can be accessed via the corresponding member
* functions, the key will decide upon its internally stored encoding
* information whether to encode itself with or without domain
* parameters, or using the domain parameter oid. Furthermore, a public
* key without domain parameters can be decoded. In that case, it
* cannot be used for verification until its domain parameters are set
* by calling the corresponding member function.
*/
class EC_PublicKey : public virtual Public_Key
   {
   public:

      /**
      * Tells whether this key knows his own domain parameters.
      * @result true if the domain parameters are set, false otherwise
      */
      bool domain_parameters_set();

      /**
      * Get the public point of this key.
      * @throw Invalid_State is thrown if the
      * domain parameters of this point are not set
      * @result the public point of this key
      */
      inline Botan::PointGFp get_public_point() const
         {
         if (!mp_public_point.get())
            {
            throw Invalid_State("EC_PublicKey::get_public_point(): public point not set because ec domain parameters are not yet set");
            }
         return *mp_public_point;
         }

      /**
      * Get the domain parameters of this key.
      * @throw Invalid_State is thrown if the
      * domain parameters of this point are not set
      * @result the domain parameters of this key
      */
      EC_Domain_Params const get_domain_parameters() const;

      /**
      * Set the domain parameter encoding to be used when encoding this key.
      * @param enc the encoding to use
      */
      void set_parameter_encoding(EC_dompar_enc enc);

      /**
      * Get the domain parameter encoding to be used when encoding this key.
      * @result the encoding to use
      */
      inline int get_parameter_encoding() const
         {
         return m_param_enc;
         }

      //ctors
      EC_PublicKey()
         : m_param_enc(ENC_EXPLICIT)
         {
         //assert(mp_dom_pars.get() == 0);
         //assert(mp_public_point.get() == 0);
         }

      /**
      * Get an x509_encoder that can be used to encode this key.
      * @result an x509_encoder for this key
      */
      X509_Encoder* x509_encoder() const;

      /**
      * Get an x509_decoder that can be used to decode a stored key into
      * this key.
      * @result an x509_decoder for this key
      */
      X509_Decoder* x509_decoder();

      /**
      * Make sure that the public point and domain parameters of this key are set.
      * @throw Invalid_State if either of the two data members is not set
      */
      virtual void affirm_init() const;

      virtual ~EC_PublicKey() {}
   protected:
      virtual void X509_load_hook();

      SecureVector<byte> m_enc_public_point; // stores the public point

      std::auto_ptr<EC_Domain_Params> mp_dom_pars;
      std::auto_ptr<Botan::PointGFp> mp_public_point;
      EC_dompar_enc m_param_enc;
   };

/**
* This abstract class represents general EC Private Keys
*/
class EC_PrivateKey : public virtual EC_PublicKey, public virtual Private_Key
   {
   public:

      /**
      * Get an PKCS#8 encoder that can be used to encoded this key.
      * @result an PKCS#8 encoder for this key
      */
      PKCS8_Encoder* pkcs8_encoder() const;
      /**
      * Get an PKCS#8 decoder that can be used to decoded a stored key into
      * this key.
      * @result an PKCS#8 decoder for this key
      */
      PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&);
      /**
      * Get the private key value of this key object.
      * @result the private key value of this key object
      */
      inline BigInt const get_value() const
         {
         return m_private_value;
         }
      /**
      * Make sure that the public key parts of this object are set
      * (calls EC_PublicKey::affirm_init()) as well as the private key
      * value.
      * @throw Invalid_State if the above conditions are not satisfied
      */
      virtual void affirm_init()  const;
      virtual ~EC_PrivateKey()
         {}
   protected:
      virtual void PKCS8_load_hook(bool = false);
      void generate_private_key(RandomNumberGenerator&);
      BigInt m_private_value;
   };

}

#endif