Botan 1.10.17

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 */
17 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher) :
18 buffer(DEFAULT_BUFFERSIZE)
19 {
20 cipher = stream_cipher;
21 }
22
23 /*
24 * StreamCipher_Filter Constructor
25 */
26 StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher,
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 {
40 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
51 Algorithm_Factory& af = global_state().algorithm_factory();
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 */
59 void StreamCipher_Filter::set_iv(const InitializationVector& iv)
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 {
86 Algorithm_Factory& af = global_state().algorithm_factory();
87 hash = af.make_hash_function(algo_spec);
88 }
89
90 /*
91 * Complete a calculation by a Hash_Filter
92 */
93 void Hash_Filter::end_msg()
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 {
108 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
118 Algorithm_Factory& af = global_state().algorithm_factory();
119 mac = af.make_mac(mac_name);
120 mac->set_key(key);
121 }
122
123 /*
124 * Complete a calculation by a MAC_Filter
125 */
126 void MAC_Filter::end_msg()
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 }
Botan::StreamCipher::set_iv
virtual void set_iv(const byte iv[], size_t iv_len)
Definition: stream_cipher.cpp:12
Botan::Algorithm_Factory
Definition: algo_factory.h:34
Botan::OctetString
Definition: symkey.h:19
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::StreamCipher_Filter::StreamCipher_Filter
StreamCipher_Filter(StreamCipher *cipher_obj)
Definition: algo_filt.cpp:17
Botan::MAC_Filter::MAC_Filter
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition: filters.h:172
Botan::Hash_Filter::Hash_Filter
Hash_Filter(HashFunction *hash_fun, size_t len=0)
Definition: filters.h:120
Botan::Algorithm_Factory::make_mac
MessageAuthenticationCode * make_mac(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:286
Botan::Algorithm_Factory::make_stream_cipher
StreamCipher * make_stream_cipher(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:262
Botan::OctetString::begin
const byte * begin() const
Definition: symkey.h:35
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Filter::send
void send(const byte in[], size_t length)
Definition: filter.cpp:28
Botan::SymmetricAlgorithm::set_key
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:60
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::StreamCipher
Definition: stream_cipher.h:18
Botan::StreamCipher::cipher
virtual void cipher(const byte in[], byte out[], size_t len)=0
Botan
Definition: algo_base.h:14
Botan::MAC_Filter::end_msg
void end_msg()
Definition: algo_filt.cpp:126
Botan::Hash_Filter::end_msg
void end_msg()
Definition: algo_filt.cpp:93
Botan::Algorithm_Factory::make_hash_function
HashFunction * make_hash_function(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:274
Botan::Buffered_Computation::final
void final(byte out[])
Definition: buf_comp.h:80
Botan::OctetString::length
size_t length() const
Definition: symkey.h:25
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::SecureVector< byte >
Botan::StreamCipher_Filter::set_iv
void set_iv(const InitializationVector &iv)
Definition: algo_filt.cpp:59
Botan::StreamCipher_Filter::write
void write(const byte input[], size_t input_len)
Definition: algo_filt.cpp:67