Botan  1.11.10
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  Key_Length_Specification key_spec() const override { return cipher->key_spec(); }
62 
63  /**
64  * Construct a stream cipher filter.
65  * @param cipher_obj a cipher object to use
66  */
67  StreamCipher_Filter(StreamCipher* cipher_obj);
68 
69  /**
70  * Construct a stream cipher filter.
71  * @param cipher_obj a cipher object to use
72  * @param key the key to use inside this filter
73  */
74  StreamCipher_Filter(StreamCipher* cipher_obj, 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 
89  ~StreamCipher_Filter() { delete cipher; }
90  private:
91  secure_vector<byte> buffer;
92  StreamCipher* cipher;
93  };
94 
95 /**
96 * Hash Filter.
97 */
98 class BOTAN_DLL Hash_Filter : public Filter
99  {
100  public:
101  void write(const byte input[], size_t len) { hash->update(input, len); }
102  void end_msg();
103 
104  std::string name() const { return hash->name(); }
105 
106  /**
107  * Construct a hash filter.
108  * @param hash_fun the hash function to use
109  * @param len the output length of this filter. Leave the default
110  * value 0 if you want to use the full output of the hashfunction
111  * hash. Otherwise, specify a smaller value here so that the
112  * output of the hash algorithm will be cut off.
113  */
114  Hash_Filter(HashFunction* hash_fun, size_t len = 0) :
115  OUTPUT_LENGTH(len), hash(hash_fun) {}
116 
117  /**
118  * Construct a hash filter.
119  * @param request the name of the hash algorithm to use
120  * @param len the output length of this filter. Leave the default
121  * value 0 if you want to use the full output of the hashfunction
122  * hash. Otherwise, specify a smaller value here so that the
123  * output of the hash algorithm will be cut off.
124  */
125  Hash_Filter(const std::string& request, size_t len = 0);
126 
127  ~Hash_Filter() { delete hash; }
128  private:
129  const size_t OUTPUT_LENGTH;
130  HashFunction* hash;
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) { mac->update(input, len); }
140  void end_msg();
141 
142  std::string name() const { return 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) { mac->set_key(key); }
149 
150  Key_Length_Specification key_spec() const override { return mac->key_spec(); }
151 
152  /**
153  * Construct a MAC filter. The MAC key will be left empty.
154  * @param mac_obj 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) : OUTPUT_LENGTH(out_len)
162  {
163  mac = mac_obj;
164  }
165 
166  /**
167  * Construct a MAC filter.
168  * @param mac_obj the MAC to use
169  * @param key the MAC key to use
170  * @param out_len the output length of this filter. Leave the default
171  * value 0 if you want to use the full output of the
172  * MAC. Otherwise, specify a smaller value here so that the
173  * output of the MAC will be cut off.
174  */
176  const SymmetricKey& key,
177  size_t out_len = 0) : OUTPUT_LENGTH(out_len)
178  {
179  mac = mac_obj;
180  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 
205  ~MAC_Filter() { delete mac; }
206  private:
207  const size_t OUTPUT_LENGTH;
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:61
void write(const byte input[], size_t len)
Definition: filters.h:139
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition: filters.h:160
void write(const byte input[], size_t len)
Definition: filters.h:101
Hash_Filter(HashFunction *hash_fun, size_t len=0)
Definition: filters.h:114
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
MAC_Filter(MessageAuthenticationCode *mac_obj, const SymmetricKey &key, size_t out_len=0)
Definition: filters.h:175
std::string name() const
Definition: filters.h:142
uint8_t byte
Definition: types.h:30
Key_Length_Specification key_spec() const override
Definition: filters.h:150
Definition: buf_comp.h:15
std::string name() const
Definition: filters.h:104
void set_key(const SymmetricKey &key)
Definition: filters.h:148