Botan 1.10.17
Botan::Buffered_Filter Class Referenceabstract

#include <buf_filt.h>

Inheritance diagram for Botan::Buffered_Filter:
Botan::CBC_Decryption Botan::CBC_Encryption Botan::ECB_Decryption Botan::ECB_Encryption Botan::XTS_Decryption Botan::XTS_Encryption

Public Member Functions

 Buffered_Filter (size_t block_size, size_t final_minimum)
void end_msg ()
void write (const byte in[], size_t length)
virtual ~Buffered_Filter ()

Protected Member Functions

void buffer_reset ()
virtual void buffered_block (const byte input[], size_t length)=0
size_t buffered_block_size () const
virtual void buffered_final (const byte input[], size_t length)=0
size_t current_position () const

Detailed Description

Filter mixin that breaks input into blocks, useful for cipher modes

Definition at line 19 of file buf_filt.h.

Constructor & Destructor Documentation

◆ Buffered_Filter()

Botan::Buffered_Filter::Buffered_Filter ( size_t block_size,
size_t final_minimum )

Initialize a Buffered_Filter

Parameters
block_sizethe function buffered_block will be called with inputs which are a multiple of this size
final_minimumthe function buffered_final will be called with at least this many bytes.

Definition at line 18 of file buf_filt.cpp.

18 :
19 main_block_mod(b), final_minimum(f)
20 {
21 if(main_block_mod == 0)
22 throw std::invalid_argument("main_block_mod == 0");
23
24 if(final_minimum > main_block_mod)
25 throw std::invalid_argument("final_minimum > main_block_mod");
26
27 buffer.resize(2 * main_block_mod);
28 buffer_pos = 0;
29 }

Referenced by Botan::CBC_Decryption::CBC_Decryption(), Botan::CBC_Decryption::CBC_Decryption(), Botan::CBC_Encryption::CBC_Encryption(), Botan::CBC_Encryption::CBC_Encryption(), Botan::ECB_Decryption::ECB_Decryption(), Botan::ECB_Decryption::ECB_Decryption(), Botan::ECB_Encryption::ECB_Encryption(), Botan::ECB_Encryption::ECB_Encryption(), Botan::XTS_Decryption::XTS_Decryption(), Botan::XTS_Decryption::XTS_Decryption(), Botan::XTS_Encryption::XTS_Encryption(), and Botan::XTS_Encryption::XTS_Encryption().

◆ ~Buffered_Filter()

virtual Botan::Buffered_Filter::~Buffered_Filter ( )
inlinevirtual

Definition at line 46 of file buf_filt.h.

46{}

Member Function Documentation

◆ buffer_reset()

void Botan::Buffered_Filter::buffer_reset ( )
inlineprotected

Reset the buffer position

Definition at line 77 of file buf_filt.h.

77{ buffer_pos = 0; }

Referenced by Botan::CBC_Decryption::set_iv(), and Botan::CBC_Encryption::set_iv().

◆ buffered_block()

virtual void Botan::Buffered_Filter::buffered_block ( const byte input[],
size_t length )
protectedpure virtual

The block processor, implemented by subclasses

Parameters
inputsome input bytes
lengththe size of input, guaranteed to be a multiple of block_size

Referenced by end_msg(), and write().

◆ buffered_block_size()

◆ buffered_final()

virtual void Botan::Buffered_Filter::buffered_final ( const byte input[],
size_t length )
protectedpure virtual

The final block, implemented by subclasses

Parameters
inputsome input bytes
lengththe size of input, guaranteed to be at least final_minimum bytes

Referenced by end_msg().

◆ current_position()

size_t Botan::Buffered_Filter::current_position ( ) const
inlineprotected
Returns
current position in the buffer

Definition at line 72 of file buf_filt.h.

72{ return buffer_pos; }

◆ end_msg()

void Botan::Buffered_Filter::end_msg ( )

Finish a message, emitting to buffered_block and buffered_final Will throw an exception if less than final_minimum bytes were written into the filter.

Definition at line 82 of file buf_filt.cpp.

83 {
84 if(buffer_pos < final_minimum)
85 throw std::runtime_error("Buffered filter end_msg without enough input");
86
87 size_t spare_blocks = (buffer_pos - final_minimum) / main_block_mod;
88
89 if(spare_blocks)
90 {
91 size_t spare_bytes = main_block_mod * spare_blocks;
92 buffered_block(&buffer[0], spare_bytes);
93 buffered_final(&buffer[spare_bytes], buffer_pos - spare_bytes);
94 }
95 else
96 {
97 buffered_final(&buffer[0], buffer_pos);
98 }
99
100 buffer_pos = 0;
101 }
virtual void buffered_block(const byte input[], size_t length)=0
virtual void buffered_final(const byte input[], size_t length)=0

References buffered_block(), and buffered_final().

◆ write()

void Botan::Buffered_Filter::write ( const byte in[],
size_t length )

Write bytes into the buffered filter, which will them emit them in calls to buffered_block in the subclass

Parameters
inthe input bytes
lengthof in in bytes

Definition at line 34 of file buf_filt.cpp.

35 {
36 if(!input_size)
37 return;
38
39 if(buffer_pos + input_size >= main_block_mod + final_minimum)
40 {
41 size_t to_copy = std::min<size_t>(buffer.size() - buffer_pos, input_size);
42
43 copy_mem(&buffer[buffer_pos], input, to_copy);
44 buffer_pos += to_copy;
45
46 input += to_copy;
47 input_size -= to_copy;
48
49 size_t total_to_consume =
50 round_down(std::min(buffer_pos,
51 buffer_pos + input_size - final_minimum),
52 main_block_mod);
53
54 buffered_block(&buffer[0], total_to_consume);
55
56 buffer_pos -= total_to_consume;
57
58 copy_mem(&buffer[0], &buffer[total_to_consume], buffer_pos);
59 }
60
61 if(input_size >= final_minimum)
62 {
63 size_t full_blocks = (input_size - final_minimum) / main_block_mod;
64 size_t to_copy = full_blocks * main_block_mod;
65
66 if(to_copy)
67 {
68 buffered_block(input, to_copy);
69
70 input += to_copy;
71 input_size -= to_copy;
72 }
73 }
74
75 copy_mem(&buffer[buffer_pos], input, input_size);
76 buffer_pos += input_size;
77 }
T round_down(T n, T align_to)
Definition rounding.h:36
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22

References buffered_block(), Botan::copy_mem(), and Botan::round_down().


The documentation for this class was generated from the following files: