Botan  2.4.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/stream_cipher.h>
12 #include <botan/hash.h>
13 #include <botan/mac.h>
14 
15 #include <botan/pipe.h>
16 #include <botan/basefilt.h>
17 #include <botan/key_filt.h>
18 #include <botan/data_snk.h>
19 
20 #if defined(BOTAN_HAS_CODEC_FILTERS)
21  #include <botan/b64_filt.h>
22  #include <botan/hex_filt.h>
23 #endif
24 
25 namespace Botan {
26 
27 /**
28 * Stream Cipher Filter
29 */
31  {
32  public:
33 
34  std::string name() const override { return m_cipher->name(); }
35 
36  /**
37  * Write input data
38  * @param input data
39  * @param input_len length of input in bytes
40  */
41  void write(const uint8_t input[], size_t input_len) override;
42 
43  bool valid_iv_length(size_t iv_len) const override
44  { return m_cipher->valid_iv_length(iv_len); }
45 
46  /**
47  * Set the initialization vector for this filter.
48  * @param iv the initialization vector to set
49  */
50  void set_iv(const InitializationVector& iv) override
51  {
52  m_cipher->set_iv(iv.begin(), iv.length());
53  }
54 
55  /**
56  * Set the key of this filter.
57  * @param key the key to set
58  */
59  void set_key(const SymmetricKey& key) override { 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  */
67  explicit StreamCipher_Filter(StreamCipher* cipher);
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  explicit 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<uint8_t> m_buffer;
90  std::unique_ptr<StreamCipher> m_cipher;
91  };
92 
93 /**
94 * Hash Filter.
95 */
96 class BOTAN_PUBLIC_API(2,0) Hash_Filter final : public Filter
97  {
98  public:
99  void write(const uint8_t input[], size_t len) override { m_hash->update(input, len); }
100  void end_msg() override;
101 
102  std::string name() const override { return m_hash->name(); }
103 
104  /**
105  * Construct a hash filter.
106  * @param hash 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  m_hash(hash), m_out_len(len) {}
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  std::unique_ptr<HashFunction> m_hash;
127  const size_t m_out_len;
128  };
129 
130 /**
131 * MessageAuthenticationCode Filter.
132 */
133 class BOTAN_PUBLIC_API(2,0) MAC_Filter final : public Keyed_Filter
134  {
135  public:
136  void write(const uint8_t input[], size_t len) override { m_mac->update(input, len); }
137  void end_msg() override;
138 
139  std::string name() const override { 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) override { 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  m_mac(mac),
160  m_out_len(out_len)
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  m_mac(mac),
177  m_out_len(out_len)
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  std::unique_ptr<MessageAuthenticationCode> m_mac;
205  const size_t m_out_len;
206  };
207 
208 }
209 
210 #endif
void set_iv(const InitializationVector &iv) override
Definition: filters.h:50
Key_Length_Specification key_spec() const override
Definition: filters.h:61
std::string name() const override
Definition: filters.h:139
void write(const uint8_t input[], size_t len) override
Definition: filters.h:99
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
bool valid_iv_length(size_t iv_len) const override
Definition: filters.h:43
void set_key(const SymmetricKey &key) override
Definition: filters.h:59
void write(const uint8_t input[], size_t len) override
Definition: filters.h:136
std::string name() const override
Definition: filters.h:102
Key_Length_Specification key_spec() const override
Definition: filters.h:147
Definition: alg_id.cpp:13
size_t length() const
Definition: symkey.h:25
std::string name() const override
Definition: filters.h:34
const uint8_t * begin() const
Definition: symkey.h:36
MAC_Filter(MessageAuthenticationCode *mac, const SymmetricKey &key, size_t out_len=0)
Definition: filters.h:173
Hash_Filter(HashFunction *hash, size_t len=0)
Definition: filters.h:112
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
MechanismType hash
MAC_Filter(MessageAuthenticationCode *mac, size_t out_len=0)
Definition: filters.h:157
void set_key(const SymmetricKey &key) override
Definition: filters.h:145