Botan  1.10.9
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 
14  {
15  BER_Decoder(ber)
17  .decode(m_r)
18  .decode(m_s)
19  .end_cons()
20  .verify_end();
21  }
22 
24  {
25  return DER_Encoder()
27  .encode(get_r())
28  .encode(get_s())
29  .end_cons()
30  .get_contents();
31  }
32 
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 
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 }
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
const BigInt & get_s() const
Definition: ecdsa_sig.h:33
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
const BigInt & get_r() const
Definition: ecdsa_sig.h:32
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:232
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
BER_Decoder & end_cons()
Definition: ber_dec.cpp:246
MemoryVector< byte > DER_encode() const
Definition: ecdsa_sig.cpp:23
MemoryVector< byte > get_concatenation() const
Definition: ecdsa_sig.cpp:33
size_t size() const
Definition: secmem.h:29
ECDSA_Signature decode_concatenation(const MemoryRegion< byte > &concat)
Definition: ecdsa_sig.cpp:46
BER_Decoder & verify_end()
Definition: ber_dec.cpp:160
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
BigInt r
Definition: numthry.cpp:26
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
size_t s
Definition: numthry.cpp:27
size_t bytes() const
Definition: bigint.cpp:245