Botan: src/pubkey/ecc_key/ecc_key.cpp Source File

Botan 1.10.16

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 * Distributed under the terms of the Botan license
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/internal/assert.h>
18
19 namespace Botan {
20
21 EC_PublicKey::EC_PublicKey(const EC_Group& dom_par,
22 const PointGFp& pub_point) :
23 domain_params(dom_par), public_key(pub_point),
24 domain_encoding(EC_DOMPAR_ENC_EXPLICIT)
25 {
26 if(domain().get_curve() != public_point().get_curve())
27 throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
28 }
29
30 EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id,
31 const MemoryRegion<byte>& key_bits)
32 {
33 domain_params = EC_Group(alg_id.parameters);
34 domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
35
36 public_key = OS2ECP(key_bits, domain().get_curve());
37 }
38
39 bool EC_PublicKey::check_key(RandomNumberGenerator&,
40 bool) const
41 {
42 return public_point().on_the_curve();
43 }
44
45 AlgorithmIdentifier EC_PublicKey::algorithm_identifier() const
46 {
47 return AlgorithmIdentifier(get_oid(), DER_domain());
48 }
49
50 MemoryVector<byte> EC_PublicKey::x509_subject_public_key() const
51 {
52 return EC2OSP(public_point(), PointGFp::COMPRESSED);
53 }
54
55 void EC_PublicKey::set_parameter_encoding(EC_Group_Encoding form)
56 {
57 if(form != EC_DOMPAR_ENC_EXPLICIT &&
58 form != EC_DOMPAR_ENC_IMPLICITCA &&
59 form != EC_DOMPAR_ENC_OID)
60 throw Invalid_Argument("Invalid encoding form for EC-key object specified");
61
62 if((form == EC_DOMPAR_ENC_OID) && (domain_params.get_oid() == ""))
63 throw Invalid_Argument("Invalid encoding form OID specified for "
64 "EC-key object whose corresponding domain "
65 "parameters are without oid");
66
67 domain_encoding = form;
68 }
69
70 const BigInt& EC_PrivateKey::private_value() const
71 {
72 if(private_key == 0)
73 throw Invalid_State("EC_PrivateKey::private_value - uninitialized");
74
75 return private_key;
76 }
77
78 /**
79 * EC_PrivateKey constructor
80 */
81 EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng,
82 const EC_Group& ec_group,
83 const BigInt& x)
84 {
85 domain_params = ec_group;
86 domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
87
88 if(x == 0)
89 private_key = BigInt::random_integer(rng, 1, domain().get_order());
90 else
91 private_key = x;
92
93 public_key = domain().get_base_point() * private_key;
94
95 BOTAN_ASSERT(public_key.on_the_curve(),
96 "ECC private key was not on the curve");
97 }
98
99 MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const
100 {
101 return DER_Encoder()
102 .start_cons(SEQUENCE)
103 .encode(static_cast<size_t>(1))
104 .encode(BigInt::encode_1363(private_key, private_key.bytes()),
105 OCTET_STRING)
106 .end_cons()
107 .get_contents();
108 }
109
110 EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id,
111 const MemoryRegion<byte>& key_bits)
112 {
113 domain_params = EC_Group(alg_id.parameters);
114 domain_encoding = EC_DOMPAR_ENC_EXPLICIT;
115
116 BER_Decoder(key_bits)
117 .start_cons(SEQUENCE)
118 .decode_and_check<size_t>(1, "Unknown version code for ECC key")
119 .decode_octet_string_bigint(private_key);
120 // possibly group params and public key follow
121
122 public_key = domain().get_base_point() * private_key;
123
124 BOTAN_ASSERT(public_key.on_the_curve(),
125 "Loaded ECC private key not on the curve");
126 }
127
128 }
Botan::PointGFp::COMPRESSED
Definition: point_gfp.h:46
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::EC_PrivateKey::private_value
const BigInt & private_value() const
Definition: ecc_key.cpp:70
Botan::EC_PrivateKey::pkcs8_private_key
MemoryVector< byte > pkcs8_private_key() const
Definition: ecc_key.cpp:99
Botan::EC_Group::get_base_point
const PointGFp & get_base_point() const
Definition: ec_group.h:95
Botan::BigInt
Definition: bigint.h:22
Botan::EC_DOMPAR_ENC_EXPLICIT
Definition: ec_group.h:23
Botan::PointGFp
Definition: point_gfp.h:41
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::EC_PublicKey::set_parameter_encoding
void set_parameter_encoding(EC_Group_Encoding enc)
Definition: ecc_key.cpp:55
Botan::EC_PublicKey::public_point
const PointGFp & public_point() const
Definition: ecc_key.h:45
Botan::BER_Decoder::decode_and_check
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
Botan::OS2ECP
PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp &curve)
Definition: point_gfp.cpp:562
Botan::EC_PublicKey::public_key
PointGFp public_key
Definition: ecc_key.h:85
Botan::Invalid_State
Definition: exceptn.h:25
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::AlgorithmIdentifier::parameters
SecureVector< byte > parameters
Definition: alg_id.h:36
Botan::BigInt::random_integer
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:50
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:238
Botan::EC_PublicKey::algorithm_identifier
AlgorithmIdentifier algorithm_identifier() const
Definition: ecc_key.cpp:45
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, msg)
Definition: assert.h:19
Botan::EC2OSP
SecureVector< byte > EC2OSP(const PointGFp &point, byte format)
Definition: point_gfp.cpp:490
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::EC_PublicKey::check_key
bool check_key(RandomNumberGenerator &rng, bool strong) const
Definition: ecc_key.cpp:39
Botan::OCTET_STRING
Definition: asn1_int.h:31
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:17
Botan::EC_DOMPAR_ENC_IMPLICITCA
Definition: ec_group.h:24
Botan::EC_PublicKey::domain
const EC_Group & domain() const
Definition: ecc_key.h:60
Botan::EC_PublicKey::DER_domain
MemoryVector< byte > DER_domain() const
Definition: ecc_key.h:72
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::EC_DOMPAR_ENC_OID
Definition: ec_group.h:25
Botan::EC_Group
Definition: ec_group.h:31
Botan::PointGFp::on_the_curve
bool on_the_curve() const
Definition: point_gfp.cpp:427
Botan::EC_PublicKey::EC_PublicKey
EC_PublicKey()
Definition: ecc_key.h:82
Botan::EC_Group_Encoding
EC_Group_Encoding
Definition: ec_group.h:22
Botan::EC_PublicKey::x509_subject_public_key
MemoryVector< byte > x509_subject_public_key() const
Definition: ecc_key.cpp:50
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:135
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::EC_PublicKey::domain_encoding
EC_Group_Encoding domain_encoding
Definition: ecc_key.h:86
Botan::EC_PrivateKey::EC_PrivateKey
EC_PrivateKey()
Definition: ecc_key.h:111
Botan::BigInt::encode_1363
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
Botan::EC_Group::get_oid
std::string get_oid() const
Definition: ec_group.h:115
Botan::EC_PublicKey::domain_params
EC_Group domain_params
Definition: ecc_key.h:84