aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-15 23:13:49 +0000
committerlloyd <[email protected]>2008-09-15 23:13:49 +0000
commitd76ed0d90389dba224697027ed66ffbd663b13e8 (patch)
tree9f09812a1958ffa830a82c2bc9f0eadac87ec502
parentc22a6379f79c70f68c26ba6b635b230f00e194be (diff)
Change the constructor for LubyRackoff. Previously it took a string,
however now instead it takes a HashFunction pointer, which it deletes in its destructor. Why the change? For one, lookup.h, while seemingly a bunch of standalone functions, actually calls into a large mass of global state (in short, it is icky). I have a plan in mind for removing much of this while still providing a high level interface (actually hopefully better than now), here is just the start. Now, calling clone() on a LubyRackoff object will now return a new object with a clone() of the HashFunction. Previously we called get_hash on the name, which goes through the whole global lookup bit. This is also good since if you construct one with (say) an OpenSSL provided hash, clones of it will now also use that implementation.
-rw-r--r--include/lubyrack.h5
-rw-r--r--src/def_alg.cpp8
-rw-r--r--src/lubyrack.cpp52
3 files changed, 36 insertions, 29 deletions
diff --git a/include/lubyrack.h b/include/lubyrack.h
index e5a2e2a20..2c4813cb6 100644
--- a/include/lubyrack.h
+++ b/include/lubyrack.h
@@ -1,6 +1,6 @@
/*************************************************
* Luby-Rackoff Header File *
-* (C) 1999-2007 Jack Lloyd *
+* (C) 1999-2008 Jack Lloyd *
*************************************************/
#ifndef BOTAN_LUBY_RACKOFF_H__
@@ -19,7 +19,8 @@ class BOTAN_DLL LubyRackoff : public BlockCipher
void clear() throw();
std::string name() const;
BlockCipher* clone() const;
- LubyRackoff(const std::string&);
+
+ LubyRackoff(HashFunction* hash);
~LubyRackoff() { delete hash; }
private:
void enc(const byte[], byte[]) const;
diff --git a/src/def_alg.cpp b/src/def_alg.cpp
index 10c8e49d9..29e99d6a0 100644
--- a/src/def_alg.cpp
+++ b/src/def_alg.cpp
@@ -130,7 +130,6 @@ Default_Engine::find_block_cipher(const std::string& algo_spec) const
HANDLE_TYPE_NO_ARGS("GOST", GOST);
HANDLE_TYPE_NO_ARGS("IDEA", IDEA);
HANDLE_TYPE_NO_ARGS("KASUMI", KASUMI);
- HANDLE_TYPE_ONE_STRING("Luby-Rackoff", LubyRackoff);
HANDLE_TYPE_NO_ARGS("MARS", MARS);
HANDLE_TYPE_ONE_U32BIT("MISTY1", MISTY1, 8);
HANDLE_TYPE_NO_ARGS("Noekeon", Noekeon);
@@ -146,6 +145,13 @@ Default_Engine::find_block_cipher(const std::string& algo_spec) const
HANDLE_TYPE_NO_ARGS("Twofish", Twofish);
HANDLE_TYPE_NO_ARGS("XTEA", XTEA);
+ if(algo_name == "Luby-Rackoff" && name.size() >= 2)
+ {
+ HashFunction* hash = find_hash(name[1]);
+ if(hash)
+ return new LubyRackoff(hash);
+ }
+
if(algo_name == "Lion")
{
if(name.size() != 4)
diff --git a/src/lubyrack.cpp b/src/lubyrack.cpp
index 4a5408e1d..c9e6fc531 100644
--- a/src/lubyrack.cpp
+++ b/src/lubyrack.cpp
@@ -1,10 +1,9 @@
/*************************************************
* Luby-Rackoff Source File *
-* (C) 1999-2007 Jack Lloyd *
+* (C) 1999-2008 Jack Lloyd *
*************************************************/
#include <botan/lubyrack.h>
-#include <botan/lookup.h>
#include <botan/xor_buf.h>
namespace Botan {
@@ -14,28 +13,28 @@ namespace Botan {
*************************************************/
void LubyRackoff::enc(const byte in[], byte out[]) const
{
- const u32bit OUTPUT_LENGTH = hash->OUTPUT_LENGTH;
+ const u32bit len = hash->OUTPUT_LENGTH;
- SecureVector<byte> buffer(OUTPUT_LENGTH);
+ SecureVector<byte> buffer(len);
hash->update(K1);
- hash->update(in, OUTPUT_LENGTH);
+ hash->update(in, len);
hash->final(buffer);
- xor_buf(out + OUTPUT_LENGTH, in + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
+ xor_buf(out + len, in + len, buffer, len);
hash->update(K2);
- hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
+ hash->update(out + len, len);
hash->final(buffer);
- xor_buf(out, in, buffer, OUTPUT_LENGTH);
+ xor_buf(out, in, buffer, len);
hash->update(K1);
- hash->update(out, OUTPUT_LENGTH);
+ hash->update(out, len);
hash->final(buffer);
- xor_buf(out + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
+ xor_buf(out + len, buffer, len);
hash->update(K2);
- hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
+ hash->update(out + len, len);
hash->final(buffer);
- xor_buf(out, buffer, OUTPUT_LENGTH);
+ xor_buf(out, buffer, len);
}
/*************************************************
@@ -43,28 +42,28 @@ void LubyRackoff::enc(const byte in[], byte out[]) const
*************************************************/
void LubyRackoff::dec(const byte in[], byte out[]) const
{
- const u32bit OUTPUT_LENGTH = hash->OUTPUT_LENGTH;
+ const u32bit len = hash->OUTPUT_LENGTH;
- SecureVector<byte> buffer(OUTPUT_LENGTH);
+ SecureVector<byte> buffer(len);
hash->update(K2);
- hash->update(in + OUTPUT_LENGTH, OUTPUT_LENGTH);
+ hash->update(in + len, len);
hash->final(buffer);
- xor_buf(out, in, buffer, OUTPUT_LENGTH);
+ xor_buf(out, in, buffer, len);
hash->update(K1);
- hash->update(out, OUTPUT_LENGTH);
+ hash->update(out, len);
hash->final(buffer);
- xor_buf(out + OUTPUT_LENGTH, in + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
+ xor_buf(out + len, in + len, buffer, len);
hash->update(K2);
- hash->update(out + OUTPUT_LENGTH, OUTPUT_LENGTH);
+ hash->update(out + len, len);
hash->final(buffer);
- xor_buf(out, buffer, OUTPUT_LENGTH);
+ xor_buf(out, buffer, len);
hash->update(K1);
- hash->update(out, OUTPUT_LENGTH);
+ hash->update(out, len);
hash->final(buffer);
- xor_buf(out + OUTPUT_LENGTH, buffer, OUTPUT_LENGTH);
+ xor_buf(out + len, buffer, len);
}
/*************************************************
@@ -91,7 +90,7 @@ void LubyRackoff::clear() throw()
*************************************************/
BlockCipher* LubyRackoff::clone() const
{
- return new LubyRackoff(hash->name());
+ return new LubyRackoff(hash->clone());
}
/*************************************************
@@ -105,9 +104,10 @@ std::string LubyRackoff::name() const
/*************************************************
* Luby-Rackoff Constructor *
*************************************************/
-LubyRackoff::LubyRackoff(const std::string& hash_name) :
- BlockCipher(2*output_length_of(hash_name), 2, 32, 2),
- hash(get_hash(hash_name))
+LubyRackoff::LubyRackoff(HashFunction* h) :
+ BlockCipher(2 * (h ? h->OUTPUT_LENGTH: 0),
+ 2, 32, 2),
+ hash(h)
{
}