aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/modes/aead
diff options
context:
space:
mode:
authorlloyd <[email protected]>2015-01-28 04:32:10 +0000
committerlloyd <[email protected]>2015-01-28 04:32:10 +0000
commit7b56f1bd570dc684ffd7c945dee0d9b5480354ff (patch)
tree0c50ad534280a292a1b76daee9a19b34cfd96367 /src/lib/modes/aead
parentb8fa304ec981d273c45d7ef31705d65ccfb00cc1 (diff)
Add a runtime map of string->func() which when called return
Transforms and BlockCiphers. Registration for all types is done at startup but is very cheap as just a std::function and a std::map entry are created, no actual objects are created until needed. This is a huge improvement over Algorithm_Factory which used T::clone() as the function and thus kept a prototype object of each type in memory. Replace existing lookup mechanisms for ciphers, AEADs, and compression to use the transform lookup. The existing Engine framework remains in place for BlockCipher, but the engines now just call to the registry instead of having hardcoded lookups. s/Transformation/Transform/ with typedefs for compatability. Remove lib/selftest code (for runtime selftesting): not the right approach.
Diffstat (limited to 'src/lib/modes/aead')
-rw-r--r--src/lib/modes/aead/aead.cpp124
-rw-r--r--src/lib/modes/aead/ccm/ccm.cpp7
-rw-r--r--src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp6
-rw-r--r--src/lib/modes/aead/eax/eax.cpp5
-rw-r--r--src/lib/modes/aead/gcm/gcm.cpp5
-rw-r--r--src/lib/modes/aead/ocb/ocb.cpp6
-rw-r--r--src/lib/modes/aead/siv/siv.cpp7
7 files changed, 27 insertions, 133 deletions
diff --git a/src/lib/modes/aead/aead.cpp b/src/lib/modes/aead/aead.cpp
index b1cce73e0..1f2099d2e 100644
--- a/src/lib/modes/aead/aead.cpp
+++ b/src/lib/modes/aead/aead.cpp
@@ -1,135 +1,23 @@
/*
-* Interface for AEAD modes
-* (C) 2013 Jack Lloyd
+* (C) 2013,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/aead.h>
-#include <botan/block_cipher.h>
-#include <botan/libstate.h>
-
-#if defined(BOTAN_HAS_AEAD_CCM)
- #include <botan/ccm.h>
-#endif
-
-#if defined(BOTAN_HAS_AEAD_EAX)
- #include <botan/eax.h>
-#endif
-
-#if defined(BOTAN_HAS_AEAD_GCM)
- #include <botan/gcm.h>
-#endif
-
-#if defined(BOTAN_HAS_AEAD_SIV)
- #include <botan/siv.h>
-#endif
-
-#if defined(BOTAN_HAS_AEAD_OCB)
- #include <botan/ocb.h>
-#endif
-
-#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
- #include <botan/chacha20poly1305.h>
-#endif
namespace Botan {
AEAD_Mode* get_aead(const std::string& algo_spec, Cipher_Dir direction)
{
-#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
- if(algo_spec == "ChaCha20Poly1305")
- {
- if(direction == ENCRYPTION)
- return new ChaCha20Poly1305_Encryption;
- else
- return new ChaCha20Poly1305_Decryption;
- }
-#endif
-
- Algorithm_Factory& af = global_state().algorithm_factory();
-
- const std::vector<std::string> algo_parts = split_on(algo_spec, '/');
- if(algo_parts.empty())
- throw Invalid_Algorithm_Name(algo_spec);
-
- if(algo_parts.size() < 2)
- return nullptr;
-
- const std::string cipher_name = algo_parts[0];
- const BlockCipher* cipher = af.prototype_block_cipher(cipher_name);
- if(!cipher)
- return nullptr;
-
- const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
-
- if(mode_info.empty())
- return nullptr;
-
- const std::string mode_name = mode_info[0];
-
- const size_t tag_size = (mode_info.size() > 1) ? to_u32bit(mode_info[1]) : cipher->block_size();
-
-#if defined(BOTAN_HAS_AEAD_CCM)
- if(mode_name == "CCM-8")
- {
- if(direction == ENCRYPTION)
- return new CCM_Encryption(cipher->clone(), 8, 3);
- else
- return new CCM_Decryption(cipher->clone(), 8, 3);
- }
-
- if(mode_name == "CCM" || mode_name == "CCM-8")
- {
- const size_t L = (mode_info.size() > 2) ? to_u32bit(mode_info[2]) : 3;
-
- if(direction == ENCRYPTION)
- return new CCM_Encryption(cipher->clone(), tag_size, L);
- else
- return new CCM_Decryption(cipher->clone(), tag_size, L);
- }
-#endif
-
-#if defined(BOTAN_HAS_AEAD_EAX)
- if(mode_name == "EAX")
- {
- if(direction == ENCRYPTION)
- return new EAX_Encryption(cipher->clone(), tag_size);
- else
- return new EAX_Decryption(cipher->clone(), tag_size);
- }
-#endif
-
-#if defined(BOTAN_HAS_AEAD_SIV)
- if(mode_name == "SIV")
- {
- BOTAN_ASSERT(tag_size == 16, "Valid tag size for SIV");
- if(direction == ENCRYPTION)
- return new SIV_Encryption(cipher->clone());
- else
- return new SIV_Decryption(cipher->clone());
- }
-#endif
-
-#if defined(BOTAN_HAS_AEAD_GCM)
- if(mode_name == "GCM")
- {
- if(direction == ENCRYPTION)
- return new GCM_Encryption(cipher->clone(), tag_size);
- else
- return new GCM_Decryption(cipher->clone(), tag_size);
- }
-#endif
+ std::unique_ptr<Cipher_Mode> mode(get_cipher_mode(algo_spec, direction));
-#if defined(BOTAN_HAS_AEAD_OCB)
- if(mode_name == "OCB")
+ if(AEAD_Mode* aead = dynamic_cast<AEAD_Mode*>(mode.get()))
{
- if(direction == ENCRYPTION)
- return new OCB_Encryption(cipher->clone(), tag_size);
- else
- return new OCB_Decryption(cipher->clone(), tag_size);
+ mode.release();
+ return aead;
}
-#endif
return nullptr;
}
diff --git a/src/lib/modes/aead/ccm/ccm.cpp b/src/lib/modes/aead/ccm/ccm.cpp
index e0b247ddb..cc692e364 100644
--- a/src/lib/modes/aead/ccm/ccm.cpp
+++ b/src/lib/modes/aead/ccm/ccm.cpp
@@ -5,13 +5,14 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/ccm.h>
#include <botan/parsing.h>
-#include <botan/internal/xor_buf.h>
-#include <algorithm>
namespace Botan {
+BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(CCM_Encryption, CCM_Decryption, 16, 3);
+
/*
* CCM_Mode Constructor
*/
@@ -57,7 +58,7 @@ size_t CCM_Mode::update_granularity() const
/*
This value does not particularly matter as regardless CCM_Mode::update
buffers all input, so in theory this could be 1. However as for instance
- Transformation_Filter creates update_granularity() byte buffers, use a
+ Transform_Filter creates update_granularity() byte buffers, use a
somewhat large size to avoid bouncing on a tiny buffer.
*/
return m_cipher->parallel_bytes();
diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
index 0961f1dc8..a278156eb 100644
--- a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
+++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
@@ -5,14 +5,16 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/chacha20poly1305.h>
#include <botan/chacha.h>
#include <botan/poly1305.h>
-#include <botan/loadstor.h>
-#include <algorithm>
namespace Botan {
+BOTAN_REGISTER_TRANSFORM_NOARGS(ChaCha20Poly1305_Encryption);
+BOTAN_REGISTER_TRANSFORM_NOARGS(ChaCha20Poly1305_Decryption);
+
bool ChaCha20Poly1305_Mode::valid_nonce_length(size_t n) const
{
return (n == 8 || n == 12);
diff --git a/src/lib/modes/aead/eax/eax.cpp b/src/lib/modes/aead/eax/eax.cpp
index 289278a52..3b0c94416 100644
--- a/src/lib/modes/aead/eax/eax.cpp
+++ b/src/lib/modes/aead/eax/eax.cpp
@@ -5,15 +5,16 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/eax.h>
#include <botan/cmac.h>
#include <botan/ctr.h>
#include <botan/parsing.h>
-#include <botan/internal/xor_buf.h>
-#include <algorithm>
namespace Botan {
+BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(EAX_Encryption, EAX_Decryption, 0);
+
namespace {
/*
diff --git a/src/lib/modes/aead/gcm/gcm.cpp b/src/lib/modes/aead/gcm/gcm.cpp
index 0acaa57e9..e4a2ad85c 100644
--- a/src/lib/modes/aead/gcm/gcm.cpp
+++ b/src/lib/modes/aead/gcm/gcm.cpp
@@ -6,9 +6,8 @@
*/
#include <botan/gcm.h>
+#include <botan/internal/mode_utils.h>
#include <botan/ctr.h>
-#include <botan/internal/xor_buf.h>
-#include <botan/loadstor.h>
#if defined(BOTAN_HAS_GCM_CLMUL)
#include <botan/internal/clmul.h>
@@ -17,6 +16,8 @@
namespace Botan {
+BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(GCM_Encryption, GCM_Decryption, 16);
+
void GHASH::gcm_multiply(secure_vector<byte>& x) const
{
#if defined(BOTAN_HAS_GCM_CLMUL)
diff --git a/src/lib/modes/aead/ocb/ocb.cpp b/src/lib/modes/aead/ocb/ocb.cpp
index 2ba30b2f9..2ba6d3ee6 100644
--- a/src/lib/modes/aead/ocb/ocb.cpp
+++ b/src/lib/modes/aead/ocb/ocb.cpp
@@ -5,14 +5,14 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/ocb.h>
#include <botan/cmac.h>
-#include <botan/internal/xor_buf.h>
-#include <botan/internal/bit_ops.h>
-#include <algorithm>
namespace Botan {
+BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(OCB_Encryption, OCB_Decryption, 16);
+
// Has to be in Botan namespace so unique_ptr can reference it
class L_computer
{
diff --git a/src/lib/modes/aead/siv/siv.cpp b/src/lib/modes/aead/siv/siv.cpp
index b183bd6a0..c1416e209 100644
--- a/src/lib/modes/aead/siv/siv.cpp
+++ b/src/lib/modes/aead/siv/siv.cpp
@@ -5,15 +5,16 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/
+#include <botan/internal/mode_utils.h>
#include <botan/siv.h>
#include <botan/cmac.h>
#include <botan/ctr.h>
#include <botan/parsing.h>
-#include <botan/internal/xor_buf.h>
-#include <algorithm>
namespace Botan {
+BOTAN_REGISTER_BLOCK_CIPHER_MODE(SIV_Encryption, SIV_Decryption);
+
SIV_Mode::SIV_Mode(BlockCipher* cipher) :
m_name(cipher->name() + "/SIV"),
m_ctr(new CTR_BE(cipher->clone())),
@@ -44,7 +45,7 @@ size_t SIV_Mode::update_granularity() const
/*
This value does not particularly matter as regardless SIV_Mode::update
buffers all input, so in theory this could be 1. However as for instance
- Transformation_Filter creates update_granularity() byte buffers, use a
+ Transform_Filter creates update_granularity() byte buffers, use a
somewhat large size to avoid bouncing on a tiny buffer.
*/
return 128;