Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/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/workfactor.h>
18
19 namespace Botan {
20
21 size_t EC_PublicKey::key_length() const
22 {
23 return domain().get_curve().get_p().bits();
24 }
25
26 size_t EC_PublicKey::estimated_strength() const
27 {
28 return ecp_work_factor(key_length());
29 }
30
31 EC_PublicKey::EC_PublicKey(const EC_Group& dom_par,
32 const PointGFp& pub_point) :
33 m_domain_params(dom_par), m_public_key(pub_point),
34 m_domain_encoding(EC_DOMPAR_ENC_EXPLICIT)
35 {
36 if(domain().get_curve() != public_point().get_curve())
37 throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
38 }
39
40 EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id,
41 const std::vector<uint8_t>& key_bits) :
42 m_domain_params{EC_Group(alg_id.parameters)},
43 m_public_key{OS2ECP(key_bits, domain().get_curve())},
44 m_domain_encoding{EC_DOMPAR_ENC_EXPLICIT}
45 {}
46
47 bool EC_PublicKey::check_key(RandomNumberGenerator& rng,
48 bool) const
49 {
50 //verify domain parameters
51 if(!m_domain_params.verify_group(rng))
52 {
53 return false;
54 }
55 //check that public point is not at infinity
56 if(public_point().is_zero())
57 {
58 return false;
59 }
60 //check that public point is on the curve
61 if(!public_point().on_the_curve())
62 {
63 return false;
64 }
65 if(m_domain_params.get_cofactor() > 1)
66 {
67 if((public_point() * m_domain_params.get_cofactor()).is_zero())
68 {
69 return false;
70 }
71 //check that public point has order q
72 if(!(public_point() * m_domain_params.get_order()).is_zero())
73 {
74 return false;
75 }
76 }
77 return true;
78 }
79
80
81 AlgorithmIdentifier EC_PublicKey::algorithm_identifier() const
82 {
83 return AlgorithmIdentifier(get_oid(), DER_domain());
84 }
85
86 std::vector<uint8_t> EC_PublicKey::public_key_bits() const
87 {
88 return unlock(EC2OSP(public_point(), PointGFp::COMPRESSED));
89 }
90
91 void EC_PublicKey::set_parameter_encoding(EC_Group_Encoding form)
92 {
93 if(form != EC_DOMPAR_ENC_EXPLICIT &&
94 form != EC_DOMPAR_ENC_IMPLICITCA &&
95 form != EC_DOMPAR_ENC_OID)
96 throw Invalid_Argument("Invalid encoding form for EC-key object specified");
97
98 if((form == EC_DOMPAR_ENC_OID) && (m_domain_params.get_oid() == ""))
99 throw Invalid_Argument("Invalid encoding form OID specified for "
100 "EC-key object whose corresponding domain "
101 "parameters are without oid");
102
103 m_domain_encoding = form;
104 }
105
106 const BigInt& EC_PrivateKey::private_value() const
107 {
108 if(m_private_key == 0)
109 throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
110
111 return m_private_key;
112 }
113
114 /**
115 * EC_PrivateKey constructor
116 */
117 EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
118 const EC_Group& ec_group,
119 const BigInt& x,
120 bool with_modular_inverse)
121 {
122 m_domain_params = ec_group;
123 m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
124
125 if(x == 0)
126 {
127 m_private_key = BigInt::random_integer(rng, 1, domain().get_order());
128 }
129 else
130 {
131 m_private_key = x;
132 }
133
134 m_public_key = domain().get_base_point() *
135 ((with_modular_inverse) ? inverse_mod(m_private_key, m_domain_params.get_order()) : m_private_key);
136
137 BOTAN_ASSERT(m_public_key.on_the_curve(),
138 "Generated public key point was on the curve");
139 }
140
141 secure_vector<uint8_t> EC_PrivateKey::private_key_bits() const
142 {
143 return DER_Encoder()
144 .start_cons(SEQUENCE)
145 .encode(static_cast<size_t>(1))
146 .encode(BigInt::encode_1363(m_private_key, m_private_key.bytes()),
147 OCTET_STRING)
148 .end_cons()
149 .get_contents();
150 }
151
152 EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id,
153 const secure_vector<uint8_t>& key_bits,
154 bool with_modular_inverse)
155 {
156 m_domain_params = EC_Group(alg_id.parameters);
157 m_domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
158
159 OID key_parameters;
160 secure_vector<uint8_t> public_key_bits;
161
162 BER_Decoder(key_bits)
163 .start_cons(SEQUENCE)
164 .decode_and_check<size_t>(1, "Unknown version code for ECC key")
165 .decode_octet_string_bigint(m_private_key)
166 .decode_optional(key_parameters, ASN1_Tag(0), PRIVATE)
167 .decode_optional_string(public_key_bits, BIT_STRING, 1, PRIVATE)
168 .end_cons();
169
170 if(!key_parameters.empty() && key_parameters != alg_id.oid)
171 throw Decoding_Error("EC_PrivateKey - inner and outer OIDs did not match");
172
173 if(public_key_bits.empty())
174 {
175 m_public_key = domain().get_base_point() *
176 ((with_modular_inverse) ? inverse_mod(m_private_key, m_domain_params.get_order()) : m_private_key);
177
178 BOTAN_ASSERT(m_public_key.on_the_curve(),
179 "Public point derived from loaded key was on the curve");
180 }
181 else
182 {
183 m_public_key = OS2ECP(public_key_bits, domain().get_curve());
184 // OS2ECP verifies that the point is on the curve
185 }
186 }
187
188 }
Botan::PointGFp::COMPRESSED
Definition: point_gfp.h:46
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::EC_PrivateKey::m_private_key
BigInt m_private_key
Definition: ecc_key.h:154
Botan::EC_Group::get_oid
std::string get_oid() const
Definition: ec_group.h:115
Botan::BigInt
Definition: bigint.h:23
Botan::EC_DOMPAR_ENC_EXPLICIT
Definition: ec_group.h:23
Botan::EC_PrivateKey::EC_PrivateKey
EC_PrivateKey()=default
Botan::EC_PublicKey::domain
const EC_Group & domain() const
Definition: ecc_key.h:73
Botan::PointGFp
Definition: point_gfp.h:41
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::EC2OSP
secure_vector< uint8_t > EC2OSP(const PointGFp &point, uint8_t format)
Definition: point_gfp.cpp:470
Botan::AlgorithmIdentifier::parameters
std::vector< uint8_t > parameters
Definition: alg_id.h:39
Botan::BER_Decoder::decode_optional_string
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:162
Botan::EC_PublicKey::set_parameter_encoding
void set_parameter_encoding(EC_Group_Encoding enc)
Definition: ecc_key.cpp:91
Botan::BER_Decoder::decode_and_check
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:146
Botan::EC_Group::get_base_point
const PointGFp & get_base_point() const
Definition: ec_group.h:95
Botan::DER_Encoder::get_contents
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:124
Botan::Invalid_State
Definition: exceptn.h:61
Botan::EC_PublicKey::m_public_key
PointGFp m_public_key
Definition: ecc_key.h:103
Botan::ecp_work_factor
size_t ecp_work_factor(size_t bits)
Definition: workfactor.cpp:14
Botan::BigInt::random_integer
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:30
Botan::EC_PublicKey::public_point
const PointGFp & public_point() const
Definition: ecc_key.h:58
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::EC_PrivateKey::private_value
const BigInt & private_value() const
Definition: ecc_key.cpp:106
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
Botan::BER_Decoder::decode_optional
BER_Decoder & decode_optional(T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
Definition: ber_dec.h:213
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::OCTET_STRING
Definition: asn1_obj.h:35
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::EC_Group::get_curve
const CurveGFp & get_curve() const
Definition: ec_group.h:89
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::PRIVATE
Definition: asn1_obj.h:29
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:272
Botan::EC_DOMPAR_ENC_IMPLICITCA
Definition: ec_group.h:24
Botan::EC_PublicKey::DER_domain
std::vector< uint8_t > DER_domain() const
Definition: ecc_key.h:85
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_obj.h:22
Botan::OS2ECP
PointGFp OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
Definition: point_gfp.cpp:544
Botan::AlgorithmIdentifier::oid
OID oid
Definition: alg_id.h:36
Botan::Decoding_Error
Definition: exceptn.h:189
Botan::SEQUENCE
Definition: asn1_obj.h:39
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:258
Botan::EC_Group::get_order
const BigInt & get_order() const
Definition: ec_group.h:101
Botan::EC_PublicKey::check_key
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition: ecc_key.cpp:47
Botan
Definition: alg_id.cpp:13
Botan::EC_DOMPAR_ENC_OID
Definition: ec_group.h:25
Botan::OID::empty
bool empty() const
Definition: asn1_oid.h:30
Botan::EC_Group
Definition: ec_group.h:31
Botan::CT::is_zero
T is_zero(T x)
Definition: ct_utils.h:110
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
Botan::EC_PublicKey::EC_PublicKey
EC_PublicKey()
Definition: ecc_key.h:99
Botan::EC_Group_Encoding
EC_Group_Encoding
Definition: ec_group.h:22
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:276
Botan::CurveGFp::get_p
const BigInt & get_p() const
Definition: curve_gfp.h:91
Botan::EC_PublicKey::public_key_bits
std::vector< uint8_t > public_key_bits() const override
Definition: ecc_key.cpp:86
Botan::BIT_STRING
Definition: asn1_obj.h:34
Botan::EC_PrivateKey::private_key_bits
secure_vector< uint8_t > private_key_bits() const override
Definition: ecc_key.cpp:141
Botan::EC_PublicKey::algorithm_identifier
AlgorithmIdentifier algorithm_identifier() const override
Definition: ecc_key.cpp:81
Botan::EC_PublicKey::key_length
size_t key_length() const override
Definition: ecc_key.cpp:21
Botan::PointGFp::on_the_curve
bool on_the_curve() const
Definition: point_gfp.cpp:414
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
Botan::BigInt::encode_1363
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:82
Botan::PointGFp::is_zero
bool is_zero() const
Definition: point_gfp.h:177
Botan::EC_Group::verify_group
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
Definition: ec_group.cpp:134
Botan::EC_PublicKey::estimated_strength
size_t estimated_strength() const override
Definition: ecc_key.cpp:26
Botan::OID
Definition: asn1_oid.h:20
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::EC_PublicKey::m_domain_params
EC_Group m_domain_params
Definition: ecc_key.h:102
Botan::EC_PublicKey::m_domain_encoding
EC_Group_Encoding m_domain_encoding
Definition: ecc_key.h:104
Botan::EC_Group::get_cofactor
const BigInt & get_cofactor() const
Definition: ec_group.h:107
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:176