SSP21-CPP
BufferTypes.h
1 
2 #ifndef SSP21_BUFFER_TYPES_H
3 #define SSP21_BUFFER_TYPES_H
4 
5 #include "ssp21/crypto/Constants.h"
6 
7 #include "ssp21/util/SequenceTypes.h"
8 
9 #include "ser4cpp/util/Uncopyable.h"
10 #include "ser4cpp/container/StaticBuffer.h"
11 
12 namespace ssp21
13 {
14  // the BufferType also indirectly defines the length
15  enum class BufferType
16  {
17  empty,
18  x25519_key,
19  ed25519_public_key,
20  ed25519_private_key,
21  sha256,
22  ed25519_signature,
23  symmetric_key
24  };
25 
27  {
28  public:
29 
30  virtual ~BufferBase() {}
31 
32  seq32_t as_seq() const;
33 
34  wseq32_t as_wseq();
35 
36  BufferType get_type() const;
37 
38  void set_type(BufferType key_type);
39 
40  void copy(const BufferBase& other);
41 
42  static uint8_t get_buffer_length(BufferType);
43 
44  protected:
45 
46  BufferBase() = default;
47 
48  private:
49 
50  uint8_t length = 0;
51  BufferType buffer_type = BufferType::empty;
52 
53  protected:
54 
56  };
57 
58  /**
59  A secure key zeros its buffer upon destruction
60  and provides a clear method
61  */
62  class SecureBuffer : public BufferBase
63  {
64  public:
65  virtual ~SecureBuffer();
66 
67  void zero();
68 
69  protected:
70  SecureBuffer() {}
71  };
72 
73  // specialized types that actually get used
74 
75  class PublicKey final : public BufferBase
76  {
77  public:
78 
79  PublicKey() {}
80 
81  PublicKey(const PublicKey& other)
82  {
83  this->copy(other);
84  }
85  };
86 
87  class PrivateKey final : public SecureBuffer
88  {
89  public:
90 
91  PrivateKey() {}
92 
93  PrivateKey(const PrivateKey& other)
94  {
95  this->copy(other);
96  }
97  };
98 
99  class DHOutput final : public SecureBuffer {};
100 
101  class HashOutput : public SecureBuffer {};
102 
103  class DSAOutput final : public SecureBuffer {};
104 
105  class SymmetricKey final : public SecureBuffer {};
106 
107  struct KeyPair final
108  {
109  PublicKey public_key;
110  PrivateKey private_key;
111  };
112 
113  struct SessionKeys final
114  {
115  SymmetricKey rx_key;
116  SymmetricKey tx_key;
117 
118  inline void zero()
119  {
120  this->rx_key.zero();
121  this->tx_key.zero();
122  }
123 
124  inline void copy(const SessionKeys& other)
125  {
126  this->rx_key.copy(other.rx_key);
127  this->tx_key.copy(other.tx_key);
128  }
129 
130  inline bool valid() const
131  {
132  return (rx_key.get_type() == BufferType::symmetric_key) && (tx_key.get_type() == BufferType::symmetric_key);
133  }
134  };
135 
136 
137 }
138 
139 #endif
SSP21-cpp main namespace.
Definition: BufferTypes.h:12