Botan  2.1.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 
13 namespace Botan {
14 
15 namespace TLS {
16 
18  {
19  public:
21 
22  virtual void new_read_cipher_state() = 0;
23  virtual void new_write_cipher_state() = 0;
24 
25  virtual uint16_t current_read_epoch() const = 0;
26  virtual uint16_t current_write_epoch() const = 0;
27 
28  virtual uint64_t next_write_sequence(uint16_t) = 0;
29  virtual uint64_t next_read_sequence() = 0;
30 
31  virtual bool already_seen(uint64_t seq) const = 0;
32  virtual void read_accept(uint64_t seq) = 0;
33  };
34 
36  {
37  public:
38  void new_read_cipher_state() override { m_read_seq_no = 0; m_read_epoch += 1; }
39  void new_write_cipher_state() override { m_write_seq_no = 0; m_write_epoch += 1; }
40 
41  uint16_t current_read_epoch() const override { return m_read_epoch; }
42  uint16_t current_write_epoch() const override { return m_write_epoch; }
43 
44  uint64_t next_write_sequence(uint16_t) override { return m_write_seq_no++; }
45  uint64_t next_read_sequence() override { return m_read_seq_no; }
46 
47  bool already_seen(uint64_t) const override { return false; }
48  void read_accept(uint64_t) override { m_read_seq_no++; }
49  private:
50  uint64_t m_write_seq_no = 0;
51  uint64_t m_read_seq_no = 0;
52  uint16_t m_read_epoch = 0;
53  uint16_t m_write_epoch = 0;
54  };
55 
57  {
58  public:
59  Datagram_Sequence_Numbers() { m_write_seqs[0] = 0; }
60 
61  void new_read_cipher_state() override { m_read_epoch += 1; }
62 
63  void new_write_cipher_state() override
64  {
65  m_write_epoch += 1;
66  m_write_seqs[m_write_epoch] = 0;
67  }
68 
69  uint16_t current_read_epoch() const override { return m_read_epoch; }
70  uint16_t current_write_epoch() const override { return m_write_epoch; }
71 
72  uint64_t next_write_sequence(uint16_t epoch) override
73  {
74  auto i = m_write_seqs.find(epoch);
75  BOTAN_ASSERT(i != m_write_seqs.end(), "Found epoch");
76  return (static_cast<uint64_t>(epoch) << 48) | i->second++;
77  }
78 
79  uint64_t next_read_sequence() override
80  {
81  throw Exception("DTLS uses explicit sequence numbers");
82  }
83 
84  bool already_seen(uint64_t sequence) const override
85  {
86  const size_t window_size = sizeof(m_window_bits) * 8;
87 
88  if(sequence > m_window_highest)
89  return false;
90 
91  const uint64_t offset = m_window_highest - sequence;
92 
93  if(offset >= window_size)
94  return true; // really old?
95 
96  return (((m_window_bits >> offset) & 1) == 1);
97  }
98 
99  void read_accept(uint64_t sequence) override
100  {
101  const size_t window_size = sizeof(m_window_bits) * 8;
102 
103  if(sequence > m_window_highest)
104  {
105  const size_t offset = sequence - m_window_highest;
106  m_window_highest += offset;
107 
108  if(offset >= window_size)
109  m_window_bits = 0;
110  else
111  m_window_bits <<= offset;
112 
113  m_window_bits |= 0x01;
114  }
115  else
116  {
117  const uint64_t offset = m_window_highest - sequence;
118  m_window_bits |= (static_cast<uint64_t>(1) << offset);
119  }
120  }
121 
122  private:
123  std::map<uint16_t, uint64_t> m_write_seqs;
124  uint16_t m_write_epoch = 0;
125  uint16_t m_read_epoch = 0;
126  uint64_t m_window_highest = 0;
127  uint64_t m_window_bits = 0;
128  };
129 
130 }
131 
132 }
133 
134 #endif
bool already_seen(uint64_t) const override
virtual bool already_seen(uint64_t seq) const =0
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:27
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