aboutsummaryrefslogtreecommitdiffstats
path: root/src/ec.cpp
blob: 7384f8082d3bc942fa081173087faac910a4ac25 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*************************************************
* ECC Key implemenation                          *
* (C) 2007 Manuel Hartl / FlexSecure GmbH        *
*                                                *
*          Falko Strenzke                        *
*          strenzke@flexsecure.de                *
*************************************************/

#include <botan/ec.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");
      }
   }

/**
* EC_PrivateKey generator
**/
void EC_PrivateKey::generate_private_key(RandomNumberGenerator& rng)
   {
   if (mp_dom_pars.get() == 0)
      {
      throw Invalid_State("cannot generate private key when domain parameters are not set");
      }
   BigInt tmp_private_value(0);
   tmp_private_value = random_integer (rng, 1, mp_dom_pars->get_order() );

   mp_public_point = std::auto_ptr<PointGFp>( new PointGFp (mp_dom_pars->get_base_point()));
   mp_public_point->mult_this_secure(tmp_private_value, mp_dom_pars->get_order(), mp_dom_pars->get_order()-1);
   //assert(mp_public_point.get() != 0);
   tmp_private_value.swap(m_private_value);
   }

/**
* Return the PKCS #8 public key encoder
**/
PKCS8_Encoder* EC_PrivateKey::pkcs8_encoder() const
   {
   class EC_Key_Encoder : public PKCS8_Encoder
      {
      public:
         AlgorithmIdentifier alg_id() const
            {
            key->affirm_init();
            SecureVector<byte> params = encode_der_ec_dompar ( * ( key->mp_dom_pars ), ENC_EXPLICIT );
            return AlgorithmIdentifier ( key->get_oid(),
                                         params );
            }

         MemoryVector<byte> key_bits() const
            {
            key->affirm_init();
            SecureVector<byte> octstr_secret = BigInt::encode_1363 ( key->m_private_value, key->m_private_value.bytes() );

            return DER_Encoder()
               .start_cons ( SEQUENCE )
               .encode ( BigInt ( 1 ) )
               .encode ( octstr_secret, OCTET_STRING )
               .end_cons()
               .get_contents();
            }

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

   return new EC_Key_Encoder(this);
   }

/**
* 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();

   }




/*************************************************
* ECDSA_PublicKey                                *
*************************************************/
void ECDSA_PublicKey::affirm_init() const // virtual
   {
   EC_PublicKey::affirm_init();
   }
void ECDSA_PublicKey::set_domain_parameters(EC_Domain_Params const& dom_pars)
   {
   if (mp_dom_pars.get())
      {
      // they are already set, we must ensure that they are equal to the arg
      if (dom_pars != *mp_dom_pars.get())
         {
         throw Invalid_Argument("EC_PublicKey::set_domain_parameters(): domain parameters are already set, and they are different from the argument");
         }
      else
         {
         // they are equal, so nothing to do
         return;
         }
      }
   // set them ...
   if (m_enc_public_point.size() == 0)
      {
      throw Invalid_State("EC_PublicKey::set_domain_parameters(): encoded public point isn´t set");
      }

   // now try to decode the public key ...
   PointGFp tmp_pp(OS2ECP(m_enc_public_point, dom_pars.get_curve()));
   try
      {
      tmp_pp.check_invariants();
      }
   catch(Illegal_Point e)
      {
      throw Invalid_State("EC_PublicKey::set_domain_parameters(): point does not lie on provided curve");
      }
   std::auto_ptr<EC_Domain_Params> p_tmp_pars(new EC_Domain_Params(dom_pars));
   ECDSA_Core tmp_ecdsa_core( *p_tmp_pars, BigInt ( 0 ), tmp_pp );
   mp_public_point.reset(new PointGFp(tmp_pp));
   m_ecdsa_core = tmp_ecdsa_core;
   mp_dom_pars = p_tmp_pars;
   }

void ECDSA_PublicKey::set_all_values ( ECDSA_PublicKey const& other )
   {
   m_param_enc = other.m_param_enc;
   m_ecdsa_core = other.m_ecdsa_core;
   m_enc_public_point = other.m_enc_public_point;
   if ( other.mp_dom_pars.get() )
      {
      mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) );
      }
   if ( other.mp_public_point.get() )
      {
      mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) );
      }
   }
ECDSA_PublicKey::ECDSA_PublicKey ( ECDSA_PublicKey const& other )
   : Public_Key(),
     EC_PublicKey(),
     PK_Verifying_wo_MR_Key()
   {
   set_all_values ( other );
   }
ECDSA_PublicKey const& ECDSA_PublicKey::operator= ( ECDSA_PublicKey const& rhs )
   {
   set_all_values ( rhs );
   return *this;
   }
bool ECDSA_PublicKey::verify ( const byte message[], u32bit mess_len, const byte signature [], u32bit sig_len ) const
   {
   affirm_init();
   ECDSA_Signature sig;
   std::auto_ptr<ECDSA_Signature_Decoder> dec(sig.x509_decoder());
   SecureVector<byte> sv_sig;
   sv_sig.set ( signature, sig_len );
   dec->signature_bits ( sv_sig );
   SecureVector<byte> sv_plain_sig = sig.get_concatenation();
   return m_ecdsa_core.verify ( sv_plain_sig, sv_plain_sig.size(), message, mess_len );
   }
ECDSA_PublicKey::ECDSA_PublicKey ( EC_Domain_Params const& dom_par, PointGFp const& public_point )
   {
   mp_dom_pars = std::auto_ptr<EC_Domain_Params> ( new EC_Domain_Params ( dom_par ) );
   mp_public_point = std::auto_ptr<PointGFp> ( new PointGFp ( public_point ) );
   m_param_enc = ENC_EXPLICIT;
   m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point );
   }
void ECDSA_PublicKey::X509_load_hook()
   {
   EC_PublicKey::X509_load_hook();
   EC_PublicKey::affirm_init();
   m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point );
   }
u32bit ECDSA_PublicKey::max_input_bits() const
   {
   if(!mp_dom_pars.get())
      {
      throw Invalid_State("ECDSA_PublicKey::max_input_bits(): domain parameters not set");
      }
   return mp_dom_pars->get_order().bits();
   }


/*************************
* ECDSA_PrivateKey       *
*************************/

void ECDSA_PrivateKey::affirm_init() const // virtual
   {
   EC_PrivateKey::affirm_init();
   }

void ECDSA_PrivateKey::PKCS8_load_hook ( bool generated )
   {
   EC_PrivateKey::PKCS8_load_hook ( generated );
   EC_PrivateKey::affirm_init();
   m_ecdsa_core = ECDSA_Core ( *mp_dom_pars, m_private_value, *mp_public_point );
   }


void ECDSA_PrivateKey::set_all_values ( ECDSA_PrivateKey const& other )
   {
   m_private_value = other.m_private_value;
   m_param_enc = other.m_param_enc;
   m_ecdsa_core = other.m_ecdsa_core;
   m_enc_public_point = other.m_enc_public_point;
   if ( other.mp_dom_pars.get() )
      {
      mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) );
      }
   if ( other.mp_public_point.get() )
      {
      mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) );
      }
   }

ECDSA_PrivateKey::ECDSA_PrivateKey(ECDSA_PrivateKey const& other)
   : Public_Key(),
     EC_PublicKey(),
     Private_Key(),
     ECDSA_PublicKey(),
     EC_PrivateKey(),
     PK_Signing_Key()
   {
   set_all_values(other);
   }
ECDSA_PrivateKey const& ECDSA_PrivateKey::operator= (ECDSA_PrivateKey const& rhs)
   {
   set_all_values(rhs);
   return *this;
   }
SecureVector<byte> ECDSA_PrivateKey::sign ( const byte message [], u32bit mess_len ) const
   {
   affirm_init();
   SecureVector<byte> sv_sig = m_ecdsa_core.sign ( message, mess_len );
   //code which der encodes the signature returned
   ECDSA_Signature sig = decode_concatenation( sv_sig );
   std::auto_ptr<ECDSA_Signature_Encoder> enc(sig.x509_encoder());
   return enc->signature_bits();

   }



/*********************************
* ECKAEG_PublicKey               *
*********************************/

void ECKAEG_PublicKey::affirm_init() const // virtual
   {
   EC_PublicKey::affirm_init();
   }

void ECKAEG_PublicKey::set_all_values ( ECKAEG_PublicKey const& other )
   {
   m_param_enc = other.m_param_enc;
   m_eckaeg_core = other.m_eckaeg_core;
   m_enc_public_point = other.m_enc_public_point;
   if ( other.mp_dom_pars.get() )
      {
      mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) );
      }
   if ( other.mp_public_point.get() )
      {
      mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) );
      }
   }
ECKAEG_PublicKey::ECKAEG_PublicKey ( ECKAEG_PublicKey const& other )
   : Public_Key(),
     EC_PublicKey()
   {
   set_all_values ( other );
   }
ECKAEG_PublicKey const& ECKAEG_PublicKey::operator= ( ECKAEG_PublicKey const& rhs )
   {
   set_all_values ( rhs );
   return *this;
   }

void ECKAEG_PublicKey::X509_load_hook()
   {
   EC_PublicKey::X509_load_hook();
   EC_PublicKey::affirm_init();
   m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point );
   }
ECKAEG_PublicKey::ECKAEG_PublicKey ( EC_Domain_Params const& dom_par, PointGFp const& public_point )
   {

   mp_dom_pars = std::auto_ptr<EC_Domain_Params> ( new EC_Domain_Params ( dom_par ) );
   mp_public_point = std::auto_ptr<PointGFp> ( new PointGFp ( public_point ) );
   if(mp_public_point->get_curve() != mp_dom_pars->get_curve())
      {
      throw Invalid_Argument("ECKAEG_PublicKey(): curve of arg. point and curve of arg. domain parameters are different");
      }
   EC_PublicKey::affirm_init();
   m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, BigInt ( 0 ), *mp_public_point );
   }


/*********************************
* ECKAEG_PrivateKey            *
*********************************/
void ECKAEG_PrivateKey::affirm_init() const // virtual
   {
   EC_PrivateKey::affirm_init();
   }
void ECKAEG_PrivateKey::PKCS8_load_hook ( bool generated )
   {
   EC_PrivateKey::PKCS8_load_hook ( generated );
   EC_PrivateKey::affirm_init();
   m_eckaeg_core = ECKAEG_Core ( *mp_dom_pars, m_private_value, *mp_public_point );
   }
void ECKAEG_PrivateKey::set_all_values ( ECKAEG_PrivateKey const& other )
   {
   m_private_value = other.m_private_value;
   m_param_enc = other.m_param_enc;
   m_eckaeg_core = other.m_eckaeg_core;
   m_enc_public_point = other.m_enc_public_point;
   if ( other.mp_dom_pars.get() )
      {
      mp_dom_pars.reset ( new EC_Domain_Params ( * ( other.mp_dom_pars ) ) );
      }
   if ( other.mp_public_point.get() )
      {
      mp_public_point.reset ( new PointGFp ( * ( other.mp_public_point ) ) );
      }
   }

ECKAEG_PrivateKey::ECKAEG_PrivateKey(ECKAEG_PrivateKey const& other)
   : Public_Key(),
     EC_PublicKey(),
     Private_Key(),
     ECKAEG_PublicKey(),
     EC_PrivateKey(),
     PK_Key_Agreement_Key()

   {
   set_all_values(other);
   }
ECKAEG_PrivateKey const& ECKAEG_PrivateKey::operator= (ECKAEG_PrivateKey const& rhs)
   {
   set_all_values(rhs);
   return *this;
   }

/**
* Derive a key
*/
SecureVector<byte> ECKAEG_PrivateKey::derive_key(const Public_Key& key) const
   {
   affirm_init();

   const EC_PublicKey * p_ec_pk = dynamic_cast<const EC_PublicKey*>(&key);
   if(!p_ec_pk)
      {
      throw Invalid_Argument("ECKAEG_PrivateKey::derive_key(): argument must be an EC_PublicKey");
      }
   p_ec_pk->affirm_init();
   return m_eckaeg_core.agree ( p_ec_pk->get_public_point() );
   }

}