#include "botan-1.6/include/filters.h"
#include "botan-1.6/include/lookup.h"
#include <algorithm>
namespace Enctain {
namespace Botan {
StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) :
buffer(DEFAULT_BUFFERSIZE)
{
base_ptr = cipher = get_stream_cipher(sc_name);
}
StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name,
const SymmetricKey& key) :
buffer(DEFAULT_BUFFERSIZE)
{
base_ptr = cipher = get_stream_cipher(sc_name);
cipher->set_key(key);
}
void StreamCipher_Filter::set_iv(const InitializationVector& iv)
{
cipher->resync(iv.begin(), iv.length());
}
void StreamCipher_Filter::write(const byte input[], u32bit length)
{
while(length)
{
u32bit copied = std::min(length, buffer.size());
cipher->encrypt(input, buffer, copied);
send(buffer, copied);
input += copied;
length -= copied;
}
}
Hash_Filter::Hash_Filter(const std::string& hash_name, u32bit len) :
OUTPUT_LENGTH(len)
{
hash = get_hash(hash_name);
}
void Hash_Filter::end_msg()
{
SecureVector<byte> output = hash->final();
if(OUTPUT_LENGTH)
send(output, std::min(OUTPUT_LENGTH, output.size()));
else
send(output);
}
MAC_Filter::MAC_Filter(const std::string& mac_name, u32bit len) :
OUTPUT_LENGTH(len)
{
base_ptr = mac = get_mac(mac_name);
}
MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key,
u32bit len) : OUTPUT_LENGTH(len)
{
base_ptr = mac = get_mac(mac_name);
mac->set_key(key);
}
void MAC_Filter::end_msg()
{
SecureVector<byte> output = mac->final();
if(OUTPUT_LENGTH)
send(output, std::min(OUTPUT_LENGTH, output.size()));
else
send(output);
}
}
}