Botan: src/cert/cvc/ecdsa_sig.cpp Source File

Botan 1.10.17

ecdsa_sig.cpp
Go to the documentation of this file.
1 /*
2 * ECDSA Signature
3 * (C) 2007 Falko Strenzke, FlexSecure GmbH
4 * (C) 2008-2010 Jack Lloyd
5 *
6 * Distributed under the terms of the Botan license
7 */
8
9 #include <botan/ecdsa_sig.h>
10
11 namespace Botan {
12
13 ECDSA_Signature::ECDSA_Signature(const MemoryRegion<byte>& ber)
14 {
15 BER_Decoder(ber)
16 .start_cons(SEQUENCE)
17 .decode(m_r)
18 .decode(m_s)
19 .end_cons()
20 .verify_end();
21 }
22
23 MemoryVector<byte> ECDSA_Signature::DER_encode() const
24 {
25 return DER_Encoder()
26 .start_cons(SEQUENCE)
27 .encode(get_r())
28 .encode(get_s())
29 .end_cons()
30 .get_contents();
31 }
32
33 MemoryVector<byte> ECDSA_Signature::get_concatenation() const
34 {
35 // use the larger
36 const size_t enc_len = m_r > m_s ? m_r.bytes() : m_s.bytes();
37
38 SecureVector<byte> sv_r = BigInt::encode_1363(m_r, enc_len);
39 SecureVector<byte> sv_s = BigInt::encode_1363(m_s, enc_len);
40
41 SecureVector<byte> result(sv_r);
42 result += sv_s;
43 return result;
44 }
45
46 ECDSA_Signature decode_concatenation(const MemoryRegion<byte>& concat)
47 {
48 if(concat.size() % 2 != 0)
49 throw Invalid_Argument("Erroneous length of signature");
50
51 const size_t rs_len = concat.size() / 2;
52
53 BigInt r = BigInt::decode(&concat[0], rs_len);
54 BigInt s = BigInt::decode(&concat[rs_len], rs_len);
55
56 return ECDSA_Signature(r, s);
57 }
58
59 }
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::BigInt
Definition: bigint.h:22
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
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::ECDSA_Signature::DER_encode
MemoryVector< byte > DER_encode() const
Definition: ecdsa_sig.cpp:23
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::ECDSA_Signature::get_concatenation
MemoryVector< byte > get_concatenation() const
Definition: ecdsa_sig.cpp:33
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:252
Botan::ECDSA_Signature::get_s
const BigInt & get_s() const
Definition: ecdsa_sig.h:33
Botan::ECDSA_Signature::ECDSA_Signature
ECDSA_Signature()
Definition: ecdsa_sig.h:26
Botan::SEQUENCE
Definition: asn1_int.h:35
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::MemoryVector< byte >
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::decode_concatenation
ECDSA_Signature decode_concatenation(const MemoryRegion< byte > &concat)
Definition: ecdsa_sig.cpp:46
Botan::ECDSA_Signature::get_r
const BigInt & get_r() const
Definition: ecdsa_sig.h:32
Botan::BER_Decoder::verify_end
BER_Decoder & verify_end()
Definition: ber_dec.cpp:163
Botan::BigInt::decode
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
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::BER_Decoder
Definition: ber_dec.h:19
Botan::ECDSA_Signature
Definition: ecdsa_sig.h:21
Botan::BigInt::encode_1363
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78