/*************************************************
* Luby-Rackoff Source File                       *
* (C) 1999-2003 The Botan Project                *
*************************************************/

#include <botan/lubyrack.h>
#include <botan/lookup.h>

namespace Botan {

/*************************************************
* Luby-Rackoff Encryption                        *
*************************************************/
void LubyRackoff::enc(const byte in[], byte out[]) const
   {
   SecureVector<byte> buffer(hash->OUTPUT_LENGTH);
   hash->update(K1, K1.size());
   hash->update(in, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out + hash->OUTPUT_LENGTH, in + hash->OUTPUT_LENGTH, buffer,
           hash->OUTPUT_LENGTH);

   hash->update(K2, K2.size());
   hash->update(out + hash->OUTPUT_LENGTH, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out, in, buffer, hash->OUTPUT_LENGTH);

   hash->update(K1, K1.size());
   hash->update(out, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out + hash->OUTPUT_LENGTH, buffer, hash->OUTPUT_LENGTH);

   hash->update(K2, K1.size());
   hash->update(out + hash->OUTPUT_LENGTH, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out, buffer, hash->OUTPUT_LENGTH);
   }

/*************************************************
* Luby-Rackoff Decryption                        *
*************************************************/
void LubyRackoff::dec(const byte in[], byte out[]) const
   {
   SecureVector<byte> buffer(hash->OUTPUT_LENGTH);
   hash->update(K2, K2.size());
   hash->update(in + hash->OUTPUT_LENGTH, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out, in, buffer, hash->OUTPUT_LENGTH);

   hash->update(K1, K1.size());
   hash->update(out, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out + hash->OUTPUT_LENGTH, in + hash->OUTPUT_LENGTH, buffer,
           hash->OUTPUT_LENGTH);

   hash->update(K2, K2.size());
   hash->update(out + hash->OUTPUT_LENGTH, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out, buffer, hash->OUTPUT_LENGTH);

   hash->update(K1, K1.size());
   hash->update(out, hash->OUTPUT_LENGTH);
   hash->final(buffer);
   xor_buf(out + hash->OUTPUT_LENGTH, buffer, hash->OUTPUT_LENGTH);
   }

/*************************************************
* Luby-Rackoff Key Schedule                      *
*************************************************/
void LubyRackoff::key(const byte key[], u32bit length)
   {
   K1.set(key, length / 2);
   K2.set(key + length / 2, length / 2);
   }

/*************************************************
* Clear memory of sensitive data                 *
*************************************************/
void LubyRackoff::clear() throw()
   {
   K1.clear();
   K2.clear();
   hash->clear();
   }

/*************************************************
* Return a clone of this object                  *
*************************************************/
BlockCipher* LubyRackoff::clone() const
   {
   return new LubyRackoff(hash->name());
   }

/*************************************************
* Return the name of this type                   *
*************************************************/
std::string LubyRackoff::name() const
   {
   return "Luby-Rackoff(" + hash->name() + ")";
   }

/*************************************************
* 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))
   {
   }

}
