Botan  2.7.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 
13 #if defined(BOTAN_HAS_STREAM_CIPHER)
14 
15 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher) :
16  m_buffer(BOTAN_DEFAULT_BUFFER_SIZE),
17  m_cipher(cipher)
18  {
19  }
20 
21 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key) :
22  StreamCipher_Filter(cipher)
23  {
24  m_cipher->set_key(key);
25  }
26 
27 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
28  m_buffer(BOTAN_DEFAULT_BUFFER_SIZE),
29  m_cipher(StreamCipher::create_or_throw(sc_name))
30  {
31  }
32 
33 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, const SymmetricKey& key) :
34  StreamCipher_Filter(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 #endif
52 
53 #if defined(BOTAN_HAS_HASH)
54 
55 Hash_Filter::Hash_Filter(const std::string& hash_name, size_t len) :
56  m_hash(HashFunction::create_or_throw(hash_name)),
57  m_out_len(len)
58  {
59  }
60 
61 void Hash_Filter::end_msg()
62  {
63  secure_vector<uint8_t> output = m_hash->final();
64  if(m_out_len)
65  send(output, std::min<size_t>(m_out_len, output.size()));
66  else
67  send(output);
68  }
69 #endif
70 
71 #if defined(BOTAN_HAS_MAC)
72 
73 MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) :
74  m_mac(MessageAuthenticationCode::create_or_throw(mac_name)),
75  m_out_len(len)
76  {
77  }
78 
79 MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, size_t len) :
80  MAC_Filter(mac_name, len)
81  {
82  m_mac->set_key(key);
83  }
84 
85 void MAC_Filter::end_msg()
86  {
87  secure_vector<uint8_t> output = m_mac->final();
88  if(m_out_len)
89  send(output, std::min<size_t>(m_out_len, output.size()));
90  else
91  send(output);
92  }
93 
94 #endif
95 
96 }
Definition: alg_id.cpp:13
OctetString SymmetricKey
Definition: symkey.h:136