Botan 3.3.0
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/internal/rounding.h>
11
12namespace Botan {
13
14namespace {
15
16size_t choose_update_size(size_t update_granularity) {
17 const size_t target_size = 1024;
18
19 if(update_granularity >= target_size) {
20 return update_granularity;
21 }
22
23 return round_up(target_size, update_granularity);
24}
25
26} // namespace
27
29 Buffered_Filter(choose_update_size(mode->ideal_granularity()), mode->minimum_final_size()),
30 m_mode(mode),
31 m_nonce(mode->default_nonce_length()),
32 m_buffer(m_mode->ideal_granularity()) {}
33
34std::string Cipher_Mode_Filter::name() const {
35 return m_mode->name();
36}
37
39 m_nonce = unlock(iv.bits_of());
40}
41
43 m_mode->set_key(key);
44}
45
47 return m_mode->key_spec();
48}
49
50bool Cipher_Mode_Filter::valid_iv_length(size_t length) const {
51 return m_mode->valid_nonce_length(length);
52}
53
54void Cipher_Mode_Filter::write(const uint8_t input[], size_t input_length) {
55 Buffered_Filter::write(input, input_length);
56}
57
58void Cipher_Mode_Filter::end_msg() {
60}
61
62void Cipher_Mode_Filter::start_msg() {
63 if(m_nonce.empty() && !m_mode->valid_nonce_length(0)) {
64 throw Invalid_State("Cipher " + m_mode->name() + " requires a fresh nonce for each message");
65 }
66
67 m_mode->start(m_nonce);
68 m_nonce.clear();
69}
70
71void Cipher_Mode_Filter::buffered_block(const uint8_t input[], size_t input_length) {
72 while(input_length) {
73 const size_t take = std::min(m_mode->ideal_granularity(), input_length);
74
75 m_buffer.assign(input, input + take);
76 m_mode->update(m_buffer);
77
78 send(m_buffer);
79
80 input += take;
81 input_length -= take;
82 }
83}
84
85void Cipher_Mode_Filter::buffered_final(const uint8_t input[], size_t input_length) {
86 secure_vector<uint8_t> buf(input, input + input_length);
87 m_mode->finish(buf);
88 send(buf);
89}
90
91} // namespace Botan
void write(const uint8_t in[], size_t length)
Definition buf_filt.cpp:35
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:27
secure_vector< uint8_t > bits_of() const
Definition symkey.h:36
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:75
size_t round_up(size_t n, size_t align_to)
Definition rounding.h:21