Botan 1.10.17
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
19namespace Botan {
20
22 const PointGFp& pub_point) :
23 domain_params(dom_par), public_key(pub_point),
25 {
26 if(domain().get_curve() != public_point().get_curve())
27 throw Invalid_Argument("EC_PublicKey: curve mismatch in constructor");
28 }
29
31 const MemoryRegion<byte>& key_bits)
32 {
35
36 public_key = OS2ECP(key_bits, domain().get_curve());
37 }
38
40 bool) const
41 {
42 return public_point().on_the_curve();
43 }
44
49
54
56 {
57 if(form != EC_DOMPAR_ENC_EXPLICIT &&
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
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*/
82 const EC_Group& ec_group,
83 const BigInt& x)
84 {
85 domain_params = ec_group;
87
88 if(x == 0)
89 private_key = BigInt::random_integer(rng, 1, domain().get_order());
90 else
91 private_key = x;
92
94
96 "ECC private key was not on the curve");
97 }
98
109
111 const MemoryRegion<byte>& key_bits)
112 {
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
123
125 "Loaded ECC private key not on the curve");
126 }
127
128}
#define BOTAN_ASSERT(expr, msg)
Definition assert.h:19
SecureVector< byte > parameters
Definition alg_id.h:36
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:78
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:50
size_t bytes() const
Definition bigint.cpp:246
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
SecureVector< byte > get_contents()
Definition der_enc.cpp:122
DER_Encoder & end_cons()
Definition der_enc.cpp:145
DER_Encoder & encode(bool b)
Definition der_enc.cpp:209
std::string get_oid() const
Definition ec_group.h:115
const PointGFp & get_base_point() const
Definition ec_group.h:95
EC_Group(const CurveGFp &curve, const PointGFp &base_point, const BigInt &order, const BigInt &cofactor)
Definition ec_group.h:42
MemoryVector< byte > pkcs8_private_key() const
Definition ecc_key.cpp:99
const BigInt & private_value() const
Definition ecc_key.cpp:70
const EC_Group & domain() const
Definition ecc_key.h:60
void set_parameter_encoding(EC_Group_Encoding enc)
Definition ecc_key.cpp:55
MemoryVector< byte > x509_subject_public_key() const
Definition ecc_key.cpp:50
EC_Group domain_params
Definition ecc_key.h:84
MemoryVector< byte > DER_domain() const
Definition ecc_key.h:72
EC_Group_Encoding domain_encoding
Definition ecc_key.h:86
PointGFp public_key
Definition ecc_key.h:85
bool check_key(RandomNumberGenerator &rng, bool strong) const
Definition ecc_key.cpp:39
AlgorithmIdentifier algorithm_identifier() const
Definition ecc_key.cpp:45
const PointGFp & public_point() const
Definition ecc_key.h:45
bool on_the_curve() const
virtual OID get_oid() const
Definition pk_keys.cpp:17
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
PointGFp OS2ECP(const byte data[], size_t data_len, const CurveGFp &curve)
@ SEQUENCE
Definition asn1_int.h:35
@ OCTET_STRING
Definition asn1_int.h:31
EC_Group_Encoding
Definition ec_group.h:22
@ EC_DOMPAR_ENC_EXPLICIT
Definition ec_group.h:23
@ EC_DOMPAR_ENC_OID
Definition ec_group.h:25
@ EC_DOMPAR_ENC_IMPLICITCA
Definition ec_group.h:24
SecureVector< byte > EC2OSP(const PointGFp &point, byte format)
Invalid_State(const std::string &err)
Definition exceptn.h:27