Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
17 class Connection_Sequence_Numbers
18 {
19 public:
20 virtual ~Connection_Sequence_Numbers() {}
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
35 class Stream_Sequence_Numbers final : public Connection_Sequence_Numbers
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
56 class Datagram_Sequence_Numbers final : public Connection_Sequence_Numbers
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
Botan::Exception
Definition: exceptn.h:21
Botan::TLS::Stream_Sequence_Numbers::already_seen
bool already_seen(uint64_t) const override
Definition: tls_seq_numbers.h:47
Botan::TLS::Connection_Sequence_Numbers::already_seen
virtual bool already_seen(uint64_t seq) const =0
Botan::TLS::Connection_Sequence_Numbers::next_read_sequence
virtual uint64_t next_read_sequence()=0
Botan::TLS::Stream_Sequence_Numbers::read_accept
void read_accept(uint64_t) override
Definition: tls_seq_numbers.h:48
Botan::TLS::Datagram_Sequence_Numbers::already_seen
bool already_seen(uint64_t sequence) const override
Definition: tls_seq_numbers.h:84
Botan::TLS::Datagram_Sequence_Numbers::current_write_epoch
uint16_t current_write_epoch() const override
Definition: tls_seq_numbers.h:70
Botan::TLS::Datagram_Sequence_Numbers::read_accept
void read_accept(uint64_t sequence) override
Definition: tls_seq_numbers.h:99
Botan::TLS::Datagram_Sequence_Numbers::new_write_cipher_state
void new_write_cipher_state() override
Definition: tls_seq_numbers.h:63
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::TLS::Stream_Sequence_Numbers
Definition: tls_seq_numbers.h:35
Botan::TLS::Connection_Sequence_Numbers::~Connection_Sequence_Numbers
virtual ~Connection_Sequence_Numbers()
Definition: tls_seq_numbers.h:20
Botan::TLS::Connection_Sequence_Numbers::new_read_cipher_state
virtual void new_read_cipher_state()=0
Botan::TLS::Datagram_Sequence_Numbers
Definition: tls_seq_numbers.h:56
Botan::TLS::Stream_Sequence_Numbers::new_write_cipher_state
void new_write_cipher_state() override
Definition: tls_seq_numbers.h:39
Botan::TLS::Stream_Sequence_Numbers::next_write_sequence
uint64_t next_write_sequence(uint16_t) override
Definition: tls_seq_numbers.h:44
Botan::TLS::Stream_Sequence_Numbers::current_read_epoch
uint16_t current_read_epoch() const override
Definition: tls_seq_numbers.h:41
Botan::TLS::Stream_Sequence_Numbers::current_write_epoch
uint16_t current_write_epoch() const override
Definition: tls_seq_numbers.h:42
Botan::TLS::Datagram_Sequence_Numbers::next_read_sequence
uint64_t next_read_sequence() override
Definition: tls_seq_numbers.h:79
Botan
Definition: alg_id.cpp:13
Botan::TLS::Stream_Sequence_Numbers::new_read_cipher_state
void new_read_cipher_state() override
Definition: tls_seq_numbers.h:38
Botan::TLS::Connection_Sequence_Numbers
Definition: tls_seq_numbers.h:17
Botan::TLS::Connection_Sequence_Numbers::next_write_sequence
virtual uint64_t next_write_sequence(uint16_t)=0
Botan::TLS::Datagram_Sequence_Numbers::current_read_epoch
uint16_t current_read_epoch() const override
Definition: tls_seq_numbers.h:69
Botan::TLS::Datagram_Sequence_Numbers::next_write_sequence
uint64_t next_write_sequence(uint16_t epoch) override
Definition: tls_seq_numbers.h:72
Botan::TLS::Connection_Sequence_Numbers::current_write_epoch
virtual uint16_t current_write_epoch() const =0
Botan::TLS::Datagram_Sequence_Numbers::new_read_cipher_state
void new_read_cipher_state() override
Definition: tls_seq_numbers.h:61
Botan::TLS::Connection_Sequence_Numbers::read_accept
virtual void read_accept(uint64_t seq)=0
Botan::TLS::Stream_Sequence_Numbers::next_read_sequence
uint64_t next_read_sequence() override
Definition: tls_seq_numbers.h:45
Botan::TLS::Connection_Sequence_Numbers::current_read_epoch
virtual uint16_t current_read_epoch() const =0
Botan::TLS::Connection_Sequence_Numbers::new_write_cipher_state
virtual void new_write_cipher_state()=0
Botan::TLS::Datagram_Sequence_Numbers::Datagram_Sequence_Numbers
Datagram_Sequence_Numbers()
Definition: tls_seq_numbers.h:59