aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-11-10 22:57:20 +0000
committerlloyd <[email protected]>2008-11-10 22:57:20 +0000
commitc975e9017032bcbce0d5beffd7fc89fa1b3210a0 (patch)
tree15053baece03059df1880ae6d79f35dff6004e1c /src/libstate
parent33cff0df7b08624e4336b79744eb82bf5112809c (diff)
Move get_bc_pad to def_engine/def_mode.cpp
Compilation fix in arc4_openssl.cpp
Diffstat (limited to 'src/libstate')
-rw-r--r--src/libstate/engine/def_engine/def_mode.cpp103
-rw-r--r--src/libstate/engine/openssl/arc4_openssl.cpp2
-rw-r--r--src/libstate/get_enc.cpp28
-rw-r--r--src/libstate/libstate.cpp11
-rw-r--r--src/libstate/lookup.h7
5 files changed, 66 insertions, 85 deletions
diff --git a/src/libstate/engine/def_engine/def_mode.cpp b/src/libstate/engine/def_engine/def_mode.cpp
index 1148d6312..52470080b 100644
--- a/src/libstate/engine/def_engine/def_mode.cpp
+++ b/src/libstate/engine/def_engine/def_mode.cpp
@@ -6,7 +6,9 @@
#include <botan/def_eng.h>
#include <botan/parsing.h>
#include <botan/filters.h>
+#include <botan/libstate.h>
#include <botan/lookup.h>
+#include <botan/mode_pad.h>
#include <memory>
#if defined(BOTAN_HAS_ECB)
@@ -39,6 +41,34 @@
namespace Botan {
+namespace {
+
+/**
+* Get a block cipher padding method by name
+*/
+BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec)
+ {
+ SCAN_Name request(algo_spec);
+
+#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)
+ if(request.algo_name() == "PKCS7")
+ return new PKCS7_Padding;
+
+ if(request.algo_name() == "OneAndZeros")
+ return new OneAndZeros_Padding;
+
+ if(request.algo_name() == "X9.23")
+ return new ANSI_X923_Padding;
+
+ if(request.algo_name() == "NoPadding")
+ return new Null_Padding;
+#endif
+
+ throw Algorithm_Not_Found(algo_spec);
+ }
+
+}
+
/*************************************************
* Get a cipher object *
*************************************************/
@@ -51,17 +81,16 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
const std::string cipher_name = algo_parts[0];
- if(have_stream_cipher(cipher_name))
- {
- if(algo_parts.size() == 1)
- return new StreamCipher_Filter(cipher_name);
- return 0;
- }
+ Algorithm_Factory& af = global_state().algo_factory();
- if(!have_block_cipher(cipher_name))
- return 0;
+ // check if it is a stream cipher first (easy case)
+ const StreamCipher* stream_cipher = af.prototype_stream_cipher(cipher_name);
+ if(stream_cipher)
+ return new StreamCipher_Filter(stream_cipher->clone());
- std::auto_ptr<BlockCipher> cipher(get_block_cipher(cipher_name));
+ const BlockCipher* block_cipher = af.prototype_block_cipher(cipher_name);
+ if(!block_cipher)
+ return 0;
if(algo_parts.size() != 2 && algo_parts.size() != 3)
return 0;
@@ -75,7 +104,7 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
std::vector<std::string> algo_info = parse_algorithm_name(mode);
mode = algo_info[0];
if(algo_info.size() == 1)
- bits = 8*cipher->BLOCK_SIZE;
+ bits = 8*block_cipher->BLOCK_SIZE;
else if(algo_info.size() == 2)
bits = to_u32bit(algo_info[1]);
else
@@ -93,47 +122,35 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
else if((mode != "CBC" && mode != "ECB") && padding != "NoPadding")
throw Invalid_Algorithm_Name(algo_spec);
- if(mode == "OFB")
- {
#if defined(BOTAN_HAS_OFB)
- return new OFB(cipher.release());
-#else
- return 0;
+ if(mode == "OFB")
+ return new OFB(block_cipher->clone());
#endif
- }
- if(mode == "CTR-BE")
- {
#if defined(BOTAN_HAS_CTR)
- return new CTR_BE(cipher.release());
-#else
- return 0;
+ if(mode == "CTR-BE")
+ return new CTR_BE(block_cipher->clone());
#endif
- }
+#if defined(BOTAN_HAS_ECB)
if(mode == "ECB")
{
-#if defined(BOTAN_HAS_ECB)
if(direction == ENCRYPTION)
- return new ECB_Encryption(cipher.release(), get_bc_pad(padding));
+ return new ECB_Encryption(block_cipher->clone(), get_bc_pad(padding));
else
- return new ECB_Decryption(cipher.release(), get_bc_pad(padding));
-#else
- return 0;
-#endif
+ return new ECB_Decryption(block_cipher->clone(), get_bc_pad(padding));
}
+#endif
+#if defined(BOTAN_HAS_CFB)
if(mode == "CFB")
{
-#if defined(BOTAN_HAS_CFB)
if(direction == ENCRYPTION)
- return new CFB_Encryption(cipher.release(), bits);
+ return new CFB_Encryption(block_cipher->clone(), bits);
else
- return new CFB_Decryption(cipher.release(), bits);
-#else
- return 0;
-#endif
+ return new CFB_Decryption(block_cipher->clone(), bits);
}
+#endif
if(mode == "CBC")
{
@@ -141,9 +158,9 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
{
#if defined(BOTAN_HAS_CTS)
if(direction == ENCRYPTION)
- return new CTS_Encryption(cipher.release());
+ return new CTS_Encryption(block_cipher->clone());
else
- return new CTS_Decryption(cipher.release());
+ return new CTS_Decryption(block_cipher->clone());
#else
return 0;
#endif
@@ -151,27 +168,25 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec,
#if defined(BOTAN_HAS_CBC)
if(direction == ENCRYPTION)
- return new CBC_Encryption(cipher.release(),
+ return new CBC_Encryption(block_cipher->clone(),
get_bc_pad(padding));
else
- return new CBC_Decryption(cipher.release(),
+ return new CBC_Decryption(block_cipher->clone(),
get_bc_pad(padding));
#else
return 0;
#endif
}
+#if defined(BOTAN_HAS_EAX)
if(mode == "EAX")
{
-#if defined(BOTAN_HAS_EAX)
if(direction == ENCRYPTION)
- return new EAX_Encryption(cipher.release(), bits);
+ return new EAX_Encryption(block_cipher->clone(), bits);
else
- return new EAX_Decryption(cipher.release(), bits);
-#else
- return 0;
-#endif
+ return new EAX_Decryption(block_cipher->clone(), bits);
}
+#endif
throw Algorithm_Not_Found("get_mode: " + cipher_name + "/" +
mode + "/" + padding);
diff --git a/src/libstate/engine/openssl/arc4_openssl.cpp b/src/libstate/engine/openssl/arc4_openssl.cpp
index 59bdd30d7..8d0c2a080 100644
--- a/src/libstate/engine/openssl/arc4_openssl.cpp
+++ b/src/libstate/engine/openssl/arc4_openssl.cpp
@@ -3,7 +3,7 @@
* (C) 1999-2007 Jack Lloyd *
*************************************************/
-#include <botan/arc4_openssl.h>
+#include <botan/eng_ossl.h>
#include <botan/parsing.h>
#include <openssl/rc4.h>
diff --git a/src/libstate/get_enc.cpp b/src/libstate/get_enc.cpp
index 4c28c4b98..f2398e318 100644
--- a/src/libstate/get_enc.cpp
+++ b/src/libstate/get_enc.cpp
@@ -76,10 +76,6 @@
#include <botan/prf_tls.h>
#endif
-#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)
- #include <botan/mode_pad.h>
-#endif
-
namespace Botan {
/*************************************************
@@ -108,30 +104,6 @@ S2K* get_s2k(const std::string& algo_spec)
}
/*************************************************
-* Get a block cipher padding method by name *
-*************************************************/
-BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec)
- {
- SCAN_Name request(algo_spec);
-
-#if defined(BOTAN_HAS_CIPHER_MODE_PADDING)
- if(request.algo_name() == "PKCS7")
- return new PKCS7_Padding;
-
- if(request.algo_name() == "OneAndZeros")
- return new OneAndZeros_Padding;
-
- if(request.algo_name() == "X9.23")
- return new ANSI_X923_Padding;
-
- if(request.algo_name() == "NoPadding")
- return new Null_Padding;
-#endif
-
- throw Algorithm_Not_Found(algo_spec);
- }
-
-/*************************************************
* Get an EMSA by name *
*************************************************/
EMSA* get_emsa(const std::string& algo_spec)
diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp
index 9096d98e6..c4d81de7a 100644
--- a/src/libstate/libstate.cpp
+++ b/src/libstate/libstate.cpp
@@ -4,6 +4,7 @@
*************************************************/
#include <botan/libstate.h>
+#include <botan/init.h>
#include <botan/engine.h>
#include <botan/stl_util.h>
#include <botan/mutex.h>
@@ -37,10 +38,6 @@
#include <botan/eng_ossl.h>
#endif
-#if defined(BOTAN_HAS_SELFTEST)
- #include <botan/selftest.h>
-#endif
-
namespace Botan {
/*************************************************
@@ -57,8 +54,12 @@ Library_State* global_lib_state = 0;
*************************************************/
Library_State& global_state()
{
+ /* Lazy initialization. Botan still needs to be deinitialized later
+ on or memory might leak.
+ */
if(!global_lib_state)
- LibraryInitializer::initialize();
+ LibraryInitializer::initialize(true);
+
return (*global_lib_state);
}
diff --git a/src/libstate/lookup.h b/src/libstate/lookup.h
index eda367543..ec7932338 100644
--- a/src/libstate/lookup.h
+++ b/src/libstate/lookup.h
@@ -82,13 +82,6 @@ BOTAN_DLL MessageAuthenticationCode* get_mac(const std::string& name);
*/
BOTAN_DLL S2K* get_s2k(const std::string& name);
-/**
-* Block cipher padding mode factory method.
-* @param name the name of the desired block cipher padding mode
-* @return the block cipher padding mode object
-*/
-BOTAN_DLL BlockCipherModePaddingMethod* get_bc_pad(const std::string& name);
-
/*************************************************
* Get an EMSA/EME/KDF/MGF function *
*************************************************/