Botan 3.4.0
Crypto and TLS for C&
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
11namespace Botan {
12
13#if defined(BOTAN_HAS_STREAM_CIPHER)
14
15StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher) : m_cipher(cipher), m_buffer(m_cipher->buffer_size()) {}
16
17StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key) : StreamCipher_Filter(cipher) {
18 m_cipher->set_key(key);
19}
20
21StreamCipher_Filter::StreamCipher_Filter(std::string_view sc_name) :
22 m_cipher(StreamCipher::create_or_throw(sc_name)), m_buffer(m_cipher->buffer_size()) {}
23
24StreamCipher_Filter::StreamCipher_Filter(std::string_view sc_name, const SymmetricKey& key) :
25 StreamCipher_Filter(sc_name) {
26 m_cipher->set_key(key);
27}
28
29void StreamCipher_Filter::write(const uint8_t input[], size_t length) {
30 while(length) {
31 size_t copied = std::min<size_t>(length, m_buffer.size());
32 m_cipher->cipher(input, m_buffer.data(), copied);
33 send(m_buffer, copied);
34 input += copied;
35 length -= copied;
36 }
37}
38
39#endif
40
41#if defined(BOTAN_HAS_HASH)
42
43Hash_Filter::Hash_Filter(std::string_view hash_name, size_t len) :
44 m_hash(HashFunction::create_or_throw(hash_name)), m_out_len(len) {}
45
46void Hash_Filter::end_msg() {
47 secure_vector<uint8_t> output = m_hash->final();
48 if(m_out_len) {
49 send(output, std::min<size_t>(m_out_len, output.size()));
50 } else {
51 send(output);
52 }
53}
54#endif
55
56#if defined(BOTAN_HAS_MAC)
57
58MAC_Filter::MAC_Filter(std::string_view mac_name, size_t len) :
59 m_mac(MessageAuthenticationCode::create_or_throw(mac_name)), m_out_len(len) {}
60
61MAC_Filter::MAC_Filter(std::string_view mac_name, const SymmetricKey& key, size_t len) : MAC_Filter(mac_name, len) {
62 m_mac->set_key(key);
63}
64
65void MAC_Filter::end_msg() {
66 secure_vector<uint8_t> output = m_mac->final();
67 if(m_out_len) {
68 send(output, std::min<size_t>(m_out_len, output.size()));
69 } else {
70 send(output);
71 }
72}
73
74#endif
75
76} // namespace Botan