Botan 3.6.1
Crypto and TLS for C&
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
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
14#include <botan/ec_point.h>
15#include <botan/numthry.h>
16#include <botan/secmem.h>
17#include <botan/internal/ec_key_data.h>
18#include <botan/internal/fmt.h>
19#include <botan/internal/workfactor.h>
20
21namespace Botan {
22
24 return domain().get_p_bits();
25}
26
30
31namespace {
32
33EC_Group_Encoding default_encoding_for(const EC_Group& group) {
34 if(group.get_curve_oid().empty()) {
36 } else {
38 }
39}
40
41} // namespace
42
44 auto pt = EC_AffinePoint(group, pub_point);
45 m_public_key = std::make_shared<const EC_PublicKey_Data>(std::move(group), std::move(pt));
46 m_domain_encoding = default_encoding_for(domain());
47}
48
50 m_public_key = std::make_shared<const EC_PublicKey_Data>(std::move(group), std::move(pub_point));
51 m_domain_encoding = default_encoding_for(domain());
52}
53
54EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
55 m_public_key = std::make_shared<const EC_PublicKey_Data>(EC_Group(alg_id.parameters()), key_bits);
56 m_domain_encoding = default_encoding_for(domain());
57}
58
61 return m_public_key->group();
62}
63
66 return m_public_key->legacy_point();
67}
68
71 return m_public_key->public_key();
72}
73
74bool EC_PublicKey::check_key(RandomNumberGenerator& rng, bool /*strong*/) const {
76}
77
81
82std::vector<uint8_t> EC_PublicKey::raw_public_key_bits() const {
84}
85
86std::vector<uint8_t> EC_PublicKey::public_key_bits() const {
87 return raw_public_key_bits();
88}
89
90std::vector<uint8_t> EC_PublicKey::DER_domain() const {
92}
93
96 throw Invalid_Argument("Invalid point encoding for EC_PublicKey");
97 }
98
99 m_point_encoding = enc;
100}
101
103 if(form == EC_Group_Encoding::NamedCurve && domain().get_curve_oid().empty()) {
104 throw Invalid_Argument("Cannot used NamedCurve encoding for a curve without an OID");
105 }
106
107 m_domain_encoding = form;
108}
109
112 return m_private_key->legacy_bigint();
113}
114
117 return m_private_key->private_key();
118}
119
120/**
121* EC_PrivateKey constructor
122*/
124 EC_Group ec_group,
125 const BigInt& x,
126 bool with_modular_inverse) {
127 if(x == 0) {
128 m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), rng);
129 } else {
130 m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), x);
131 }
132
133 m_public_key = m_private_key->public_key(rng, with_modular_inverse);
134 m_domain_encoding = default_encoding_for(domain());
135}
136
137EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, EC_Group ec_group, bool with_modular_inverse) {
138 m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), rng);
139 m_public_key = m_private_key->public_key(rng, with_modular_inverse);
140 m_domain_encoding = default_encoding_for(domain());
141}
142
143EC_PrivateKey::EC_PrivateKey(EC_Group ec_group, EC_Scalar x, bool with_modular_inverse) {
144 m_private_key = std::make_shared<EC_PrivateKey_Data>(std::move(ec_group), std::move(x));
145 m_public_key = m_private_key->public_key(with_modular_inverse);
146 m_domain_encoding = default_encoding_for(domain());
147}
148
153
155 BOTAN_STATE_CHECK(m_private_key != nullptr && m_public_key != nullptr);
156
157 return DER_Encoder()
159 .encode(static_cast<size_t>(1))
162 .encode(m_public_key->public_key().serialize_uncompressed(), ASN1_Type::BitString)
163 .end_cons()
164 .end_cons()
165 .get_contents();
166}
167
169 std::span<const uint8_t> key_bits,
170 bool with_modular_inverse) {
171 EC_Group group(alg_id.parameters());
172
173 OID key_parameters;
176
177 BER_Decoder(key_bits)
179 .decode_and_check<size_t>(1, "Unknown version code for ECC key")
181 .decode_optional(key_parameters, ASN1_Type(0), ASN1_Class::ExplicitContextSpecific)
183 .end_cons();
184
185 m_private_key = std::make_shared<EC_PrivateKey_Data>(group, private_key_bits);
186
187 if(public_key_bits.empty()) {
188 m_public_key = m_private_key->public_key(with_modular_inverse);
189 } else {
190 m_public_key = std::make_shared<EC_PublicKey_Data>(group, public_key_bits);
191 }
192
193 m_domain_encoding = default_encoding_for(domain());
194}
195
196bool EC_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
197 if(!m_private_key) {
198 return false;
199 }
200
201 return EC_PublicKey::check_key(rng, strong);
202}
203
204const BigInt& EC_PublicKey::get_int_field(std::string_view field) const {
205 if(field == "public_x" || field == "public_y") {
206 throw Not_Implemented(fmt("EC_PublicKey::get_int_field no longer implements getter for {}", field));
207 } else if(field == "base_x") {
208 return this->domain().get_g_x();
209 } else if(field == "base_y") {
210 return this->domain().get_g_y();
211 } else if(field == "p") {
212 return this->domain().get_p();
213 } else if(field == "a") {
214 return this->domain().get_a();
215 } else if(field == "b") {
216 return this->domain().get_b();
217 } else if(field == "cofactor") {
218 return this->domain().get_cofactor();
219 } else if(field == "order") {
220 return this->domain().get_order();
221 } else {
222 return Public_Key::get_int_field(field);
223 }
224}
225
226const BigInt& EC_PrivateKey::get_int_field(std::string_view field) const {
227 if(field == "x") {
228 return this->private_value();
229 } else {
230 return EC_PublicKey::get_int_field(field);
231 }
232}
233
234} // namespace Botan
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:466
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:18
virtual OID object_identifier() const
Definition pk_keys.cpp:22
BER_Decoder start_sequence()
Definition ber_dec.h:123
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:272
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:132
DER_Encoder & start_explicit_context_specific(uint32_t tag)
Definition der_enc.h:72
DER_Encoder & start_sequence()
Definition der_enc.h:64
DER_Encoder & end_cons()
Definition der_enc.cpp:171
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250
const BigInt & get_b() const
Definition ec_group.cpp:442
const BigInt & get_a() const
Definition ec_group.cpp:438
const BigInt & get_g_y() const
Definition ec_group.cpp:462
const BigInt & get_cofactor() const
Definition ec_group.cpp:466
bool verify_public_element(const EC_Point &y) const
Definition ec_group.cpp:544
const BigInt & get_p() const
Definition ec_group.cpp:434
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
Definition ec_group.cpp:569
const BigInt & get_order() const
Definition ec_group.cpp:454
size_t get_p_bits() const
Definition ec_group.cpp:418
const BigInt & get_g_x() const
Definition ec_group.cpp:458
const OID & get_curve_oid() const
Definition ec_group.cpp:478
std::vector< uint8_t > DER_encode(EC_Group_Encoding form) const
Definition ec_group.cpp:496
std::vector< uint8_t > encode(EC_Point_Format format) const
Definition ec_point.cpp:589
const EC_Scalar & _private_key() const
Definition ecc_key.cpp:115
const BigInt & private_value() const
Definition ecc_key.cpp:110
secure_vector< uint8_t > raw_private_key_bits() const final
Definition ecc_key.cpp:149
std::shared_ptr< const EC_PrivateKey_Data > m_private_key
Definition ecc_key.h:212
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:196
const BigInt & get_int_field(std::string_view field) const final
Definition ecc_key.cpp:226
secure_vector< uint8_t > private_key_bits() const final
Definition ecc_key.cpp:154
const EC_Group & domain() const
Definition ecc_key.cpp:59
std::vector< uint8_t > DER_domain() const
Definition ecc_key.cpp:90
void set_parameter_encoding(EC_Group_Encoding enc)
Definition ecc_key.cpp:102
EC_Point_Format m_point_encoding
Definition ecc_key.h:133
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:132
const EC_AffinePoint & _public_key() const
Definition ecc_key.cpp:69
size_t estimated_strength() const override
Definition ecc_key.cpp:27
AlgorithmIdentifier algorithm_identifier() const override
Definition ecc_key.cpp:78
size_t key_length() const override
Definition ecc_key.cpp:23
EC_Group_Encoding domain_format() const
Definition ecc_key.h:90
std::shared_ptr< const EC_PublicKey_Data > m_public_key
Definition ecc_key.h:131
std::vector< uint8_t > raw_public_key_bits() const override
Definition ecc_key.cpp:82
void set_point_encoding(EC_Point_Format enc)
Definition ecc_key.cpp:94
const BigInt & get_int_field(std::string_view field) const override
Definition ecc_key.cpp:204
EC_PublicKey()=default
EC_Point_Format point_encoding() const
Definition ecc_key.h:96
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:74
const EC_Point & public_point() const
Definition ecc_key.cpp:64
std::vector< uint8_t > public_key_bits() const override
Definition ecc_key.cpp:86
bool empty() const
Definition asn1_obj.h:266
size_t ecp_work_factor(size_t bits)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:44
EC_Point_Format
Definition ec_point.h:19
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
EC_Group_Encoding
Definition ec_group.h:26