#include "botan-1.6/include/ecb.h"
#include "botan-1.6/include/lookup.h"
namespace Enctain {
namespace Botan {
ECB::ECB(const std::string& cipher_name, const std::string& padding_name) :
BlockCipherMode(cipher_name, "ECB", 0), padder(get_bc_pad(padding_name))
{
}
bool ECB::valid_iv_size(u32bit iv_size) const
{
if(iv_size == 0)
return true;
return false;
}
std::string ECB::name() const
{
return (cipher->name() + "/" + mode_name + "/" + padder->name());
}
ECB_Encryption::ECB_Encryption(const std::string& cipher_name,
const std::string& padding_name) :
ECB(cipher_name, padding_name)
{
}
ECB_Encryption::ECB_Encryption(const std::string& cipher_name,
const std::string& padding_name,
const SymmetricKey& key) :
ECB(cipher_name, padding_name)
{
set_key(key);
}
void ECB_Encryption::write(const byte input[], u32bit length)
{
buffer.copy(position, input, length);
if(position + length >= BLOCK_SIZE)
{
cipher->encrypt(buffer);
send(buffer, BLOCK_SIZE);
input += (BLOCK_SIZE - position);
length -= (BLOCK_SIZE - position);
while(length >= BLOCK_SIZE)
{
cipher->encrypt(input, buffer);
send(buffer, BLOCK_SIZE);
input += BLOCK_SIZE;
length -= BLOCK_SIZE;
}
buffer.copy(input, length);
position = 0;
}
position += length;
}
void ECB_Encryption::end_msg()
{
SecureVector<byte> padding(BLOCK_SIZE);
padder->pad(padding, padding.size(), position);
write(padding, padder->pad_bytes(BLOCK_SIZE, position));
if(position != 0)
throw Encoding_Error(name() + ": Did not pad to full blocksize");
}
ECB_Decryption::ECB_Decryption(const std::string& cipher_name,
const std::string& padding_name) :
ECB(cipher_name, padding_name)
{
}
ECB_Decryption::ECB_Decryption(const std::string& cipher_name,
const std::string& padding_name,
const SymmetricKey& key) :
ECB(cipher_name, padding_name)
{
set_key(key);
}
void ECB_Decryption::write(const byte input[], u32bit length)
{
buffer.copy(position, input, length);
if(position + length > BLOCK_SIZE)
{
cipher->decrypt(buffer);
send(buffer, BLOCK_SIZE);
input += (BLOCK_SIZE - position);
length -= (BLOCK_SIZE - position);
while(length > BLOCK_SIZE)
{
cipher->decrypt(input, buffer);
send(buffer, BLOCK_SIZE);
input += BLOCK_SIZE;
length -= BLOCK_SIZE;
}
buffer.copy(input, length);
position = 0;
}
position += length;
}
void ECB_Decryption::end_msg()
{
if(position != BLOCK_SIZE)
throw Decoding_Error(name());
cipher->decrypt(buffer);
send(buffer, padder->unpad(buffer, BLOCK_SIZE));
state = buffer;
position = 0;
}
}
}