Botan 3.4.0
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/workfactor.h>
18
19namespace Botan {
20
22 return domain().get_p_bits();
23}
24
28
29namespace {
30
31EC_Group_Encoding default_encoding_for(EC_Group& group) {
32 if(group.get_curve_oid().empty()) {
34 } else {
36 }
37}
38
39} // namespace
40
41EC_PublicKey::EC_PublicKey(const EC_Group& dom_par, const EC_Point& pub_point) :
42 m_domain_params(dom_par), m_public_key(pub_point), m_domain_encoding(default_encoding_for(m_domain_params)) {}
43
44EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
45 m_domain_params{EC_Group(alg_id.parameters())},
46 m_public_key{domain().OS2ECP(key_bits)},
47 m_domain_encoding(default_encoding_for(m_domain_params)) {}
48
52
56
57std::vector<uint8_t> EC_PublicKey::public_key_bits() const {
59}
60
63 throw Invalid_Argument("Invalid point encoding for EC_PublicKey");
64 }
65
66 m_point_encoding = enc;
67}
68
71 throw Invalid_Argument("Cannot used NamedCurve encoding for a curve without an OID");
72 }
73
74 m_domain_encoding = form;
75}
76
78 if(m_private_key == 0) {
79 throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
80 }
81
82 return m_private_key;
83}
84
85/**
86* EC_PrivateKey constructor
87*/
89 const EC_Group& ec_group,
90 const BigInt& x,
91 bool with_modular_inverse) {
92 m_domain_params = ec_group;
93 m_domain_encoding = default_encoding_for(m_domain_params);
94
95 if(x == 0) {
96 m_private_key = ec_group.random_scalar(rng);
97 } else {
98 m_private_key = x;
99 }
100
101 std::vector<BigInt> ws;
102
103 if(with_modular_inverse) {
104 // ECKCDSA
106 } else {
108 }
109
110 BOTAN_ASSERT(m_public_key.on_the_curve(), "Generated public key point was on the curve");
111}
112
116
128
130 std::span<const uint8_t> key_bits,
131 bool with_modular_inverse) {
133 m_domain_encoding = default_encoding_for(m_domain_params);
134
135 OID key_parameters;
137
138 BER_Decoder(key_bits)
140 .decode_and_check<size_t>(1, "Unknown version code for ECC key")
141 .decode_octet_string_bigint(m_private_key)
144 .end_cons();
145
146 if(public_key_bits.empty()) {
147 if(with_modular_inverse) {
148 // ECKCDSA
150 } else {
152 }
153
154 BOTAN_ASSERT(m_public_key.on_the_curve(), "Public point derived from loaded key was on the curve");
155 } else {
157 // OS2ECP verifies that the point is on the curve
158 }
159}
160
161bool EC_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
162 if(m_private_key < 1 || m_private_key >= m_domain_params.get_order()) {
163 return false;
164 }
165
166 return EC_PublicKey::check_key(rng, strong);
167}
168
169const BigInt& EC_PublicKey::get_int_field(std::string_view field) const {
170 if(field == "public_x") {
171 BOTAN_ASSERT_NOMSG(this->public_point().is_affine());
172 return this->public_point().get_x();
173 } else if(field == "public_y") {
174 BOTAN_ASSERT_NOMSG(this->public_point().is_affine());
175 return this->public_point().get_y();
176 } else if(field == "base_x") {
177 return this->domain().get_g_x();
178 } else if(field == "base_y") {
179 return this->domain().get_g_y();
180 } else if(field == "p") {
181 return this->domain().get_p();
182 } else if(field == "a") {
183 return this->domain().get_a();
184 } else if(field == "b") {
185 return this->domain().get_b();
186 } else if(field == "cofactor") {
187 return this->domain().get_cofactor();
188 } else if(field == "order") {
189 return this->domain().get_order();
190 } else {
191 return Public_Key::get_int_field(field);
192 }
193}
194
195const BigInt& EC_PrivateKey::get_int_field(std::string_view field) const {
196 if(field == "x") {
197 return this->private_value();
198 } else {
199 return EC_PublicKey::get_int_field(field);
200 }
201}
202
203} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:457
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 & end_cons()
Definition ber_dec.cpp:295
BER_Decoder start_sequence()
Definition ber_dec.h:113
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:317
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:257
static secure_vector< uint8_t > encode_locked(const BigInt &n)
Definition bigint.h:761
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
size_t bytes() const
Definition bigint.cpp:277
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:132
DER_Encoder & start_explicit_context_specific(uint32_t tag)
Definition der_enc.h:73
DER_Encoder & start_sequence()
Definition der_enc.h:65
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:500
const BigInt & get_a() const
Definition ec_group.cpp:496
const BigInt & get_g_y() const
Definition ec_group.cpp:516
const BigInt & get_cofactor() const
Definition ec_group.cpp:520
bool verify_public_element(const EC_Point &y) const
Definition ec_group.cpp:697
const BigInt & get_p() const
Definition ec_group.cpp:492
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
Definition ec_group.cpp:722
const BigInt & get_order() const
Definition ec_group.cpp:508
size_t get_p_bits() const
Definition ec_group.cpp:476
const EC_Point & get_base_point() const
Definition ec_group.cpp:504
EC_Point blinded_base_point_multiply(const BigInt &k, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
Definition ec_group.cpp:575
const BigInt & get_g_x() const
Definition ec_group.cpp:512
const OID & get_curve_oid() const
Definition ec_group.cpp:544
BigInt inverse_mod_order(const BigInt &x) const
Definition ec_group.cpp:540
EC_Point OS2ECP(const uint8_t bits[], size_t len) const
Definition ec_group.cpp:561
BigInt random_scalar(RandomNumberGenerator &rng) const
Definition ec_group.cpp:592
const BigInt & get_y() const
Definition ec_point.h:147
bool on_the_curve() const
Definition ec_point.cpp:510
const BigInt & get_x() const
Definition ec_point.h:140
std::vector< uint8_t > encode(EC_Point_Format format) const
Definition ec_point.cpp:568
const BigInt & private_value() const
Definition ecc_key.cpp:77
secure_vector< uint8_t > raw_private_key_bits() const final
Definition ecc_key.cpp:113
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:161
const BigInt & get_int_field(std::string_view field) const final
Definition ecc_key.cpp:195
secure_vector< uint8_t > private_key_bits() const final
Definition ecc_key.cpp:117
const EC_Group & domain() const
Definition ecc_key.h:54
std::vector< uint8_t > DER_domain() const
Definition ecc_key.h:72
void set_parameter_encoding(EC_Group_Encoding enc)
Definition ecc_key.cpp:69
EC_Point_Format m_point_encoding
Definition ecc_key.h:111
EC_Group_Encoding m_domain_encoding
Definition ecc_key.h:110
size_t estimated_strength() const override
Definition ecc_key.cpp:25
AlgorithmIdentifier algorithm_identifier() const override
Definition ecc_key.cpp:53
size_t key_length() const override
Definition ecc_key.cpp:21
EC_Group m_domain_params
Definition ecc_key.h:108
void set_point_encoding(EC_Point_Format enc)
Definition ecc_key.cpp:61
const BigInt & get_int_field(std::string_view field) const override
Definition ecc_key.cpp:169
EC_Point_Format point_encoding() const
Definition ecc_key.h:84
EC_Point m_public_key
Definition ecc_key.h:109
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition ecc_key.cpp:49
const EC_Point & public_point() const
Definition ecc_key.h:40
std::vector< uint8_t > public_key_bits() const override
Definition ecc_key.cpp:57
bool empty() const
Definition asn1_obj.h:265
size_t ecp_work_factor(size_t bits)
ASN1_Type
Definition asn1_obj.h:43
EC_Point_Format
Definition ec_point.h:19
EC_Point OS2ECP(const uint8_t data[], size_t data_len, const CurveGFp &curve)
Definition ec_point.cpp:627
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
EC_Group_Encoding
Definition ec_group.h:24