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