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