Botan  1.10.9
ecc_key.cpp
Go to the documentation of this file.
1 /*
2 * ECC Key implemenation
3 * (C) 2007 Manuel Hartl, FlexSecure GmbH
4 * Falko Strenzke, FlexSecure GmbH
5 * 2008-2010 Jack Lloyd
6 *
7 * Distributed under the terms of the Botan license
8 */
9 
10 #include <botan/ecc_key.h>
11 #include <botan/x509_key.h>
12 #include <botan/numthry.h>
13 #include <botan/der_enc.h>
14 #include <botan/ber_dec.h>
15 #include <botan/secmem.h>
16 #include <botan/point_gfp.h>
17 #include <botan/internal/assert.h>
18 
19 namespace Botan {
20 
22  const PointGFp& pub_point) :
23  domain_params(dom_par), public_key(pub_point),
24  domain_encoding(EC_DOMPAR_ENC_EXPLICIT)
25  {
26  if(domain().get_curve() != public_point().get_curve())
27  throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
28  }
29 
31  const MemoryRegion<byte>& key_bits)
32  {
35 
36  public_key = OS2ECP(key_bits, domain().get_curve());
37  }
38 
40  bool) const
41  {
42  return public_point().on_the_curve();
43  }
44 
46  {
48  }
49 
51  {
53  }
54 
56  {
57  if(form != EC_DOMPAR_ENC_EXPLICIT &&
58  form != EC_DOMPAR_ENC_IMPLICITCA &&
59  form != EC_DOMPAR_ENC_OID)
60  throw Invalid_Argument("Invalid encoding form for EC-key object specified");
61 
62  if((form == EC_DOMPAR_ENC_OID) && (domain_params.get_oid() == ""))
63  throw Invalid_Argument("Invalid encoding form OID specified for "
64  "EC-key object whose corresponding domain "
65  "parameters are without oid");
66 
67  domain_encoding = form;
68  }
69 
71  {
72  if(private_key == 0)
73  throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
74 
75  return private_key;
76  }
77 
78 /**
79 * EC_PrivateKey constructor
80 */
82  const EC_Group& ec_group,
83  const BigInt& x)
84  {
85  domain_params = ec_group;
87 
88  if(x == 0)
89  private_key = BigInt::random_integer(rng, 1, domain().get_order());
90  else
91  private_key = x;
92 
94 
96  "ECC private key was not on the curve");
97  }
98 
100  {
101  return DER_Encoder()
103  .encode(static_cast<size_t>(1))
105  OCTET_STRING)
106  .end_cons()
107  .get_contents();
108  }
109 
111  const MemoryRegion<byte>& key_bits)
112  {
115 
116  BER_Decoder(key_bits)
118  .decode_and_check<size_t>(1, "Unknown version code for ECC key")
119  .decode_octet_string_bigint(private_key);
120  // possibly group params and public key follow
121 
123 
125  "Loaded ECC private key not on the curve");
126  }
127 
128 }
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
std::string get_oid() const
Definition: ec_group.h:115
MemoryVector< byte > DER_domain() const
Definition: ecc_key.h:72
const EC_Group & domain() const
Definition: ecc_key.h:60
void set_parameter_encoding(EC_Group_Encoding enc)
Definition: ecc_key.cpp:55
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp &curve)
Definition: point_gfp.cpp:554
MemoryVector< byte > pkcs8_private_key() const
Definition: ecc_key.cpp:99
const PointGFp & get_base_point() const
Definition: ec_group.h:95
PointGFp public_key
Definition: ecc_key.h:85
bool check_key(RandomNumberGenerator &rng, bool strong) const
Definition: ecc_key.cpp:39
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
SecureVector< byte > parameters
Definition: alg_id.h:36
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:50
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:232
#define BOTAN_ASSERT(expr, msg)
Definition: assert.h:19
virtual OID get_oid() const
Definition: pk_keys.cpp:17
const PointGFp & public_point() const
Definition: ecc_key.h:45
SecureVector< byte > EC2OSP(const PointGFp &point, byte format)
Definition: point_gfp.cpp:482
const BigInt & private_value() const
Definition: ecc_key.cpp:70
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
EC_Group_Encoding
Definition: ec_group.h:22
bool on_the_curve() const
Definition: point_gfp.cpp:419
AlgorithmIdentifier algorithm_identifier() const
Definition: ecc_key.cpp:45
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
EC_Group_Encoding domain_encoding
Definition: ecc_key.h:86
MemoryVector< byte > x509_subject_public_key() const
Definition: ecc_key.cpp:50
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
EC_Group domain_params
Definition: ecc_key.h:84
size_t bytes() const
Definition: bigint.cpp:245