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
12namespace 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*/
37StreamCipher_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*/
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*/
67void 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*/
82Hash_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*/
105MAC_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*/
115MAC_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}
StreamCipher * make_stream_cipher(const std::string &algo_spec, const std::string &provider="")
HashFunction * make_hash_function(const std::string &algo_spec, const std::string &provider="")
MessageAuthenticationCode * make_mac(const std::string &algo_spec, const std::string &provider="")
void send(const byte in[], size_t length)
Definition filter.cpp:28
Hash_Filter(HashFunction *hash_fun, size_t len=0)
Definition filters.h:120
Algorithm_Factory & algorithm_factory() const
Definition libstate.cpp:173
MAC_Filter(MessageAuthenticationCode *mac_obj, size_t out_len=0)
Definition filters.h:172
size_t size() const
Definition secmem.h:29
const byte * begin() const
Definition symkey.h:35
size_t length() const
Definition symkey.h:25
void write(const byte input[], size_t input_len)
Definition algo_filt.cpp:67
void set_iv(const InitializationVector &iv)
Definition algo_filt.cpp:59
StreamCipher_Filter(StreamCipher *cipher_obj)
Definition algo_filt.cpp:17
void set_key(const SymmetricKey &key)
Definition sym_algo.h:60
OctetString SymmetricKey
Definition symkey.h:147
Library_State & global_state()
OctetString InitializationVector
Definition symkey.h:152