aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/modes/mode_utils.h
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/mode_utils.h
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/mode_utils.h')
-rw-r--r--src/lib/modes/mode_utils.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h
new file mode 100644
index 000000000..0333403a3
--- /dev/null
+++ b/src/lib/modes/mode_utils.h
@@ -0,0 +1,76 @@
+/*
+* Helper include for mode implementations
+* (C) 2015 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_MODE_UTILS_H__
+#define BOTAN_MODE_UTILS_H__
+
+#include <botan/cipher_mode.h>
+#include <botan/algo_registry.h>
+#include <botan/block_cipher.h>
+#include <botan/libstate.h>
+#include <botan/loadstor.h>
+#include <botan/internal/xor_buf.h>
+#include <botan/internal/rounding.h>
+#include <botan/internal/bit_ops.h>
+#include <algorithm>
+
+namespace Botan {
+
+template<typename T>
+T* make_block_cipher_mode(const Transform::Spec& spec)
+ {
+ Algorithm_Factory& af = global_state().algorithm_factory();
+
+ if(const BlockCipher* bc = af.prototype_block_cipher(spec.arg(0)))
+ return new T(bc->clone());
+ return nullptr;
+ }
+
+template<typename T, size_t LEN1>
+T* make_block_cipher_mode_len(const Transform::Spec& spec)
+ {
+ Algorithm_Factory& af = global_state().algorithm_factory();
+
+ if(const BlockCipher* bc = af.prototype_block_cipher(spec.arg(0)))
+ {
+ const size_t len1 = spec.arg_as_integer(1, LEN1);
+ return new T(bc->clone(), len1);
+ }
+
+ return nullptr;
+ }
+
+template<typename T, size_t LEN1, size_t LEN2>
+T* make_block_cipher_mode_len2(const Transform::Spec& spec)
+ {
+ Algorithm_Factory& af = global_state().algorithm_factory();
+
+ if(const BlockCipher* bc = af.prototype_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 nullptr;
+ }
+
+#define BOTAN_REGISTER_BLOCK_CIPHER_MODE(E, D) \
+ namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode<E>); \
+ Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode<D>); }
+
+#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN(E, D, LEN) \
+ namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode_len<E, LEN>); \
+ Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode_len<D, LEN>); }
+
+#define BOTAN_REGISTER_BLOCK_CIPHER_MODE_LEN2(E, D, LEN1, LEN2) \
+ namespace { Algo_Registry<Transform>::Add g_ ## E ## _reg(#E, make_block_cipher_mode_len2<E, LEN1, LEN2>); \
+ Algo_Registry<Transform>::Add g_ ## D ## _reg(#D, make_block_cipher_mode_len2<D, LEN1, LEN2>); }
+
+}
+
+#endif