Botan  1.10.9
algo_filt.cpp
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 #include <botan/filters.h>
9 #include <botan/libstate.h>
10 #include <algorithm>
11 
12 namespace Botan {
13 
14 /*
15 * StreamCipher_Filter Constructor
16 */
18  buffer(DEFAULT_BUFFERSIZE)
19  {
20  cipher = stream_cipher;
21  }
22 
23 /*
24 * StreamCipher_Filter Constructor
25 */
27  const SymmetricKey& key) :
28  buffer(DEFAULT_BUFFERSIZE)
29  {
30  cipher = stream_cipher;
31  cipher->set_key(key);
32  }
33 
34 /*
35 * StreamCipher_Filter Constructor
36 */
37 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
38  buffer(DEFAULT_BUFFERSIZE)
39  {
41  cipher = af.make_stream_cipher(sc_name);
42  }
43 
44 /*
45 * StreamCipher_Filter Constructor
46 */
47 StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name,
48  const SymmetricKey& key) :
49  buffer(DEFAULT_BUFFERSIZE)
50  {
52  cipher = af.make_stream_cipher(sc_name);
53  cipher->set_key(key);
54  }
55 
56 /*
57 * Set the IV of a stream cipher
58 */
60  {
61  cipher->set_iv(iv.begin(), iv.length());
62  }
63 
64 /*
65 * Write data into a StreamCipher_Filter
66 */
67 void StreamCipher_Filter::write(const byte input[], size_t length)
68  {
69  while(length)
70  {
71  size_t copied = std::min<size_t>(length, buffer.size());
72  cipher->cipher(input, &buffer[0], copied);
73  send(buffer, copied);
74  input += copied;
75  length -= copied;
76  }
77  }
78 
79 /*
80 * Hash_Filter Constructor
81 */
82 Hash_Filter::Hash_Filter(const std::string& algo_spec,
83  size_t len) :
84  OUTPUT_LENGTH(len)
85  {
87  hash = af.make_hash_function(algo_spec);
88  }
89 
90 /*
91 * Complete a calculation by a Hash_Filter
92 */
94  {
95  SecureVector<byte> output = hash->final();
96  if(OUTPUT_LENGTH)
97  send(output, std::min<size_t>(OUTPUT_LENGTH, output.size()));
98  else
99  send(output);
100  }
101 
102 /*
103 * MAC_Filter Constructor
104 */
105 MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) :
106  OUTPUT_LENGTH(len)
107  {
109  mac = af.make_mac(mac_name);
110  }
111 
112 /*
113 * MAC_Filter Constructor
114 */
115 MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key,
116  size_t len) : OUTPUT_LENGTH(len)
117  {
119  mac = af.make_mac(mac_name);
120  mac->set_key(key);
121  }
122 
123 /*
124 * Complete a calculation by a MAC_Filter
125 */
127  {
128  SecureVector<byte> output = mac->final();
129  if(OUTPUT_LENGTH)
130  send(output, std::min<size_t>(OUTPUT_LENGTH, output.size()));
131  else
132  send(output);
133  }
134 
135 }
virtual void set_iv(const byte iv[], size_t iv_len)
StreamCipher_Filter(StreamCipher *cipher_obj)
Definition: algo_filt.cpp:17
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition: filters.h:172
Hash_Filter(HashFunction *hash_fun, size_t len=0)
Definition: filters.h:120
MessageAuthenticationCode * make_mac(const std::string &algo_spec, const std::string &provider="")
size_t length() const
Definition: symkey.h:25
StreamCipher * make_stream_cipher(const std::string &algo_spec, const std::string &provider="")
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
unsigned char byte
Definition: types.h:22
void send(const byte in[], size_t length)
Definition: filter.cpp:28
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
const byte * begin() const
Definition: symkey.h:35
Library_State & global_state()
virtual void cipher(const byte in[], byte out[], size_t len)=0
size_t size() const
Definition: secmem.h:29
HashFunction * make_hash_function(const std::string &algo_spec, const std::string &provider="")
void final(byte out[])
Definition: buf_comp.h:80
void set_iv(const InitializationVector &iv)
Definition: algo_filt.cpp:59
void write(const byte input[], size_t input_len)
Definition: algo_filt.cpp:67