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

Botan 1.10.12

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

ec_group.cpp
Go to the documentation of this file.
1 /*
2 * ECC Domain Parameters
3 *
4 * (C) 2007 Falko Strenzke, FlexSecure GmbH
5 * 2008 Jack Lloyd
6 *
7 * Distributed under the terms of the Botan license
8 */
9
10 #include <botan/ec_group.h>
11 #include <botan/ber_dec.h>
12 #include <botan/der_enc.h>
13 #include <botan/libstate.h>
14 #include <botan/oids.h>
15 #include <botan/pem.h>
16
17 namespace Botan {
18
19 EC_Group::EC_Group(const OID& domain_oid)
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 }
30
31 EC_Group::EC_Group(const std::string& str)
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 }
50
51 EC_Group::EC_Group(const MemoryRegion<byte>& ber_data)
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 }
93
94 SecureVector<byte>
95 EC_Group::DER_encode(EC_Group_Encoding form) const
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),
113 OCTET_STRING)
114 .encode(BigInt::encode_1363(curve.get_b(), p_bytes),
115 OCTET_STRING)
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 }
130
131 std::string EC_Group::PEM_encode() const
132 {
133 SecureVector<byte> der = DER_encode(EC_DOMPAR_ENC_EXPLICIT);
134 return PEM_Code::encode(der, "EC PARAMETERS");
135 }
136
137 }
Botan::CurveGFp::get_a
const BigInt & get_a() const
Definition: curve_gfp.h:53
Botan::OBJECT_ID
Definition: asn1_int.h:33
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::EC_Group::get_oid
std::string get_oid() const
Definition: ec_group.h:115
Botan::PointGFp::UNCOMPRESSED
Definition: point_gfp.h:45
Botan::BigInt
Definition: bigint.h:22
Botan::EC_DOMPAR_ENC_EXPLICIT
Definition: ec_group.h:23
Botan::CurveGFp::get_b
const BigInt & get_b() const
Definition: curve_gfp.h:58
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:341
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::Internal_Error
Definition: exceptn.h:45
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:235
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::OCTET_STRING
Definition: asn1_int.h:31
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::BER_Decoder::decode_octet_string_bigint
BER_Decoder & decode_octet_string_bigint(class BigInt &)
Definition: ber_dec.cpp:362
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:249
Botan::EC_DOMPAR_ENC_IMPLICITCA
Definition: ec_group.h:24
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:31
Botan::EC_Group::PEM_encode
std::string PEM_encode() const
Definition: ec_group.cpp:131
Botan::DER_Encoder::encode_null
DER_Encoder & encode_null()
Definition: der_enc.cpp:201
Botan::EC_Group::DER_encode
SecureVector< byte > DER_encode(EC_Group_Encoding form) const
Definition: ec_group.cpp:95
Botan::Lookup_Error
Definition: exceptn.h:35
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::EC_Group::EC_Group
EC_Group(const CurveGFp &curve, const PointGFp &base_point, const BigInt &order, const BigInt &cofactor)
Definition: ec_group.h:42
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::EC_DOMPAR_ENC_OID
Definition: ec_group.h:25
Botan::NULL_TAG
Definition: asn1_int.h:32
Botan::BER_Decoder::get_next_object
BER_Object get_next_object()
Definition: ber_dec.cpp:193
Botan::EC_Group_Encoding
EC_Group_Encoding
Definition: ec_group.h:22
Botan::PEM_Code::encode
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
Botan::CurveGFp::get_p
const BigInt & get_p() const
Definition: curve_gfp.h:64
Botan::BER_Object::type_tag
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:160
Botan::DataSource_Memory
Definition: data_src.h:94
Botan::OID::as_string
std::string as_string() const
Definition: asn1_oid.cpp:50
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Botan::SecureVector< byte >
Botan::OID
Definition: asn1_oid.h:20
Botan::BER_Decoder
Definition: ber_dec.h:19
Botan::BER_Object
Definition: asn1_int.h:77
Botan::BigInt::encode_1363
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
Botan::PEM_Code::decode_check_label
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Botan::Library_State::get
std::string get(const std::string &section, const std::string &key) const
Definition: libstate.cpp:114
Botan::CurveGFp
Definition: curve_gfp.h:20
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:245