aboutsummaryrefslogtreecommitdiffstats
path: root/src/cert/cvc/ecdsa_sig.h
blob: 15015c76d4f7fcc1240b8cead56935cf6848f169 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* ECDSA
* (C) 2007 Falko Strenzke, FlexSecure GmbH
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#ifndef BOTAN_ECDSA_SIGNATURE_H__
#define BOTAN_ECDSA_SIGNATURE_H__

#include <botan/bigint.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>

namespace Botan {

class BOTAN_DLL ECDSA_Signature
   {
   public:
      friend class ECDSA_Signature_Decoder;

      ECDSA_Signature() {}
      ECDSA_Signature(const BigInt& r, const BigInt& s);
      ECDSA_Signature(ECDSA_Signature const& other);
      ECDSA_Signature const& operator=(ECDSA_Signature const& other);

      const BigInt& get_r() const { return m_r; }
      const BigInt& get_s() const { return m_s; }

      /**
      * return the r||s
      */
      SecureVector<byte> const get_concatenation() const;
   private:
      BigInt m_r;
      BigInt m_s;
   };

/* Equality of ECDSA_Signature */
bool operator==(const ECDSA_Signature& lhs, const ECDSA_Signature& rhs);
inline bool operator!=(const ECDSA_Signature& lhs, const ECDSA_Signature& rhs)
   {
   return !(lhs == rhs);
   }

class BOTAN_DLL ECDSA_Signature_Decoder
   {
   public:
      void signature_bits(const MemoryRegion<byte>& bits)
         {
         BER_Decoder(bits)
            .start_cons(SEQUENCE)
            .decode(m_signature->m_r)
            .decode(m_signature->m_s)
            .verify_end()
            .end_cons();
         }
      ECDSA_Signature_Decoder(ECDSA_Signature* signature) : m_signature(signature)
         {}
   private:
      ECDSA_Signature* m_signature;
   };

class BOTAN_DLL ECDSA_Signature_Encoder
   {
   public:
      MemoryVector<byte> signature_bits() const
         {
         return DER_Encoder()
            .start_cons(SEQUENCE)
            .encode(m_signature->get_r())
            .encode(m_signature->get_s())
            .end_cons()
            .get_contents();
         }
      ECDSA_Signature_Encoder(const ECDSA_Signature* signature) : m_signature(signature)
         {}
   private:
      const ECDSA_Signature* m_signature;
   };

ECDSA_Signature const decode_seq(MemoryRegion<byte> const& seq);
ECDSA_Signature const decode_concatenation(MemoryRegion<byte> const& concatenation);

}

#endif