Botan  2.4.0
Crypto and TLS for C++11
algo_filt.cpp
Go to the documentation of this file.
1 /*
2 * Filters
3 * (C) 1999-2007,2015 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/filters.h>
9 #include <algorithm>
10 
11 namespace Botan {
12 
14  m_buffer(DEFAULT_BUFFERSIZE),
15  m_cipher(cipher)
16  {
17  }
18 
20  m_buffer(DEFAULT_BUFFERSIZE),
21  m_cipher(cipher)
22  {
23  m_cipher->set_key(key);
24  }
25 
26 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
27  m_buffer(DEFAULT_BUFFERSIZE),
28  m_cipher(StreamCipher::create_or_throw(sc_name))
29  {
30  }
31 
32 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, const SymmetricKey& key) :
33  m_buffer(DEFAULT_BUFFERSIZE),
34  m_cipher(StreamCipher::create_or_throw(sc_name))
35  {
36  m_cipher->set_key(key);
37  }
38 
39 void StreamCipher_Filter::write(const uint8_t input[], size_t length)
40  {
41  while(length)
42  {
43  size_t copied = std::min<size_t>(length, m_buffer.size());
44  m_cipher->cipher(input, m_buffer.data(), copied);
45  send(m_buffer, copied);
46  input += copied;
47  length -= copied;
48  }
49  }
50 
51 Hash_Filter::Hash_Filter(const std::string& hash_name, size_t len) :
52  m_hash(HashFunction::create_or_throw(hash_name)),
53  m_out_len(len)
54  {
55  }
56 
58  {
59  secure_vector<uint8_t> output = m_hash->final();
60  if(m_out_len)
61  send(output, std::min<size_t>(m_out_len, output.size()));
62  else
63  send(output);
64  }
65 
66 MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) :
67  m_mac(MessageAuthenticationCode::create_or_throw(mac_name)),
68  m_out_len(len)
69  {
70  }
71 
72 MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, size_t len) :
73  m_mac(MessageAuthenticationCode::create_or_throw(mac_name)),
74  m_out_len(len)
75  {
76  m_mac->set_key(key);
77  }
78 
80  {
81  secure_vector<uint8_t> output = m_mac->final();
82  if(m_out_len)
83  send(output, std::min<size_t>(m_out_len, output.size()));
84  else
85  send(output);
86  }
87 
88 }
void end_msg() override
Definition: algo_filt.cpp:79
virtual void send(const uint8_t in[], size_t length)
Definition: filter.cpp:27
StreamCipher_Filter(StreamCipher *cipher)
Definition: algo_filt.cpp:13
Definition: alg_id.cpp:13
void write(const uint8_t input[], size_t input_len) override
Definition: algo_filt.cpp:39
Hash_Filter(HashFunction *hash, size_t len=0)
Definition: filters.h:112
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
MAC_Filter(MessageAuthenticationCode *mac, size_t out_len=0)
Definition: filters.h:157
void end_msg() override
Definition: algo_filt.cpp:57