aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-24 21:19:49 +0000
committerlloyd <[email protected]>2009-12-24 21:19:49 +0000
commit68c31ee72402946812e655ef3b9697462aab6b32 (patch)
treec6ff29549c1a32fe3fca05c1491f89b7eeef1d4f /src
parent7a9ab62f67c995e612c33556ef4730021b3c2910 (diff)
Remove modebase entirely. It made doing optimizations rather obnoxious and
didn't really contribute much in terms of code savings. CBC, CFB, and CTS now derive directly from Keyed_Filter. All the other modes already did this.
Diffstat (limited to 'src')
-rw-r--r--src/filters/modes/cbc/cbc.cpp97
-rw-r--r--src/filters/modes/cbc/cbc.h56
-rw-r--r--src/filters/modes/cfb/cfb.cpp148
-rw-r--r--src/filters/modes/cfb/cfb.h55
-rw-r--r--src/filters/modes/cts/cts.cpp148
-rw-r--r--src/filters/modes/cts/cts.h52
-rw-r--r--src/filters/modes/ecb/ecb.h1
-rw-r--r--src/filters/modes/info.txt6
-rw-r--r--src/filters/modes/mode_pad/mode_pad.cpp3
-rw-r--r--src/filters/modes/modebase.cpp54
-rw-r--r--src/filters/modes/modebase.h44
11 files changed, 393 insertions, 271 deletions
diff --git a/src/filters/modes/cbc/cbc.cpp b/src/filters/modes/cbc/cbc.cpp
index a52f4b2e1..48ecdf509 100644
--- a/src/filters/modes/cbc/cbc.cpp
+++ b/src/filters/modes/cbc/cbc.cpp
@@ -16,11 +16,14 @@ namespace Botan {
*/
CBC_Encryption::CBC_Encryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad) :
- BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE),
- padder(pad)
+ cipher(ciph), padder(pad)
{
- if(!padder->valid_blocksize(BLOCK_SIZE))
+ if(!padder->valid_blocksize(cipher->BLOCK_SIZE))
throw Invalid_Block_Size(name(), padder->name());
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
}
/*
@@ -30,31 +33,48 @@ CBC_Encryption::CBC_Encryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad,
const SymmetricKey& key,
const InitializationVector& iv) :
- BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE),
- padder(pad)
+ cipher(ciph), padder(pad)
{
- if(!padder->valid_blocksize(BLOCK_SIZE))
+ if(!padder->valid_blocksize(cipher->BLOCK_SIZE))
throw Invalid_Block_Size(name(), padder->name());
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
set_key(key);
set_iv(iv);
}
/*
+* Set the IV
+*/
+void CBC_Encryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
+ }
+
+/*
* Encrypt in CBC mode
*/
void CBC_Encryption::write(const byte input[], u32bit length)
{
while(length)
{
- u32bit xored = std::min(BLOCK_SIZE - position, length);
+ u32bit xored = std::min(cipher->BLOCK_SIZE - position, length);
xor_buf(state + position, input, xored);
input += xored;
length -= xored;
position += xored;
- if(position == BLOCK_SIZE)
+ if(position == cipher->BLOCK_SIZE)
{
cipher->encrypt(state);
- send(state, BLOCK_SIZE);
+ send(state, cipher->BLOCK_SIZE);
position = 0;
}
}
@@ -65,9 +85,9 @@ void CBC_Encryption::write(const byte input[], u32bit length)
*/
void CBC_Encryption::end_msg()
{
- SecureVector<byte> padding(BLOCK_SIZE);
+ SecureVector<byte> padding(cipher->BLOCK_SIZE);
padder->pad(padding, padding.size(), position);
- write(padding, padder->pad_bytes(BLOCK_SIZE, position));
+ write(padding, padder->pad_bytes(cipher->BLOCK_SIZE, position));
if(position != 0)
throw Exception(name() + ": Did not pad to full blocksize");
}
@@ -77,7 +97,7 @@ void CBC_Encryption::end_msg()
*/
std::string CBC_Encryption::name() const
{
- return (cipher->name() + "/" + mode_name + "/" + padder->name());
+ return (cipher->name() + "/CBC/" + padder->name());
}
/*
@@ -85,12 +105,15 @@ std::string CBC_Encryption::name() const
*/
CBC_Decryption::CBC_Decryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad) :
- BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE),
- padder(pad)
+ cipher(ciph), padder(pad)
{
- if(!padder->valid_blocksize(BLOCK_SIZE))
+ if(!padder->valid_blocksize(cipher->BLOCK_SIZE))
throw Invalid_Block_Size(name(), padder->name());
- temp.resize(BLOCK_SIZE);
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ temp.resize(cipher->BLOCK_SIZE);
+ position = 0;
}
/*
@@ -100,32 +123,50 @@ CBC_Decryption::CBC_Decryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad,
const SymmetricKey& key,
const InitializationVector& iv) :
- BlockCipherMode(ciph, "CBC", ciph->BLOCK_SIZE),
- padder(pad)
+ cipher(ciph), padder(pad)
{
- if(!padder->valid_blocksize(BLOCK_SIZE))
+ if(!padder->valid_blocksize(cipher->BLOCK_SIZE))
throw Invalid_Block_Size(name(), padder->name());
- temp.resize(BLOCK_SIZE);
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ temp.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
set_key(key);
set_iv(iv);
}
/*
+* Set the IV
+*/
+void CBC_Decryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
+ }
+
+/*
* Decrypt in CBC mode
*/
void CBC_Decryption::write(const byte input[], u32bit length)
{
while(length)
{
- if(position == BLOCK_SIZE)
+ if(position == cipher->BLOCK_SIZE)
{
cipher->decrypt(buffer, temp);
- xor_buf(temp, state, BLOCK_SIZE);
- send(temp, BLOCK_SIZE);
+ xor_buf(temp, state, cipher->BLOCK_SIZE);
+ send(temp, cipher->BLOCK_SIZE);
state = buffer;
position = 0;
}
- u32bit added = std::min(BLOCK_SIZE - position, length);
+
+ u32bit added = std::min(cipher->BLOCK_SIZE - position, length);
buffer.copy(position, input, added);
input += added;
length -= added;
@@ -138,11 +179,11 @@ void CBC_Decryption::write(const byte input[], u32bit length)
*/
void CBC_Decryption::end_msg()
{
- if(position != BLOCK_SIZE)
+ if(position != cipher->BLOCK_SIZE)
throw Decoding_Error(name());
cipher->decrypt(buffer, temp);
- xor_buf(temp, state, BLOCK_SIZE);
- send(temp, padder->unpad(temp, BLOCK_SIZE));
+ xor_buf(temp, state, cipher->BLOCK_SIZE);
+ send(temp, padder->unpad(temp, cipher->BLOCK_SIZE));
state = buffer;
position = 0;
}
@@ -152,7 +193,7 @@ void CBC_Decryption::end_msg()
*/
std::string CBC_Decryption::name() const
{
- return (cipher->name() + "/" + mode_name + "/" + padder->name());
+ return (cipher->name() + "/CBC/" + padder->name());
}
}
diff --git a/src/filters/modes/cbc/cbc.h b/src/filters/modes/cbc/cbc.h
index a926ac180..91ab21ab6 100644
--- a/src/filters/modes/cbc/cbc.h
+++ b/src/filters/modes/cbc/cbc.h
@@ -8,7 +8,8 @@
#ifndef BOTAN_CBC_H__
#define BOTAN_CBC_H__
-#include <botan/modebase.h>
+#include <botan/block_cipher.h>
+#include <botan/key_filt.h>
#include <botan/mode_pad.h>
namespace Botan {
@@ -16,38 +17,69 @@ namespace Botan {
/*
* CBC Encryption
*/
-class BOTAN_DLL CBC_Encryption : public BlockCipherMode
+class BOTAN_DLL CBC_Encryption : public Keyed_Filter
{
public:
- CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*);
- CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*,
- const SymmetricKey&, const InitializationVector&);
+ std::string name() const;
+
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CBC_Encryption(BlockCipher* cipher,
+ BlockCipherModePaddingMethod* padding);
+
+ CBC_Encryption(BlockCipher* cipher,
+ BlockCipherModePaddingMethod* padding,
+ const SymmetricKey& key,
+ const InitializationVector& iv);
~CBC_Encryption() { delete padder; }
private:
- std::string name() const;
void write(const byte[], u32bit);
void end_msg();
+
+ BlockCipher* cipher;
const BlockCipherModePaddingMethod* padder;
+ SecureVector<byte> buffer, state;
+ u32bit position;
};
/*
* CBC Decryption
*/
-class BOTAN_DLL CBC_Decryption : public BlockCipherMode
+class BOTAN_DLL CBC_Decryption : public Keyed_Filter
{
public:
- CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*);
- CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*,
- const SymmetricKey&, const InitializationVector&);
+ std::string name() const;
+
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CBC_Decryption(BlockCipher* cipher,
+ BlockCipherModePaddingMethod* padding);
+
+ CBC_Decryption(BlockCipher* cipher,
+ BlockCipherModePaddingMethod* padding,
+ const SymmetricKey& key,
+ const InitializationVector& iv);
~CBC_Decryption() { delete padder; }
private:
- std::string name() const;
void write(const byte[], u32bit);
void end_msg();
+
+ BlockCipher* cipher;
const BlockCipherModePaddingMethod* padder;
- SecureVector<byte> temp;
+ SecureVector<byte> buffer, state, temp;
+ u32bit position;
};
}
diff --git a/src/filters/modes/cfb/cfb.cpp b/src/filters/modes/cfb/cfb.cpp
index 777673d6f..778d47484 100644
--- a/src/filters/modes/cfb/cfb.cpp
+++ b/src/filters/modes/cfb/cfb.cpp
@@ -12,30 +12,21 @@
namespace Botan {
-namespace {
-
/*
-* Check the feedback size
+* CFB Encryption Constructor
*/
-void check_feedback(u32bit BLOCK_SIZE, u32bit FEEDBACK_SIZE, u32bit bits,
- const std::string& name)
+CFB_Encryption::CFB_Encryption(BlockCipher* ciph, u32bit fback_bits)
{
- if(FEEDBACK_SIZE == 0 || FEEDBACK_SIZE > BLOCK_SIZE || bits % 8 != 0)
- throw Invalid_Argument(name + ": Invalid feedback size " +
- to_string(bits));
- }
+ cipher = ciph;
+ feedback = fback_bits ? fback_bits / 8: cipher->BLOCK_SIZE;
-}
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
-/*
-* CFB Encryption Constructor
-*/
-CFB_Encryption::CFB_Encryption(BlockCipher* ciph,
- u32bit fback_bits) :
- BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1),
- FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE)
- {
- check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
+ if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->BLOCK_SIZE)
+ throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
+ to_string(fback_bits));
}
/*
@@ -44,15 +35,35 @@ CFB_Encryption::CFB_Encryption(BlockCipher* ciph,
CFB_Encryption::CFB_Encryption(BlockCipher* ciph,
const SymmetricKey& key,
const InitializationVector& iv,
- u32bit fback_bits) :
- BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1),
- FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE)
+ u32bit fback_bits)
{
- check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
+ cipher = ciph;
+ feedback = fback_bits ? fback_bits / 8: cipher->BLOCK_SIZE;
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
+ if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->BLOCK_SIZE)
+ throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
+ to_string(fback_bits));
+
set_key(key);
set_iv(iv);
}
+void CFB_Encryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
+
+ cipher->encrypt(state, buffer);
+ }
+
/*
* Encrypt data in CFB mode
*/
@@ -60,38 +71,39 @@ void CFB_Encryption::write(const byte input[], u32bit length)
{
while(length)
{
- u32bit xored = std::min(FEEDBACK_SIZE - position, length);
+ u32bit xored = std::min(feedback - position, length);
xor_buf(buffer + position, input, xored);
send(buffer + position, xored);
input += xored;
length -= xored;
position += xored;
- if(position == FEEDBACK_SIZE)
- feedback();
+
+ if(position == feedback)
+ {
+ for(u32bit j = 0; j != cipher->BLOCK_SIZE - feedback; ++j)
+ state[j] = state[j + feedback];
+ state.copy(cipher->BLOCK_SIZE - feedback, buffer, feedback);
+ cipher->encrypt(state, buffer);
+ position = 0;
+ }
}
}
/*
-* Do the feedback
+* CFB Decryption Constructor
*/
-void CFB_Encryption::feedback()
+CFB_Decryption::CFB_Decryption(BlockCipher* ciph, u32bit fback_bits)
{
- for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j)
- state[j] = state[j + FEEDBACK_SIZE];
- state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE);
- cipher->encrypt(state, buffer);
+ cipher = ciph;
+ feedback = fback_bits ? fback_bits / 8: cipher->BLOCK_SIZE;
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
position = 0;
- }
-/*
-* CFB Decryption Constructor
-*/
-CFB_Decryption::CFB_Decryption(BlockCipher* ciph,
- u32bit fback_bits) :
- BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1),
- FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE)
- {
- check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
+ if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->BLOCK_SIZE)
+ throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
+ to_string(fback_bits));
}
/*
@@ -100,15 +112,35 @@ CFB_Decryption::CFB_Decryption(BlockCipher* ciph,
CFB_Decryption::CFB_Decryption(BlockCipher* ciph,
const SymmetricKey& key,
const InitializationVector& iv,
- u32bit fback_bits) :
- BlockCipherMode(ciph, "CFB", ciph->BLOCK_SIZE, 1),
- FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE)
+ u32bit fback_bits)
{
- check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name());
+ cipher = ciph;
+ feedback = fback_bits ? fback_bits / 8: cipher->BLOCK_SIZE;
+
+ buffer.resize(cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
+ if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->BLOCK_SIZE)
+ throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
+ to_string(fback_bits));
+
set_key(key);
set_iv(iv);
}
+void CFB_Decryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
+
+ cipher->encrypt(state, buffer);
+ }
+
/*
* Decrypt data in CFB mode
*/
@@ -116,28 +148,22 @@ void CFB_Decryption::write(const byte input[], u32bit length)
{
while(length)
{
- u32bit xored = std::min(FEEDBACK_SIZE - position, length);
+ u32bit xored = std::min(feedback - position, length);
xor_buf(buffer + position, input, xored);
send(buffer + position, xored);
buffer.copy(position, input, xored);
input += xored;
length -= xored;
position += xored;
- if(position == FEEDBACK_SIZE)
- feedback();
+ if(position == feedback)
+ {
+ for(u32bit j = 0; j != cipher->BLOCK_SIZE - feedback; ++j)
+ state[j] = state[j + feedback];
+ state.copy(cipher->BLOCK_SIZE - feedback, buffer, feedback);
+ cipher->encrypt(state, buffer);
+ position = 0;
+ }
}
}
-/*
-* Do the feedback
-*/
-void CFB_Decryption::feedback()
- {
- for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j)
- state[j] = state[j + FEEDBACK_SIZE];
- state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE);
- cipher->encrypt(state, buffer);
- position = 0;
- }
-
}
diff --git a/src/filters/modes/cfb/cfb.h b/src/filters/modes/cfb/cfb.h
index 7810c00e4..917125e46 100644
--- a/src/filters/modes/cfb/cfb.h
+++ b/src/filters/modes/cfb/cfb.h
@@ -8,38 +8,67 @@
#ifndef BOTAN_CFB_H__
#define BOTAN_CFB_H__
-#include <botan/modebase.h>
+#include <botan/block_cipher.h>
+#include <botan/key_filt.h>
namespace Botan {
/*
* CFB Encryption
*/
-class BOTAN_DLL CFB_Encryption : public BlockCipherMode
+class BOTAN_DLL CFB_Encryption : public Keyed_Filter
{
public:
- CFB_Encryption(BlockCipher*, u32bit = 0);
- CFB_Encryption(BlockCipher*, const SymmetricKey&,
- const InitializationVector&, u32bit = 0);
+ std::string name() const { return cipher->name() + "/CFB"; }
+
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CFB_Encryption(BlockCipher* cipher, u32bit feedback = 0);
+
+ CFB_Encryption(BlockCipher* cipher,
+ const SymmetricKey& key,
+ const InitializationVector& iv,
+ u32bit feedback = 0);
private:
void write(const byte[], u32bit);
- void feedback();
- const u32bit FEEDBACK_SIZE;
+
+ BlockCipher* cipher;
+ SecureVector<byte> buffer, state;
+ u32bit position, feedback;
};
/*
* CFB Decryption
*/
-class BOTAN_DLL CFB_Decryption : public BlockCipherMode
+class BOTAN_DLL CFB_Decryption : public Keyed_Filter
{
public:
- CFB_Decryption(BlockCipher*, u32bit = 0);
- CFB_Decryption(BlockCipher*, const SymmetricKey&,
- const InitializationVector&, u32bit = 0);
+ std::string name() const { return cipher->name() + "/CFB"; }
+
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CFB_Decryption(BlockCipher* cipher, u32bit feedback = 0);
+
+ CFB_Decryption(BlockCipher* cipher,
+ const SymmetricKey& key,
+ const InitializationVector& iv,
+ u32bit feedback = 0);
private:
void write(const byte[], u32bit);
- void feedback();
- const u32bit FEEDBACK_SIZE;
+
+ BlockCipher* cipher;
+ SecureVector<byte> buffer, state;
+ u32bit position, feedback;
};
}
diff --git a/src/filters/modes/cts/cts.cpp b/src/filters/modes/cts/cts.cpp
index 226a31898..3a15a1d68 100644
--- a/src/filters/modes/cts/cts.cpp
+++ b/src/filters/modes/cts/cts.cpp
@@ -12,13 +12,53 @@
namespace Botan {
/*
+* CTS Encryption Constructor
+*/
+CTS_Encryption::CTS_Encryption(BlockCipher* ciph) :
+ cipher(ciph)
+ {
+ buffer.resize(2 * cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
+ }
+
+/*
+* CTS Encryption Constructor
+*/
+CTS_Encryption::CTS_Encryption(BlockCipher* ciph,
+ const SymmetricKey& key,
+ const InitializationVector& iv) :
+ cipher(ciph)
+ {
+ buffer.resize(2 * cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
+ set_key(key);
+ set_iv(iv);
+ }
+
+/*
+* Set the IV
+*/
+void CTS_Encryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
+ }
+
+/*
* Encrypt a block
*/
void CTS_Encryption::encrypt(const byte block[])
{
- xor_buf(state, block, BLOCK_SIZE);
+ xor_buf(state, block, cipher->BLOCK_SIZE);
cipher->encrypt(state);
- send(state, BLOCK_SIZE);
+ send(state, cipher->BLOCK_SIZE);
}
/*
@@ -26,7 +66,7 @@ void CTS_Encryption::encrypt(const byte block[])
*/
void CTS_Encryption::write(const byte input[], u32bit length)
{
- u32bit copied = std::min(BUFFER_SIZE - position, length);
+ u32bit copied = std::min(buffer.size() - position, length);
buffer.copy(position, input, copied);
length -= copied;
input += copied;
@@ -35,21 +75,21 @@ void CTS_Encryption::write(const byte input[], u32bit length)
if(length == 0) return;
encrypt(buffer);
- if(length > BLOCK_SIZE)
+ if(length > cipher->BLOCK_SIZE)
{
- encrypt(buffer + BLOCK_SIZE);
- while(length > 2*BLOCK_SIZE)
+ encrypt(buffer + cipher->BLOCK_SIZE);
+ while(length > 2*cipher->BLOCK_SIZE)
{
encrypt(input);
- length -= BLOCK_SIZE;
- input += BLOCK_SIZE;
+ length -= cipher->BLOCK_SIZE;
+ input += cipher->BLOCK_SIZE;
}
position = 0;
}
else
{
- copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE);
- position = BLOCK_SIZE;
+ copy_mem(buffer.begin(), buffer + cipher->BLOCK_SIZE, cipher->BLOCK_SIZE);
+ position = cipher->BLOCK_SIZE;
}
buffer.copy(position, input, length);
position += length;
@@ -60,14 +100,56 @@ void CTS_Encryption::write(const byte input[], u32bit length)
*/
void CTS_Encryption::end_msg()
{
- if(position < BLOCK_SIZE + 1)
+ if(position < cipher->BLOCK_SIZE + 1)
throw Exception("CTS_Encryption: insufficient data to encrypt");
- xor_buf(state, buffer, BLOCK_SIZE);
+ xor_buf(state, buffer, cipher->BLOCK_SIZE);
cipher->encrypt(state);
SecureVector<byte> cn = state;
- clear_mem(buffer + position, BUFFER_SIZE - position);
- encrypt(buffer + BLOCK_SIZE);
- send(cn, position - BLOCK_SIZE);
+ clear_mem(buffer + position, buffer.size() - position);
+ encrypt(buffer + cipher->BLOCK_SIZE);
+ send(cn, position - cipher->BLOCK_SIZE);
+ }
+
+/*
+* CTS Decryption Constructor
+*/
+CTS_Decryption::CTS_Decryption(BlockCipher* ciph) :
+ cipher(ciph)
+ {
+ buffer.resize(2 * cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ temp.resize(cipher->BLOCK_SIZE);
+ position = 0;
+ }
+
+/*
+* CTS Decryption Constructor
+*/
+CTS_Decryption::CTS_Decryption(BlockCipher* ciph,
+ const SymmetricKey& key,
+ const InitializationVector& iv) :
+ cipher(ciph)
+ {
+ buffer.resize(2 * cipher->BLOCK_SIZE);
+ state.resize(cipher->BLOCK_SIZE);
+ temp.resize(cipher->BLOCK_SIZE);
+ position = 0;
+
+ set_key(key);
+ set_iv(iv);
+ }
+
+/*
+* Set the IV
+*/
+void CTS_Decryption::set_iv(const InitializationVector& iv)
+ {
+ if(iv.length() != state.size())
+ throw Invalid_IV_Length(name(), iv.length());
+
+ state = iv.bits_of();
+ buffer.clear();
+ position = 0;
}
/*
@@ -76,9 +158,9 @@ void CTS_Encryption::end_msg()
void CTS_Decryption::decrypt(const byte block[])
{
cipher->decrypt(block, temp);
- xor_buf(temp, state, BLOCK_SIZE);
- send(temp, BLOCK_SIZE);
- state.copy(block, BLOCK_SIZE);
+ xor_buf(temp, state, cipher->BLOCK_SIZE);
+ send(temp, cipher->BLOCK_SIZE);
+ state.copy(block, cipher->BLOCK_SIZE);
}
/*
@@ -86,7 +168,7 @@ void CTS_Decryption::decrypt(const byte block[])
*/
void CTS_Decryption::write(const byte input[], u32bit length)
{
- u32bit copied = std::min(BUFFER_SIZE - position, length);
+ u32bit copied = std::min(buffer.size() - position, length);
buffer.copy(position, input, copied);
length -= copied;
input += copied;
@@ -95,21 +177,21 @@ void CTS_Decryption::write(const byte input[], u32bit length)
if(length == 0) return;
decrypt(buffer);
- if(length > BLOCK_SIZE)
+ if(length > cipher->BLOCK_SIZE)
{
- decrypt(buffer + BLOCK_SIZE);
- while(length > 2*BLOCK_SIZE)
+ decrypt(buffer + cipher->BLOCK_SIZE);
+ while(length > 2*cipher->BLOCK_SIZE)
{
decrypt(input);
- length -= BLOCK_SIZE;
- input += BLOCK_SIZE;
+ length -= cipher->BLOCK_SIZE;
+ input += cipher->BLOCK_SIZE;
}
position = 0;
}
else
{
- copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE);
- position = BLOCK_SIZE;
+ copy_mem(buffer.begin(), buffer + cipher->BLOCK_SIZE, cipher->BLOCK_SIZE);
+ position = cipher->BLOCK_SIZE;
}
buffer.copy(position, input, length);
position += length;
@@ -121,14 +203,14 @@ void CTS_Decryption::write(const byte input[], u32bit length)
void CTS_Decryption::end_msg()
{
cipher->decrypt(buffer, temp);
- xor_buf(temp, buffer + BLOCK_SIZE, position - BLOCK_SIZE);
+ xor_buf(temp, buffer + cipher->BLOCK_SIZE, position - cipher->BLOCK_SIZE);
SecureVector<byte> xn = temp;
- copy_mem(buffer + position, xn + (position - BLOCK_SIZE),
- BUFFER_SIZE - position);
- cipher->decrypt(buffer + BLOCK_SIZE, temp);
- xor_buf(temp, state, BLOCK_SIZE);
- send(temp, BLOCK_SIZE);
- send(xn, position - BLOCK_SIZE);
+ copy_mem(buffer + position, xn + (position - cipher->BLOCK_SIZE),
+ buffer.size() - position);
+ cipher->decrypt(buffer + cipher->BLOCK_SIZE, temp);
+ xor_buf(temp, state, cipher->BLOCK_SIZE);
+ send(temp, cipher->BLOCK_SIZE);
+ send(xn, position - cipher->BLOCK_SIZE);
}
}
diff --git a/src/filters/modes/cts/cts.h b/src/filters/modes/cts/cts.h
index 1a2cae44e..4a7513fa0 100644
--- a/src/filters/modes/cts/cts.h
+++ b/src/filters/modes/cts/cts.h
@@ -8,51 +8,69 @@
#ifndef BOTAN_CTS_H__
#define BOTAN_CTS_H__
-#include <botan/modebase.h>
#include <botan/block_cipher.h>
+#include <botan/key_filt.h>
namespace Botan {
/*
* CTS Encryption
*/
-class BOTAN_DLL CTS_Encryption : public BlockCipherMode
+class BOTAN_DLL CTS_Encryption : public Keyed_Filter
{
public:
- CTS_Encryption(BlockCipher* ciph) :
- BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) {}
+ std::string name() const { return cipher->name() + "/CTS"; }
- CTS_Encryption(BlockCipher* ciph,
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CTS_Encryption(BlockCipher* cipher);
+
+ CTS_Encryption(BlockCipher* cipher,
const SymmetricKey& key,
- const InitializationVector& iv) :
- BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
- { set_key(key); set_iv(iv); }
+ const InitializationVector& iv);
private:
void write(const byte[], u32bit);
void end_msg();
void encrypt(const byte[]);
+
+ BlockCipher* cipher;
+ SecureVector<byte> buffer, state;
+ u32bit position;
};
/*
* CTS Decryption
*/
-class BOTAN_DLL CTS_Decryption : public BlockCipherMode
+class BOTAN_DLL CTS_Decryption : public Keyed_Filter
{
public:
- CTS_Decryption(BlockCipher* ciph) :
- BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
- { temp.resize(BLOCK_SIZE); }
+ std::string name() const { return cipher->name() + "/CTS"; }
- CTS_Decryption(BlockCipher* ciph,
+ void set_iv(const InitializationVector&);
+
+ void set_key(const SymmetricKey& key) { cipher->set_key(key); }
+
+ bool valid_keylength(u32bit key_len) const
+ { return cipher->valid_keylength(key_len); }
+
+ CTS_Decryption(BlockCipher* cipher);
+
+ CTS_Decryption(BlockCipher* cipher,
const SymmetricKey& key,
- const InitializationVector& iv) :
- BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
- { set_key(key); set_iv(iv); temp.resize(BLOCK_SIZE); }
+ const InitializationVector& iv);
private:
void write(const byte[], u32bit);
void end_msg();
void decrypt(const byte[]);
- SecureVector<byte> temp;
+
+ BlockCipher* cipher;
+ SecureVector<byte> buffer, state, temp;
+ u32bit position;
};
}
diff --git a/src/filters/modes/ecb/ecb.h b/src/filters/modes/ecb/ecb.h
index f0d75fdc3..2b3b3fe83 100644
--- a/src/filters/modes/ecb/ecb.h
+++ b/src/filters/modes/ecb/ecb.h
@@ -8,7 +8,6 @@
#ifndef BOTAN_ECB_H__
#define BOTAN_ECB_H__
-#include <botan/basefilt.h>
#include <botan/block_cipher.h>
#include <botan/mode_pad.h>
#include <botan/key_filt.h>
diff --git a/src/filters/modes/info.txt b/src/filters/modes/info.txt
deleted file mode 100644
index 6d27c9709..000000000
--- a/src/filters/modes/info.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-define CIPHER_MODEBASE
-
-<requires>
-block
-filters
-</requires>
diff --git a/src/filters/modes/mode_pad/mode_pad.cpp b/src/filters/modes/mode_pad/mode_pad.cpp
index 2204c28b5..94f84fa03 100644
--- a/src/filters/modes/mode_pad/mode_pad.cpp
+++ b/src/filters/modes/mode_pad/mode_pad.cpp
@@ -120,8 +120,7 @@ u32bit OneAndZeros_Padding::unpad(const byte block[], u32bit size) const
*/
bool OneAndZeros_Padding::valid_blocksize(u32bit size) const
{
- if(size) return true;
- else return false;
+ return (size > 0);
}
}
diff --git a/src/filters/modes/modebase.cpp b/src/filters/modes/modebase.cpp
deleted file mode 100644
index 59ee55a8a..000000000
--- a/src/filters/modes/modebase.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
-* Block Cipher Mode
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/modebase.h>
-
-namespace Botan {
-
-/*
-* Block Cipher Mode Constructor
-*/
-BlockCipherMode::BlockCipherMode(BlockCipher* cipher_ptr,
- const std::string& cipher_mode_name,
- u32bit iv_size, u32bit iv_meth,
- u32bit buf_mult) :
- BLOCK_SIZE(cipher_ptr->BLOCK_SIZE), BUFFER_SIZE(buf_mult * BLOCK_SIZE),
- IV_METHOD(iv_meth), mode_name(cipher_mode_name)
- {
- cipher = cipher_ptr;
- buffer.resize(BUFFER_SIZE);
- state.resize(iv_size);
- position = 0;
- }
-
-/*
-* Return the name of this type
-*/
-std::string BlockCipherMode::name() const
- {
- return (cipher->name() + "/" + mode_name);
- }
-
-/*
-* Set the IV
-*/
-void BlockCipherMode::set_iv(const InitializationVector& new_iv)
- {
- if(new_iv.length() != state.size())
- throw Invalid_IV_Length(name(), new_iv.length());
-
- state = new_iv.bits_of();
- buffer.clear();
- position = 0;
-
- if(IV_METHOD == 1)
- cipher->encrypt(state, buffer);
- else if(IV_METHOD == 2)
- cipher->encrypt(state);
- }
-
-}
diff --git a/src/filters/modes/modebase.h b/src/filters/modes/modebase.h
deleted file mode 100644
index 4a15524b6..000000000
--- a/src/filters/modes/modebase.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
-* Block Cipher Mode
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#ifndef BOTAN_MODEBASE_H__
-#define BOTAN_MODEBASE_H__
-
-#include <botan/key_filt.h>
-#include <botan/block_cipher.h>
-
-namespace Botan {
-
-/**
-* This class represents an abstract block cipher mode
-*/
-class BOTAN_DLL BlockCipherMode : public Keyed_Filter
- {
- public:
- std::string name() const;
-
- void set_iv(const InitializationVector&);
- void set_key(const SymmetricKey& key) { cipher->set_key(key); }
-
- bool valid_keylength(u32bit key_len) const
- { return cipher->valid_keylength(key_len); }
-
- BlockCipherMode(BlockCipher*, const std::string&,
- u32bit, u32bit = 0, u32bit = 1);
-
- virtual ~BlockCipherMode() { delete cipher; }
- protected:
- const u32bit BLOCK_SIZE, BUFFER_SIZE, IV_METHOD;
- const std::string mode_name;
- BlockCipher* cipher;
- SecureVector<byte> buffer, state;
- u32bit position;
- };
-
-}
-
-#endif