aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/ecc_key/ecc_key.cpp
blob: 78284b6ae7072be65f307b03b62b2045db4aedd1 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*************************************************
* ECC Key implemenation                          *
* (C) 2007 Manuel Hartl / FlexSecure GmbH        *
*                                                *
*          Falko Strenzke                        *
*          strenzke@flexsecure.de                *
*************************************************/

#include <botan/ecc_key.h>
#include <botan/x509_key.h>
#include <botan/numthry.h>
#include <botan/util.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/secmem.h>
#include <botan/point_gfp.h>

namespace Botan {

/*************************************************
* EC_PublicKey                                   *
*************************************************/
void EC_PublicKey::affirm_init() const // virtual
   {
   if ((mp_dom_pars.get() == 0) || (mp_public_point.get() == 0))
      {
      throw Invalid_State("cannot use uninitialized EC_Key");
      }
   }
EC_Domain_Params const EC_PublicKey::get_domain_parameters() const
   {
   if(!mp_dom_pars.get())
      {
      throw Invalid_State("EC_PublicKey::get_domain_parameters(): ec domain parameters are not yet set");
      }
   return *mp_dom_pars;
   }
bool EC_PublicKey::domain_parameters_set()
   {
   if (mp_dom_pars.get())
      {
      return true;
      }
   return false;
   }
void EC_PublicKey::X509_load_hook()
   {
   try
      {
      // the base point is checked to be on curve already when decoding it
      affirm_init();
      mp_public_point->check_invariants();
      }
   catch ( Illegal_Point exc )
      {
      throw Decoding_Error ( "decoded public point was found not to lie on curve" );
      }
   }


X509_Encoder* EC_PublicKey::x509_encoder() const
   {
   class EC_Key_Encoder : public X509_Encoder
      {
      public:
         AlgorithmIdentifier alg_id() const
            {
            key->affirm_init();
            SecureVector<byte> params = encode_der_ec_dompar ( * ( key->mp_dom_pars ), key->m_param_enc );
            return AlgorithmIdentifier ( key->get_oid(),
                                         params );
            }

         MemoryVector<byte> key_bits() const
            {
            key->affirm_init();
            return EC2OSP ( * ( key->mp_public_point ), PointGFp::COMPRESSED );

            }

         EC_Key_Encoder ( const EC_PublicKey* k ) : key ( k )
            {}
      private:
         const EC_PublicKey* key;
      };

   return new EC_Key_Encoder(this);
   }

X509_Decoder* EC_PublicKey::x509_decoder()
   {
   class EC_Key_Decoder : public X509_Decoder
      {
      public:
         void alg_id ( const AlgorithmIdentifier& alg_id )
            {
            key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) );
            }

         void key_bits ( const MemoryRegion<byte>& bits )
            {
            key->mp_public_point.reset ( new PointGFp ( OS2ECP ( bits, key->mp_dom_pars->get_curve() ) ) );
            key->X509_load_hook();
            }

         EC_Key_Decoder ( EC_PublicKey* k ) : key ( k )
            {}
      private:
         EC_PublicKey* key;
      };

   return new EC_Key_Decoder(this);
   }

void EC_PublicKey::set_parameter_encoding ( EC_dompar_enc type )
   {
   if ( ( type != ENC_EXPLICIT ) && ( type != ENC_IMPLICITCA ) && ( type != ENC_OID ) )
      {
      throw Invalid_Argument ( "invalid encoding type for EC-key object specified" );
      }
   affirm_init();
   if ( ( mp_dom_pars->get_oid() == "" ) && ( type == ENC_OID ) )
      {
      throw Invalid_Argument ( "invalid encoding type ENC_OID specified for EC-key object whose corresponding domain parameters are without oid" );
      }
   m_param_enc = type;
   }

/********************************
* EC_PrivateKey                 *
********************************/
void EC_PrivateKey::affirm_init() const // virtual
   {
   EC_PublicKey::affirm_init();
   if (m_private_value == 0)
      {
      throw Invalid_State("cannot use EC_PrivateKey when private key is uninitialized");
      }
   }

/**
* Return the PKCS #8 public key decoder
*/
PKCS8_Decoder* EC_PrivateKey::pkcs8_decoder(RandomNumberGenerator&)
   {
   class EC_Key_Decoder : public PKCS8_Decoder
      {
      public:
         void alg_id ( const AlgorithmIdentifier& alg_id )
            {
            key->mp_dom_pars.reset ( new EC_Domain_Params ( decode_ber_ec_dompar ( alg_id.parameters ) ) );
            }

         void key_bits ( const MemoryRegion<byte>& bits )
            {
            u32bit version;
            SecureVector<byte> octstr_secret;
            BER_Decoder ( bits )
               .start_cons ( SEQUENCE )
               .decode ( version )
               .decode ( octstr_secret, OCTET_STRING )
               .verify_end()
               .end_cons();
            key->m_private_value = BigInt::decode ( octstr_secret, octstr_secret.size() );
            if ( version != 1 )
               throw Decoding_Error ( "Wrong PKCS #1 key format version for EC key" );
            key->PKCS8_load_hook();
            }

         EC_Key_Decoder ( EC_PrivateKey* k ) : key ( k )
            {}
      private:
         EC_PrivateKey* key;
      };

   return new EC_Key_Decoder(this);
   }


void EC_PrivateKey::PKCS8_load_hook ( bool )
   {
   // we cannot use affirm_init() here because mp_public_point might still be null
   if (mp_dom_pars.get() == 0 )
      {
      throw Invalid_State("attempt to set public point for an uninitialized key");
      }
   mp_public_point.reset ( new PointGFp ( m_private_value * mp_dom_pars->get_base_point() ) );
   mp_public_point->check_invariants();

   }

}