aboutsummaryrefslogtreecommitdiffstats
path: root/src/cert/cvc/ecdsa_sig.cpp
blob: e003bb3696699ced16dc7ba40c66bfe5c926510a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* ECDSA Signature
* (C) 2007 Falko Strenzke, FlexSecure GmbH
* (C) 2008-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/ecdsa_sig.h>

namespace Botan {

ECDSA_Signature::ECDSA_Signature(const MemoryRegion<byte>& ber)
   {
   BER_Decoder(ber)
      .start_cons(SEQUENCE)
         .decode(m_r)
         .decode(m_s)
      .end_cons()
      .verify_end();
   }

MemoryVector<byte> ECDSA_Signature::DER_encode() const
   {
   return DER_Encoder()
      .start_cons(SEQUENCE)
        .encode(get_r())
        .encode(get_s())
      .end_cons()
      .get_contents();
   }

MemoryVector<byte> ECDSA_Signature::get_concatenation() const
   {
   u32bit enc_len = m_r > m_s ? m_r.bytes() : m_s.bytes(); // use the larger

   SecureVector<byte> sv_r = BigInt::encode_1363(m_r, enc_len);
   SecureVector<byte> sv_s = BigInt::encode_1363(m_s, enc_len);

   SecureVector<byte> result(sv_r);
   result.append(sv_s);
   return result;
   }

ECDSA_Signature decode_concatenation(const MemoryRegion<byte>& concat)
   {
   if(concat.size() % 2 != 0)
      throw Invalid_Argument("Erroneous length of signature");

   u32bit rs_len = concat.size()/2;
   SecureVector<byte> sv_r;
   SecureVector<byte> sv_s;
   sv_r.set(concat.begin(), rs_len);
   sv_s.set(&concat[rs_len], rs_len);
   BigInt r = BigInt::decode(sv_r, sv_r.size());
   BigInt s = BigInt::decode(sv_s, sv_s.size());
   return ECDSA_Signature(r, s);
   }

}