diff options
author | lloyd <[email protected]> | 2014-01-18 19:45:16 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2014-01-18 19:45:16 +0000 |
commit | ef465af87d61c0cfbba17b86a3e1cc48b90ab391 (patch) | |
tree | 151aafc54f2a57c1ca037653b647398616221060 /src/lib/block | |
parent | 1822ba0d828d2c7bec51313597a9a64a54ccc559 (diff) |
Use unique_ptr instead of bare pointers and explicit delete in block, mac, hash.
m_ namespaced everything while I'm in there. Changed CMAC poly_double signature.
Diffstat (limited to 'src/lib/block')
-rw-r--r-- | src/lib/block/cascade/cascade.cpp | 42 | ||||
-rw-r--r-- | src/lib/block/cascade/cascade.h | 14 | ||||
-rw-r--r-- | src/lib/block/lion/lion.cpp | 86 | ||||
-rw-r--r-- | src/lib/block/lion/lion.h | 34 | ||||
-rw-r--r-- | src/lib/block/lubyrack/lubyrack.cpp | 75 | ||||
-rw-r--r-- | src/lib/block/lubyrack/lubyrack.h | 27 |
6 files changed, 133 insertions, 145 deletions
diff --git a/src/lib/block/cascade/cascade.cpp b/src/lib/block/cascade/cascade.cpp index f1b1a8f2c..67846204d 100644 --- a/src/lib/block/cascade/cascade.cpp +++ b/src/lib/block/cascade/cascade.cpp @@ -12,46 +12,46 @@ namespace Botan { void Cascade_Cipher::encrypt_n(const byte in[], byte out[], size_t blocks) const { - size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); - size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); + size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size()); + size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size()); - cipher1->encrypt_n(in, out, c1_blocks); - cipher2->encrypt_n(out, out, c2_blocks); + m_cipher1->encrypt_n(in, out, c1_blocks); + m_cipher2->encrypt_n(out, out, c2_blocks); } void Cascade_Cipher::decrypt_n(const byte in[], byte out[], size_t blocks) const { - size_t c1_blocks = blocks * (block_size() / cipher1->block_size()); - size_t c2_blocks = blocks * (block_size() / cipher2->block_size()); + size_t c1_blocks = blocks * (block_size() / m_cipher1->block_size()); + size_t c2_blocks = blocks * (block_size() / m_cipher2->block_size()); - cipher2->decrypt_n(in, out, c2_blocks); - cipher1->decrypt_n(out, out, c1_blocks); + m_cipher2->decrypt_n(in, out, c2_blocks); + m_cipher1->decrypt_n(out, out, c1_blocks); } void Cascade_Cipher::key_schedule(const byte key[], size_t) { - const byte* key2 = key + cipher1->maximum_keylength(); + const byte* key2 = key + m_cipher1->maximum_keylength(); - cipher1->set_key(key , cipher1->maximum_keylength()); - cipher2->set_key(key2, cipher2->maximum_keylength()); + m_cipher1->set_key(key , m_cipher1->maximum_keylength()); + m_cipher2->set_key(key2, m_cipher2->maximum_keylength()); } void Cascade_Cipher::clear() { - cipher1->clear(); - cipher2->clear(); + m_cipher1->clear(); + m_cipher2->clear(); } std::string Cascade_Cipher::name() const { - return "Cascade(" + cipher1->name() + "," + cipher2->name() + ")"; + return "Cascade(" + m_cipher1->name() + "," + m_cipher2->name() + ")"; } BlockCipher* Cascade_Cipher::clone() const { - return new Cascade_Cipher(cipher1->clone(), - cipher2->clone()); + return new Cascade_Cipher(m_cipher1->clone(), + m_cipher2->clone()); } namespace { @@ -81,18 +81,12 @@ size_t block_size_for_cascade(size_t bs, size_t bs2) } Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) : - cipher1(c1), cipher2(c2) + m_cipher1(c1), m_cipher2(c2) { - block = block_size_for_cascade(c1->block_size(), c2->block_size()); + m_block = block_size_for_cascade(c1->block_size(), c2->block_size()); if(block_size() % c1->block_size() || block_size() % c2->block_size()) throw Internal_Error("Failure in " + name() + " constructor"); } -Cascade_Cipher::~Cascade_Cipher() - { - delete cipher1; - delete cipher2; - } - } diff --git a/src/lib/block/cascade/cascade.h b/src/lib/block/cascade/cascade.h index 9b7d44fdf..3634b2f5b 100644 --- a/src/lib/block/cascade/cascade.h +++ b/src/lib/block/cascade/cascade.h @@ -9,6 +9,7 @@ #define BOTAN_CASCADE_H__ #include <botan/block_cipher.h> +#include <memory> namespace Botan { @@ -21,12 +22,12 @@ class BOTAN_DLL Cascade_Cipher : public BlockCipher 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_t block_size() const { return m_block; } Key_Length_Specification key_spec() const { - return Key_Length_Specification(cipher1->maximum_keylength() + - cipher2->maximum_keylength()); + return Key_Length_Specification(m_cipher1->maximum_keylength() + + m_cipher2->maximum_keylength()); } void clear(); @@ -42,14 +43,11 @@ class BOTAN_DLL Cascade_Cipher : public BlockCipher Cascade_Cipher(const Cascade_Cipher&) = delete; Cascade_Cipher& operator=(const Cascade_Cipher&) = delete; - - ~Cascade_Cipher(); private: void key_schedule(const byte[], size_t); - size_t block; - BlockCipher* cipher1; - BlockCipher* cipher2; + size_t m_block; + std::unique_ptr<BlockCipher> m_cipher1, m_cipher2; }; diff --git a/src/lib/block/lion/lion.cpp b/src/lib/block/lion/lion.cpp index e1e0b56c6..95f703f8b 100644 --- a/src/lib/block/lion/lion.cpp +++ b/src/lib/block/lion/lion.cpp @@ -1,6 +1,6 @@ /* * Lion -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2007,2014 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -16,25 +16,28 @@ namespace Botan { */ void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t LEFT_SIZE = left_size(); + const size_t RIGHT_SIZE = right_size(); + 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); + xor_buf(buffer, in, &m_key1[0], LEFT_SIZE); + m_cipher->set_key(buffer, LEFT_SIZE); + m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); - hash->update(out + LEFT_SIZE, RIGHT_SIZE); - hash->final(buffer); + m_hash->update(out + LEFT_SIZE, RIGHT_SIZE); + m_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); + xor_buf(buffer, out, &m_key2[0], LEFT_SIZE); + m_cipher->set_key(buffer, LEFT_SIZE); + m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += m_block_size; + out += m_block_size; } } @@ -43,25 +46,28 @@ void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const { + const size_t LEFT_SIZE = left_size(); + const size_t RIGHT_SIZE = right_size(); + 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); + xor_buf(buffer, in, &m_key2[0], LEFT_SIZE); + m_cipher->set_key(buffer, LEFT_SIZE); + m_cipher->cipher(in + LEFT_SIZE, out + LEFT_SIZE, RIGHT_SIZE); - hash->update(out + LEFT_SIZE, RIGHT_SIZE); - hash->final(buffer); + m_hash->update(out + LEFT_SIZE, RIGHT_SIZE); + m_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); + xor_buf(buffer, out, &m_key1[0], LEFT_SIZE); + m_cipher->set_key(buffer, LEFT_SIZE); + m_cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += BLOCK_SIZE; - out += BLOCK_SIZE; + in += m_block_size; + out += m_block_size; } } @@ -73,8 +79,8 @@ 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); + copy_mem(&m_key1[0], key, half); + copy_mem(&m_key2[0], key + half, half); } /* @@ -82,9 +88,9 @@ void Lion::key_schedule(const byte key[], size_t length) */ std::string Lion::name() const { - return "Lion(" + hash->name() + "," + - cipher->name() + "," + - std::to_string(BLOCK_SIZE) + ")"; + return "Lion(" + m_hash->name() + "," + + m_cipher->name() + "," + + std::to_string(block_size()) + ")"; } /* @@ -92,7 +98,7 @@ std::string Lion::name() const */ BlockCipher* Lion::clone() const { - return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); + return new Lion(m_hash->clone(), m_cipher->clone(), block_size()); } /* @@ -100,30 +106,28 @@ BlockCipher* Lion::clone() const */ void Lion::clear() { - zeroise(key1); - zeroise(key2); - hash->clear(); - cipher->clear(); + zeroise(m_key1); + zeroise(m_key2); + m_hash->clear(); + m_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) +Lion::Lion(HashFunction* hash, StreamCipher* cipher, size_t block_size) : + m_block_size(std::max<size_t>(2*hash->output_length() + 1, block_size)), + m_hash(hash), + m_cipher(cipher) { - if(2*LEFT_SIZE + 1 > BLOCK_SIZE) + if(2*left_size() + 1 > m_block_size) throw Invalid_Argument(name() + ": Chosen block size is too small"); - if(!cipher->valid_keylength(LEFT_SIZE)) + if(!m_cipher->valid_keylength(left_size())) throw Invalid_Argument(name() + ": This stream/hash combo is invalid"); - key1.resize(LEFT_SIZE); - key2.resize(LEFT_SIZE); + m_key1.resize(left_size()); + m_key2.resize(left_size()); } } diff --git a/src/lib/block/lion/lion.h b/src/lib/block/lion/lion.h index 37aad19f2..afee95b4e 100644 --- a/src/lib/block/lion/lion.h +++ b/src/lib/block/lion/lion.h @@ -1,6 +1,6 @@ /* * Lion -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2007,2014 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -11,6 +11,7 @@ #include <botan/block_cipher.h> #include <botan/stream_cipher.h> #include <botan/hash.h> +#include <memory> namespace Botan { @@ -25,19 +26,19 @@ namespace Botan { 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; + void encrypt_n(const byte in[], byte out[], size_t blocks) const override; + void decrypt_n(const byte in[], byte out[], size_t blocks) const override; - size_t block_size() const { return BLOCK_SIZE; } + size_t block_size() const override { return m_block_size; } - Key_Length_Specification key_spec() const + Key_Length_Specification key_spec() const override { - return Key_Length_Specification(2, 2*hash->output_length(), 2); + return Key_Length_Specification(2, 2*m_hash->output_length(), 2); } - void clear(); - std::string name() const; - BlockCipher* clone() const; + void clear() override; + std::string name() const override; + BlockCipher* clone() const override; /** * @param hash the hash to use internally @@ -47,19 +48,16 @@ class BOTAN_DLL Lion : public BlockCipher 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; + size_t left_size() const { return m_hash->output_length(); } + size_t right_size() const { return m_block_size - left_size(); } - HashFunction* hash; - StreamCipher* cipher; - secure_vector<byte> key1, key2; + const size_t m_block_size; + std::unique_ptr<HashFunction> m_hash; + std::unique_ptr<StreamCipher> m_cipher; + secure_vector<byte> m_key1, m_key2; }; } diff --git a/src/lib/block/lubyrack/lubyrack.cpp b/src/lib/block/lubyrack/lubyrack.cpp index 9be079003..92974b761 100644 --- a/src/lib/block/lubyrack/lubyrack.cpp +++ b/src/lib/block/lubyrack/lubyrack.cpp @@ -1,6 +1,6 @@ /* * Luby-Rackoff -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2008,2014 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -15,31 +15,31 @@ namespace Botan { */ void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const { - const size_t len = hash->output_length(); + const size_t len = m_hash->output_length(); secure_vector<byte> buffer_vec(len); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { - hash->update(K1); - hash->update(in, len); - hash->final(buffer); + m_hash->update(m_K1); + m_hash->update(in, len); + m_hash->final(buffer); xor_buf(out + len, in + len, buffer, len); - hash->update(K2); - hash->update(out + len, len); - hash->final(buffer); + m_hash->update(m_K2); + m_hash->update(out + len, len); + m_hash->final(buffer); xor_buf(out, in, buffer, len); - hash->update(K1); - hash->update(out, len); - hash->final(buffer); + m_hash->update(m_K1); + m_hash->update(out, len); + m_hash->final(buffer); xor_buf(out + len, buffer, len); - hash->update(K2); - hash->update(out + len, len); - hash->final(buffer); + m_hash->update(m_K2); + m_hash->update(out + len, len); + m_hash->final(buffer); xor_buf(out, buffer, len); in += 2 * len; @@ -52,31 +52,31 @@ void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const */ void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const { - const size_t len = hash->output_length(); + const size_t len = m_hash->output_length(); secure_vector<byte> buffer_vec(len); byte* buffer = &buffer_vec[0]; for(size_t i = 0; i != blocks; ++i) { - hash->update(K2); - hash->update(in + len, len); - hash->final(buffer); + m_hash->update(m_K2); + m_hash->update(in + len, len); + m_hash->final(buffer); xor_buf(out, in, buffer, len); - hash->update(K1); - hash->update(out, len); - hash->final(buffer); + m_hash->update(m_K1); + m_hash->update(out, len); + m_hash->final(buffer); xor_buf(out + len, in + len, buffer, len); - hash->update(K2); - hash->update(out + len, len); - hash->final(buffer); + m_hash->update(m_K2); + m_hash->update(out + len, len); + m_hash->final(buffer); xor_buf(out, buffer, len); - hash->update(K1); - hash->update(out, len); - hash->final(buffer); + m_hash->update(m_K1); + m_hash->update(out, len); + m_hash->final(buffer); xor_buf(out + len, buffer, len); in += 2 * len; @@ -89,8 +89,8 @@ void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const */ void LubyRackoff::key_schedule(const byte key[], size_t length) { - K1.assign(key, key + (length / 2)); - K2.assign(key + (length / 2), key + length); + m_K1.assign(key, key + (length / 2)); + m_K2.assign(key + (length / 2), key + length); } /* @@ -98,9 +98,9 @@ void LubyRackoff::key_schedule(const byte key[], size_t length) */ void LubyRackoff::clear() { - zap(K1); - zap(K2); - hash->clear(); + zap(m_K1); + zap(m_K2); + m_hash->clear(); } /* @@ -108,7 +108,7 @@ void LubyRackoff::clear() */ BlockCipher* LubyRackoff::clone() const { - return new LubyRackoff(hash->clone()); + return new LubyRackoff(m_hash->clone()); } /* @@ -116,14 +116,7 @@ BlockCipher* LubyRackoff::clone() const */ std::string LubyRackoff::name() const { - return "Luby-Rackoff(" + hash->name() + ")"; - } - -/* -* Luby-Rackoff Constructor -*/ -LubyRackoff::LubyRackoff(HashFunction* h) : hash(h) - { + return "Luby-Rackoff(" + m_hash->name() + ")"; } } diff --git a/src/lib/block/lubyrack/lubyrack.h b/src/lib/block/lubyrack/lubyrack.h index e28c60be7..236be53d6 100644 --- a/src/lib/block/lubyrack/lubyrack.h +++ b/src/lib/block/lubyrack/lubyrack.h @@ -1,6 +1,6 @@ /* * Luby-Rackoff -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2008,2014 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -10,6 +10,7 @@ #include <botan/block_cipher.h> #include <botan/hash.h> +#include <memory> namespace Botan { @@ -19,30 +20,30 @@ namespace Botan { class BOTAN_DLL LubyRackoff : 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; + void encrypt_n(const byte in[], byte out[], size_t blocks) const override; + void decrypt_n(const byte in[], byte out[], size_t blocks) const override; - size_t block_size() const { return 2 * hash->output_length(); } + size_t block_size() const override { return 2 * m_hash->output_length(); } - Key_Length_Specification key_spec() const + Key_Length_Specification key_spec() const override { return Key_Length_Specification(2, 32, 2); } - void clear(); - std::string name() const; - BlockCipher* clone() const; + void clear() override; + std::string name() const override; + BlockCipher* clone() const override; /** * @param hash function to use to form the block cipher */ - LubyRackoff(HashFunction* hash); - ~LubyRackoff() { delete hash; } + LubyRackoff(HashFunction* hash) : m_hash(hash) {} + private: - void key_schedule(const byte[], size_t); + void key_schedule(const byte[], size_t) override; - HashFunction* hash; - secure_vector<byte> K1, K2; + std::unique_ptr<HashFunction> m_hash; + secure_vector<byte> m_K1, m_K2; }; } |