aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/modes/cipher_mode.cpp
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/cipher_mode.cpp
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/cipher_mode.cpp')
-rw-r--r--src/lib/modes/cipher_mode.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/lib/modes/cipher_mode.cpp b/src/lib/modes/cipher_mode.cpp
new file mode 100644
index 000000000..ded7b4c81
--- /dev/null
+++ b/src/lib/modes/cipher_mode.cpp
@@ -0,0 +1,59 @@
+/*
+* Cipher Modes
+* (C) 2015 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include <botan/cipher_mode.h>
+#include <sstream>
+
+namespace Botan {
+
+Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction)
+ {
+ const char* dir_string = (direction == ENCRYPTION) ? "_Encryption" : "_Decryption";
+
+ const std::string provider = "";
+
+ std::unique_ptr<Transform> t;
+
+ t.reset(get_transform(algo_spec, provider, dir_string));
+
+ if(Cipher_Mode* cipher = dynamic_cast<Cipher_Mode*>(t.get()))
+ {
+ t.release();
+ return cipher;
+ }
+
+ const std::vector<std::string> algo_parts = split_on(algo_spec, '/');
+ if(algo_parts.size() < 2)
+ return nullptr;
+
+ const std::string cipher_name = algo_parts[0];
+ const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
+
+ if(mode_info.empty())
+ return nullptr;
+
+ std::ostringstream t_name;
+
+ t_name << mode_info[0] << dir_string << '(' << cipher_name;
+ for(size_t i = 1; i < mode_info.size(); ++i)
+ t_name << ',' << mode_info[i];
+ for(size_t i = 2; i < algo_parts.size(); ++i)
+ t_name << ',' << algo_parts[i];
+ t_name << ')';
+
+ t.reset(get_transform(t_name.str(), provider));
+
+ if(Cipher_Mode* cipher = dynamic_cast<Cipher_Mode*>(t.get()))
+ {
+ t.release();
+ return cipher;
+ }
+
+ return nullptr;
+ }
+
+}