Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

cipher_filter.cpp
Go to the documentation of this file.
1 /*
2 * Filter interface for Cipher_Modes
3 * (C) 2013,2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/cipher_filter.h>
9 #include <botan/internal/rounding.h>
10
11 namespace Botan {
12
13 namespace {
14
15 size_t choose_update_size(size_t update_granularity)
16 {
17 const size_t target_size = 1024;
18
19 if(update_granularity >= target_size)
20 return update_granularity;
21
22 return round_up(target_size, update_granularity);
23 }
24
25 }
26
27 Cipher_Mode_Filter::Cipher_Mode_Filter(Cipher_Mode* mode) :
28 Buffered_Filter(choose_update_size(mode->update_granularity()),
29 mode->minimum_final_size()),
30 m_nonce(mode->default_nonce_length() == 0),
31 m_mode(mode),
32 m_buffer(m_mode->update_granularity())
33 {
34 }
35
36 std::string Cipher_Mode_Filter::name() const
37 {
38 return m_mode->name();
39 }
40
41 void Cipher_Mode_Filter::Nonce_State::update(const InitializationVector& iv)
42 {
43 m_nonce = unlock(iv.bits_of());
44 m_fresh_nonce = true;
45 }
46
47 std::vector<uint8_t> Cipher_Mode_Filter::Nonce_State::get()
48 {
49 BOTAN_ASSERT(m_fresh_nonce, "The nonce is fresh for this message");
50
51 if(!m_nonce.empty())
52 m_fresh_nonce = false;
53 return m_nonce;
54 }
55
56 void Cipher_Mode_Filter::set_iv(const InitializationVector& iv)
57 {
58 m_nonce.update(iv);
59 }
60
61 void Cipher_Mode_Filter::set_key(const SymmetricKey& key)
62 {
63 m_mode->set_key(key);
64 }
65
66 Key_Length_Specification Cipher_Mode_Filter::key_spec() const
67 {
68 return m_mode->key_spec();
69 }
70
71 bool Cipher_Mode_Filter::valid_iv_length(size_t length) const
72 {
73 return m_mode->valid_nonce_length(length);
74 }
75
76 void Cipher_Mode_Filter::write(const uint8_t input[], size_t input_length)
77 {
78 Buffered_Filter::write(input, input_length);
79 }
80
81 void Cipher_Mode_Filter::end_msg()
82 {
83 Buffered_Filter::end_msg();
84 }
85
86 void Cipher_Mode_Filter::start_msg()
87 {
88 m_mode->start(m_nonce.get());
89 }
90
91 void Cipher_Mode_Filter::buffered_block(const uint8_t input[], size_t input_length)
92 {
93 while(input_length)
94 {
95 const size_t take = std::min(m_mode->update_granularity(), input_length);
96
97 m_buffer.assign(input, input + take);
98 m_mode->update(m_buffer);
99
100 send(m_buffer);
101
102 input += take;
103 input_length -= take;
104 }
105 }
106
107 void Cipher_Mode_Filter::buffered_final(const uint8_t input[], size_t input_length)
108 {
109 secure_vector<uint8_t> buf(input, input + input_length);
110 m_mode->finish(buf);
111 send(buf);
112 }
113
114 }
Botan::OctetString::bits_of
secure_vector< uint8_t > bits_of() const
Definition: symkey.h:31
Botan::Buffered_Filter
Definition: buf_filt.h:19
Botan::OctetString
Definition: symkey.h:19
Botan::Cipher_Mode_Filter::set_iv
void set_iv(const InitializationVector &iv) override
Definition: cipher_filter.cpp:56
Botan::Cipher_Mode_Filter::valid_iv_length
bool valid_iv_length(size_t length) const override
Definition: cipher_filter.cpp:71
Botan::Filter::send
virtual void send(const uint8_t in[], size_t length)
Definition: filter.cpp:27
Botan::Key_Length_Specification
Definition: key_spec.h:18
Botan::Buffered_Filter::end_msg
void end_msg()
Definition: buf_filt.cpp:82
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::Cipher_Mode_Filter::Cipher_Mode_Filter
Cipher_Mode_Filter(Cipher_Mode *t)
Definition: cipher_filter.cpp:27
Botan::Buffered_Filter::write
void write(const uint8_t in[], size_t length)
Definition: buf_filt.cpp:34
Botan::Cipher_Mode_Filter::set_key
void set_key(const SymmetricKey &key) override
Definition: cipher_filter.cpp:61
Botan::Cipher_Mode_Filter::name
std::string name() const override
Definition: cipher_filter.cpp:36
Botan
Definition: alg_id.cpp:13
Botan::Cipher_Mode_Filter::key_spec
Key_Length_Specification key_spec() const override
Definition: cipher_filter.cpp:66
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
Botan::Cipher_Mode
Definition: cipher_mode.h:23
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:180
Botan::round_up
size_t round_up(size_t n, size_t align_to)
Definition: rounding.h:22