Botan  1.11.34
Crypto and TLS for C++11
filters.h
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 #ifndef BOTAN_FILTERS_H__
9 #define BOTAN_FILTERS_H__
10 
11 #include <botan/block_cipher.h>
12 #include <botan/stream_cipher.h>
13 #include <botan/hash.h>
14 #include <botan/mac.h>
15 
16 #include <botan/pipe.h>
17 #include <botan/basefilt.h>
18 #include <botan/key_filt.h>
19 #include <botan/data_snk.h>
20 
21 #if defined(BOTAN_HAS_CODEC_FILTERS)
22  #include <botan/b64_filt.h>
23  #include <botan/hex_filt.h>
24 #endif
25 
26 namespace Botan {
27 
28 /**
29 * Stream Cipher Filter
30 */
31 class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter
32  {
33  public:
34 
35  std::string name() const override { return m_cipher->name(); }
36 
37  /**
38  * Write input data
39  * @param input data
40  * @param input_len length of input in bytes
41  */
42  void write(const byte input[], size_t input_len) override;
43 
44  bool valid_iv_length(size_t iv_len) const override
45  { return m_cipher->valid_iv_length(iv_len); }
46 
47  /**
48  * Set the initialization vector for this filter.
49  * @param iv the initialization vector to set
50  */
51  void set_iv(const InitializationVector& iv) override
52  {
53  m_cipher->set_iv(iv.begin(), iv.length());
54  }
55 
56  /**
57  * Set the key of this filter.
58  * @param key the key to set
59  */
60  void set_key(const SymmetricKey& key) override { m_cipher->set_key(key); }
61 
62  Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
63 
64  /**
65  * Construct a stream cipher filter.
66  * @param cipher a cipher object to use
67  */
68  explicit StreamCipher_Filter(StreamCipher* cipher);
69 
70  /**
71  * Construct a stream cipher filter.
72  * @param cipher a cipher object to use
73  * @param key the key to use inside this filter
74  */
75  StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key);
76 
77  /**
78  * Construct a stream cipher filter.
79  * @param cipher the name of the desired cipher
80  */
81  explicit StreamCipher_Filter(const std::string& cipher);
82 
83  /**
84  * Construct a stream cipher filter.
85  * @param cipher the name of the desired cipher
86  * @param key the key to use inside this filter
87  */
88  StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key);
89  private:
90  secure_vector<byte> m_buffer;
91  std::unique_ptr<StreamCipher> m_cipher;
92  };
93 
94 /**
95 * Hash Filter.
96 */
97 class BOTAN_DLL Hash_Filter : public Filter
98  {
99  public:
100  void write(const byte input[], size_t len) override { m_hash->update(input, len); }
101  void end_msg() override;
102 
103  std::string name() const override { return m_hash->name(); }
104 
105  /**
106  * Construct a hash filter.
107  * @param hash the hash function to use
108  * @param len the output length of this filter. Leave the default
109  * value 0 if you want to use the full output of the hashfunction
110  * hash. Otherwise, specify a smaller value here so that the
111  * output of the hash algorithm will be cut off.
112  */
113  Hash_Filter(HashFunction* hash, size_t len = 0) :
114  m_hash(hash), m_out_len(len) {}
115 
116  /**
117  * Construct a hash filter.
118  * @param request the name of the hash algorithm to use
119  * @param len the output length of this filter. Leave the default
120  * value 0 if you want to use the full output of the hashfunction
121  * hash. Otherwise, specify a smaller value here so that the
122  * output of the hash algorithm will be cut off.
123  */
124  Hash_Filter(const std::string& request, size_t len = 0);
125 
126  private:
127  std::unique_ptr<HashFunction> m_hash;
128  const size_t m_out_len;
129  };
130 
131 /**
132 * MessageAuthenticationCode Filter.
133 */
134 class BOTAN_DLL MAC_Filter : public Keyed_Filter
135  {
136  public:
137  void write(const byte input[], size_t len) override { m_mac->update(input, len); }
138  void end_msg() override;
139 
140  std::string name() const override { return m_mac->name(); }
141 
142  /**
143  * Set the key of this filter.
144  * @param key the key to set
145  */
146  void set_key(const SymmetricKey& key) override { m_mac->set_key(key); }
147 
148  Key_Length_Specification key_spec() const override { return m_mac->key_spec(); }
149 
150  /**
151  * Construct a MAC filter. The MAC key will be left empty.
152  * @param mac the MAC to use
153  * @param out_len the output length of this filter. Leave the default
154  * value 0 if you want to use the full output of the
155  * MAC. Otherwise, specify a smaller value here so that the
156  * output of the MAC will be cut off.
157  */
159  size_t out_len = 0) :
160  m_mac(mac),
161  m_out_len(out_len)
162  {
163  }
164 
165  /**
166  * Construct a MAC filter.
167  * @param mac the MAC to use
168  * @param key the MAC key to use
169  * @param out_len the output length of this filter. Leave the default
170  * value 0 if you want to use the full output of the
171  * MAC. Otherwise, specify a smaller value here so that the
172  * output of the MAC will be cut off.
173  */
175  const SymmetricKey& key,
176  size_t out_len = 0) :
177  m_mac(mac),
178  m_out_len(out_len)
179  {
180  m_mac->set_key(key);
181  }
182 
183  /**
184  * Construct a MAC filter. The MAC key will be left empty.
185  * @param mac the name of the MAC to use
186  * @param len the output length of this filter. Leave the default
187  * value 0 if you want to use the full output of the
188  * MAC. Otherwise, specify a smaller value here so that the
189  * output of the MAC will be cut off.
190  */
191  MAC_Filter(const std::string& mac, size_t len = 0);
192 
193  /**
194  * Construct a MAC filter.
195  * @param mac the name of the MAC to use
196  * @param key the MAC key to use
197  * @param len the output length of this filter. Leave the default
198  * value 0 if you want to use the full output of the
199  * MAC. Otherwise, specify a smaller value here so that the
200  * output of the MAC will be cut off.
201  */
202  MAC_Filter(const std::string& mac, const SymmetricKey& key,
203  size_t len = 0);
204  private:
205  std::unique_ptr<MessageAuthenticationCode> m_mac;
206  const size_t m_out_len;
207  };
208 
209 }
210 
211 #endif
void set_iv(const InitializationVector &iv) override
Definition: filters.h:51
Key_Length_Specification key_spec() const override
Definition: filters.h:62
std::string name() const override
Definition: filters.h:140
bool valid_iv_length(size_t iv_len) const override
Definition: filters.h:44
void set_key(const SymmetricKey &key) override
Definition: filters.h:60
const byte * begin() const
Definition: symkey.h:36
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
std::string name() const override
Definition: filters.h:103
Key_Length_Specification key_spec() const override
Definition: filters.h:148
Definition: alg_id.cpp:13
size_t length() const
Definition: symkey.h:25
std::string name() const override
Definition: filters.h:35
void write(const byte input[], size_t len) override
Definition: filters.h:100
MAC_Filter(MessageAuthenticationCode *mac, const SymmetricKey &key, size_t out_len=0)
Definition: filters.h:174
void write(const byte input[], size_t len) override
Definition: filters.h:137
Hash_Filter(HashFunction *hash, size_t len=0)
Definition: filters.h:113
MechanismType hash
MAC_Filter(MessageAuthenticationCode *mac, size_t out_len=0)
Definition: filters.h:158
void set_key(const SymmetricKey &key) override
Definition: filters.h:146
std::uint8_t byte
Definition: types.h:31