diff options
Diffstat (limited to 'src/lib')
38 files changed, 149 insertions, 159 deletions
diff --git a/src/lib/block/cascade/cascade.cpp b/src/lib/block/cascade/cascade.cpp index 66ff293ff..100fb33ab 100644 --- a/src/lib/block/cascade/cascade.cpp +++ b/src/lib/block/cascade/cascade.cpp @@ -6,14 +6,13 @@ */ #include <botan/cascade.h> -#include <botan/lookup.h> namespace Botan { Cascade_Cipher* Cascade_Cipher::make(const BlockCipher::Spec& spec) { - std::unique_ptr<BlockCipher> c1(get_block_cipher(spec.arg(0))); - std::unique_ptr<BlockCipher> c2(get_block_cipher(spec.arg(1))); + std::unique_ptr<BlockCipher> c1(BlockCipher::create(spec.arg(0))); + std::unique_ptr<BlockCipher> c2(BlockCipher::create(spec.arg(1))); if(c1 && c2) return new Cascade_Cipher(c1.release(), c2.release()); diff --git a/src/lib/block/lion/lion.cpp b/src/lib/block/lion/lion.cpp index bbd24a3c1..559816aea 100644 --- a/src/lib/block/lion/lion.cpp +++ b/src/lib/block/lion/lion.cpp @@ -6,7 +6,6 @@ */ #include <botan/lion.h> -#include <botan/lookup.h> #include <botan/parsing.h> namespace Botan { @@ -15,8 +14,8 @@ Lion* Lion::make(const BlockCipher::Spec& spec) { if(spec.arg_count_between(2, 3)) { - std::unique_ptr<HashFunction> hash(get_hash_function(spec.arg(0))); - std::unique_ptr<StreamCipher> stream(get_stream_cipher(spec.arg(1))); + std::unique_ptr<HashFunction> hash(HashFunction::create(spec.arg(0))); + std::unique_ptr<StreamCipher> stream(StreamCipher::create(spec.arg(1))); if(hash && stream) { diff --git a/src/lib/cert/x509/ocsp_types.cpp b/src/lib/cert/x509/ocsp_types.cpp index 0877f848d..04ab1ea03 100644 --- a/src/lib/cert/x509/ocsp_types.cpp +++ b/src/lib/cert/x509/ocsp_types.cpp @@ -9,7 +9,6 @@ #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/x509_ext.h> -#include <botan/lookup.h> #include <botan/hash.h> #include <botan/oids.h> @@ -24,7 +23,7 @@ CertID::CertID(const X509_Certificate& issuer, In practice it seems some responders, including, notably, ocsp.verisign.com, will reject anything but SHA-1 here */ - std::unique_ptr<HashFunction> hash(get_hash("SHA-160")); + std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-160")); m_hash_id = AlgorithmIdentifier(hash->name(), AlgorithmIdentifier::USE_NULL_PARAM); m_issuer_key_hash = unlock(hash->process(extract_key_bitstr(issuer))); @@ -54,7 +53,7 @@ bool CertID::is_id_for(const X509_Certificate& issuer, if(BigInt::decode(subject.serial_number()) != m_subject_serial) return false; - std::unique_ptr<HashFunction> hash(get_hash(OIDS::lookup(m_hash_id.oid))); + std::unique_ptr<HashFunction> hash(HashFunction::create(OIDS::lookup(m_hash_id.oid))); if(m_issuer_dn_hash != unlock(hash->process(subject.raw_issuer_dn()))) return false; diff --git a/src/lib/cert/x509/x509_ca.cpp b/src/lib/cert/x509/x509_ca.cpp index e6f689016..b6bb5d8ce 100644 --- a/src/lib/cert/x509/x509_ca.cpp +++ b/src/lib/cert/x509/x509_ca.cpp @@ -11,7 +11,6 @@ #include <botan/ber_dec.h> #include <botan/bigint.h> #include <botan/parsing.h> -#include <botan/lookup.h> #include <botan/oids.h> #include <botan/hash.h> #include <botan/key_constraint.h> @@ -102,6 +101,7 @@ X509_Certificate X509_CA::make_cert(PK_Signer* signer, BigInt serial_no(rng, SERIAL_BITS); + // clang-format off return X509_Certificate(X509_Object::make_signed( signer, rng, sig_algo, DER_Encoder().start_cons(SEQUENCE) @@ -130,6 +130,7 @@ X509_Certificate X509_CA::make_cert(PK_Signer* signer, .end_cons() .get_contents() ));; + // clang-format on } /* @@ -179,6 +180,7 @@ X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked, new Cert_Extension::Authority_Key_ID(cert.subject_key_id())); extensions.add(new Cert_Extension::CRL_Number(crl_number)); + // clang-format off const std::vector<byte> crl = X509_Object::make_signed( signer, rng, ca_sig_algo, DER_Encoder().start_cons(SEQUENCE) @@ -200,6 +202,7 @@ X509_CRL X509_CA::make_crl(const std::vector<CRL_Entry>& revoked, .end_explicit() .end_cons() .get_contents()); + // clang-format on return X509_CRL(crl); } @@ -221,7 +224,7 @@ PK_Signer* choose_sig_format(const Private_Key& key, { const std::string algo_name = key.algo_name(); - std::unique_ptr<HashFunction> hash(get_hash(hash_fn)); + std::unique_ptr<HashFunction> hash(HashFunction::create(hash_fn)); if(!hash) throw Algorithm_Not_Found(hash_fn); diff --git a/src/lib/cert/x509/x509cert.cpp b/src/lib/cert/x509/x509cert.cpp index f6f87bbf4..48e437352 100644 --- a/src/lib/cert/x509/x509cert.cpp +++ b/src/lib/cert/x509/x509cert.cpp @@ -12,7 +12,6 @@ #include <botan/internal/stl_util.h> #include <botan/parsing.h> #include <botan/bigint.h> -#include <botan/lookup.h> #include <botan/oids.h> #include <botan/pem.h> #include <botan/hash.h> @@ -369,7 +368,7 @@ bool cert_subject_dns_match(const std::string& name, std::string X509_Certificate::fingerprint(const std::string& hash_name) const { - std::unique_ptr<HashFunction> hash(get_hash(hash_name)); + std::unique_ptr<HashFunction> hash(HashFunction::create(hash_name)); hash->update(this->BER_encode()); const auto hex_print = hex_encode(hash->final()); diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 8d96a0fc7..aad6e0faf 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -7,7 +7,6 @@ #include <botan/ffi.h> #include <botan/system_rng.h> #include <botan/auto_rng.h> -#include <botan/lookup.h> #include <botan/aead.h> #include <botan/hash.h> #include <botan/mac.h> @@ -277,9 +276,10 @@ int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags) if(flags != 0) return BOTAN_FFI_ERROR_BAD_FLAG; - if(auto h = Botan::get_hash_function(hash_name)) + auto h = Botan::HashFunction::create(hash_name); + if(h) { - *hash = new botan_hash_struct(h); + *hash = new botan_hash_struct(h.release()); return 0; } } @@ -328,9 +328,10 @@ int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags) if(!mac || !mac_name || flags != 0) return -1; - if(auto m = Botan::get_mac(mac_name)) + auto m = Botan::MessageAuthenticationCode::create(mac_name); + if(m) { - *mac = new botan_mac_struct(m); + *mac = new botan_mac_struct(m.release()); return 0; } } @@ -898,7 +899,7 @@ int botan_pubkey_fingerprint(botan_pubkey_t key, const char* hash_fn, uint8_t out[], size_t* out_len) { return BOTAN_FFI_DO(Botan::Public_Key, key, { - std::unique_ptr<Botan::HashFunction> h(Botan::get_hash(hash_fn)); + std::unique_ptr<Botan::HashFunction> h(Botan::HashFunction::create(hash_fn)); return write_vec_output(out, out_len, h->process(key.x509_subject_public_key())); }); } diff --git a/src/lib/filters/algo_filt.cpp b/src/lib/filters/algo_filt.cpp index 6ec8cdd1d..bfadef924 100644 --- a/src/lib/filters/algo_filt.cpp +++ b/src/lib/filters/algo_filt.cpp @@ -6,7 +6,6 @@ */ #include <botan/filters.h> -#include <botan/lookup.h> #include <algorithm> namespace Botan { @@ -26,14 +25,18 @@ StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher, const SymmetricKe StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) : m_buffer(DEFAULT_BUFFERSIZE), - m_cipher(make_stream_cipher(sc_name)) + m_cipher(StreamCipher::create(sc_name)) { + if(!m_cipher) + throw Algorithm_Not_Found(sc_name); } StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, const SymmetricKey& key) : m_buffer(DEFAULT_BUFFERSIZE), - m_cipher(make_stream_cipher(sc_name)) + m_cipher(StreamCipher::create(sc_name)) { + if(!m_cipher) + throw Algorithm_Not_Found(sc_name); m_cipher->set_key(key); } @@ -50,13 +53,13 @@ void StreamCipher_Filter::write(const byte input[], size_t length) } Hash_Filter::Hash_Filter(const std::string& hash_name, size_t len) : - m_hash(make_hash_function(hash_name)), + m_hash(HashFunction::create(hash_name)), m_out_len(len) { + if(!m_hash) + throw Algorithm_Not_Found(hash_name); } - -void Hash_Filter::end_msg() - { +void Hash_Filter::end_msg() { secure_vector<byte> output = m_hash->final(); if(m_out_len) send(output, std::min<size_t>(m_out_len, output.size())); @@ -65,15 +68,19 @@ void Hash_Filter::end_msg() } MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) : - m_mac(make_message_auth(mac_name)), + m_mac(MessageAuthenticationCode::create(mac_name)), m_out_len(len) { + if(!m_mac) + throw Algorithm_Not_Found(mac_name); } MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, size_t len) : - m_mac(make_message_auth(mac_name)), + m_mac(MessageAuthenticationCode::create(mac_name)), m_out_len(len) { + if(!m_mac) + throw Algorithm_Not_Found(mac_name); m_mac->set_key(key); } diff --git a/src/lib/kdf/hkdf/hkdf.cpp b/src/lib/kdf/hkdf/hkdf.cpp index d4c688afc..6f83853f9 100644 --- a/src/lib/kdf/hkdf/hkdf.cpp +++ b/src/lib/kdf/hkdf/hkdf.cpp @@ -6,17 +6,16 @@ */ #include <botan/hkdf.h> -#include <botan/lookup.h> namespace Botan { HKDF* HKDF::make(const Spec& spec) { - if(auto mac = get_mac(spec.arg(0))) - return new HKDF(mac); + if(auto mac = MessageAuthenticationCode::create(spec.arg(0))) + return new HKDF(mac.release()); - if(auto mac = get_mac("HMAC(" + spec.arg(0) + ")")) - return new HKDF(mac); + if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")")) + return new HKDF(mac.release()); return nullptr; } diff --git a/src/lib/kdf/prf_tls/prf_tls.cpp b/src/lib/kdf/prf_tls/prf_tls.cpp index 382f527ea..547b0c9c8 100644 --- a/src/lib/kdf/prf_tls/prf_tls.cpp +++ b/src/lib/kdf/prf_tls/prf_tls.cpp @@ -7,23 +7,26 @@ #include <botan/prf_tls.h> #include <botan/hmac.h> -#include <botan/lookup.h> namespace Botan { TLS_12_PRF* TLS_12_PRF::make(const Spec& spec) { - if(auto mac = get_mac(spec.arg(0))) - return new TLS_12_PRF(mac); - if(auto hash = get_hash_function(spec.arg(0))) - return new TLS_12_PRF(new HMAC(hash)); + if(auto mac = MessageAuthenticationCode::create(spec.arg(0))) + return new TLS_12_PRF(mac.release()); + + if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")")) + return new TLS_12_PRF(mac.release()); + return nullptr; } TLS_PRF::TLS_PRF() : - m_hmac_md5(make_message_auth("HMAC(MD5)")), - m_hmac_sha1(make_message_auth("HMAC(SHA-1)")) + m_hmac_md5(MessageAuthenticationCode::create("HMAC(MD5)")), + m_hmac_sha1(MessageAuthenticationCode::create("HMAC(SHA-1)")) { + if(!m_hmac_md5 || !m_hmac_sha1) + throw Algorithm_Not_Found("TLS_PRF HMACs not available"); } namespace { diff --git a/src/lib/kdf/prf_x942/prf_x942.cpp b/src/lib/kdf/prf_x942/prf_x942.cpp index 443e207f2..fb8de1e85 100644 --- a/src/lib/kdf/prf_x942/prf_x942.cpp +++ b/src/lib/kdf/prf_x942/prf_x942.cpp @@ -10,7 +10,6 @@ #include <botan/oids.h> #include <botan/hash.h> #include <botan/loadstor.h> -#include <botan/lookup.h> #include <algorithm> namespace Botan { @@ -33,7 +32,7 @@ size_t X942_PRF::kdf(byte key[], size_t key_len, const byte secret[], size_t secret_len, const byte salt[], size_t salt_len) const { - std::unique_ptr<HashFunction> hash(make_hash_function("SHA-160")); + std::unique_ptr<HashFunction> hash(HashFunction::create("SHA-160")); const OID kek_algo(m_key_wrap_oid); secure_vector<byte> h; diff --git a/src/lib/mac/cbc_mac/cbc_mac.cpp b/src/lib/mac/cbc_mac/cbc_mac.cpp index 8133b9a6f..449865255 100644 --- a/src/lib/mac/cbc_mac/cbc_mac.cpp +++ b/src/lib/mac/cbc_mac/cbc_mac.cpp @@ -6,7 +6,6 @@ */ #include <botan/cbc_mac.h> -#include <botan/lookup.h> namespace Botan { @@ -14,7 +13,7 @@ CBC_MAC* CBC_MAC::make(const Spec& spec) { if(spec.arg_count() == 1) { - if(auto bc = make_block_cipher(spec.arg(0))) + if(auto bc = BlockCipher::create(spec.arg(0))) return new CBC_MAC(bc.release()); } return nullptr; diff --git a/src/lib/mac/cmac/cmac.cpp b/src/lib/mac/cmac/cmac.cpp index 0e42e3823..27edda233 100644 --- a/src/lib/mac/cmac/cmac.cpp +++ b/src/lib/mac/cmac/cmac.cpp @@ -6,7 +6,6 @@ */ #include <botan/cmac.h> -#include <botan/lookup.h> namespace Botan { @@ -14,8 +13,8 @@ CMAC* CMAC::make(const Spec& spec) { if(spec.arg_count() == 1) { - if(BlockCipher* bc = get_block_cipher(spec.arg(0))) - return new CMAC(bc); + if(auto bc = BlockCipher::create(spec.arg(0))) + return new CMAC(bc.release()); } return nullptr; } diff --git a/src/lib/mac/hmac/hmac.cpp b/src/lib/mac/hmac/hmac.cpp index 40da31887..f445ab0cf 100644 --- a/src/lib/mac/hmac/hmac.cpp +++ b/src/lib/mac/hmac/hmac.cpp @@ -7,7 +7,6 @@ */ #include <botan/hmac.h> -#include <botan/lookup.h> namespace Botan { @@ -15,8 +14,8 @@ HMAC* HMAC::make(const Spec& spec) { if(spec.arg_count() == 1) { - if(HashFunction* h = get_hash_function(spec.arg(0))) - return new HMAC(h); + if(auto h = HashFunction::create(spec.arg(0))) + return new HMAC(h.release()); } return nullptr; } diff --git a/src/lib/mac/x919_mac/x919_mac.cpp b/src/lib/mac/x919_mac/x919_mac.cpp index 2e1fa374f..205d812c2 100644 --- a/src/lib/mac/x919_mac/x919_mac.cpp +++ b/src/lib/mac/x919_mac/x919_mac.cpp @@ -6,7 +6,6 @@ */ #include <botan/x919_mac.h> -#include <botan/lookup.h> namespace Botan { @@ -86,10 +85,11 @@ MessageAuthenticationCode* ANSI_X919_MAC::clone() const /* * ANSI X9.19 MAC Constructor */ -ANSI_X919_MAC::ANSI_X919_MAC() : m_state(8), m_position(0) +ANSI_X919_MAC::ANSI_X919_MAC() : + m_des1(BlockCipher::create("DES")), + m_des2(BlockCipher::create("DES")), + m_state(8), m_position(0) { - m_des1.reset(get_block_cipher("DES")); - m_des2.reset(m_des1->clone()); } } diff --git a/src/lib/math/numbertheory/dsa_gen.cpp b/src/lib/math/numbertheory/dsa_gen.cpp index bd3c0e4a1..60151355a 100644 --- a/src/lib/math/numbertheory/dsa_gen.cpp +++ b/src/lib/math/numbertheory/dsa_gen.cpp @@ -6,7 +6,6 @@ */ #include <botan/numthry.h> -#include <botan/lookup.h> #include <botan/hash.h> #include <botan/parsing.h> #include <algorithm> @@ -52,7 +51,10 @@ bool generate_dsa_primes(RandomNumberGenerator& rng, "Generating a DSA parameter set with a " + std::to_string(qbits) + "long q requires a seed at least as many bits long"); - std::unique_ptr<HashFunction> hash(make_hash_function("SHA-" + std::to_string(qbits))); + const std::string hash_name = "SHA-" + std::to_string(qbits); + std::unique_ptr<HashFunction> hash(HashFunction::create(hash_name)); + if(!hash) + throw Algorithm_Not_Found(hash_name); const size_t HASH_SIZE = hash->output_length(); diff --git a/src/lib/misc/benchmark/benchmark.cpp b/src/lib/misc/benchmark/benchmark.cpp index 152b45d37..d5e3694b5 100644 --- a/src/lib/misc/benchmark/benchmark.cpp +++ b/src/lib/misc/benchmark/benchmark.cpp @@ -7,7 +7,6 @@ #include <botan/benchmark.h> #include <botan/exceptn.h> -#include <botan/lookup.h> #include <botan/buf_comp.h> #include <botan/cipher_mode.h> #include <botan/block_cipher.h> @@ -56,10 +55,8 @@ time_algorithm_ops(const std::string& name, const double mb_mult = buffer.size() / static_cast<double>(Mebibyte); - if(BlockCipher* p = get_block_cipher(name, provider)) + if(auto bc = BlockCipher::create(name, provider)) { - std::unique_ptr<BlockCipher> bc(p); - const SymmetricKey key(rng, bc->maximum_keylength()); return std::map<std::string, double>({ @@ -68,10 +65,8 @@ time_algorithm_ops(const std::string& name, { "decrypt", mb_mult * time_op(runtime / 2, [&]() { bc->decrypt(buffer); }) }, }); } - else if(StreamCipher* p = get_stream_cipher(name, provider)) + else if(auto sc = StreamCipher::create(name, provider)) { - std::unique_ptr<StreamCipher> sc(p); - const SymmetricKey key(rng, sc->maximum_keylength()); return std::map<std::string, double>({ @@ -79,18 +74,14 @@ time_algorithm_ops(const std::string& name, { "", mb_mult * time_op(runtime, [&]() { sc->encipher(buffer); }) }, }); } - else if(HashFunction* p = get_hash_function(name, provider)) + else if(auto h = HashFunction::create(name, provider)) { - std::unique_ptr<HashFunction> h(p); - return std::map<std::string, double>({ { "", mb_mult * time_op(runtime, [&]() { h->update(buffer); }) }, }); } - else if(MessageAuthenticationCode* p = get_mac(name, provider)) + else if(auto mac = MessageAuthenticationCode::create(name, provider)) { - std::unique_ptr<MessageAuthenticationCode> mac(p); - const SymmetricKey key(rng, mac->maximum_keylength()); return std::map<std::string, double>({ @@ -137,10 +128,10 @@ std::set<std::string> get_all_providers_of(const std::string& algo) auto add_to_set = [&provs](const std::vector<std::string>& str) { for(auto&& s : str) { provs.insert(s); } }; - add_to_set(get_block_cipher_providers(algo)); - add_to_set(get_stream_cipher_providers(algo)); - add_to_set(get_hash_function_providers(algo)); - add_to_set(get_mac_providers(algo)); + add_to_set(BlockCipher::providers(algo)); + add_to_set(StreamCipher::providers(algo)); + add_to_set(HashFunction::providers(algo)); + add_to_set(MessageAuthenticationCode::providers(algo)); return provs; } diff --git a/src/lib/misc/cryptobox/cryptobox.cpp b/src/lib/misc/cryptobox/cryptobox.cpp index 752561248..c0fc9b777 100644 --- a/src/lib/misc/cryptobox/cryptobox.cpp +++ b/src/lib/misc/cryptobox/cryptobox.cpp @@ -8,7 +8,6 @@ #include <botan/cryptobox.h> #include <botan/filters.h> #include <botan/pipe.h> -#include <botan/lookup.h> #include <botan/sha2_64.h> #include <botan/hmac.h> #include <botan/pbkdf2.h> diff --git a/src/lib/misc/pbes2/pbes2.cpp b/src/lib/misc/pbes2/pbes2.cpp index 89af01e9d..ab740ff5d 100644 --- a/src/lib/misc/pbes2/pbes2.cpp +++ b/src/lib/misc/pbes2/pbes2.cpp @@ -7,7 +7,6 @@ #include <botan/pbes2.h> #include <botan/cipher_mode.h> -#include <botan/lookup.h> #include <botan/pbkdf.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> diff --git a/src/lib/misc/rfc3394/rfc3394.cpp b/src/lib/misc/rfc3394/rfc3394.cpp index 3e1ed8b40..582e8c92d 100644 --- a/src/lib/misc/rfc3394/rfc3394.cpp +++ b/src/lib/misc/rfc3394/rfc3394.cpp @@ -6,7 +6,6 @@ */ #include <botan/rfc3394.h> -#include <botan/lookup.h> #include <botan/block_cipher.h> #include <botan/loadstor.h> #include <botan/exceptn.h> @@ -22,7 +21,10 @@ secure_vector<byte> rfc3394_keywrap(const secure_vector<byte>& key, if(kek.size() != 16 && kek.size() != 24 && kek.size() != 32) throw std::invalid_argument("Bad KEK length " + std::to_string(kek.size()) + " for NIST key wrap"); - std::unique_ptr<BlockCipher> aes(make_block_cipher("AES-" + std::to_string(8*kek.size()))); + const std::string cipher_name = "AES-" + std::to_string(8*kek.size()); + std::unique_ptr<BlockCipher> aes(BlockCipher::create(cipher_name)); + if(!aes) + throw Algorithm_Not_Found(cipher_name); aes->set_key(kek); const size_t n = key.size() / 8; @@ -66,7 +68,10 @@ secure_vector<byte> rfc3394_keyunwrap(const secure_vector<byte>& key, if(kek.size() != 16 && kek.size() != 24 && kek.size() != 32) throw std::invalid_argument("Bad KEK length " + std::to_string(kek.size()) + " for NIST key unwrap"); - std::unique_ptr<BlockCipher> aes(make_block_cipher("AES-" + std::to_string(8*kek.size()))); + const std::string cipher_name = "AES-" + std::to_string(8*kek.size()); + std::unique_ptr<BlockCipher> aes(BlockCipher::create(cipher_name)); + if(!aes) + throw Algorithm_Not_Found(cipher_name); aes->set_key(kek); const size_t n = (key.size() - 8) / 8; diff --git a/src/lib/misc/srp6/srp6.cpp b/src/lib/misc/srp6/srp6.cpp index d3f7338bd..f567db875 100644 --- a/src/lib/misc/srp6/srp6.cpp +++ b/src/lib/misc/srp6/srp6.cpp @@ -8,7 +8,6 @@ #include <botan/srp6.h> #include <botan/dl_group.h> #include <botan/numthry.h> -#include <botan/lookup.h> namespace Botan { @@ -19,7 +18,10 @@ BigInt hash_seq(const std::string& hash_id, const BigInt& in1, const BigInt& in2) { - std::unique_ptr<HashFunction> hash_fn(get_hash(hash_id)); + std::unique_ptr<HashFunction> hash_fn(HashFunction::create(hash_id)); + + if(!hash_fn) + throw Algorithm_Not_Found(hash_id); hash_fn->update(BigInt::encode_1363(in1, pad_to)); hash_fn->update(BigInt::encode_1363(in2, pad_to)); @@ -32,7 +34,10 @@ BigInt compute_x(const std::string& hash_id, const std::string& password, const std::vector<byte>& salt) { - std::unique_ptr<HashFunction> hash_fn(get_hash(hash_id)); + std::unique_ptr<HashFunction> hash_fn(HashFunction::create(hash_id)); + + if(!hash_fn) + throw Algorithm_Not_Found(hash_id); hash_fn->update(identifier); hash_fn->update(":"); diff --git a/src/lib/modes/aead/aead.cpp b/src/lib/modes/aead/aead.cpp index 1e66dbd43..61918c310 100644 --- a/src/lib/modes/aead/aead.cpp +++ b/src/lib/modes/aead/aead.cpp @@ -6,7 +6,6 @@ #include <botan/internal/mode_utils.h> #include <botan/aead.h> -#include <botan/lookup.h> #if defined(BOTAN_HAS_AEAD_CCM) #include <botan/ccm.h> diff --git a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp index 329e2e713..0aef6a747 100644 --- a/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp +++ b/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp @@ -11,9 +11,12 @@ namespace Botan { ChaCha20Poly1305_Mode::ChaCha20Poly1305_Mode() : - m_chacha(make_stream_cipher("ChaCha")), - m_poly1305(make_message_auth("Poly1305")) - {} + m_chacha(StreamCipher::create("ChaCha")), + m_poly1305(MessageAuthenticationCode::create("Poly1305")) + { + if(!m_chacha || !m_poly1305) + throw Algorithm_Not_Found("ChaCha20Poly1305"); + } bool ChaCha20Poly1305_Mode::valid_nonce_length(size_t n) const { diff --git a/src/lib/modes/cipher_mode.cpp b/src/lib/modes/cipher_mode.cpp index 262c41434..27ee26327 100644 --- a/src/lib/modes/cipher_mode.cpp +++ b/src/lib/modes/cipher_mode.cpp @@ -7,7 +7,6 @@ #include <botan/cipher_mode.h> #include <botan/stream_mode.h> -#include <botan/lookup.h> #include <botan/internal/mode_utils.h> #include <sstream> @@ -34,7 +33,7 @@ namespace Botan { template<typename T> Transform* make_ecb_mode(const Transform::Spec& spec) { - std::unique_ptr<BlockCipher> bc(get_block_cipher(spec.arg(0))); + std::unique_ptr<BlockCipher> bc(BlockCipher::create(spec.arg(0))); std::unique_ptr<BlockCipherModePaddingMethod> pad(get_bc_pad(spec.arg(1, "NoPadding"))); if(bc && pad) return new T(bc.release(), pad.release()); @@ -50,7 +49,7 @@ BOTAN_REGISTER_TRANSFORM(ECB_Decryption, make_ecb_mode<ECB_Decryption>); template<typename CBC_T, typename CTS_T> Transform* make_cbc_mode(const Transform::Spec& spec) { - std::unique_ptr<BlockCipher> bc(get_block_cipher(spec.arg(0))); + std::unique_ptr<BlockCipher> bc(BlockCipher::create(spec.arg(0))); if(bc) { @@ -131,8 +130,8 @@ Cipher_Mode* get_cipher_mode(const std::string& algo_spec, Cipher_Dir direction) return cipher; } - if(StreamCipher* stream_cipher = get_stream_cipher(mode_name, provider)) - return new Stream_Cipher_Mode(stream_cipher); + if(auto sc = StreamCipher::create(mode_name, provider)) + return new Stream_Cipher_Mode(sc.release()); return nullptr; } diff --git a/src/lib/modes/mode_utils.h b/src/lib/modes/mode_utils.h index b93884206..a61c22a4f 100644 --- a/src/lib/modes/mode_utils.h +++ b/src/lib/modes/mode_utils.h @@ -10,7 +10,6 @@ #include <botan/cipher_mode.h> #include <botan/internal/algo_registry.h> -#include <botan/lookup.h> #include <botan/block_cipher.h> #include <botan/loadstor.h> #include <botan/internal/rounding.h> @@ -22,18 +21,18 @@ namespace Botan { template<typename T> T* make_block_cipher_mode(const Transform::Spec& spec) { - if(BlockCipher* bc = get_block_cipher(spec.arg(0))) - return new T(bc); + if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(spec.arg(0))) + return new T(bc.release()); return nullptr; } template<typename T, size_t LEN1> T* make_block_cipher_mode_len(const Transform::Spec& spec) { - if(BlockCipher* bc = get_block_cipher(spec.arg(0))) + if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(spec.arg(0))) { const size_t len1 = spec.arg_as_integer(1, LEN1); - return new T(bc, len1); + return new T(bc.release(), len1); } return nullptr; @@ -42,11 +41,11 @@ T* make_block_cipher_mode_len(const Transform::Spec& spec) template<typename T, size_t LEN1, size_t LEN2> T* make_block_cipher_mode_len2(const Transform::Spec& spec) { - if(BlockCipher* bc = get_block_cipher(spec.arg(0))) + if(std::unique_ptr<BlockCipher> bc = BlockCipher::create(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, len1, len2); + return new T(bc.release(), len1, len2); } return nullptr; diff --git a/src/lib/passhash/passhash9/passhash9.cpp b/src/lib/passhash/passhash9/passhash9.cpp index f30684ec6..b457fc5c7 100644 --- a/src/lib/passhash/passhash9/passhash9.cpp +++ b/src/lib/passhash/passhash9/passhash9.cpp @@ -7,7 +7,6 @@ #include <botan/passhash9.h> #include <botan/loadstor.h> -#include <botan/lookup.h> #include <botan/pbkdf2.h> #include <botan/base64.h> @@ -24,18 +23,18 @@ const size_t PASSHASH9_PBKDF_OUTPUT_LEN = 24; // 192 bits output const size_t WORK_FACTOR_SCALE = 10000; -MessageAuthenticationCode* get_pbkdf_prf(byte alg_id) +std::unique_ptr<MessageAuthenticationCode> get_pbkdf_prf(byte alg_id) { if(alg_id == 0) - return get_mac("HMAC(SHA-1)"); + return MessageAuthenticationCode::create("HMAC(SHA-1)"); else if(alg_id == 1) - return get_mac("HMAC(SHA-256)"); + return MessageAuthenticationCode::create("HMAC(SHA-256)"); else if(alg_id == 2) - return get_mac("CMAC(Blowfish)"); + return MessageAuthenticationCode::create("CMAC(Blowfish)"); else if(alg_id == 3) - return get_mac("HMAC(SHA-384)"); + return MessageAuthenticationCode::create("HMAC(SHA-384)"); else if(alg_id == 4) - return get_mac("HMAC(SHA-512)"); + return MessageAuthenticationCode::create("HMAC(SHA-512)"); return nullptr; } @@ -46,14 +45,14 @@ std::string generate_passhash9(const std::string& pass, u16bit work_factor, byte alg_id) { - MessageAuthenticationCode* prf = get_pbkdf_prf(alg_id); + std::unique_ptr<MessageAuthenticationCode> prf = get_pbkdf_prf(alg_id); if(!prf) throw Invalid_Argument("Passhash9: Algorithm id " + std::to_string(alg_id) + " is not defined"); - PKCS5_PBKDF2 kdf(prf); // takes ownership of pointer + PKCS5_PBKDF2 kdf(prf.release()); // takes ownership of pointer secure_vector<byte> salt(SALT_BYTES); rng.randomize(salt.data(), salt.size()); @@ -110,12 +109,12 @@ bool check_passhash9(const std::string& pass, const std::string& hash) const size_t kdf_iterations = WORK_FACTOR_SCALE * work_factor; - MessageAuthenticationCode* pbkdf_prf = get_pbkdf_prf(alg_id); + std::unique_ptr<MessageAuthenticationCode> pbkdf_prf = get_pbkdf_prf(alg_id); if(!pbkdf_prf) return false; // unknown algorithm, reject - PKCS5_PBKDF2 kdf(pbkdf_prf); // takes ownership of pointer + PKCS5_PBKDF2 kdf(pbkdf_prf.release()); // takes ownership of pointer secure_vector<byte> cmp = kdf.derive_key( PASSHASH9_PBKDF_OUTPUT_LEN, diff --git a/src/lib/pbkdf/pbkdf2/pbkdf2.cpp b/src/lib/pbkdf/pbkdf2/pbkdf2.cpp index 480b43d4b..c1ac2c534 100644 --- a/src/lib/pbkdf/pbkdf2/pbkdf2.cpp +++ b/src/lib/pbkdf/pbkdf2/pbkdf2.cpp @@ -6,7 +6,6 @@ */ #include <botan/pbkdf2.h> -#include <botan/lookup.h> #include <botan/loadstor.h> #include <botan/internal/rounding.h> @@ -14,11 +13,11 @@ namespace Botan { PKCS5_PBKDF2* PKCS5_PBKDF2::make(const Spec& spec) { - if(auto mac = get_mac(spec.arg(0))) - return new PKCS5_PBKDF2(mac); + if(auto mac = MessageAuthenticationCode::create(spec.arg(0))) + return new PKCS5_PBKDF2(mac.release()); - if(auto mac = get_mac("HMAC(" + spec.arg(0) + ")")) - return new PKCS5_PBKDF2(mac); + if(auto mac = MessageAuthenticationCode::create("HMAC(" + spec.arg(0) + ")")) + return new PKCS5_PBKDF2(mac.release()); return nullptr; } diff --git a/src/lib/pk_pad/eme_oaep/oaep.cpp b/src/lib/pk_pad/eme_oaep/oaep.cpp index a484202da..f214c25d2 100644 --- a/src/lib/pk_pad/eme_oaep/oaep.cpp +++ b/src/lib/pk_pad/eme_oaep/oaep.cpp @@ -8,7 +8,6 @@ #include <botan/oaep.h> #include <botan/mgf1.h> #include <botan/mem_ops.h> -#include <botan/lookup.h> namespace Botan { @@ -19,8 +18,8 @@ OAEP* OAEP::make(const Spec& request) if(request.arg_count() == 1 || (request.arg_count() == 2 && request.arg(1) == "MGF1")) { - if(HashFunction* hash = get_hash_function(request.arg(0))) - return new OAEP(hash); + if(auto hash = HashFunction::create(request.arg(0))) + return new OAEP(hash.release()); } } diff --git a/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp b/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp index 5e8462d4f..940f91c9a 100644 --- a/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp +++ b/src/lib/pk_pad/emsa_pkcs1/emsa_pkcs1.cpp @@ -7,7 +7,6 @@ #include <botan/emsa_pkcs1.h> #include <botan/hash_id.h> -#include <botan/lookup.h> namespace Botan { @@ -17,8 +16,8 @@ EMSA* EMSA_PKCS1v15::make(const EMSA::Spec& spec) return new EMSA_PKCS1v15_Raw; else { - if(HashFunction* h = get_hash_function(spec.arg(0))) - return new EMSA_PKCS1v15(h); + if(auto h = HashFunction::create(spec.arg(0))) + return new EMSA_PKCS1v15(h.release()); } return nullptr; } diff --git a/src/lib/pk_pad/emsa_pssr/pssr.cpp b/src/lib/pk_pad/emsa_pssr/pssr.cpp index 06ca007c8..36b0ab64c 100644 --- a/src/lib/pk_pad/emsa_pssr/pssr.cpp +++ b/src/lib/pk_pad/emsa_pssr/pssr.cpp @@ -8,7 +8,6 @@ #include <botan/pssr.h> #include <botan/mgf1.h> #include <botan/internal/bit_ops.h> -#include <botan/lookup.h> namespace Botan { @@ -17,10 +16,10 @@ PSSR* PSSR::make(const Spec& request) if(request.arg(1, "MGF1") != "MGF1") return nullptr; - if(HashFunction* hash = get_hash_function(request.arg(0))) + if(auto h = HashFunction::create(request.arg(0))) { - const size_t salt_size = request.arg_as_integer(2, hash->output_length()); - return new PSSR(hash, salt_size); + const size_t salt_size = request.arg_as_integer(2, h->output_length()); + return new PSSR(h.release(), salt_size); } return nullptr; diff --git a/src/lib/pubkey/rfc6979/rfc6979.cpp b/src/lib/pubkey/rfc6979/rfc6979.cpp index 5f606891d..f749b039f 100644 --- a/src/lib/pubkey/rfc6979/rfc6979.cpp +++ b/src/lib/pubkey/rfc6979/rfc6979.cpp @@ -7,8 +7,8 @@ #include <botan/rfc6979.h> #include <botan/hmac_drbg.h> +#include <botan/mac.h> #include <botan/scan_name.h> -#include <botan/lookup.h> namespace Botan { @@ -31,7 +31,7 @@ RFC6979_Nonce_Generator::RFC6979_Nonce_Generator(const std::string& hash, m_order(order), m_qlen(m_order.bits()), m_rlen(m_qlen / 8 + (m_qlen % 8 ? 1 : 0)), - m_hmac_drbg(new HMAC_DRBG(make_message_auth("HMAC(" + hash + ")").release())), + m_hmac_drbg(new HMAC_DRBG(MessageAuthenticationCode::create("HMAC(" + hash + ")").release())), m_rng_in(m_rlen * 2), m_rng_out(m_rlen) { diff --git a/src/lib/rng/rng.cpp b/src/lib/rng/rng.cpp index 76e868b93..d4fd5fb10 100644 --- a/src/lib/rng/rng.cpp +++ b/src/lib/rng/rng.cpp @@ -7,14 +7,16 @@ #include <botan/rng.h> #include <botan/hmac_rng.h> -#include <botan/lookup.h> namespace Botan { RandomNumberGenerator* RandomNumberGenerator::make_rng() { - std::unique_ptr<MessageAuthenticationCode> h1(make_message_auth("HMAC(SHA-512)")); - std::unique_ptr<MessageAuthenticationCode> h2(h1->clone()); + std::unique_ptr<MessageAuthenticationCode> h1(MessageAuthenticationCode::create("HMAC(SHA-512)")); + std::unique_ptr<MessageAuthenticationCode> h2(MessageAuthenticationCode::create("HMAC(SHA-512)")); + + if(!h1 || !h2) + throw Algorithm_Not_Found("HMAC_RNG HMACs"); std::unique_ptr<RandomNumberGenerator> rng(new HMAC_RNG(h1.release(), h2.release())); rng->reseed(256); diff --git a/src/lib/stream/ctr/ctr.cpp b/src/lib/stream/ctr/ctr.cpp index 3b573448b..e90bb43a4 100644 --- a/src/lib/stream/ctr/ctr.cpp +++ b/src/lib/stream/ctr/ctr.cpp @@ -6,7 +6,6 @@ */ #include <botan/ctr.h> -#include <botan/lookup.h> namespace Botan { @@ -14,8 +13,8 @@ CTR_BE* CTR_BE::make(const Spec& spec) { if(spec.algo_name() == "CTR-BE" && spec.arg_count() == 1) { - if(BlockCipher* c = get_block_cipher(spec.arg(0))) - return new CTR_BE(c); + if(auto c = BlockCipher::create(spec.arg(0))) + return new CTR_BE(c.release()); } return nullptr; } diff --git a/src/lib/stream/ofb/ofb.cpp b/src/lib/stream/ofb/ofb.cpp index 1e6615fc0..e8cb463db 100644 --- a/src/lib/stream/ofb/ofb.cpp +++ b/src/lib/stream/ofb/ofb.cpp @@ -6,7 +6,6 @@ */ #include <botan/ofb.h> -#include <botan/lookup.h> namespace Botan { @@ -14,8 +13,8 @@ OFB* OFB::make(const Spec& spec) { if(spec.algo_name() == "OFB" && spec.arg_count() == 1) { - if(BlockCipher* c = get_block_cipher(spec.arg(0))) - return new OFB(c); + if(auto c = BlockCipher::create(spec.arg(0))) + return new OFB(c.release()); } return nullptr; } diff --git a/src/lib/tls/msg_hello_verify.cpp b/src/lib/tls/msg_hello_verify.cpp index a3c439750..c1dc574d4 100644 --- a/src/lib/tls/msg_hello_verify.cpp +++ b/src/lib/tls/msg_hello_verify.cpp @@ -7,7 +7,6 @@ #include <botan/internal/tls_messages.h> #include <botan/mac.h> -#include <botan/lookup.h> namespace Botan { @@ -36,7 +35,7 @@ Hello_Verify_Request::Hello_Verify_Request(const std::vector<byte>& client_hello const std::string& client_identity, const SymmetricKey& secret_key) { - std::unique_ptr<MessageAuthenticationCode> hmac(get_mac("HMAC(SHA-256)")); + std::unique_ptr<MessageAuthenticationCode> hmac(MessageAuthenticationCode::create("HMAC(SHA-256)")); hmac->set_key(secret_key); hmac->update_be(client_hello_bits.size()); diff --git a/src/lib/tls/sessions_sql/tls_session_manager_sql.cpp b/src/lib/tls/sessions_sql/tls_session_manager_sql.cpp index 508f8ff2f..ed207972e 100644 --- a/src/lib/tls/sessions_sql/tls_session_manager_sql.cpp +++ b/src/lib/tls/sessions_sql/tls_session_manager_sql.cpp @@ -8,7 +8,6 @@ #include <botan/tls_session_manager_sql.h> #include <botan/database.h> #include <botan/pbkdf.h> -#include <botan/lookup.h> #include <botan/hex.h> #include <botan/loadstor.h> #include <chrono> diff --git a/src/lib/tls/tls_ciphersuite.cpp b/src/lib/tls/tls_ciphersuite.cpp index c0f9dbf76..4fdf33811 100644 --- a/src/lib/tls/tls_ciphersuite.cpp +++ b/src/lib/tls/tls_ciphersuite.cpp @@ -7,7 +7,6 @@ #include <botan/tls_ciphersuite.h> #include <botan/parsing.h> -#include <botan/lookup.h> #include <botan/block_cipher.h> #include <botan/stream_cipher.h> #include <botan/hash.h> @@ -104,16 +103,13 @@ namespace { bool have_hash(const std::string& prf) { - return (!get_hash_function_providers(prf).empty()); + return (HashFunction::providers(prf).size() > 0); } bool have_cipher(const std::string& cipher) { - if(!get_block_cipher_providers(cipher).empty()) - return true; - if(!get_stream_cipher_providers(cipher).empty()) - return true; - return false; + return (BlockCipher::providers(cipher).size() > 0) || + (StreamCipher::providers(cipher).size() > 0); } } diff --git a/src/lib/tls/tls_handshake_hash.cpp b/src/lib/tls/tls_handshake_hash.cpp index 94c2774c5..615767cc2 100644 --- a/src/lib/tls/tls_handshake_hash.cpp +++ b/src/lib/tls/tls_handshake_hash.cpp @@ -7,7 +7,6 @@ #include <botan/internal/tls_handshake_hash.h> #include <botan/tls_exceptn.h> -#include <botan/lookup.h> #include <botan/hash.h> namespace Botan { @@ -29,7 +28,7 @@ secure_vector<byte> Handshake_Hash::final(Protocol_Version version, return mac_algo.c_str(); }; - std::unique_ptr<HashFunction> hash(make_hash_function(choose_hash())); + std::unique_ptr<HashFunction> hash(HashFunction::create(choose_hash())); hash->update(data); return hash->final(); } diff --git a/src/lib/tls/tls_record.cpp b/src/lib/tls/tls_record.cpp index 53521c5da..71542de16 100644 --- a/src/lib/tls/tls_record.cpp +++ b/src/lib/tls/tls_record.cpp @@ -12,7 +12,6 @@ #include <botan/internal/tls_seq_numbers.h> #include <botan/internal/tls_session_key.h> #include <botan/internal/rounding.h> -#include <botan/lookup.h> #include <botan/rng.h> namespace Botan { @@ -62,20 +61,17 @@ Connection_Cipher_State::Connection_Cipher_State(Protocol_Version version, return; } - if(BlockCipher* bc = get_block_cipher(cipher_algo)) - { - m_block_cipher.reset(bc); - m_block_cipher->set_key(cipher_key); - m_block_cipher_cbc_state = iv.bits_of(); - m_block_size = bc->block_size(); - - if(version.supports_explicit_cbc_ivs()) - m_iv_size = m_block_size; - } - else + m_block_cipher = BlockCipher::create(cipher_algo); + m_mac = MessageAuthenticationCode::create("HMAC(" + mac_algo + ")"); + if(!m_block_cipher) throw Invalid_Argument("Unknown TLS cipher " + cipher_algo); - m_mac.reset(get_mac("HMAC(" + mac_algo + ")")); + m_block_cipher->set_key(cipher_key); + m_block_cipher_cbc_state = iv.bits_of(); + m_block_size = m_block_cipher->block_size(); + + if(version.supports_explicit_cbc_ivs()) + m_iv_size = m_block_size; m_mac->set_key(mac_key); } |