diff options
author | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-10 03:41:59 +0000 |
commit | 6894dca64c04936d07048c0e8cbf7e25858548c3 (patch) | |
tree | 5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/block/lion | |
parent | 9efa3be92442afb3d0b69890a36c7f122df18eda (diff) |
Move lib into src
Diffstat (limited to 'src/lib/block/lion')
-rw-r--r-- | src/lib/block/lion/info.txt | 6 | ||||
-rw-r--r-- | src/lib/block/lion/lion.cpp | 129 | ||||
-rw-r--r-- | src/lib/block/lion/lion.h | 67 |
3 files changed, 202 insertions, 0 deletions
diff --git a/src/lib/block/lion/info.txt b/src/lib/block/lion/info.txt new file mode 100644 index 000000000..c775ff428 --- /dev/null +++ b/src/lib/block/lion/info.txt @@ -0,0 +1,6 @@ +define LION 20131128 + +<requires> +hash +stream +</requires> diff --git a/src/lib/block/lion/lion.cpp b/src/lib/block/lion/lion.cpp new file mode 100644 index 000000000..e1e0b56c6 --- /dev/null +++ b/src/lib/block/lion/lion.cpp @@ -0,0 +1,129 @@ +/* +* Lion +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/lion.h> +#include <botan/internal/xor_buf.h> +#include <botan/parsing.h> + +namespace Botan { + +/* +* Lion Encryption +*/ +void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const + { + secure_vector<byte> buffer_vec(LEFT_SIZE); + byte* buffer = &buffer_vec[0]; + + for(size_t i = 0; i != blocks; ++i) + { + xor_buf(buffer, in, &key1[0], LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); + + hash->update(out + LEFT_SIZE, RIGHT_SIZE); + hash->final(buffer); + xor_buf(out, in, buffer, LEFT_SIZE); + + xor_buf(buffer, out, &key2[0], LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); + + in += BLOCK_SIZE; + out += BLOCK_SIZE; + } + } + +/* +* Lion Decryption +*/ +void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const + { + secure_vector<byte> buffer_vec(LEFT_SIZE); + byte* buffer = &buffer_vec[0]; + + for(size_t i = 0; i != blocks; ++i) + { + xor_buf(buffer, in, &key2[0], LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); + + hash->update(out + LEFT_SIZE, RIGHT_SIZE); + hash->final(buffer); + xor_buf(out, in, buffer, LEFT_SIZE); + + xor_buf(buffer, out, &key1[0], LEFT_SIZE); + cipher->set_key(buffer, LEFT_SIZE); + cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); + + in += BLOCK_SIZE; + out += BLOCK_SIZE; + } + } + +/* +* Lion Key Schedule +*/ +void Lion::key_schedule(const byte key[], size_t length) + { + clear(); + + const size_t half = length / 2; + copy_mem(&key1[0], key, half); + copy_mem(&key2[0], key + half, half); + } + +/* +* Return the name of this type +*/ +std::string Lion::name() const + { + return "Lion(" + hash->name() + "," + + cipher->name() + "," + + std::to_string(BLOCK_SIZE) + ")"; + } + +/* +* Return a clone of this object +*/ +BlockCipher* Lion::clone() const + { + return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); + } + +/* +* Clear memory of sensitive data +*/ +void Lion::clear() + { + zeroise(key1); + zeroise(key2); + hash->clear(); + cipher->clear(); + } + +/* +* Lion Constructor +*/ +Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) : + BLOCK_SIZE(std::max<size_t>(2*hash_in->output_length() + 1, block_len)), + LEFT_SIZE(hash_in->output_length()), + RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE), + hash(hash_in), + cipher(sc_in) + { + if(2*LEFT_SIZE + 1 > BLOCK_SIZE) + throw Invalid_Argument(name() + ": Chosen block size is too small"); + + if(!cipher->valid_keylength(LEFT_SIZE)) + throw Invalid_Argument(name() + ": This stream/hash combo is invalid"); + + key1.resize(LEFT_SIZE); + key2.resize(LEFT_SIZE); + } + +} diff --git a/src/lib/block/lion/lion.h b/src/lib/block/lion/lion.h new file mode 100644 index 000000000..37aad19f2 --- /dev/null +++ b/src/lib/block/lion/lion.h @@ -0,0 +1,67 @@ +/* +* Lion +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_LION_H__ +#define BOTAN_LION_H__ + +#include <botan/block_cipher.h> +#include <botan/stream_cipher.h> +#include <botan/hash.h> + +namespace Botan { + +/** +* Lion is a block cipher construction designed by Ross Anderson and +* Eli Biham, described in "Two Practical and Provably Secure Block +* Ciphers: BEAR and LION". It has a variable block size and is +* designed to encrypt very large blocks (up to a megabyte) + +* http://www.cl.cam.ac.uk/~rja14/Papers/bear-lion.pdf +*/ +class BOTAN_DLL Lion : public BlockCipher + { + public: + void encrypt_n(const byte in[], byte out[], size_t blocks) const; + void decrypt_n(const byte in[], byte out[], size_t blocks) const; + + size_t block_size() const { return BLOCK_SIZE; } + + Key_Length_Specification key_spec() const + { + return Key_Length_Specification(2, 2*hash->output_length(), 2); + } + + void clear(); + std::string name() const; + BlockCipher* clone() const; + + /** + * @param hash the hash to use internally + * @param cipher the stream cipher to use internally + * @param block_size the size of the block to use + */ + Lion(HashFunction* hash, + StreamCipher* cipher, + size_t block_size); + + Lion(const Lion&) = delete; + Lion& operator=(const Lion&) = delete; + + ~Lion() { delete hash; delete cipher; } + private: + void key_schedule(const byte[], size_t); + + const size_t BLOCK_SIZE, LEFT_SIZE, RIGHT_SIZE; + + HashFunction* hash; + StreamCipher* cipher; + secure_vector<byte> key1, key2; + }; + +} + +#endif |