diff options
author | Jack Lloyd <[email protected]> | 2016-10-11 13:00:57 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-10-21 16:53:16 -0400 |
commit | 558808900bffc3c48da5e6d79ba602e88e619154 (patch) | |
tree | fd76ee2d009c2b707d888683cbd767351c4ff6b3 /src/lib/prov/openssl | |
parent | 6aa855bba613c7b6fedfbe71d15930964acb1633 (diff) |
Remove Algo_Registry
I repent my use of global constructors.
I repent my use of global locks.
Hopefully I will never touch this code again.
:)
Diffstat (limited to 'src/lib/prov/openssl')
-rw-r--r-- | src/lib/prov/openssl/openssl.h | 33 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_block.cpp | 120 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_hash.cpp | 66 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_rc4.cpp | 9 |
4 files changed, 127 insertions, 101 deletions
diff --git a/src/lib/prov/openssl/openssl.h b/src/lib/prov/openssl/openssl.h index 58a7d77dc..8b2b41c1d 100644 --- a/src/lib/prov/openssl/openssl.h +++ b/src/lib/prov/openssl/openssl.h @@ -1,12 +1,12 @@ /* * Utils for calling OpenSSL -* (C) 2015 Jack Lloyd +* (C) 2015,2016 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ -#ifndef BOTAN_OPENSSL_H__ -#define BOTAN_OPENSSL_H__ +#ifndef BOTAN_INTERNAL_OPENSSL_H__ +#define BOTAN_INTERNAL_OPENSSL_H__ #include <botan/internal/pk_ops.h> #include <botan/secmem.h> @@ -15,9 +15,18 @@ #include <string> #include <openssl/err.h> +#include <openssl/evp.h> + +#if defined(BOTAN_HAS_RC4) +#include <openssl/rc4.h> +#endif namespace Botan { +class BlockCipher; +class StreamCipher; +class HashFunction; + class OpenSSL_Error : public Exception { public: @@ -25,9 +34,15 @@ class OpenSSL_Error : public Exception Exception(what + " failed: " + ERR_error_string(ERR_get_error(), nullptr)) {} }; -#define BOTAN_OPENSSL_BLOCK_PRIO 150 -#define BOTAN_OPENSSL_HASH_PRIO 150 -#define BOTAN_OPENSSL_RC4_PRIO 150 +/* Block Ciphers */ + +std::unique_ptr<BlockCipher> +make_openssl_block_cipher(const std::string& name); + +/* Hash */ + +std::unique_ptr<HashFunction> +make_openssl_hash(const std::string& name); /* RSA */ @@ -73,6 +88,12 @@ make_openssl_ecdh_ka_op(const ECDH_PrivateKey& key, const std::string& params); #endif +#if defined(BOTAN_HAS_RC4) + +std::unique_ptr<StreamCipher> +make_openssl_rc4(size_t skip); + +#endif } diff --git a/src/lib/prov/openssl/openssl_block.cpp b/src/lib/prov/openssl/openssl_block.cpp index 8f5ae89b1..cb98be70d 100644 --- a/src/lib/prov/openssl/openssl_block.cpp +++ b/src/lib/prov/openssl/openssl_block.cpp @@ -6,7 +6,6 @@ */ #include <botan/block_cipher.h> -#include <botan/internal/algo_registry.h> #include <botan/internal/openssl.h> #include <openssl/evp.h> @@ -17,10 +16,12 @@ namespace { class OpenSSL_BlockCipher : public BlockCipher { public: - OpenSSL_BlockCipher(const EVP_CIPHER*, const std::string&); + OpenSSL_BlockCipher(const std::string& name, + const EVP_CIPHER* cipher); - OpenSSL_BlockCipher(const EVP_CIPHER*, const std::string&, - size_t, size_t, size_t); + OpenSSL_BlockCipher(const std::string& name, + const EVP_CIPHER* cipher, + size_t kl_min, size_t kl_max, size_t kl_mod); ~OpenSSL_BlockCipher(); @@ -53,8 +54,8 @@ class OpenSSL_BlockCipher : public BlockCipher mutable EVP_CIPHER_CTX m_encrypt, m_decrypt; }; -OpenSSL_BlockCipher::OpenSSL_BlockCipher(const EVP_CIPHER* algo, - const std::string& algo_name) : +OpenSSL_BlockCipher::OpenSSL_BlockCipher(const std::string& algo_name, + const EVP_CIPHER* algo) : m_block_sz(EVP_CIPHER_block_size(algo)), m_cipher_key_spec(EVP_CIPHER_key_length(algo)), m_cipher_name(algo_name) @@ -72,10 +73,11 @@ OpenSSL_BlockCipher::OpenSSL_BlockCipher(const EVP_CIPHER* algo, EVP_CIPHER_CTX_set_padding(&m_decrypt, 0); } -OpenSSL_BlockCipher::OpenSSL_BlockCipher(const EVP_CIPHER* algo, - const std::string& algo_name, - size_t key_min, size_t key_max, - size_t key_mod) : +OpenSSL_BlockCipher::OpenSSL_BlockCipher(const std::string& algo_name, + const EVP_CIPHER* algo, + size_t key_min, + size_t key_max, + size_t key_mod) : m_block_sz(EVP_CIPHER_block_size(algo)), m_cipher_key_spec(key_min, key_max, key_mod), m_cipher_name(algo_name) @@ -125,11 +127,11 @@ void OpenSSL_BlockCipher::key_schedule(const byte key[], size_t length) */ BlockCipher* OpenSSL_BlockCipher::clone() const { - return new OpenSSL_BlockCipher(EVP_CIPHER_CTX_cipher(&m_encrypt), - m_cipher_name, - m_cipher_key_spec.minimum_keylength(), - m_cipher_key_spec.maximum_keylength(), - m_cipher_key_spec.keylength_multiple()); + return new OpenSSL_BlockCipher(m_cipher_name, + EVP_CIPHER_CTX_cipher(&m_encrypt), + m_cipher_key_spec.minimum_keylength(), + m_cipher_key_spec.maximum_keylength(), + m_cipher_key_spec.keylength_multiple()); } /* @@ -149,67 +151,63 @@ void OpenSSL_BlockCipher::clear() EVP_CIPHER_CTX_set_padding(&m_decrypt, 0); } -std::function<BlockCipher* (const BlockCipher::Spec&)> -make_evp_block_maker(const EVP_CIPHER* cipher, const char* algo) - { - return [cipher,algo](const BlockCipher::Spec&) - { - return new OpenSSL_BlockCipher(cipher, algo); - }; - } +} -std::function<BlockCipher* (const BlockCipher::Spec&)> -make_evp_block_maker_keylen(const EVP_CIPHER* cipher, const char* algo, - size_t kmin, size_t kmax, size_t kmod) +std::unique_ptr<BlockCipher> +make_openssl_block_cipher(const std::string& name) { - return [cipher,algo,kmin,kmax,kmod](const BlockCipher::Spec&) - { - return new OpenSSL_BlockCipher(cipher, algo, kmin, kmax, kmod); - }; - } - -#define BOTAN_REGISTER_OPENSSL_EVP_BLOCK(NAME, EVP) \ - BOTAN_REGISTER_TYPE(BlockCipher, EVP_BlockCipher ## EVP, NAME, \ - make_evp_block_maker(EVP(), NAME), "openssl", BOTAN_OPENSSL_BLOCK_PRIO) - -#define BOTAN_REGISTER_OPENSSL_EVP_BLOCK_KEYLEN(NAME, EVP, KMIN, KMAX, KMOD) \ - BOTAN_REGISTER_TYPE(BlockCipher, OpenSSL_BlockCipher ## EVP, NAME, \ - make_evp_block_maker_keylen(EVP(), NAME, KMIN, KMAX, KMOD), \ - "openssl", BOTAN_OPENSSL_BLOCK_PRIO) - -#if !defined(OPENSSL_NO_AES) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("AES-128", EVP_aes_128_ecb); - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("AES-192", EVP_aes_192_ecb); - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("AES-256", EVP_aes_256_ecb); +#define MAKE_OPENSSL_BLOCK(evp_fn) \ + std::unique_ptr<BlockCipher>(new OpenSSL_BlockCipher(name, evp_fn())) +#define MAKE_OPENSSL_BLOCK_KEYLEN(evp_fn, kl_min, kl_max, kl_mod) \ + std::unique_ptr<BlockCipher>(new OpenSSL_BlockCipher(name, evp_fn(), kl_min, kl_max, kl_mod)) + +#if defined(BOTAN_HAS_AES) && !defined(OPENSSL_NO_AES) + if(name == "AES-128") + return MAKE_OPENSSL_BLOCK(EVP_aes_128_ecb); + if(name == "AES-192") + return MAKE_OPENSSL_BLOCK(EVP_aes_192_ecb); + if(name == "AES-256") + return MAKE_OPENSSL_BLOCK(EVP_aes_256_ecb); #endif -#if !defined(OPENSSL_NO_DES) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("DES", EVP_des_ecb); - BOTAN_REGISTER_OPENSSL_EVP_BLOCK_KEYLEN("TripleDES", EVP_des_ede3_ecb, 16, 24, 8); +#if defined(BOTAN_HAS_CAMELLIA) && !defined(OPENSSL_NO_CAMELLIA) + if(name == "Camellia-128") + return MAKE_OPENSSL_BLOCK(EVP_camellia_128_ecb); + if(name == "Camellia-192") + return MAKE_OPENSSL_BLOCK(EVP_camellia_192_ecb); + if(name == "Camellia-256") + return MAKE_OPENSSL_BLOCK(EVP_camellia_256_ecb); #endif -#if !defined(OPENSSL_NO_BF) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK_KEYLEN("Blowfish", EVP_bf_ecb, 1, 56, 1); +#if defined(BOTAN_HAS_DES) && !defined(OPENSSL_NO_DES) + if(name == "DES") + return MAKE_OPENSSL_BLOCK(EVP_des_ecb); + if(name == "TripleDES") + return MAKE_OPENSSL_BLOCK_KEYLEN(EVP_des_ede3_ecb, 16, 24, 8); #endif -#if !defined(OPENSSL_NO_CAST) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK_KEYLEN("CAST-128", EVP_cast5_ecb, 1, 16, 1); +#if defined(BOTAN_HAS_BLOWFISH) && !defined(OPENSSL_NO_BF) + if(name == "Blowfish") + return MAKE_OPENSSL_BLOCK_KEYLEN(EVP_bf_ecb, 1, 56, 1); #endif -#if !defined(OPENSSL_NO_CAMELLIA) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("Camellia-128", EVP_camellia_128_ecb); - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("Camellia-192", EVP_camellia_192_ecb); - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("Camellia-256", EVP_camellia_256_ecb); +#if defined(BOTAN_HAS_CAST) && !defined(OPENSSL_NO_CAST) + if(name == "CAST-128") + return MAKE_OPENSSL_BLOCK_KEYLEN(EVP_cast5_ecb, 1, 16, 1); #endif -#if !defined(OPENSSL_NO_IDEA) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("IDEA", EVP_idea_ecb); +#if defined(BOTAN_HAS_IDEA) && !defined(OPENSSL_NO_IDEA) + if(name == "IDEA") + return MAKE_OPENSSL_BLOCK(EVP_idea_ecb); #endif -#if !defined(OPENSSL_NO_SEED) - BOTAN_REGISTER_OPENSSL_EVP_BLOCK("SEED", EVP_seed_ecb); +#if defined(BOTAN_HAS_SEED) && !defined(OPENSSL_NO_SEED) + if(name == "SEED") + return MAKE_OPENSSL_BLOCK(EVP_seed_ecb); #endif -} + return nullptr; + } } + diff --git a/src/lib/prov/openssl/openssl_hash.cpp b/src/lib/prov/openssl/openssl_hash.cpp index 15aebeb94..8e36866a1 100644 --- a/src/lib/prov/openssl/openssl_hash.cpp +++ b/src/lib/prov/openssl/openssl_hash.cpp @@ -7,8 +7,8 @@ #include <botan/hash.h> #include <botan/internal/openssl.h> -#include <botan/internal/algo_registry.h> #include <openssl/evp.h> +#include <unordered_map> namespace Botan { @@ -29,7 +29,7 @@ class OpenSSL_HashFunction : public HashFunction HashFunction* clone() const override { const EVP_MD* algo = EVP_MD_CTX_md(&m_md); - return new OpenSSL_HashFunction(algo, name()); + return new OpenSSL_HashFunction(name(), algo); } size_t output_length() const override @@ -42,7 +42,7 @@ class OpenSSL_HashFunction : public HashFunction return EVP_MD_block_size(EVP_MD_CTX_md(&m_md)); } - OpenSSL_HashFunction(const EVP_MD* md, const std::string& name) : m_name(name) + OpenSSL_HashFunction(const std::string& name, const EVP_MD* md) : m_name(name) { EVP_MD_CTX_init(&m_md); EVP_DigestInit_ex(&m_md, md, nullptr); @@ -70,45 +70,49 @@ class OpenSSL_HashFunction : public HashFunction EVP_MD_CTX m_md; }; -std::function<HashFunction* (const HashFunction::Spec&)> -make_evp_hash_maker(const EVP_MD* md, const char* algo) - { - return [md,algo](const HashFunction::Spec&) - { - return new OpenSSL_HashFunction(md, algo); - }; - } - -#define BOTAN_REGISTER_OPENSSL_EVP_HASH(NAME, EVP) \ - BOTAN_REGISTER_TYPE(HashFunction, OpenSSL_HashFunction ## EVP, NAME, \ - make_evp_hash_maker(EVP(), NAME), "openssl", BOTAN_OPENSSL_HASH_PRIO) +} -#if !defined(OPENSSL_NO_SHA) - BOTAN_REGISTER_OPENSSL_EVP_HASH("SHA-160", EVP_sha1); +std::unique_ptr<HashFunction> +make_openssl_hash(const std::string& name) + { +#define MAKE_OPENSSL_HASH(fn) \ + std::unique_ptr<HashFunction>(new OpenSSL_HashFunction(name, fn ())) + +#if defined(BOTAN_HAS_SHA2_32) && !defined(OPENSSL_NO_SHA256) + if(name == "SHA-224") + return MAKE_OPENSSL_HASH(EVP_sha224); + if(name == "SHA-256") + return MAKE_OPENSSL_HASH(EVP_sha256); #endif -#if !defined(OPENSSL_NO_SHA256) - BOTAN_REGISTER_OPENSSL_EVP_HASH("SHA-224", EVP_sha224); - BOTAN_REGISTER_OPENSSL_EVP_HASH("SHA-256", EVP_sha256); +#if defined(BOTAN_HAS_SHA2_64) && !defined(OPENSSL_NO_SHA512) + if(name == "SHA-384") + return MAKE_OPENSSL_HASH(EVP_sha384); + if(name == "SHA-512") + return MAKE_OPENSSL_HASH(EVP_sha512); #endif -#if !defined(OPENSSL_NO_SHA512) - BOTAN_REGISTER_OPENSSL_EVP_HASH("SHA-384", EVP_sha384); - BOTAN_REGISTER_OPENSSL_EVP_HASH("SHA-512", EVP_sha512); +#if defined(BOTAN_HAS_SHA1) && !defined(OPENSSL_NO_SHA) + if(name == "SHA-160") + return MAKE_OPENSSL_HASH(EVP_sha1); #endif -#if !defined(OPENSSL_NO_MD4) - BOTAN_REGISTER_OPENSSL_EVP_HASH("MD4", EVP_md4); +#if defined(BOTAN_HAS_RIPEMD_160) && !defined(OPENSSL_NO_RIPEMD) + if(name == "RIPEMD-160") + return MAKE_OPENSSL_HASH(EVP_ripemd160); #endif -#if !defined(OPENSSL_NO_MD5) - BOTAN_REGISTER_OPENSSL_EVP_HASH("MD5", EVP_md5); -#endif +#if defined(BOTAN_HAS_MD5) && !defined(OPENSSL_NO_MD5) + if(name == "MD5") + return MAKE_OPENSSL_HASH(EVP_md5); + #endif -#if !defined(OPENSSL_NO_RIPEMD) - BOTAN_REGISTER_OPENSSL_EVP_HASH("RIPEMD-160", EVP_ripemd160); +#if defined(BOTAN_HAS_MD4) && !defined(OPENSSL_NO_MD4) + if(name == "MD4") + return MAKE_OPENSSL_HASH(EVP_md4); #endif -} + return nullptr; + } } diff --git a/src/lib/prov/openssl/openssl_rc4.cpp b/src/lib/prov/openssl/openssl_rc4.cpp index 3db62c32a..a9b793678 100644 --- a/src/lib/prov/openssl/openssl_rc4.cpp +++ b/src/lib/prov/openssl/openssl_rc4.cpp @@ -9,7 +9,6 @@ #if defined(BOTAN_HAS_OPENSSL) && defined(BOTAN_HAS_RC4) -#include <botan/internal/algo_registry.h> #include <botan/internal/openssl.h> #include <botan/parsing.h> #include <botan/exceptn.h> @@ -78,8 +77,12 @@ class OpenSSL_RC4 : public StreamCipher } -BOTAN_REGISTER_TYPE(StreamCipher, OpenSSL_RC4, "RC4", (make_new_T_1len<OpenSSL_RC4,0>), - "openssl", BOTAN_OPENSSL_RC4_PRIO); +std::unique_ptr<StreamCipher> +make_openssl_rc4(size_t skip) + { + return std::unique_ptr<StreamCipher>(new OpenSSL_RC4(skip)); + } + } |