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