Botan  1.11.11
filters.h
Go to the documentation of this file.
1 /*
2 * Filters
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
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  /**
56  * Set the key of this filter.
57  * @param key the key to set
58  */
59  void set_key(const SymmetricKey& key) { m_cipher->set_key(key); }
60 
61  Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); }
62 
63  /**
64  * Construct a stream cipher filter.
65  * @param cipher a cipher object to use
66  */
68 
69  /**
70  * Construct a stream cipher filter.
71  * @param cipher a cipher object to use
72  * @param key the key to use inside this filter
73  */
74  StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key);
75 
76  /**
77  * Construct a stream cipher filter.
78  * @param cipher the name of the desired cipher
79  */
80  StreamCipher_Filter(const std::string& cipher);
81 
82  /**
83  * Construct a stream cipher filter.
84  * @param cipher the name of the desired cipher
85  * @param key the key to use inside this filter
86  */
87  StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key);
88  private:
89  secure_vector<byte> buffer;
90  std::unique_ptr<StreamCipher> m_cipher;
91  };
92 
93 /**
94 * Hash Filter.
95 */
96 class BOTAN_DLL Hash_Filter : public Filter
97  {
98  public:
99  void write(const byte input[], size_t len) { m_hash->update(input, len); }
100  void end_msg();
101 
102  std::string name() const { return m_hash->name(); }
103 
104  /**
105  * Construct a hash filter.
106  * @param hash_fun the hash function to use
107  * @param len the output length of this filter. Leave the default
108  * value 0 if you want to use the full output of the hashfunction
109  * hash. Otherwise, specify a smaller value here so that the
110  * output of the hash algorithm will be cut off.
111  */
112  Hash_Filter(HashFunction* hash, size_t len = 0) :
113  OUTPUT_LENGTH(len), m_hash(hash) {}
114 
115  /**
116  * Construct a hash filter.
117  * @param request the name of the hash algorithm to use
118  * @param len the output length of this filter. Leave the default
119  * value 0 if you want to use the full output of the hashfunction
120  * hash. Otherwise, specify a smaller value here so that the
121  * output of the hash algorithm will be cut off.
122  */
123  Hash_Filter(const std::string& request, size_t len = 0);
124 
125  private:
126  const size_t OUTPUT_LENGTH;
127  std::unique_ptr<HashFunction> m_hash;
128  };
129 
130 /**
131 * MessageAuthenticationCode Filter.
132 */
133 class BOTAN_DLL MAC_Filter : public Keyed_Filter
134  {
135  public:
136  void write(const byte input[], size_t len) { m_mac->update(input, len); }
137  void end_msg();
138 
139  std::string name() const { return m_mac->name(); }
140 
141  /**
142  * Set the key of this filter.
143  * @param key the key to set
144  */
145  void set_key(const SymmetricKey& key) { m_mac->set_key(key); }
146 
147  Key_Length_Specification key_spec() const override { return m_mac->key_spec(); }
148 
149  /**
150  * Construct a MAC filter. The MAC key will be left empty.
151  * @param mac the MAC to use
152  * @param out_len the output length of this filter. Leave the default
153  * value 0 if you want to use the full output of the
154  * MAC. Otherwise, specify a smaller value here so that the
155  * output of the MAC will be cut off.
156  */
158  size_t out_len = 0) :
159  OUTPUT_LENGTH(out_len),
160  m_mac(mac)
161  {
162  }
163 
164  /**
165  * Construct a MAC filter.
166  * @param mac the MAC to use
167  * @param key the MAC key to use
168  * @param out_len the output length of this filter. Leave the default
169  * value 0 if you want to use the full output of the
170  * MAC. Otherwise, specify a smaller value here so that the
171  * output of the MAC will be cut off.
172  */
174  const SymmetricKey& key,
175  size_t out_len = 0) :
176  OUTPUT_LENGTH(out_len),
177  m_mac(mac)
178  {
179  m_mac->set_key(key);
180  }
181 
182  /**
183  * Construct a MAC filter. The MAC key will be left empty.
184  * @param mac the name of the MAC to use
185  * @param len the output length of this filter. Leave the default
186  * value 0 if you want to use the full output of the
187  * MAC. Otherwise, specify a smaller value here so that the
188  * output of the MAC will be cut off.
189  */
190  MAC_Filter(const std::string& mac, size_t len = 0);
191 
192  /**
193  * Construct a MAC filter.
194  * @param mac the name of the MAC to use
195  * @param key the MAC key to use
196  * @param len the output length of this filter. Leave the default
197  * value 0 if you want to use the full output of the
198  * MAC. Otherwise, specify a smaller value here so that the
199  * output of the MAC will be cut off.
200  */
201  MAC_Filter(const std::string& mac, const SymmetricKey& key,
202  size_t len = 0);
203  private:
204  const size_t OUTPUT_LENGTH;
205  std::unique_ptr<MessageAuthenticationCode> m_mac;
206  };
207 
208 }
209 
210 #endif
std::unique_ptr< MessageAuthenticationCode > mac
Definition: fpe_fe1.cpp:89
Key_Length_Specification key_spec() const override
Definition: filters.h:61
void write(const byte input[], size_t len)
Definition: filters.h:136
void write(const byte input[], size_t len)
Definition: filters.h:99
std::string name() const
Definition: filters.h:37
bool valid_iv_length(size_t iv_len) const
Definition: filters.h:46
void set_key(const SymmetricKey &key)
Definition: filters.h:59
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:92
std::string name() const
Definition: filters.h:139
uint8_t byte
Definition: types.h:30
Key_Length_Specification key_spec() const override
Definition: filters.h:147
Definition: buf_comp.h:15
std::string name() const
Definition: filters.h:102
MAC_Filter(MessageAuthenticationCode *mac, const SymmetricKey &key, size_t out_len=0)
Definition: filters.h:173
void set_key(const SymmetricKey &key)
Definition: filters.h:145
Hash_Filter(HashFunction *hash, size_t len=0)
Definition: filters.h:112
MAC_Filter(MessageAuthenticationCode *mac, size_t out_len=0)
Definition: filters.h:157