aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-10-14 22:35:03 +0000
committerlloyd <[email protected]>2009-10-14 22:35:03 +0000
commit09a17201a8132f8422a4c371cf1e56553317bc66 (patch)
tree912dff1d664d10a473554d6517ba44c8e980545e
parent28f875732c6379531e28c12091c44031941e0dff (diff)
Cleanups/random changes in the stream cipher code:
Remove encrypt, decrypt - replace by cipher() and cipher1() Remove seek() - not well supported/tested, I want to redo with a new interface once CTR and OFB modes become stream ciphers. Rename resync to set_iv() Remove StreamCipher::IV_LENGTH and add StreamCipher::valid_iv_length() to allow multiple IV lengths (as for instance Turing allows, as would Salsa20 if XSalsa20 were supported).
-rw-r--r--src/benchmark/benchmark.cpp2
-rw-r--r--src/block/lion/lion.cpp8
-rw-r--r--src/filters/algo_filt.cpp4
-rw-r--r--src/filters/filters.h14
-rw-r--r--src/stream/arc4/arc4.h5
-rw-r--r--src/stream/info.txt1
-rw-r--r--src/stream/salsa20/salsa20.cpp8
-rw-r--r--src/stream/salsa20/salsa20.h12
-rw-r--r--src/stream/stream_cipher.cpp30
-rw-r--r--src/stream/stream_cipher.h67
-rw-r--r--src/stream/turing/turing.cpp6
-rw-r--r--src/stream/turing/turing.h8
-rw-r--r--src/stream/wid_wake/wid_wake.cpp9
-rw-r--r--src/stream/wid_wake/wid_wake.h10
14 files changed, 73 insertions, 111 deletions
diff --git a/src/benchmark/benchmark.cpp b/src/benchmark/benchmark.cpp
index 6e61baa59..41c9cd10c 100644
--- a/src/benchmark/benchmark.cpp
+++ b/src/benchmark/benchmark.cpp
@@ -85,7 +85,7 @@ bench_stream_cipher(StreamCipher* stream_cipher,
while(nanoseconds_used < nanoseconds_max)
{
- stream_cipher->encrypt(buf, buf_len);
+ stream_cipher->cipher1(buf, buf_len);
++reps;
nanoseconds_used = timer.clock() - start;
}
diff --git a/src/block/lion/lion.cpp b/src/block/lion/lion.cpp
index 83c1e3aa3..0ed7b2630 100644
--- a/src/block/lion/lion.cpp
+++ b/src/block/lion/lion.cpp
@@ -22,7 +22,7 @@ void Lion::encrypt_n(const byte in[], byte out[], u32bit blocks) const
{
xor_buf(buffer, in, key1, LEFT_SIZE);
cipher->set_key(buffer, LEFT_SIZE);
- cipher->encrypt(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
+ cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
hash->update(out + LEFT_SIZE, RIGHT_SIZE);
hash->final(buffer);
@@ -30,7 +30,7 @@ void Lion::encrypt_n(const byte in[], byte out[], u32bit blocks) const
xor_buf(buffer, out, key2, LEFT_SIZE);
cipher->set_key(buffer, LEFT_SIZE);
- cipher->encrypt(out + LEFT_SIZE, RIGHT_SIZE);
+ cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
@@ -48,7 +48,7 @@ void Lion::decrypt_n(const byte in[], byte out[], u32bit blocks) const
{
xor_buf(buffer, in, key2, LEFT_SIZE);
cipher->set_key(buffer, LEFT_SIZE);
- cipher->encrypt(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
+ cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE);
hash->update(out + LEFT_SIZE, RIGHT_SIZE);
hash->final(buffer);
@@ -56,7 +56,7 @@ void Lion::decrypt_n(const byte in[], byte out[], u32bit blocks) const
xor_buf(buffer, out, key1, LEFT_SIZE);
cipher->set_key(buffer, LEFT_SIZE);
- cipher->encrypt(out + LEFT_SIZE, RIGHT_SIZE);
+ cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE);
in += BLOCK_SIZE;
out += BLOCK_SIZE;
diff --git a/src/filters/algo_filt.cpp b/src/filters/algo_filt.cpp
index 3268276a6..9a469b2d8 100644
--- a/src/filters/algo_filt.cpp
+++ b/src/filters/algo_filt.cpp
@@ -47,7 +47,7 @@ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name,
*/
void StreamCipher_Filter::set_iv(const InitializationVector& iv)
{
- cipher->resync(iv.begin(), iv.length());
+ cipher->set_iv(iv.begin(), iv.length());
}
/*
@@ -58,7 +58,7 @@ void StreamCipher_Filter::write(const byte input[], u32bit length)
while(length)
{
u32bit copied = std::min(length, buffer.size());
- cipher->encrypt(input, buffer, copied);
+ cipher->cipher(input, buffer, copied);
send(buffer, copied);
input += copied;
length -= copied;
diff --git a/src/filters/filters.h b/src/filters/filters.h
index 964be0bd8..418caf0aa 100644
--- a/src/filters/filters.h
+++ b/src/filters/filters.h
@@ -44,18 +44,8 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter
*/
void write(const byte input[], u32bit input_len);
- /**
- * Seek in the stream.
- * @param position the position to seek ahead
- */
- void seek(u32bit position) { cipher->seek(position); }
-
- /**
- * Find out whether the cipher underlying this filter supports
- * resyncing.
- * @return true if the cipher supports resyncing
- */
- bool supports_resync() const { return (cipher->IV_LENGTH != 0); }
+ bool valid_iv_length(u32bit iv_len)
+ { return cipher->valid_iv_length(iv_len); }
/**
* Set the initialization vector for this filter.
diff --git a/src/stream/arc4/arc4.h b/src/stream/arc4/arc4.h
index aa2cea7fe..3f92fa914 100644
--- a/src/stream/arc4/arc4.h
+++ b/src/stream/arc4/arc4.h
@@ -19,13 +19,16 @@ namespace Botan {
class BOTAN_DLL ARC4 : public StreamCipher
{
public:
+ void cipher(const byte in[], byte out[], u32bit length);
+
void clear() throw();
std::string name() const;
+
StreamCipher* clone() const { return new ARC4(SKIP); }
+
ARC4(u32bit = 0);
~ARC4() { clear(); }
private:
- void cipher(const byte[], byte[], u32bit);
void key_schedule(const byte[], u32bit);
void generate();
diff --git a/src/stream/info.txt b/src/stream/info.txt
index 295c73708..f8f4b22d5 100644
--- a/src/stream/info.txt
+++ b/src/stream/info.txt
@@ -6,7 +6,6 @@ define STREAM_CIPHER
<add>
stream_cipher.h
-stream_cipher.cpp
</add>
<requires>
diff --git a/src/stream/salsa20/salsa20.cpp b/src/stream/salsa20/salsa20.cpp
index 9c7c811f0..a147cdb45 100644
--- a/src/stream/salsa20/salsa20.cpp
+++ b/src/stream/salsa20/salsa20.cpp
@@ -162,15 +162,15 @@ void Salsa20::key_schedule(const byte key[], u32bit length)
}
const byte ZERO[8] = { 0 };
- resync(ZERO, sizeof(ZERO));
+ set_iv(ZERO, sizeof(ZERO));
}
/*
* Return the name of this type
*/
-void Salsa20::resync(const byte iv[], u32bit length)
+void Salsa20::set_iv(const byte iv[], u32bit length)
{
- if(length != IV_LENGTH)
+ if(!valid_iv_length(length))
throw Invalid_IV_Length(name(), length);
state[6] = load_le<u32bit>(iv, 0);
@@ -207,7 +207,7 @@ void Salsa20::clear() throw()
/*
* Salsa20 Constructor
*/
-Salsa20::Salsa20() : StreamCipher(16, 32, 16, 8)
+Salsa20::Salsa20() : StreamCipher(16, 32, 16)
{
clear();
}
diff --git a/src/stream/salsa20/salsa20.h b/src/stream/salsa20/salsa20.h
index 3dbfddb50..a3e9a3706 100644
--- a/src/stream/salsa20/salsa20.h
+++ b/src/stream/salsa20/salsa20.h
@@ -18,17 +18,21 @@ namespace Botan {
class BOTAN_DLL Salsa20 : public StreamCipher
{
public:
+ void cipher(const byte in[], byte out[], u32bit length);
+
+ void set_iv(const byte iv[], u32bit iv_len);
+
+ bool valid_iv_length(u32bit iv_len) const
+ { return (iv_len == 8); }
+
void clear() throw();
std::string name() const;
StreamCipher* clone() const { return new Salsa20; }
- void resync(const byte[], u32bit);
-
Salsa20();
~Salsa20() { clear(); }
private:
- void cipher(const byte[], byte[], u32bit);
- void key_schedule(const byte[], u32bit);
+ void key_schedule(const byte key[], u32bit key_len);
SecureBuffer<u32bit, 16> state;
diff --git a/src/stream/stream_cipher.cpp b/src/stream/stream_cipher.cpp
deleted file mode 100644
index 68bb5d4f0..000000000
--- a/src/stream/stream_cipher.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
-* Stream Cipher Default Implementation for IV and Seek
-* (C) 1999-2007 Jack Lloyd
-*
-* Distributed under the terms of the Botan license
-*/
-
-#include <botan/stream_cipher.h>
-
-namespace Botan {
-
-/*
-* Default StreamCipher Resync Operation
-*/
-void StreamCipher::resync(const byte[], u32bit length)
- {
- if(length)
- throw Exception("The stream cipher " + name() +
- " does not support resyncronization");
- }
-
-/*
-* Default StreamCipher Seek Operation
-*/
-void StreamCipher::seek(u32bit)
- {
- throw Exception("The stream cipher " + name() + " does not support seek()");
- }
-
-}
diff --git a/src/stream/stream_cipher.h b/src/stream/stream_cipher.h
index 8ea359131..d6abb37fc 100644
--- a/src/stream/stream_cipher.h
+++ b/src/stream/stream_cipher.h
@@ -18,53 +18,40 @@ namespace Botan {
class BOTAN_DLL StreamCipher : public SymmetricAlgorithm
{
public:
- const u32bit IV_LENGTH;
-
- /**
- * Encrypt a message.
- * @param i the plaintext
- * @param o the byte array to hold the output, i.e. the ciphertext
- * @param len the length of both i and o
- */
- void encrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
-
/**
- * Decrypt a message.
- * @param i the ciphertext to decrypt
- * @param o the byte array to hold the output, i.e. the plaintext
- * @param len the length of both i and o
+ * Encrypt or decrypt a message
+ * @param in the plaintext
+ * @param out the byte array to hold the output, i.e. the ciphertext
+ * @param len the length of both in and out in bytes
*/
- void decrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
+ virtual void cipher(const byte in[], byte out[], u32bit len) = 0;
/**
- * Encrypt a message.
- * @param in the plaintext as input, after the function has
- * returned it will hold the ciphertext
-
- * @param len the length of in
+ * Encrypt or decrypt a message
+ * @param buf the plaintext / ciphertext
+ * @param len the length of buf in bytes
*/
- void encrypt(byte in[], u32bit len) { cipher(in, in, len); }
-
- /**
- * Decrypt a message.
- * @param in the ciphertext as input, after the function has
- * returned it will hold the plaintext
- * @param len the length of in
- */
- void decrypt(byte in[], u32bit len) { cipher(in, in, len); }
+ void cipher1(byte buf[], u32bit len)
+ { cipher(buf, buf, len); }
/**
* Resync the cipher using the IV
* @param iv the initialization vector
* @param iv_len the length of the IV in bytes
*/
- virtual void resync(const byte iv[], u32bit iv_len);
+ virtual void set_iv(const byte[], u32bit iv_len)
+ {
+ if(iv_len)
+ throw Exception("The stream cipher " + name() +
+ " does not support resyncronization");
+ }
/**
- * Seek ahead in the stream.
- * @param len the length to seek ahead.
+ * @param iv_len the length of the IV in bytes
+ * @return if the length is valid for this algorithm
*/
- virtual void seek(u32bit len);
+ virtual bool valid_iv_length(u32bit iv_len) const
+ { return (iv_len == 0); }
/**
* Get a new object representing the same algorithm as *this
@@ -76,15 +63,15 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm
*/
virtual void clear() throw() = 0;
- StreamCipher(u32bit key_min, u32bit key_max = 0,
- u32bit key_mod = 1,
- u32bit iv_len = 0) :
- SymmetricAlgorithm(key_min, key_max, key_mod),
- IV_LENGTH(iv_len) {}
+ /**
+ * StreamCipher constructor
+ */
+ StreamCipher(u32bit key_min,
+ u32bit key_max = 0,
+ u32bit key_mod = 1) :
+ SymmetricAlgorithm(key_min, key_max, key_mod) {}
virtual ~StreamCipher() {}
- private:
- virtual void cipher(const byte[], byte[], u32bit) = 0;
};
}
diff --git a/src/stream/turing/turing.cpp b/src/stream/turing/turing.cpp
index 1e2203480..8336a70a7 100644
--- a/src/stream/turing/turing.cpp
+++ b/src/stream/turing/turing.cpp
@@ -257,15 +257,15 @@ void Turing::key_schedule(const byte key[], u32bit length)
S3[i] = (W3 & 0xFFFFFF00) | C3;
}
- resync(0, 0);
+ set_iv(0, 0);
}
/*
* Resynchronization
*/
-void Turing::resync(const byte iv[], u32bit length)
+void Turing::set_iv(const byte iv[], u32bit length)
{
- if(length % 4 != 0 || length > 16)
+ if(!valid_iv_length(length))
throw Invalid_IV_Length(name(), length);
SecureVector<u32bit> IV(length / 4);
diff --git a/src/stream/turing/turing.h b/src/stream/turing/turing.h
index 455d3c612..59290f640 100644
--- a/src/stream/turing/turing.h
+++ b/src/stream/turing/turing.h
@@ -18,14 +18,18 @@ namespace Botan {
class BOTAN_DLL Turing : public StreamCipher
{
public:
+ void cipher(const byte in[], byte out[], u32bit length);
+ void set_iv(const byte[], u32bit);
+
+ bool valid_iv_length(u32bit iv_len) const
+ { return (iv_len % 4 == 0 && iv_len <= 16); }
+
void clear() throw();
std::string name() const { return "Turing"; }
StreamCipher* clone() const { return new Turing; }
Turing() : StreamCipher(4, 32, 4) { position = 0; }
private:
- void cipher(const byte[], byte[], u32bit);
void key_schedule(const byte[], u32bit);
- void resync(const byte[], u32bit);
void generate();
static u32bit fixedS(u32bit);
diff --git a/src/stream/wid_wake/wid_wake.cpp b/src/stream/wid_wake/wid_wake.cpp
index 1dc0fd7f9..56f938fac 100644
--- a/src/stream/wid_wake/wid_wake.cpp
+++ b/src/stream/wid_wake/wid_wake.cpp
@@ -110,16 +110,17 @@ void WiderWake_41_BE::key_schedule(const byte key[], u32bit)
T[X] = Z;
position = 0;
- const byte iv[8] = { 0 };
- resync(iv, 8);
+
+ const byte ZEROS[8] = { 0 };
+ set_iv(ZEROS, sizeof(ZEROS));
}
/*
* Resynchronization
*/
-void WiderWake_41_BE::resync(const byte iv[], u32bit length)
+void WiderWake_41_BE::set_iv(const byte iv[], u32bit length)
{
- if(length != 8)
+ if(!valid_iv_length(length))
throw Invalid_IV_Length(name(), length);
for(u32bit j = 0; j != 4; ++j)
diff --git a/src/stream/wid_wake/wid_wake.h b/src/stream/wid_wake/wid_wake.h
index 4720afdb2..a037a056e 100644
--- a/src/stream/wid_wake/wid_wake.h
+++ b/src/stream/wid_wake/wid_wake.h
@@ -18,14 +18,18 @@ namespace Botan {
class BOTAN_DLL WiderWake_41_BE : public StreamCipher
{
public:
+ void cipher(const byte[], byte[], u32bit);
+ void set_iv(const byte[], u32bit);
+
+ bool valid_iv_length(u32bit iv_len) const
+ { return (iv_len == 8); }
+
void clear() throw();
std::string name() const { return "WiderWake4+1-BE"; }
StreamCipher* clone() const { return new WiderWake_41_BE; }
- WiderWake_41_BE() : StreamCipher(16, 16, 1, 8) {}
+ WiderWake_41_BE() : StreamCipher(16, 16, 1) {}
private:
- void cipher(const byte[], byte[], u32bit);
void key_schedule(const byte[], u32bit);
- void resync(const byte[], u32bit);
void generate(u32bit);