Botan  2.8.0
Crypto and TLS for C++11
tls_seq_numbers.h
Go to the documentation of this file.
1 /*
2 * TLS Sequence Number Handling
3 * (C) 2012 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_TLS_SEQ_NUMBERS_H_
9 #define BOTAN_TLS_SEQ_NUMBERS_H_
10 
11 #include <botan/types.h>
12 #include <map>
13 
14 namespace Botan {
15 
16 namespace TLS {
17 
19  {
20  public:
21  virtual ~Connection_Sequence_Numbers() = default;
22 
23  virtual void new_read_cipher_state() = 0;
24  virtual void new_write_cipher_state() = 0;
25 
26  virtual uint16_t current_read_epoch() const = 0;
27  virtual uint16_t current_write_epoch() const = 0;
28 
29  virtual uint64_t next_write_sequence(uint16_t) = 0;
30  virtual uint64_t next_read_sequence() = 0;
31 
32  virtual bool already_seen(uint64_t seq) const = 0;
33  virtual void read_accept(uint64_t seq) = 0;
34  };
35 
37  {
38  public:
39  void new_read_cipher_state() override { m_read_seq_no = 0; m_read_epoch++; }
40  void new_write_cipher_state() override { m_write_seq_no = 0; m_write_epoch++; }
41 
42  uint16_t current_read_epoch() const override { return m_read_epoch; }
43  uint16_t current_write_epoch() const override { return m_write_epoch; }
44 
45  uint64_t next_write_sequence(uint16_t) override { return m_write_seq_no++; }
46  uint64_t next_read_sequence() override { return m_read_seq_no; }
47 
48  bool already_seen(uint64_t) const override { return false; }
49  void read_accept(uint64_t) override { m_read_seq_no++; }
50  private:
51  uint64_t m_write_seq_no = 0;
52  uint64_t m_read_seq_no = 0;
53  uint16_t m_read_epoch = 0;
54  uint16_t m_write_epoch = 0;
55  };
56 
58  {
59  public:
60  Datagram_Sequence_Numbers() { m_write_seqs[0] = 0; }
61 
62  void new_read_cipher_state() override { m_read_epoch++; }
63 
64  void new_write_cipher_state() override
65  {
66  m_write_epoch++;
67  m_write_seqs[m_write_epoch] = 0;
68  }
69 
70  uint16_t current_read_epoch() const override { return m_read_epoch; }
71  uint16_t current_write_epoch() const override { return m_write_epoch; }
72 
73  uint64_t next_write_sequence(uint16_t epoch) override
74  {
75  auto i = m_write_seqs.find(epoch);
76  BOTAN_ASSERT(i != m_write_seqs.end(), "Found epoch");
77  return (static_cast<uint64_t>(epoch) << 48) | i->second++;
78  }
79 
80  uint64_t next_read_sequence() override
81  {
82  throw Exception("DTLS uses explicit sequence numbers");
83  }
84 
85  bool already_seen(uint64_t sequence) const override
86  {
87  const size_t window_size = sizeof(m_window_bits) * 8;
88 
89  if(sequence > m_window_highest)
90  return false;
91 
92  const uint64_t offset = m_window_highest - sequence;
93 
94  if(offset >= window_size)
95  return true; // really old?
96 
97  return (((m_window_bits >> offset) & 1) == 1);
98  }
99 
100  void read_accept(uint64_t sequence) override
101  {
102  const size_t window_size = sizeof(m_window_bits) * 8;
103 
104  if(sequence > m_window_highest)
105  {
106  const uint64_t offset = sequence - m_window_highest;
107  m_window_highest += offset;
108 
109  if(offset >= window_size)
110  m_window_bits = 0;
111  else
112  m_window_bits <<= offset;
113 
114  m_window_bits |= 0x01;
115  }
116  else
117  {
118  const uint64_t offset = m_window_highest - sequence;
119  m_window_bits |= (static_cast<uint64_t>(1) << offset);
120  }
121  }
122 
123  private:
124  std::map<uint16_t, uint64_t> m_write_seqs;
125  uint16_t m_write_epoch = 0;
126  uint16_t m_read_epoch = 0;
127  uint64_t m_window_highest = 0;
128  uint64_t m_window_bits = 0;
129  };
130 
131 }
132 
133 }
134 
135 #endif
bool already_seen(uint64_t) const override
virtual bool already_seen(uint64_t seq) const =0
int(* final)(unsigned char *, CTX *)
void read_accept(uint64_t) override
bool already_seen(uint64_t sequence) const override
uint16_t current_write_epoch() const override
void read_accept(uint64_t sequence) override
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
virtual ~Connection_Sequence_Numbers()=default
uint64_t next_write_sequence(uint16_t) override
uint16_t current_read_epoch() const override
uint16_t current_write_epoch() const override
Definition: alg_id.cpp:13
virtual uint64_t next_write_sequence(uint16_t)=0
uint16_t current_read_epoch() const override
uint64_t next_write_sequence(uint16_t epoch) override
virtual uint16_t current_write_epoch() const =0
virtual void read_accept(uint64_t seq)=0
uint64_t next_read_sequence() override
virtual uint16_t current_read_epoch() const =0