Botan  2.6.0
Crypto and TLS for C++11
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 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #include <botan/ecc_key.h>
11 #include <botan/numthry.h>
12 #include <botan/der_enc.h>
13 #include <botan/ber_dec.h>
14 #include <botan/secmem.h>
15 #include <botan/point_gfp.h>
16 #include <botan/workfactor.h>
17 
18 namespace Botan {
19 
21  {
22  return domain().get_p_bits();
23  }
24 
26  {
27  return ecp_work_factor(key_length());
28  }
29 
31  const PointGFp& pub_point) :
32  m_domain_params(dom_par), m_public_key(pub_point)
33  {
34  if (!dom_par.get_curve_oid().empty())
36  else
38 
39 #if 0
40  if(domain().get_curve() != public_point().get_curve())
41  throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
42 #endif
43  }
44 
46  const std::vector<uint8_t>& key_bits) :
47  m_domain_params{EC_Group(alg_id.get_parameters())},
48  m_public_key{domain().OS2ECP(key_bits)}
49  {
50  if (!domain().get_curve_oid().empty())
51  m_domain_encoding = EC_DOMPAR_ENC_OID;
52  else
53  m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
54  }
55 
57  bool) const
58  {
59  return m_domain_params.verify_group(rng) &&
61  }
62 
63 
65  {
67  }
68 
69 std::vector<uint8_t> EC_PublicKey::public_key_bits() const
70  {
72  }
73 
75  {
76  if(enc != PointGFp::COMPRESSED &&
77  enc != PointGFp::UNCOMPRESSED &&
78  enc != PointGFp::HYBRID)
79  throw Invalid_Argument("Invalid point encoding for EC_PublicKey");
80 
81  m_point_encoding = enc;
82  }
83 
85  {
86  if(form != EC_DOMPAR_ENC_EXPLICIT &&
87  form != EC_DOMPAR_ENC_IMPLICITCA &&
88  form != EC_DOMPAR_ENC_OID)
89  throw Invalid_Argument("Invalid encoding form for EC-key object specified");
90 
92  throw Invalid_Argument("Invalid encoding form OID specified for "
93  "EC-key object whose corresponding domain "
94  "parameters are without oid");
95 
96  m_domain_encoding = form;
97  }
98 
100  {
101  if(m_private_key == 0)
102  throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
103 
104  return m_private_key;
105  }
106 
107 /**
108 * EC_PrivateKey constructor
109 */
111  const EC_Group& ec_group,
112  const BigInt& x,
113  bool with_modular_inverse)
114  {
115  m_domain_params = ec_group;
116  if (!ec_group.get_curve_oid().empty())
118  else
120 
121  const BigInt& order = m_domain_params.get_order();
122 
123  if(x == 0)
124  {
125  m_private_key = ec_group.random_scalar(rng);
126  }
127  else
128  {
129  m_private_key = x;
130  }
131 
132  // Can't use rng here because ffi load functions use Null_RNG
133  if(with_modular_inverse)
134  {
135  // ECKCDSA
137  }
138  else
139  {
141  }
142 
144  "Generated public key point was on the curve");
145  }
146 
148  {
149  return DER_Encoder()
151  .encode(static_cast<size_t>(1))
153  OCTET_STRING)
154  .end_cons()
155  .get_contents();
156  }
157 
159  const secure_vector<uint8_t>& key_bits,
160  bool with_modular_inverse)
161  {
164 
165  if (!domain().get_curve_oid().empty())
167  else
169 
170  OID key_parameters;
172 
173  BER_Decoder(key_bits)
175  .decode_and_check<size_t>(1, "Unknown version code for ECC key")
176  .decode_octet_string_bigint(m_private_key)
177  .decode_optional(key_parameters, ASN1_Tag(0), PRIVATE)
179  .end_cons();
180 
181  if(public_key_bits.empty())
182  {
183  if(with_modular_inverse)
184  {
185  // ECKCDSA
186  const BigInt& order = m_domain_params.get_order();
188  }
189  else
190  {
192  }
193 
195  "Public point derived from loaded key was on the curve");
196  }
197  else
198  {
200  // OS2ECP verifies that the point is on the curve
201  }
202  }
203 
204 }
BigInt m_private_key
Definition: ecc_key.h:167
const OID & get_curve_oid() const
Definition: ec_group.cpp:472
const BigInt & private_value() const
Definition: ecc_key.cpp:99
size_t get_p_bits() const
Definition: ec_group.cpp:402
const BigInt & get_order() const
Definition: ec_group.cpp:442
std::vector< uint8_t > encode(PointGFp::Compression_Type format) const
Definition: point_gfp.cpp:587
BER_Decoder & decode_optional_string(std::vector< uint8_t, Alloc > &out, ASN1_Tag real_type, uint16_t type_no, ASN1_Tag class_tag=CONTEXT_SPECIFIC)
Definition: ber_dec.h:179
void set_parameter_encoding(EC_Group_Encoding enc)
Definition: ecc_key.cpp:84
const PointGFp & public_point() const
Definition: ecc_key.h:57
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:163
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:123
size_t ecp_work_factor(size_t bits)
Definition: workfactor.cpp:14
PointGFp m_public_key
Definition: ecc_key.h:115
ASN1_Tag
Definition: asn1_obj.h:22
const CurveGFp & get_curve() const
Definition: ec_group.cpp:392
PointGFp::Compression_Type point_encoding() const
Definition: ecc_key.h:104
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
PointGFp::Compression_Type m_point_encoding
Definition: ecc_key.h:117
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:30
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
BER_Decoder & decode_optional(T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
Definition: ber_dec.h:232
bool verify_public_element(const PointGFp &y) const
Definition: ec_group.cpp:598
virtual OID get_oid() const
Definition: pk_keys.cpp:51
BigInt random_scalar(RandomNumberGenerator &rng) const
Definition: ec_group.cpp:512
const PointGFp & get_base_point() const
Definition: ec_group.cpp:437
BER_Decoder & end_cons()
Definition: ber_dec.cpp:253
const EC_Group & domain() const
Definition: ecc_key.h:72
bool empty() const
Definition: asn1_oid.h:30
PointGFp OS2ECP(const uint8_t bits[], size_t len) const
Definition: ec_group.cpp:477
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:279
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:239
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: ecc_key.cpp:56
Definition: alg_id.cpp:13
size_t bytes() const
Definition: bigint.cpp:208
std::vector< uint8_t > DER_domain() const
Definition: ecc_key.h:90
const std::vector< uint8_t > & get_parameters() const
Definition: alg_id.h:38
bool on_the_curve() const
Definition: point_gfp.cpp:530
EC_Group_Encoding
Definition: ec_group.h:23
std::vector< uint8_t > public_key_bits() const override
Definition: ecc_key.cpp:69
secure_vector< uint8_t > private_key_bits() const override
Definition: ecc_key.cpp:147
AlgorithmIdentifier algorithm_identifier() const override
Definition: ecc_key.cpp:64
size_t key_length() const override
Definition: ecc_key.cpp:20
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
size_t estimated_strength() const override
Definition: ecc_key.cpp:25
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
const CurveGFp & get_curve() const
Definition: point_gfp.h:262
void set_point_encoding(PointGFp::Compression_Type enc)
Definition: ecc_key.cpp:74
EC_Group m_domain_params
Definition: ecc_key.h:114
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
Definition: ec_group.cpp:621
EC_Group_Encoding m_domain_encoding
Definition: ecc_key.h:116