Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Member Functions | Protected Member Functions | List of all members
Botan::Buffered_Filter Class Referenceabstract

#include <filters.h>

Inheritance diagram for Botan::Buffered_Filter:
Botan::Cipher_Mode_Filter

Public Member Functions

 Buffered_Filter (size_t block_size, size_t final_minimum)
 
void end_msg ()
 
template<typename Alloc >
void write (const std::vector< uint8_t, Alloc > &in, size_t length)
 
void write (const uint8_t in[], size_t length)
 
virtual ~Buffered_Filter ()=default
 

Protected Member Functions

void buffer_reset ()
 
virtual void buffered_block (const uint8_t input[], size_t length)=0
 
size_t buffered_block_size () const
 
virtual void buffered_final (const uint8_t 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 40 of file filters.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 m_main_block_mod(b), m_final_minimum(f)
20 {
21 if(m_main_block_mod == 0)
22 throw Invalid_Argument("m_main_block_mod == 0");
23
24 if(m_final_minimum > m_main_block_mod)
25 throw Invalid_Argument("m_final_minimum > m_main_block_mod");
26
27 m_buffer.resize(2 * m_main_block_mod);
28 m_buffer_pos = 0;
29 }
PolynomialVector b
Definition: kyber.cpp:821

◆ ~Buffered_Filter()

virtual Botan::Buffered_Filter::~Buffered_Filter ( )
virtualdefault

Member Function Documentation

◆ buffer_reset()

void Botan::Buffered_Filter::buffer_reset ( )
inlineprotected

Reset the buffer position

Definition at line 104 of file filters.h.

104{ m_buffer_pos = 0; }

◆ buffered_block()

virtual void Botan::Buffered_Filter::buffered_block ( const uint8_t  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()

size_t Botan::Buffered_Filter::buffered_block_size ( ) const
inlineprotected
Returns
block size of inputs

Definition at line 94 of file filters.h.

94{ return m_main_block_mod; }

◆ buffered_final()

virtual void Botan::Buffered_Filter::buffered_final ( const uint8_t  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 99 of file filters.h.

99{ return m_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(m_buffer_pos < m_final_minimum)
85 throw Invalid_State("Buffered filter end_msg without enough input");
86
87 size_t spare_blocks = (m_buffer_pos - m_final_minimum) / m_main_block_mod;
88
89 if(spare_blocks)
90 {
91 size_t spare_bytes = m_main_block_mod * spare_blocks;
92 buffered_block(m_buffer.data(), spare_bytes);
93 buffered_final(&m_buffer[spare_bytes], m_buffer_pos - spare_bytes);
94 }
95 else
96 {
97 buffered_final(m_buffer.data(), m_buffer_pos);
98 }
99
100 m_buffer_pos = 0;
101 }
virtual void buffered_block(const uint8_t input[], size_t length)=0
virtual void buffered_final(const uint8_t input[], size_t length)=0

References buffered_block(), and buffered_final().

◆ write() [1/2]

template<typename Alloc >
void Botan::Buffered_Filter::write ( const std::vector< uint8_t, Alloc > &  in,
size_t  length 
)
inline

Definition at line 52 of file filters.h.

53 {
54 write(in.data(), length);
55 }
void write(const uint8_t in[], size_t length)
Definition: buf_filt.cpp:34

◆ write() [2/2]

void Botan::Buffered_Filter::write ( const uint8_t  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(m_buffer_pos + input_size >= m_main_block_mod + m_final_minimum)
40 {
41 size_t to_copy = std::min<size_t>(m_buffer.size() - m_buffer_pos, input_size);
42
43 copy_mem(&m_buffer[m_buffer_pos], input, to_copy);
44 m_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(m_buffer_pos,
51 m_buffer_pos + input_size - m_final_minimum),
52 m_main_block_mod);
53
54 buffered_block(m_buffer.data(), total_to_consume);
55
56 m_buffer_pos -= total_to_consume;
57
58 copy_mem(m_buffer.data(), m_buffer.data() + total_to_consume, m_buffer_pos);
59 }
60
61 if(input_size >= m_final_minimum)
62 {
63 size_t full_blocks = (input_size - m_final_minimum) / m_main_block_mod;
64 size_t to_copy = full_blocks * m_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(&m_buffer[m_buffer_pos], input, input_size);
76 m_buffer_pos += input_size;
77 }
constexpr T round_down(T n, T align_to)
Definition: rounding.h:37
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126

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


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