Botan 1.10.17
Botan::EC_Group Class Reference

#include <ec_group.h>

Public Member Functions

SecureVector< byteDER_encode (EC_Group_Encoding form) const
 EC_Group (const CurveGFp &curve, const PointGFp &base_point, const BigInt &order, const BigInt &cofactor)
 EC_Group (const MemoryRegion< byte > &ber_encoding)
 EC_Group (const OID &oid)
 EC_Group (const std::string &pem_or_oid="")
const PointGFpget_base_point () const
const BigIntget_cofactor () const
const CurveGFpget_curve () const
std::string get_oid () const
const BigIntget_order () const
bool initialized () const
bool operator== (const EC_Group &other) const
std::string PEM_encode () const

Detailed Description

Class representing an elliptic curve

Definition at line 31 of file ec_group.h.

Constructor & Destructor Documentation

◆ EC_Group() [1/4]

Botan::EC_Group::EC_Group ( const CurveGFp & curve,
const PointGFp & base_point,
const BigInt & order,
const BigInt & cofactor )
inline

Construct Domain paramers from specified parameters

Parameters
curveelliptic curve
base_pointa base point
orderthe order of the base point
cofactorthe cofactor

Definition at line 42 of file ec_group.h.

45 :
46 curve(curve),
47 base_point(base_point),
48 order(order),
49 cofactor(cofactor),
50 oid("")
51 {}

Referenced by EC_Group(), EC_Group(), EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::EC_PublicKey::EC_PublicKey(), and operator==().

◆ EC_Group() [2/4]

Botan::EC_Group::EC_Group ( const MemoryRegion< byte > & ber_encoding)

Decode a BER encoded ECC domain parameter set

Parameters
ber_encodingthe bytes of the BER encoding

Definition at line 51 of file ec_group.cpp.

52 {
53 BER_Decoder ber(ber_data);
54 BER_Object obj = ber.get_next_object();
55
56 if(obj.type_tag == NULL_TAG)
57 throw Decoding_Error("Cannot handle ImplicitCA ECDSA parameters");
58 else if(obj.type_tag == OBJECT_ID)
59 {
60 OID dom_par_oid;
61 BER_Decoder(ber_data).decode(dom_par_oid);
62 *this = EC_Group(dom_par_oid);
63 }
64 else if(obj.type_tag == SEQUENCE)
65 {
66 BigInt p, a, b;
67 SecureVector<byte> sv_base_point;
68
69 BER_Decoder(ber_data)
70 .start_cons(SEQUENCE)
71 .decode_and_check<size_t>(1, "Unknown ECC param version code")
72 .start_cons(SEQUENCE)
73 .decode_and_check(OID("1.2.840.10045.1.1"),
74 "Only prime ECC fields supported")
75 .decode(p)
76 .end_cons()
77 .start_cons(SEQUENCE)
78 .decode_octet_string_bigint(a)
79 .decode_octet_string_bigint(b)
80 .end_cons()
81 .decode(sv_base_point, OCTET_STRING)
82 .decode(order)
83 .decode(cofactor)
84 .end_cons()
85 .verify_end();
86
87 curve = CurveGFp(p, a, b);
88 base_point = OS2ECP(sv_base_point, curve);
89 }
90 else
91 throw Decoding_Error("Unexpected tag while decoding ECC domain params");
92 }
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
EC_Group(const CurveGFp &curve, const PointGFp &base_point, const BigInt &order, const BigInt &cofactor)
Definition ec_group.h:42
OID(const std::string &str="")
Definition asn1_oid.cpp:19
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
@ NULL_TAG
Definition asn1_int.h:32
@ OBJECT_ID
Definition asn1_int.h:33
Decoding_Error(const std::string &name)
Definition exceptn.h:140

References Botan::BER_Decoder::BER_Decoder(), Botan::CurveGFp::CurveGFp(), Botan::Decoding_Error::Decoding_Error(), EC_Group(), Botan::BER_Decoder::get_next_object(), Botan::NULL_TAG, Botan::OBJECT_ID, Botan::OCTET_STRING, Botan::OID::OID(), Botan::OS2ECP(), Botan::SEQUENCE, and Botan::BER_Object::type_tag.

◆ EC_Group() [3/4]

Botan::EC_Group::EC_Group ( const OID & oid)

Create an EC domain by OID (or throw if unknown)

Parameters
oidthe OID of the EC domain to create

Definition at line 19 of file ec_group.cpp.

20 {
21 std::string pem =
22 global_state().get("ec", OIDS::lookup(domain_oid));
23
24 if(pem == "")
25 throw Lookup_Error("No ECC domain data for " + domain_oid.as_string());
26
27 *this = EC_Group(pem);
28 oid = domain_oid.as_string();
29 }
std::string get(const std::string &section, const std::string &key) const
Definition libstate.cpp:114
std::string lookup(const OID &oid)
Definition oids.cpp:31
Library_State & global_state()
Lookup_Error(const std::string &err)
Definition exceptn.h:37

References Botan::OID::as_string(), EC_Group(), Botan::Library_State::get(), Botan::global_state(), Botan::OIDS::lookup(), and Botan::Lookup_Error::Lookup_Error().

◆ EC_Group() [4/4]

Botan::EC_Group::EC_Group ( const std::string & pem_or_oid = "")

Create an EC domain from PEM encoding (as from PEM_encode), or from an OID name (eg "secp160r1", or "1.3.132.0.8")

Parameters
pem_or_oidPEM-encoded data, or an OID

Definition at line 31 of file ec_group.cpp.

32 {
33 if(str == "")
34 return; // no initialization / uninitialized
35
36 try
37 {
38 DataSource_Memory input(str);
39
40 SecureVector<byte> ber =
41 PEM_Code::decode_check_label(input, "EC PARAMETERS");
42
43 *this = EC_Group(ber);
44 }
45 catch(Decoding_Error) // hmm, not PEM?
46 {
47 *this = EC_Group(OIDS::lookup(str));
48 }
49 }
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition pem.cpp:42

References Botan::PEM_Code::decode_check_label(), EC_Group(), and Botan::OIDS::lookup().

Member Function Documentation

◆ DER_encode()

SecureVector< byte > Botan::EC_Group::DER_encode ( EC_Group_Encoding form) const

Create the DER encoding of this domain

Parameters
formof encoding to use
Returns
bytes encododed as DER

Definition at line 95 of file ec_group.cpp.

96 {
97 if(form == EC_DOMPAR_ENC_EXPLICIT)
98 {
99 const size_t ecpVers1 = 1;
100 OID curve_type("1.2.840.10045.1.1");
101
102 const size_t p_bytes = curve.get_p().bytes();
103
104 return DER_Encoder()
105 .start_cons(SEQUENCE)
106 .encode(ecpVers1)
107 .start_cons(SEQUENCE)
108 .encode(curve_type)
109 .encode(curve.get_p())
110 .end_cons()
111 .start_cons(SEQUENCE)
112 .encode(BigInt::encode_1363(curve.get_a(), p_bytes),
114 .encode(BigInt::encode_1363(curve.get_b(), p_bytes),
116 .end_cons()
117 .encode(EC2OSP(base_point, PointGFp::UNCOMPRESSED), OCTET_STRING)
118 .encode(order)
119 .encode(cofactor)
120 .end_cons()
121 .get_contents();
122 }
123 else if(form == EC_DOMPAR_ENC_OID)
124 return DER_Encoder().encode(OID(get_oid())).get_contents();
125 else if(form == EC_DOMPAR_ENC_IMPLICITCA)
126 return DER_Encoder().encode_null().get_contents();
127 else
128 throw Internal_Error("EC_Group::DER_encode: Unknown encoding");
129 }
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:78
std::string get_oid() const
Definition ec_group.h:115
@ 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)
Internal_Error(const std::string &err)
Definition exceptn.h:47

References Botan::EC2OSP(), Botan::EC_DOMPAR_ENC_EXPLICIT, Botan::EC_DOMPAR_ENC_IMPLICITCA, Botan::EC_DOMPAR_ENC_OID, Botan::DER_Encoder::encode(), Botan::BigInt::encode_1363(), Botan::DER_Encoder::encode_null(), Botan::DER_Encoder::end_cons(), Botan::DER_Encoder::get_contents(), get_oid(), Botan::Internal_Error::Internal_Error(), Botan::OCTET_STRING, Botan::OID::OID(), Botan::SEQUENCE, Botan::DER_Encoder::start_cons(), and Botan::PointGFp::UNCOMPRESSED.

Referenced by PEM_encode().

◆ get_base_point()

const PointGFp & Botan::EC_Group::get_base_point ( ) const
inline

Return domain parameter curve

Returns
domain parameter curve

Definition at line 95 of file ec_group.h.

95{ return base_point; }

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), Botan::EC_PrivateKey::EC_PrivateKey(), and operator==().

◆ get_cofactor()

const BigInt & Botan::EC_Group::get_cofactor ( ) const
inline

Return the cofactor

Returns
the cofactor

Definition at line 107 of file ec_group.h.

107{ return cofactor; }

Referenced by operator==().

◆ get_curve()

const CurveGFp & Botan::EC_Group::get_curve ( ) const
inline

Return domain parameter curve

Returns
domain parameter curve

Definition at line 89 of file ec_group.h.

89{ return curve; }

Referenced by operator==().

◆ get_oid()

std::string Botan::EC_Group::get_oid ( ) const
inline

Return the OID of these domain parameters

Returns
the OID

Definition at line 115 of file ec_group.h.

115{ return oid; }

Referenced by DER_encode(), and Botan::EC_PublicKey::set_parameter_encoding().

◆ get_order()

const BigInt & Botan::EC_Group::get_order ( ) const
inline

Return the order of the base point

Returns
order of the base point

Definition at line 101 of file ec_group.h.

101{ return order; }

Referenced by Botan::ECDH_KA_Operation::ECDH_KA_Operation(), and operator==().

◆ initialized()

bool Botan::EC_Group::initialized ( ) const
inline

Definition at line 109 of file ec_group.h.

109{ return !base_point.is_zero(); }

◆ operator==()

bool Botan::EC_Group::operator== ( const EC_Group & other) const
inline

Definition at line 117 of file ec_group.h.

118 {
119 return ((get_curve() == other.get_curve()) &&
120 (get_base_point() == other.get_base_point()) &&
121 (get_order() == other.get_order()) &&
122 (get_cofactor() == other.get_cofactor()));
123 }
const BigInt & get_cofactor() const
Definition ec_group.h:107
const BigInt & get_order() const
Definition ec_group.h:101
const PointGFp & get_base_point() const
Definition ec_group.h:95
const CurveGFp & get_curve() const
Definition ec_group.h:89

References EC_Group(), get_base_point(), get_cofactor(), get_curve(), and get_order().

◆ PEM_encode()

std::string Botan::EC_Group::PEM_encode ( ) const

Return the PEM encoding (always in explicit form)

Returns
string containing PEM data

Definition at line 131 of file ec_group.cpp.

132 {
133 SecureVector<byte> der = DER_encode(EC_DOMPAR_ENC_EXPLICIT);
134 return PEM_Code::encode(der, "EC PARAMETERS");
135 }
SecureVector< byte > DER_encode(EC_Group_Encoding form) const
Definition ec_group.cpp:95
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19

References DER_encode(), Botan::EC_DOMPAR_ENC_EXPLICIT, and Botan::PEM_Code::encode().


The documentation for this class was generated from the following files: