Botan 3.14.1
Crypto and TLS for C&
cipher_filter.cpp
Go to the documentation of this file.
1/*
2* Filter interface for Cipher_Modes
3* (C) 2013,2014,2017 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/filters.h>
9
10#include <botan/assert.h>
11#include <botan/internal/bit_ops.h>
12
13namespace Botan {
14
15namespace {
16
17size_t choose_update_size(size_t update_granularity) {
18 BOTAN_ARG_CHECK(update_granularity > 0, "Cipher mode granularity must be positive");
19
20 const size_t target_size = 1024;
21
22 if(update_granularity >= target_size) {
23 return update_granularity;
24 }
25
26 return update_granularity * ceil_division(target_size, update_granularity);
27}
28
29} // namespace
30
32 Buffered_Filter(choose_update_size(mode->ideal_granularity()), mode->minimum_final_size()),
33 m_mode(mode),
34 m_buffer(m_mode->ideal_granularity()) {}
35
36std::string Cipher_Mode_Filter::name() const {
37 return m_mode->name();
38}
39
41 m_nonce = unlock(iv.bits_of());
42}
43
45 m_mode->set_key(key);
46}
47
49 return m_mode->key_spec();
50}
51
52bool Cipher_Mode_Filter::valid_iv_length(size_t length) const {
53 return m_mode->valid_nonce_length(length);
54}
55
56void Cipher_Mode_Filter::write(const uint8_t input[], size_t input_length) {
57 Buffered_Filter::write(input, input_length);
58}
59
62}
63
65 // m_nonce is empty unless set_iv() was called (it is cleared after each message).
66 if(m_nonce.empty() && !m_mode->valid_nonce_length(0)) {
67 throw Invalid_State("Cipher " + m_mode->name() + " requires a fresh nonce for each message");
68 }
69
70 m_mode->start(m_nonce);
71 m_nonce.clear();
72}
73
74void Cipher_Mode_Filter::buffered_block(const uint8_t input[], size_t input_length) {
75 while(input_length > 0) {
76 const size_t take = std::min(m_mode->ideal_granularity(), input_length);
77
78 m_buffer.assign(input, input + take);
79 m_mode->update(m_buffer);
80
81 send(m_buffer);
82
83 input += take;
84 input_length -= take;
85 }
86}
87
88void Cipher_Mode_Filter::buffered_final(const uint8_t input[], size_t input_length) {
89 secure_vector<uint8_t> buf(input, input + input_length);
90 m_mode->finish(buf);
91 send(buf);
92}
93
94} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
Buffered_Filter(size_t block_size, size_t final_minimum)
Definition buf_filt.cpp:18
void write(const uint8_t in[], size_t length)
Definition buf_filt.cpp:34
Key_Length_Specification key_spec() const override
void set_iv(const InitializationVector &iv) override
bool valid_iv_length(size_t length) const override
void set_key(const SymmetricKey &key) override
std::string name() const override
Cipher_Mode_Filter(Cipher_Mode *t)
virtual void send(const uint8_t in[], size_t length)
Definition filter.cpp:30
virtual void end_msg()
Definition filter.h:47
virtual void start_msg()
Definition filter.h:41
secure_vector< uint8_t > bits_of() const
Definition symkey.h:46
OctetString SymmetricKey
Definition symkey.h:153
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
Definition bit_ops.h:167
OctetString InitializationVector
Definition symkey.h:158
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:155
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128