9#ifndef BOTAN_FILTERS_H_
10#define BOTAN_FILTERS_H_
12#include <botan/cipher_mode.h>
13#include <botan/data_snk.h>
14#include <botan/pipe.h>
15#include <botan/secmem.h>
16#include <botan/symkey.h>
18#if defined(BOTAN_TARGET_OS_HAS_THREADS)
22#if defined(BOTAN_HAS_STREAM_CIPHER)
23 #include <botan/stream_cipher.h>
26#if defined(BOTAN_HAS_HASH)
27 #include <botan/hash.h>
30#if defined(BOTAN_HAS_MAC)
31 #include <botan/mac.h>
48 void write(
const uint8_t in[],
size_t length);
50 template <
typename Alloc>
51 void write(
const std::vector<uint8_t, Alloc>& in,
size_t length) {
52 write(in.data(), length);
106 size_t m_main_block_mod, m_final_minimum;
140 bool valid_keylength(
size_t length)
const {
return key_spec().valid_keylength(length); }
171 bool valid_iv_length(
size_t length)
const override;
173 std::string
name()
const override;
176 void write(
const uint8_t input[],
size_t input_length)
override;
177 void start_msg()
override;
178 void end_msg()
override;
180 void buffered_block(
const uint8_t input[],
size_t input_length)
override;
181 void buffered_final(
const uint8_t input[],
size_t input_length)
override;
183 std::unique_ptr<Cipher_Mode> m_mode;
184 std::vector<uint8_t> m_nonce;
242#if defined(BOTAN_HAS_STREAM_CIPHER)
249 std::string
name()
const override {
return m_cipher->name(); }
256 void write(
const uint8_t input[],
size_t input_len)
override;
258 bool valid_iv_length(
size_t iv_len)
const override {
return m_cipher->valid_iv_length(iv_len); }
264 void set_iv(
const InitializationVector& iv)
override { m_cipher->set_iv(iv.begin(), iv.length()); }
270 void set_key(
const SymmetricKey& key)
override { m_cipher->set_key(key); }
272 Key_Length_Specification key_spec()
const override {
return m_cipher->key_spec(); }
278 explicit StreamCipher_Filter(StreamCipher* cipher);
285 StreamCipher_Filter(StreamCipher* cipher,
const SymmetricKey& key);
291 explicit StreamCipher_Filter(std::string_view cipher);
298 StreamCipher_Filter(std::string_view cipher,
const SymmetricKey& key);
301 std::unique_ptr<StreamCipher> m_cipher;
302 secure_vector<uint8_t> m_buffer;
306#if defined(BOTAN_HAS_HASH)
313 void write(
const uint8_t input[],
size_t len)
override { m_hash->update(input, len); }
315 void end_msg()
override;
317 std::string
name()
const override {
return m_hash->name(); }
327 Hash_Filter(HashFunction* hash,
size_t len = 0) : m_hash(hash), m_out_len(len) {}
337 Hash_Filter(std::string_view request,
size_t len = 0);
340 std::unique_ptr<HashFunction> m_hash;
341 const size_t m_out_len;
345#if defined(BOTAN_HAS_MAC)
352 void write(
const uint8_t input[],
size_t len)
override { m_mac->update(input, len); }
354 void end_msg()
override;
356 std::string
name()
const override {
return m_mac->name(); }
362 void set_key(
const SymmetricKey& key)
override { m_mac->set_key(key); }
364 Key_Length_Specification key_spec()
const override {
return m_mac->key_spec(); }
374 MAC_Filter(MessageAuthenticationCode* mac,
size_t out_len = 0) : m_mac(mac), m_out_len(out_len) {}
385 MAC_Filter(MessageAuthenticationCode* mac,
const SymmetricKey& key,
size_t out_len = 0) :
386 m_mac(mac), m_out_len(out_len) {
398 MAC_Filter(std::string_view mac,
size_t len = 0);
409 MAC_Filter(std::string_view mac,
const SymmetricKey& key,
size_t len = 0);
412 std::unique_ptr<MessageAuthenticationCode> m_mac;
413 const size_t m_out_len;
417#if defined(BOTAN_HAS_COMPRESSION)
419class Compression_Algorithm;
420class Decompression_Algorithm;
427 void start_msg()
override;
428 void write(
const uint8_t input[],
size_t input_length)
override;
429 void end_msg()
override;
433 std::string
name()
const override;
435 Compression_Filter(std::string_view type,
size_t compression_level,
size_t buffer_size = 4096);
437 ~Compression_Filter()
override;
440 std::unique_ptr<Compression_Algorithm> m_comp;
441 size_t m_buffersize, m_level;
442 secure_vector<uint8_t> m_buffer;
450 void start_msg()
override;
451 void write(
const uint8_t input[],
size_t input_length)
override;
452 void end_msg()
override;
454 std::string
name()
const override;
456 Decompression_Filter(std::string_view type,
size_t buffer_size = 4096);
458 ~Decompression_Filter()
override;
461 std::unique_ptr<Decompression_Algorithm> m_comp;
462 std::size_t m_buffersize;
463 secure_vector<uint8_t> m_buffer;
473 std::string
name()
const override {
return "Base64_Encoder"; }
480 void write(
const uint8_t input[],
size_t length)
override;
485 void end_msg()
override;
493 Base64_Encoder(
bool line_breaks =
false,
size_t line_length = 72,
bool trailing_newline =
false);
496 void encode_and_send(
const uint8_t input[],
size_t length,
bool final_inputs =
false);
497 void do_output(
const uint8_t output[],
size_t length);
499 const size_t m_line_length;
500 const bool m_trailing_newline;
501 std::vector<uint8_t> m_in, m_out;
502 size_t m_position, m_out_position;
510 std::string
name()
const override {
return "Base64_Decoder"; }
517 void write(
const uint8_t input[],
size_t length)
override;
522 void end_msg()
override;
533 std::vector<uint8_t> m_in, m_out;
546 enum Case { Uppercase, Lowercase };
548 std::string
name()
const override {
return "Hex_Encoder"; }
550 void write(
const uint8_t in[],
size_t length)
override;
551 void end_msg()
override;
565 Hex_Encoder(
bool newlines =
false,
size_t line_length = 72, Case the_case = Uppercase);
568 void encode_and_send(
const uint8_t[],
size_t);
571 const size_t m_line_length;
572 std::vector<uint8_t> m_in, m_out;
573 size_t m_position, m_counter;
581 std::string
name()
const override {
return "Hex_Decoder"; }
583 void write(
const uint8_t[],
size_t)
override;
584 void end_msg()
override;
595 std::vector<uint8_t> m_in, m_out;
604 void write(
const uint8_t[],
size_t)
override {
607 std::string
name()
const override {
return "BitBucket"; }
618 void write(
const uint8_t input[],
size_t length)
override { send(input, length); }
620 std::string
name()
const override {
return "Chain"; }
643 void write(
const uint8_t input[],
size_t length)
override { send(input, length); }
645 void set_port(
size_t n) { Fanout_Filter::set_port(n); }
647 std::string
name()
const override {
return "Fork"; }
662#if defined(BOTAN_HAS_THREAD_UTILS)
671 std::string
name()
const override;
676 Threaded_Fork(Filter*, Filter*, Filter* =
nullptr, Filter* =
nullptr);
683 Threaded_Fork(Filter* filter_arr[],
size_t length);
685 ~Threaded_Fork()
override;
688 void set_next(Filter* f[],
size_t n);
689 void send(
const uint8_t in[],
size_t length)
override;
690 void thread_delegate_work(
const uint8_t input[],
size_t length);
691 void thread_entry(Filter* filter);
693 std::vector<std::shared_ptr<std::thread>> m_threads;
694 std::unique_ptr<struct Threaded_Fork_Data> m_thread_data;
std::string name() const override
std::string name() const override
std::string name() const override
void write(const uint8_t[], size_t) override
virtual ~Buffered_Filter()=default
void write(const std::vector< uint8_t, Alloc > &in, size_t length)
size_t current_position() const
virtual void buffered_block(const uint8_t input[], size_t length)=0
virtual void buffered_final(const uint8_t input[], size_t length)=0
size_t buffered_block_size() const
void write(const uint8_t input[], size_t length) override
std::string name() const override
Cipher_Mode_Filter(std::unique_ptr< Cipher_Mode > t)
static std::unique_ptr< Cipher_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
std::string name() const override
void write(const uint8_t input[], size_t length) override
std::string name() const override
std::string name() const override
virtual void set_key(const SymmetricKey &key)=0
bool valid_keylength(size_t length) const
virtual void set_iv(const InitializationVector &iv)
virtual bool valid_iv_length(size_t length) const
virtual Key_Length_Specification key_spec() const =0
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
std::vector< T, secure_allocator< T > > secure_vector
Keyed_Filter * get_cipher(std::string_view algo_spec, Cipher_Dir direction)