aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/modes
diff options
context:
space:
mode:
authorlloyd <[email protected]>2015-03-04 04:30:20 +0000
committerlloyd <[email protected]>2015-03-04 04:30:20 +0000
commit2591a2cd863696b91128ff4a8461bb96d497e7b4 (patch)
treeacb7a179a0790ec63c0c21ecb2ea9d7939e05248 /src/lib/modes
parentc794f78bd9b7eebc58c39fd00de90b26fb4cfb67 (diff)
Hide Algorithm_Factory and use the functions in lookup.h internally.
Fix two memory leaks (in TLS and modes) caused by calling get_foo and then cloning the result before saving it (leaking the original object), a holdover from the conversion between construction techniques in 1.11.14
Diffstat (limited to 'src/lib/modes')
-rw-r--r--src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp13
-rw-r--r--src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h2
-rw-r--r--src/lib/modes/cbc/cbc.cpp2
-rw-r--r--src/lib/modes/ecb/ecb.cpp2
-rw-r--r--src/lib/modes/mode_utils.h12
5 files changed, 17 insertions, 14 deletions
diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
index 15fd8f959..37e0ef96b 100644
--- a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
+++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
@@ -13,6 +13,11 @@ namespace Botan {
BOTAN_REGISTER_TRANSFORM_NOARGS(ChaCha20Poly1305_Encryption);
BOTAN_REGISTER_TRANSFORM_NOARGS(ChaCha20Poly1305_Decryption);
+ChaCha20Poly1305_Mode::ChaCha20Poly1305_Mode() :
+ m_chacha(make_stream_cipher("ChaCha")),
+ m_poly1305(make_message_auth("Poly1305"))
+ {}
+
bool ChaCha20Poly1305_Mode::valid_nonce_length(size_t n) const
{
return (n == 8 || n == 12);
@@ -20,16 +25,14 @@ bool ChaCha20Poly1305_Mode::valid_nonce_length(size_t n) const
void ChaCha20Poly1305_Mode::clear()
{
- m_chacha.reset();
- m_poly1305.reset();
+ m_chacha->clear();
+ m_poly1305->clear();
m_ad.clear();
m_ctext_len = 0;
}
void ChaCha20Poly1305_Mode::key_schedule(const byte key[], size_t length)
{
- if(!m_chacha.get())
- m_chacha.reset(make_a<StreamCipher>("ChaCha"));
m_chacha->set_key(key, length);
}
@@ -60,8 +63,6 @@ secure_vector<byte> ChaCha20Poly1305_Mode::start_raw(const byte nonce[], size_t
secure_vector<byte> zeros(64);
m_chacha->encrypt(zeros);
- if(!m_poly1305.get())
- m_poly1305.reset(make_a<MessageAuthenticationCode>("Poly1305"));
m_poly1305->set_key(&zeros[0], 32);
// Remainder of output is discard
diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
index a3b83dc63..f496590af 100644
--- a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
+++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.h
@@ -41,6 +41,8 @@ class BOTAN_DLL ChaCha20Poly1305_Mode : public AEAD_Mode
std::unique_ptr<StreamCipher> m_chacha;
std::unique_ptr<MessageAuthenticationCode> m_poly1305;
+ ChaCha20Poly1305_Mode();
+
secure_vector<byte> m_ad;
size_t m_nonce_len = 0;
size_t m_ctext_len = 0;
diff --git a/src/lib/modes/cbc/cbc.cpp b/src/lib/modes/cbc/cbc.cpp
index 1fb27f2e0..7cee72081 100644
--- a/src/lib/modes/cbc/cbc.cpp
+++ b/src/lib/modes/cbc/cbc.cpp
@@ -14,7 +14,7 @@ namespace Botan {
template<typename CBC_T, typename CTS_T>
Transform* make_cbc_mode(const Transform::Spec& spec)
{
- std::unique_ptr<BlockCipher> bc(Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0)));
+ std::unique_ptr<BlockCipher> bc(get_block_cipher(spec.arg(0)));
if(bc)
{
diff --git a/src/lib/modes/ecb/ecb.cpp b/src/lib/modes/ecb/ecb.cpp
index 6318671ca..eaab1810d 100644
--- a/src/lib/modes/ecb/ecb.cpp
+++ b/src/lib/modes/ecb/ecb.cpp
@@ -13,7 +13,7 @@ namespace Botan {
template<typename T>
Transform* make_ecb_mode(const Transform::Spec& spec)
{
- std::unique_ptr<BlockCipher> bc(Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0)));
+ std::unique_ptr<BlockCipher> bc(get_block_cipher(spec.arg(0)));
std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(spec.arg(1, "NoPadding")));
if(bc && pad)
return new T(bc.release(), pad.release());
diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h
index ef2840000..e2e2af83e 100644
--- a/src/lib/modes/mode_utils.h
+++ b/src/lib/modes/mode_utils.h
@@ -22,18 +22,18 @@ namespace Botan {
template<typename T>
T* make_block_cipher_mode(const Transform::Spec& spec)
{
- if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0)))
- return new T(bc->clone());
+ if(BlockCipher* bc = get_block_cipher(spec.arg(0)))
+ return new T(bc);
return nullptr;
}
template<typename T, size_t LEN1>
T* make_block_cipher_mode_len(const Transform::Spec& spec)
{
- if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0)))
+ if(BlockCipher* bc = get_block_cipher(spec.arg(0)))
{
const size_t len1 = spec.arg_as_integer(1, LEN1);
- return new T(bc->clone(), len1);
+ return new T(bc, len1);
}
return nullptr;
@@ -42,11 +42,11 @@ T* make_block_cipher_mode_len(const Transform::Spec& spec)
template<typename T, size_t LEN1, size_t LEN2>
T* make_block_cipher_mode_len2(const Transform::Spec& spec)
{
- if(BlockCipher* bc = Algo_Registry<BlockCipher>::global_registry().make(spec.arg(0)))
+ if(BlockCipher* bc = get_block_cipher(spec.arg(0)))
{
const size_t len1 = spec.arg_as_integer(1, LEN1);
const size_t len2 = spec.arg_as_integer(2, LEN2);
- return new T(bc->clone(), len1, len2);
+ return new T(bc, len1, len2);
}
return nullptr;