Botan  2.4.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_curve().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_oid().empty())
36  else
38  if(domain().get_curve() != public_point().get_curve())
39  throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
40  }
41 
43  const std::vector<uint8_t>& key_bits) :
45  m_public_key{OS2ECP(key_bits, domain().get_curve())}
46  {
47  if (!domain().get_oid().empty())
49  else
51  }
52 
54  bool) const
55  {
56  //verify domain parameters
58  {
59  return false;
60  }
61  //check that public point is not at infinity
62  if(public_point().is_zero())
63  {
64  return false;
65  }
66  //check that public point is on the curve
67  if(!public_point().on_the_curve())
68  {
69  return false;
70  }
72  {
74  {
75  return false;
76  }
77  //check that public point has order q
79  {
80  return false;
81  }
82  }
83  return true;
84  }
85 
86 
88  {
90  }
91 
92 std::vector<uint8_t> EC_PublicKey::public_key_bits() const
93  {
95  }
96 
98  {
99  if(form != EC_DOMPAR_ENC_EXPLICIT &&
100  form != EC_DOMPAR_ENC_IMPLICITCA &&
101  form != EC_DOMPAR_ENC_OID)
102  throw Invalid_Argument("Invalid encoding form for EC-key object specified");
103 
104  if((form == EC_DOMPAR_ENC_OID) && (m_domain_params.get_oid() == ""))
105  throw Invalid_Argument("Invalid encoding form OID specified for "
106  "EC-key object whose corresponding domain "
107  "parameters are without oid");
108 
109  m_domain_encoding = form;
110  }
111 
113  {
114  if(m_private_key == 0)
115  throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
116 
117  return m_private_key;
118  }
119 
120 /**
121 * EC_PrivateKey constructor
122 */
124  const EC_Group& ec_group,
125  const BigInt& x,
126  bool with_modular_inverse)
127  {
128  m_domain_params = ec_group;
129  if (!ec_group.get_oid().empty())
131  else
133 
134  if(x == 0)
135  {
136  m_private_key = BigInt::random_integer(rng, 1, domain().get_order());
137  }
138  else
139  {
140  m_private_key = x;
141  }
142 
144  ((with_modular_inverse) ? inverse_mod(m_private_key, m_domain_params.get_order()) : m_private_key);
145 
147  "Generated public key point was on the curve");
148  }
149 
151  {
152  return DER_Encoder()
154  .encode(static_cast<size_t>(1))
155  .encode(BigInt::encode_1363(m_private_key, m_private_key.bytes()),
156  OCTET_STRING)
157  .end_cons()
158  .get_contents();
159  }
160 
162  const secure_vector<uint8_t>& key_bits,
163  bool with_modular_inverse)
164  {
167 
168  if (!domain().get_oid().empty())
169  m_domain_encoding = EC_DOMPAR_ENC_OID;
170  else
171  m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
172 
173  OID key_parameters;
175 
176  BER_Decoder(key_bits)
178  .decode_and_check<size_t>(1, "Unknown version code for ECC key")
179  .decode_octet_string_bigint(m_private_key)
180  .decode_optional(key_parameters, ASN1_Tag(0), PRIVATE)
181  .decode_optional_string(public_key_bits, BIT_STRING, 1, PRIVATE)
182  .end_cons();
183 
184  if(public_key_bits.empty())
185  {
187  ((with_modular_inverse) ? inverse_mod(m_private_key, m_domain_params.get_order()) : m_private_key);
188 
190  "Public point derived from loaded key was on the curve");
191  }
192  else
193  {
194  m_public_key = OS2ECP(public_key_bits, domain().get_curve());
195  // OS2ECP verifies that the point is on the curve
196  }
197  }
198 
199 }
const BigInt & private_value() const
Definition: ecc_key.cpp:112
const PointGFp & get_base_point() const
Definition: ec_group.h:96
size_t bits() const
Definition: bigint.cpp:183
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:97
const PointGFp & public_point() const
Definition: ecc_key.h:57
const BigInt & get_cofactor() const
Definition: ec_group.h:108
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:102
ASN1_Tag
Definition: asn1_obj.h:22
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
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:230
virtual OID get_oid() const
Definition: pk_keys.cpp:51
BER_Decoder & end_cons()
Definition: ber_dec.cpp:265
const EC_Group & domain() const
Definition: ecc_key.h:72
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:277
secure_vector< uint8_t > EC2OSP(const PointGFp &point, uint8_t format)
Definition: point_gfp.cpp:469
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:251
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: ecc_key.cpp:53
Definition: alg_id.cpp:13
const BigInt & get_order() const
Definition: ec_group.h:102
std::vector< uint8_t > DER_domain() const
Definition: ecc_key.h:84
T is_zero(T x)
Definition: ct_utils.h:118
const std::vector< uint8_t > & get_parameters() const
Definition: alg_id.h:38
bool on_the_curve() const
Definition: point_gfp.cpp:413
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:95
EC_Group_Encoding
Definition: ec_group.h:23
std::vector< uint8_t > public_key_bits() const override
Definition: ecc_key.cpp:92
secure_vector< uint8_t > private_key_bits() const override
Definition: ecc_key.cpp:150
AlgorithmIdentifier algorithm_identifier() const override
Definition: ecc_key.cpp:87
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
bool is_zero() const
Definition: point_gfp.h:179
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
const CurveGFp & get_curve() const
Definition: ec_group.h:90
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:161
const BigInt & get_p() const
Definition: curve_gfp.h:91
EC_Group m_domain_params
Definition: ecc_key.h:101
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
Definition: ec_group.cpp:142
std::string get_oid() const
Definition: ec_group.h:116
PointGFp OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
Definition: point_gfp.cpp:543
EC_Group_Encoding m_domain_encoding
Definition: ecc_key.h:103