diff options
author | Jack Lloyd <[email protected]> | 2021-04-03 15:11:35 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2021-04-03 15:39:52 -0400 |
commit | 75741f8654cd54bb8130696aa7836c5949be60e1 (patch) | |
tree | e4fdbd8448bac7b82b14774e22d0fa5de94e5fa8 /src | |
parent | a7e30a97fe3748e970283bf03936c239642036e6 (diff) |
Add BlockCipher::new_object
Diffstat (limited to 'src')
26 files changed, 92 insertions, 58 deletions
diff --git a/src/lib/block/aes/aes.h b/src/lib/block/aes/aes.h index f454fd857..908a9a7ae 100644 --- a/src/lib/block/aes/aes.h +++ b/src/lib/block/aes/aes.h @@ -25,7 +25,7 @@ class AES_128 final : public Block_Cipher_Fixed_Params<16, 16> std::string provider() const override; std::string name() const override { return "AES-128"; } - BlockCipher* clone() const override { return new AES_128; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<AES_128>(); } size_t parallelism() const override; private: @@ -62,7 +62,7 @@ class AES_192 final : public Block_Cipher_Fixed_Params<16, 24> std::string provider() const override; std::string name() const override { return "AES-192"; } - BlockCipher* clone() const override { return new AES_192; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<AES_192>(); } size_t parallelism() const override; private: @@ -100,7 +100,7 @@ class AES_256 final : public Block_Cipher_Fixed_Params<16, 32> std::string provider() const override; std::string name() const override { return "AES-256"; } - BlockCipher* clone() const override { return new AES_256; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<AES_256>(); } size_t parallelism() const override; private: diff --git a/src/lib/block/aria/aria.h b/src/lib/block/aria/aria.h index fc0522275..f3bf69de3 100644 --- a/src/lib/block/aria/aria.h +++ b/src/lib/block/aria/aria.h @@ -31,7 +31,7 @@ class ARIA_128 final : public Block_Cipher_Fixed_Params<16, 16> void clear() override; std::string name() const override { return "ARIA-128"; } - BlockCipher* clone() const override { return new ARIA_128; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<ARIA_128>(); } private: void key_schedule(const uint8_t key[], size_t length) override; @@ -50,7 +50,7 @@ class ARIA_192 final : public Block_Cipher_Fixed_Params<16, 24> void clear() override; std::string name() const override { return "ARIA-192"; } - BlockCipher* clone() const override { return new ARIA_192; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<ARIA_192>(); } private: void key_schedule(const uint8_t key[], size_t length) override; @@ -69,7 +69,7 @@ class ARIA_256 final : public Block_Cipher_Fixed_Params<16, 32> void clear() override; std::string name() const override { return "ARIA-256"; } - BlockCipher* clone() const override { return new ARIA_256; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<ARIA_256>(); } private: void key_schedule(const uint8_t key[], size_t length) override; diff --git a/src/lib/block/block_cipher.cpp b/src/lib/block/block_cipher.cpp index f89bbcb7f..8c47d6a65 100644 --- a/src/lib/block/block_cipher.cpp +++ b/src/lib/block/block_cipher.cpp @@ -269,7 +269,7 @@ BlockCipher::create(const std::string& algo, std::unique_ptr<BlockCipher> c2 = BlockCipher::create(req.arg(1)); if(c1 && c2) - return std::make_unique<Cascade_Cipher>(c1.release(), c2.release()); + return std::make_unique<Cascade_Cipher>(std::move(c1), std::move(c2)); } #endif @@ -282,7 +282,7 @@ BlockCipher::create(const std::string& algo, if(hash && stream) { const size_t block_size = req.arg_as_integer(2, 1024); - return std::make_unique<Lion>(hash.release(), stream.release(), block_size); + return std::make_unique<Lion>(std::move(hash), std::move(stream), block_size); } } #endif diff --git a/src/lib/block/block_cipher.h b/src/lib/block/block_cipher.h index 68cdd1afe..2d1258cfc 100644 --- a/src/lib/block/block_cipher.h +++ b/src/lib/block/block_cipher.h @@ -193,7 +193,12 @@ class BOTAN_PUBLIC_API(2,0) BlockCipher : public SymmetricAlgorithm /** * @return new object representing the same algorithm as *this */ - virtual BlockCipher* clone() const = 0; + virtual std::unique_ptr<BlockCipher> new_object() const = 0; + + BlockCipher* clone() const + { + return this->new_object().release(); + } virtual ~BlockCipher() = default; }; diff --git a/src/lib/block/blowfish/blowfish.h b/src/lib/block/blowfish/blowfish.h index 8ead03e51..71f545531 100644 --- a/src/lib/block/blowfish/blowfish.h +++ b/src/lib/block/blowfish/blowfish.h @@ -30,7 +30,7 @@ class BOTAN_TEST_API Blowfish final : public Block_Cipher_Fixed_Params<8, 1, 56> void clear() override; std::string name() const override { return "Blowfish"; } - BlockCipher* clone() const override { return new Blowfish; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Blowfish>(); } private: void key_schedule(const uint8_t key[], size_t length) override; diff --git a/src/lib/block/camellia/camellia.h b/src/lib/block/camellia/camellia.h index 421b1ed7c..f8ac9d2b7 100644 --- a/src/lib/block/camellia/camellia.h +++ b/src/lib/block/camellia/camellia.h @@ -23,7 +23,7 @@ class Camellia_128 final : public Block_Cipher_Fixed_Params<16, 16> void clear() override; std::string name() const override { return "Camellia-128"; } - BlockCipher* clone() const override { return new Camellia_128; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Camellia_128>(); } private: void key_schedule(const uint8_t key[], size_t length) override; @@ -41,7 +41,7 @@ class Camellia_192 final : public Block_Cipher_Fixed_Params<16, 24> void clear() override; std::string name() const override { return "Camellia-192"; } - BlockCipher* clone() const override { return new Camellia_192; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Camellia_192>(); } private: void key_schedule(const uint8_t key[], size_t length) override; @@ -59,7 +59,7 @@ class Camellia_256 final : public Block_Cipher_Fixed_Params<16, 32> void clear() override; std::string name() const override { return "Camellia-256"; } - BlockCipher* clone() const override { return new Camellia_256; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Camellia_256>(); } private: void key_schedule(const uint8_t key[], size_t length) override; diff --git a/src/lib/block/cascade/cascade.cpp b/src/lib/block/cascade/cascade.cpp index 8dbc850fe..641dd192d 100644 --- a/src/lib/block/cascade/cascade.cpp +++ b/src/lib/block/cascade/cascade.cpp @@ -48,10 +48,10 @@ std::string Cascade_Cipher::name() const return "Cascade(" + m_cipher1->name() + "," + m_cipher2->name() + ")"; } -BlockCipher* Cascade_Cipher::clone() const +std::unique_ptr<BlockCipher> Cascade_Cipher::new_object() const { - return new Cascade_Cipher(m_cipher1->clone(), - m_cipher2->clone()); + return std::make_unique<Cascade_Cipher>(m_cipher1->new_object(), + m_cipher2->new_object()); } namespace { @@ -80,13 +80,14 @@ size_t block_size_for_cascade(size_t bs, size_t bs2) } -Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) : - m_cipher1(c1), m_cipher2(c2) +Cascade_Cipher::Cascade_Cipher(std::unique_ptr<BlockCipher> cipher1, + std::unique_ptr<BlockCipher> cipher2) : + m_cipher1(std::move(cipher1)), + m_cipher2(std::move(cipher2)), + m_block_size(block_size_for_cascade(m_cipher1->block_size(), m_cipher2->block_size())) { - m_block = block_size_for_cascade(c1->block_size(), c2->block_size()); - - BOTAN_ASSERT(m_block % c1->block_size() == 0 && - m_block % c2->block_size() == 0, + BOTAN_ASSERT(m_block_size % m_cipher1->block_size() == 0 && + m_block_size % m_cipher2->block_size() == 0, "Combined block size is a multiple of each ciphers block"); } diff --git a/src/lib/block/cascade/cascade.h b/src/lib/block/cascade/cascade.h index b5f9b5653..6021bfc73 100644 --- a/src/lib/block/cascade/cascade.h +++ b/src/lib/block/cascade/cascade.h @@ -21,7 +21,7 @@ class Cascade_Cipher final : public BlockCipher void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override; void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override; - size_t block_size() const override { return m_block; } + size_t block_size() const override { return m_block_size; } Key_Length_Specification key_spec() const override { @@ -31,22 +31,23 @@ class Cascade_Cipher final : public BlockCipher void clear() override; std::string name() const override; - BlockCipher* clone() const override; + std::unique_ptr<BlockCipher> new_object() const override; /** * Create a cascade of two block ciphers * @param cipher1 the first cipher * @param cipher2 the second cipher */ - Cascade_Cipher(BlockCipher* cipher1, BlockCipher* cipher2); + Cascade_Cipher(std::unique_ptr<BlockCipher> cipher1, + std::unique_ptr<BlockCipher> cipher2); Cascade_Cipher(const Cascade_Cipher&) = delete; Cascade_Cipher& operator=(const Cascade_Cipher&) = delete; private: void key_schedule(const uint8_t[], size_t) override; - size_t m_block; std::unique_ptr<BlockCipher> m_cipher1, m_cipher2; + size_t m_block_size; }; diff --git a/src/lib/block/cast128/cast128.h b/src/lib/block/cast128/cast128.h index 301b1dc5e..c0b139e02 100644 --- a/src/lib/block/cast128/cast128.h +++ b/src/lib/block/cast128/cast128.h @@ -23,7 +23,7 @@ class CAST_128 final : public Block_Cipher_Fixed_Params<8, 11, 16> void clear() override; std::string name() const override { return "CAST-128"; } - BlockCipher* clone() const override { return new CAST_128; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<CAST_128>(); } private: void key_schedule(const uint8_t[], size_t) override; diff --git a/src/lib/block/des/des.h b/src/lib/block/des/des.h index 4c834c6f6..1fd65cff5 100644 --- a/src/lib/block/des/des.h +++ b/src/lib/block/des/des.h @@ -23,7 +23,7 @@ class DES final : public Block_Cipher_Fixed_Params<8, 8> void clear() override; std::string name() const override { return "DES"; } - BlockCipher* clone() const override { return new DES; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<DES>(); } private: void key_schedule(const uint8_t[], size_t) override; @@ -41,7 +41,7 @@ class TripleDES final : public Block_Cipher_Fixed_Params<8, 16, 24, 8> void clear() override; std::string name() const override { return "TripleDES"; } - BlockCipher* clone() const override { return new TripleDES; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<TripleDES>(); } private: void key_schedule(const uint8_t[], size_t) override; diff --git a/src/lib/block/gost_28147/gost_28147.h b/src/lib/block/gost_28147/gost_28147.h index e9d11e091..12f1ab8a6 100644 --- a/src/lib/block/gost_28147/gost_28147.h +++ b/src/lib/block/gost_28147/gost_28147.h @@ -64,7 +64,7 @@ class GOST_28147_89 final : public Block_Cipher_Fixed_Params<8, 32> void clear() override; std::string name() const override; - BlockCipher* clone() const override { return new GOST_28147_89(m_SBOX); } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<GOST_28147_89>(m_SBOX); } /** * @param params the sbox parameters to use @@ -73,10 +73,11 @@ class GOST_28147_89 final : public Block_Cipher_Fixed_Params<8, 32> explicit GOST_28147_89(const std::string& param_name) : GOST_28147_89(GOST_28147_89_Params(param_name)) {} - private: + explicit GOST_28147_89(const std::vector<uint32_t>& other_SBOX) : m_SBOX(other_SBOX), m_EK(8) {} + private: void key_schedule(const uint8_t[], size_t) override; /* diff --git a/src/lib/block/idea/idea.h b/src/lib/block/idea/idea.h index c15d34dd8..4628f55e2 100644 --- a/src/lib/block/idea/idea.h +++ b/src/lib/block/idea/idea.h @@ -25,7 +25,7 @@ class IDEA final : public Block_Cipher_Fixed_Params<8, 16> std::string provider() const override; std::string name() const override { return "IDEA"; } - BlockCipher* clone() const override { return new IDEA; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<IDEA>(); } size_t parallelism() const override; private: diff --git a/src/lib/block/lion/lion.cpp b/src/lib/block/lion/lion.cpp index 43270f3a7..ff5918391 100644 --- a/src/lib/block/lion/lion.cpp +++ b/src/lib/block/lion/lion.cpp @@ -101,12 +101,9 @@ std::string Lion::name() const std::to_string(block_size()) + ")"; } -/* -* Return a clone of this object -*/ -BlockCipher* Lion::clone() const +std::unique_ptr<BlockCipher> Lion::new_object() const { - return new Lion(m_hash->clone(), m_cipher->clone(), block_size()); + return std::make_unique<Lion>(m_hash->new_object(), m_cipher->new_object(), block_size()); } /* @@ -123,10 +120,12 @@ void Lion::clear() /* * Lion Constructor */ -Lion::Lion(HashFunction* hash, StreamCipher* cipher, size_t bs) : +Lion::Lion(std::unique_ptr<HashFunction> hash, + std::unique_ptr<StreamCipher> cipher, + size_t bs) : m_block_size(std::max<size_t>(2*hash->output_length() + 1, bs)), - m_hash(hash), - m_cipher(cipher) + m_hash(std::move(hash)), + m_cipher(std::move(cipher)) { if(2*left_size() + 1 > m_block_size) throw Invalid_Argument(name() + ": Chosen block size is too small"); diff --git a/src/lib/block/lion/lion.h b/src/lib/block/lion/lion.h index 6ffaa9260..375d40232 100644 --- a/src/lib/block/lion/lion.h +++ b/src/lib/block/lion/lion.h @@ -37,15 +37,15 @@ class Lion final : public BlockCipher void clear() override; std::string name() const override; - BlockCipher* clone() const override; + std::unique_ptr<BlockCipher> new_object() const override; /** * @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, + Lion(std::unique_ptr<HashFunction> hash, + std::unique_ptr<StreamCipher> cipher, size_t block_size); private: void key_schedule(const uint8_t[], size_t) override; diff --git a/src/lib/block/noekeon/noekeon.h b/src/lib/block/noekeon/noekeon.h index b2a39638b..05244b523 100644 --- a/src/lib/block/noekeon/noekeon.h +++ b/src/lib/block/noekeon/noekeon.h @@ -24,7 +24,7 @@ class Noekeon final : public Block_Cipher_Fixed_Params<16, 16> std::string provider() const override; void clear() override; std::string name() const override { return "Noekeon"; } - BlockCipher* clone() const override { return new Noekeon; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Noekeon>(); } size_t parallelism() const override; private: diff --git a/src/lib/block/seed/seed.h b/src/lib/block/seed/seed.h index 1105d5075..b054202bb 100644 --- a/src/lib/block/seed/seed.h +++ b/src/lib/block/seed/seed.h @@ -23,7 +23,7 @@ class SEED final : public Block_Cipher_Fixed_Params<16, 16> void clear() override; std::string name() const override { return "SEED"; } - BlockCipher* clone() const override { return new SEED; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<SEED>(); } private: void key_schedule(const uint8_t[], size_t) override; diff --git a/src/lib/block/serpent/serpent.h b/src/lib/block/serpent/serpent.h index 81c841190..fc895fbce 100644 --- a/src/lib/block/serpent/serpent.h +++ b/src/lib/block/serpent/serpent.h @@ -25,7 +25,7 @@ class Serpent final : public Block_Cipher_Fixed_Params<16, 16, 32, 8> void clear() override; std::string provider() const override; std::string name() const override { return "Serpent"; } - BlockCipher* clone() const override { return new Serpent; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Serpent>(); } size_t parallelism() const override { return 4; } diff --git a/src/lib/block/shacal2/shacal2.h b/src/lib/block/shacal2/shacal2.h index c27689655..3e8b6a056 100644 --- a/src/lib/block/shacal2/shacal2.h +++ b/src/lib/block/shacal2/shacal2.h @@ -24,7 +24,7 @@ class SHACAL2 final : public Block_Cipher_Fixed_Params<32, 16, 64, 4> std::string provider() const override; void clear() override; std::string name() const override { return "SHACAL2"; } - BlockCipher* clone() const override { return new SHACAL2; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<SHACAL2>(); } size_t parallelism() const override; private: diff --git a/src/lib/block/sm4/sm4.h b/src/lib/block/sm4/sm4.h index ed6c6742f..4948b09b7 100644 --- a/src/lib/block/sm4/sm4.h +++ b/src/lib/block/sm4/sm4.h @@ -23,7 +23,7 @@ class SM4 final : public Block_Cipher_Fixed_Params<16, 16> void clear() override; std::string name() const override { return "SM4"; } - BlockCipher* clone() const override { return new SM4; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<SM4>(); } std::string provider() const override; size_t parallelism() const override; diff --git a/src/lib/block/threefish_512/threefish_512.h b/src/lib/block/threefish_512/threefish_512.h index 10876fd84..76c7837a8 100644 --- a/src/lib/block/threefish_512/threefish_512.h +++ b/src/lib/block/threefish_512/threefish_512.h @@ -27,7 +27,7 @@ class Threefish_512 final : void clear() override; std::string provider() const override; std::string name() const override { return "Threefish-512"; } - BlockCipher* clone() const override { return new Threefish_512; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Threefish_512>(); } size_t parallelism() const override; private: diff --git a/src/lib/block/twofish/twofish.h b/src/lib/block/twofish/twofish.h index 5d95c198c..5e5b86ab4 100644 --- a/src/lib/block/twofish/twofish.h +++ b/src/lib/block/twofish/twofish.h @@ -23,7 +23,7 @@ class Twofish final : public Block_Cipher_Fixed_Params<16, 16, 32, 8> void clear() override; std::string name() const override { return "Twofish"; } - BlockCipher* clone() const override { return new Twofish; } + std::unique_ptr<BlockCipher> new_object() const override { return std::make_unique<Twofish>(); } private: void key_schedule(const uint8_t[], size_t) override; diff --git a/src/lib/hash/hash.h b/src/lib/hash/hash.h index 8c6440e65..dc2b3fe06 100644 --- a/src/lib/hash/hash.h +++ b/src/lib/hash/hash.h @@ -47,11 +47,6 @@ class BOTAN_PUBLIC_API(2,0) HashFunction : public Buffered_Computation static std::vector<std::string> providers(const std::string& algo_spec); /** - * @return new object representing the same algorithm as *this - */ - virtual HashFunction* clone() const = 0; - - /** * @return provider information about this implementation. Default is "base", * might also return "sse2", "avx2", "openssl", or some other arbitrary string. */ @@ -84,6 +79,19 @@ class BOTAN_PUBLIC_API(2,0) HashFunction : public Buffered_Computation * @return new hash object */ virtual std::unique_ptr<HashFunction> copy_state() const = 0; + + /** + * @return new object representing the same algorithm as *this + */ + std::unique_ptr<HashFunction> new_object() const + { + return std::unique_ptr<HashFunction>(this->clone()); + } + + /** + * @return new object representing the same algorithm as *this + */ + virtual HashFunction* clone() const = 0; }; } diff --git a/src/lib/mac/mac.h b/src/lib/mac/mac.h index de30b7dbb..522d71a58 100644 --- a/src/lib/mac/mac.h +++ b/src/lib/mac/mac.h @@ -124,6 +124,14 @@ class BOTAN_PUBLIC_API(2,0) MessageAuthenticationCode : public Buffered_Computat } /** + * @return new object representing the same algorithm as *this + */ + std::unique_ptr<MessageAuthenticationCode> new_object() const + { + return std::unique_ptr<MessageAuthenticationCode>(this->clone()); + } + + /** * Get a new object representing the same algorithm as *this */ virtual MessageAuthenticationCode* clone() const = 0; diff --git a/src/lib/stream/stream_cipher.h b/src/lib/stream/stream_cipher.h index a07f21041..d3039dec7 100644 --- a/src/lib/stream/stream_cipher.h +++ b/src/lib/stream/stream_cipher.h @@ -130,6 +130,14 @@ class BOTAN_PUBLIC_API(2,0) StreamCipher : public SymmetricAlgorithm virtual StreamCipher* clone() const = 0; /** + * @return new object representing the same algorithm as *this + */ + std::unique_ptr<StreamCipher> new_object() const + { + return std::unique_ptr<StreamCipher>(this->clone()); + } + + /** * Set the offset and the state used later to generate the keystream * @param offset the offset where we begin to generate the keystream */ diff --git a/src/tests/test_ocb.cpp b/src/tests/test_ocb.cpp index 5c41d7001..dbb9cbadc 100644 --- a/src/tests/test_ocb.cpp +++ b/src/tests/test_ocb.cpp @@ -30,9 +30,9 @@ class OCB_Wide_Test_Block_Cipher final : public Botan::BlockCipher size_t block_size() const override { return m_bs; } void clear() override { m_key.clear(); } - Botan::BlockCipher* clone() const override + std::unique_ptr<Botan::BlockCipher> new_object() const override { - return new OCB_Wide_Test_Block_Cipher(m_bs); + return std::make_unique<OCB_Wide_Test_Block_Cipher>(m_bs); } void key_schedule(const uint8_t key[], size_t length) override diff --git a/src/tests/test_tls.cpp b/src/tests/test_tls.cpp index 40dac7cf6..1361df73f 100644 --- a/src/tests/test_tls.cpp +++ b/src/tests/test_tls.cpp @@ -167,7 +167,10 @@ class TLS_CBC_Tests final : public Text_Based_Test return Botan::Key_Length_Specification(0, 0, 1); } - virtual BlockCipher* clone() const override { return new Noop_Block_Cipher(m_bs); } + std::unique_ptr<BlockCipher> new_object() const override + { + return std::make_unique<Noop_Block_Cipher>(m_bs); + } private: void key_schedule(const uint8_t[], size_t) override {} |