Botan  1.10.9
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 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 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) { cipher->set_key(key); }
60 
61  /**
62  * Check whether a key length is valid for this filter.
63  * @param length the key length to be checked for validity
64  * @return true if the key length is valid, false otherwise
65  */
66  bool valid_keylength(size_t length) const
67  { return cipher->valid_keylength(length); }
68 
69  /**
70  * Construct a stream cipher filter.
71  * @param cipher_obj a cipher object to use
72  */
73  StreamCipher_Filter(StreamCipher* cipher_obj);
74 
75  /**
76  * Construct a stream cipher filter.
77  * @param cipher_obj a cipher object to use
78  * @param key the key to use inside this filter
79  */
80  StreamCipher_Filter(StreamCipher* cipher_obj, const SymmetricKey& key);
81 
82  /**
83  * Construct a stream cipher filter.
84  * @param cipher the name of the desired cipher
85  */
86  StreamCipher_Filter(const std::string& cipher);
87 
88  /**
89  * Construct a stream cipher filter.
90  * @param cipher the name of the desired cipher
91  * @param key the key to use inside this filter
92  */
93  StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key);
94 
95  ~StreamCipher_Filter() { delete cipher; }
96  private:
97  SecureVector<byte> buffer;
98  StreamCipher* cipher;
99  };
100 
101 /**
102 * Hash Filter.
103 */
104 class BOTAN_DLL Hash_Filter : public Filter
105  {
106  public:
107  void write(const byte input[], size_t len) { hash->update(input, len); }
108  void end_msg();
109 
110  std::string name() const { return hash->name(); }
111 
112  /**
113  * Construct a hash filter.
114  * @param hash_fun the hash function to use
115  * @param len the output length of this filter. Leave the default
116  * value 0 if you want to use the full output of the hashfunction
117  * hash. Otherwise, specify a smaller value here so that the
118  * output of the hash algorithm will be cut off.
119  */
120  Hash_Filter(HashFunction* hash_fun, size_t len = 0) :
121  OUTPUT_LENGTH(len), hash(hash_fun) {}
122 
123  /**
124  * Construct a hash filter.
125  * @param request the name of the hash algorithm to use
126  * @param len the output length of this filter. Leave the default
127  * value 0 if you want to use the full output of the hashfunction
128  * hash. Otherwise, specify a smaller value here so that the
129  * output of the hash algorithm will be cut off.
130  */
131  Hash_Filter(const std::string& request, size_t len = 0);
132 
133  ~Hash_Filter() { delete hash; }
134  private:
135  const size_t OUTPUT_LENGTH;
136  HashFunction* hash;
137  };
138 
139 /**
140 * MessageAuthenticationCode Filter.
141 */
142 class BOTAN_DLL MAC_Filter : public Keyed_Filter
143  {
144  public:
145  void write(const byte input[], size_t len) { mac->update(input, len); }
146  void end_msg();
147 
148  std::string name() const { return mac->name(); }
149 
150  /**
151  * Set the key of this filter.
152  * @param key the key to set
153  */
154  void set_key(const SymmetricKey& key) { mac->set_key(key); }
155 
156  /**
157  * Check whether a key length is valid for this filter.
158  * @param length the key length to be checked for validity
159  * @return true if the key length is valid, false otherwise
160  */
161  bool valid_keylength(size_t length) const
162  { return mac->valid_keylength(length); }
163 
164  /**
165  * Construct a MAC filter. The MAC key will be left empty.
166  * @param mac_obj the MAC to use
167  * @param out_len the output length of this filter. Leave the default
168  * value 0 if you want to use the full output of the
169  * MAC. Otherwise, specify a smaller value here so that the
170  * output of the MAC will be cut off.
171  */
173  size_t out_len = 0) : OUTPUT_LENGTH(out_len)
174  {
175  mac = mac_obj;
176  }
177 
178  /**
179  * Construct a MAC filter.
180  * @param mac_obj the MAC to use
181  * @param key the MAC key to use
182  * @param out_len the output length of this filter. Leave the default
183  * value 0 if you want to use the full output of the
184  * MAC. Otherwise, specify a smaller value here so that the
185  * output of the MAC will be cut off.
186  */
188  const SymmetricKey& key,
189  size_t out_len = 0) : OUTPUT_LENGTH(out_len)
190  {
191  mac = mac_obj;
192  mac->set_key(key);
193  }
194 
195  /**
196  * Construct a MAC filter. The MAC key will be left empty.
197  * @param mac the name of the MAC to use
198  * @param len the output length of this filter. Leave the default
199  * value 0 if you want to use the full output of the
200  * MAC. Otherwise, specify a smaller value here so that the
201  * output of the MAC will be cut off.
202  */
203  MAC_Filter(const std::string& mac, size_t len = 0);
204 
205  /**
206  * Construct a MAC filter.
207  * @param mac the name of the MAC to use
208  * @param key the MAC key to use
209  * @param len the output length of this filter. Leave the default
210  * value 0 if you want to use the full output of the
211  * MAC. Otherwise, specify a smaller value here so that the
212  * output of the MAC will be cut off.
213  */
214  MAC_Filter(const std::string& mac, const SymmetricKey& key,
215  size_t len = 0);
216 
217  ~MAC_Filter() { delete mac; }
218  private:
219  const size_t OUTPUT_LENGTH;
221  };
222 
223 }
224 
225 #endif
void write(const byte input[], size_t len)
Definition: filters.h:145
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition: filters.h:172
void write(const byte input[], size_t len)
Definition: filters.h:107
Hash_Filter(HashFunction *hash_fun, size_t len=0)
Definition: filters.h:120
std::string name() const
Definition: filters.h:37
bool valid_iv_length(size_t iv_len) const
Definition: filters.h:46
bool valid_keylength(size_t length) const
Definition: filters.h:161
void set_key(const SymmetricKey &key)
Definition: filters.h:59
unsigned char byte
Definition: types.h:22
MessageAuthenticationCode * mac
Definition: fpe_fe1.cpp:94
MAC_Filter(MessageAuthenticationCode *mac_obj, const SymmetricKey &key, size_t out_len=0)
Definition: filters.h:187
std::string name() const
Definition: filters.h:148
std::string name() const
Definition: filters.h:110
void set_key(const SymmetricKey &key)
Definition: filters.h:154
bool valid_keylength(size_t length) const
Definition: filters.h:66