9#ifndef BOTAN_FILTERS_H_
10#define BOTAN_FILTERS_H_
12#include <botan/secmem.h>
13#include <botan/data_snk.h>
14#include <botan/pipe.h>
15#include <botan/symkey.h>
16#include <botan/cipher_mode.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>
49 void write(
const uint8_t in[],
size_t length);
51 template<
typename Alloc>
52 void write(
const std::vector<uint8_t, Alloc>& in,
size_t length)
54 write(in.data(), length);
106 size_t m_main_block_mod, m_final_minimum;
143 return key_spec().valid_keylength(length);
157 {
return (length == 0); }
178 bool valid_iv_length(
size_t length)
const override;
180 std::string
name()
const override;
183 void write(
const uint8_t input[],
size_t input_length)
override;
184 void start_msg()
override;
185 void end_msg()
override;
187 void buffered_block(
const uint8_t input[],
size_t input_length)
override;
188 void buffered_final(
const uint8_t input[],
size_t input_length)
override;
190 std::unique_ptr<Cipher_Mode> m_mode;
191 std::vector<uint8_t> m_nonce;
258#if defined(BOTAN_HAS_STREAM_CIPHER)
267 std::string
name()
const override {
return m_cipher->name(); }
274 void write(
const uint8_t input[],
size_t input_len)
override;
276 bool valid_iv_length(
size_t iv_len)
const override
277 {
return m_cipher->valid_iv_length(iv_len); }
283 void set_iv(
const InitializationVector& iv)
override
285 m_cipher->set_iv(iv.begin(), iv.length());
292 void set_key(
const SymmetricKey& key)
override { m_cipher->set_key(key); }
294 Key_Length_Specification key_spec()
const override {
return m_cipher->key_spec(); }
300 explicit StreamCipher_Filter(StreamCipher* cipher);
307 StreamCipher_Filter(StreamCipher* cipher,
const SymmetricKey& key);
313 explicit StreamCipher_Filter(
const std::string& cipher);
320 StreamCipher_Filter(
const std::string& cipher,
const SymmetricKey& key);
322 secure_vector<uint8_t> m_buffer;
323 std::unique_ptr<StreamCipher> m_cipher;
327#if defined(BOTAN_HAS_HASH)
335 void write(
const uint8_t input[],
size_t len)
override { m_hash->update(input, len); }
336 void end_msg()
override;
338 std::string
name()
const override {
return m_hash->name(); }
348 Hash_Filter(HashFunction*
hash,
size_t len = 0) :
349 m_hash(
hash), m_out_len(len) {}
359 Hash_Filter(
const std::string& request,
size_t len = 0);
362 std::unique_ptr<HashFunction> m_hash;
363 const size_t m_out_len;
367#if defined(BOTAN_HAS_MAC)
375 void write(
const uint8_t input[],
size_t len)
override { m_mac->update(input, len); }
376 void end_msg()
override;
378 std::string
name()
const override {
return m_mac->name(); }
384 void set_key(
const SymmetricKey& key)
override { m_mac->set_key(key); }
386 Key_Length_Specification key_spec()
const override {
return m_mac->key_spec(); }
396 MAC_Filter(MessageAuthenticationCode* mac,
397 size_t out_len = 0) :
412 MAC_Filter(MessageAuthenticationCode* mac,
413 const SymmetricKey& key,
414 size_t out_len = 0) :
429 MAC_Filter(
const std::string& mac,
size_t len = 0);
440 MAC_Filter(
const std::string& mac,
const SymmetricKey& key,
443 std::unique_ptr<MessageAuthenticationCode> m_mac;
444 const size_t m_out_len;
448#if defined(BOTAN_HAS_COMPRESSION)
450class Compression_Algorithm;
451class Decompression_Algorithm;
459 void start_msg()
override;
460 void write(
const uint8_t input[],
size_t input_length)
override;
461 void end_msg()
override;
465 std::string
name()
const override;
467 Compression_Filter(
const std::string&
type,
468 size_t compression_level,
469 size_t buffer_size = 4096);
471 ~Compression_Filter();
473 std::unique_ptr<Compression_Algorithm> m_comp;
474 size_t m_buffersize, m_level;
475 secure_vector<uint8_t> m_buffer;
484 void start_msg()
override;
485 void write(
const uint8_t input[],
size_t input_length)
override;
486 void end_msg()
override;
488 std::string
name()
const override;
490 Decompression_Filter(
const std::string&
type,
491 size_t buffer_size = 4096);
493 ~Decompression_Filter();
495 std::unique_ptr<Decompression_Algorithm> m_comp;
496 std::size_t m_buffersize;
497 secure_vector<uint8_t> m_buffer;
508 std::string
name()
const override {
return "Base64_Encoder"; }
515 void write(
const uint8_t input[],
size_t length)
override;
520 void end_msg()
override;
531 void encode_and_send(
const uint8_t input[],
size_t length,
532 bool final_inputs =
false);
533 void do_output(
const uint8_t output[],
size_t length);
535 const size_t m_line_length;
536 const bool m_trailing_newline;
537 std::vector<uint8_t> m_in, m_out;
538 size_t m_position, m_out_position;
547 std::string
name()
const override {
return "Base64_Decoder"; }
554 void write(
const uint8_t input[],
size_t length)
override;
559 void end_msg()
override;
569 std::vector<uint8_t> m_in, m_out;
583 enum Case { Uppercase, Lowercase };
585 std::string
name()
const override {
return "Hex_Encoder"; }
587 void write(
const uint8_t in[],
size_t length)
override;
588 void end_msg()
override;
603 size_t line_length = 72,
604 Case the_case = Uppercase);
606 void encode_and_send(
const uint8_t[],
size_t);
609 const size_t m_line_length;
610 std::vector<uint8_t> m_in, m_out;
611 size_t m_position, m_counter;
620 std::string
name()
const override {
return "Hex_Decoder"; }
622 void write(
const uint8_t[],
size_t)
override;
623 void end_msg()
override;
633 std::vector<uint8_t> m_in, m_out;
643 void write(
const uint8_t[],
size_t)
override { }
645 std::string
name()
const override {
return "BitBucket"; }
657 void write(
const uint8_t input[],
size_t length)
override { send(input, length); }
659 std::string
name()
const override {
return "Chain"; }
684 void write(
const uint8_t input[],
size_t length)
override { send(input, length); }
685 void set_port(
size_t n) { Fanout_Filter::set_port(n); }
687 std::string
name()
const override {
return "Fork"; }
702#if defined(BOTAN_HAS_THREAD_UTILS)
712 std::string
name()
const override;
717 Threaded_Fork(Filter*, Filter*, Filter* =
nullptr, Filter* =
nullptr);
724 Threaded_Fork(Filter* filter_arr[],
size_t length);
729 void set_next(Filter* f[],
size_t n);
730 void send(
const uint8_t in[],
size_t length)
override;
731 void thread_delegate_work(
const uint8_t input[],
size_t length);
732 void thread_entry(Filter* filter);
734 std::vector<std::shared_ptr<std::thread>> m_threads;
735 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(const std::string &algo, Cipher_Dir direction, const std::string &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)
Transform_Filter Transformation_Filter
Cipher_Mode_Filter Transform_Filter
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
std::vector< T, secure_allocator< T > > secure_vector