diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/filters/algo_filt.cpp | 35 | ||||
-rw-r--r-- | src/lib/filters/filters.h | 63 | ||||
-rw-r--r-- | src/lib/selftest/selftest.cpp | 24 |
3 files changed, 57 insertions, 65 deletions
diff --git a/src/lib/filters/algo_filt.cpp b/src/lib/filters/algo_filt.cpp index dbc46c7e6..2bc14dc16 100644 --- a/src/lib/filters/algo_filt.cpp +++ b/src/lib/filters/algo_filt.cpp @@ -14,21 +14,20 @@ namespace Botan { /* * StreamCipher_Filter Constructor */ -StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher) : - buffer(DEFAULT_BUFFERSIZE) +StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher) : + buffer(DEFAULT_BUFFERSIZE), m_cipher(cipher) { - cipher = stream_cipher; } /* * StreamCipher_Filter Constructor */ -StreamCipher_Filter::StreamCipher_Filter(StreamCipher* stream_cipher, +StreamCipher_Filter::StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key) : - buffer(DEFAULT_BUFFERSIZE) + buffer(DEFAULT_BUFFERSIZE), + m_cipher(cipher) { - cipher = stream_cipher; - cipher->set_key(key); + m_cipher->set_key(key); } /* @@ -38,7 +37,7 @@ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name) : buffer(DEFAULT_BUFFERSIZE) { Algorithm_Factory& af = global_state().algorithm_factory(); - cipher = af.make_stream_cipher(sc_name); + m_cipher.reset(af.make_stream_cipher(sc_name)); } /* @@ -49,8 +48,8 @@ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, buffer(DEFAULT_BUFFERSIZE) { Algorithm_Factory& af = global_state().algorithm_factory(); - cipher = af.make_stream_cipher(sc_name); - cipher->set_key(key); + m_cipher.reset(af.make_stream_cipher(sc_name)); + m_cipher->set_key(key); } /* @@ -58,7 +57,7 @@ StreamCipher_Filter::StreamCipher_Filter(const std::string& sc_name, */ void StreamCipher_Filter::set_iv(const InitializationVector& iv) { - cipher->set_iv(iv.begin(), iv.length()); + m_cipher->set_iv(iv.begin(), iv.length()); } /* @@ -69,7 +68,7 @@ void StreamCipher_Filter::write(const byte input[], size_t length) while(length) { size_t copied = std::min<size_t>(length, buffer.size()); - cipher->cipher(input, &buffer[0], copied); + m_cipher->cipher(input, &buffer[0], copied); send(buffer, copied); input += copied; length -= copied; @@ -84,7 +83,7 @@ Hash_Filter::Hash_Filter(const std::string& algo_spec, OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); - hash = af.make_hash_function(algo_spec); + m_hash.reset(af.make_hash_function(algo_spec)); } /* @@ -92,7 +91,7 @@ Hash_Filter::Hash_Filter(const std::string& algo_spec, */ void Hash_Filter::end_msg() { - secure_vector<byte> output = hash->final(); + secure_vector<byte> output = m_hash->final(); if(OUTPUT_LENGTH) send(output, std::min<size_t>(OUTPUT_LENGTH, output.size())); else @@ -106,7 +105,7 @@ MAC_Filter::MAC_Filter(const std::string& mac_name, size_t len) : OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); - mac = af.make_mac(mac_name); + m_mac.reset(af.make_mac(mac_name)); } /* @@ -116,8 +115,8 @@ MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, size_t len) : OUTPUT_LENGTH(len) { Algorithm_Factory& af = global_state().algorithm_factory(); - mac = af.make_mac(mac_name); - mac->set_key(key); + m_mac.reset(af.make_mac(mac_name)); + m_mac->set_key(key); } /* @@ -125,7 +124,7 @@ MAC_Filter::MAC_Filter(const std::string& mac_name, const SymmetricKey& key, */ void MAC_Filter::end_msg() { - secure_vector<byte> output = mac->final(); + secure_vector<byte> output = m_mac->final(); if(OUTPUT_LENGTH) send(output, std::min<size_t>(OUTPUT_LENGTH, output.size())); else diff --git a/src/lib/filters/filters.h b/src/lib/filters/filters.h index 5973cbf50..9c7d6b858 100644 --- a/src/lib/filters/filters.h +++ b/src/lib/filters/filters.h @@ -34,7 +34,7 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter { public: - std::string name() const { return cipher->name(); } + std::string name() const { return m_cipher->name(); } /** * Write input data @@ -44,7 +44,7 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter void write(const byte input[], size_t input_len); bool valid_iv_length(size_t iv_len) const - { return cipher->valid_iv_length(iv_len); } + { return m_cipher->valid_iv_length(iv_len); } /** * Set the initialization vector for this filter. @@ -56,22 +56,22 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter * Set the key of this filter. * @param key the key to set */ - void set_key(const SymmetricKey& key) { cipher->set_key(key); } + void set_key(const SymmetricKey& key) { m_cipher->set_key(key); } - Key_Length_Specification key_spec() const override { return cipher->key_spec(); } + Key_Length_Specification key_spec() const override { return m_cipher->key_spec(); } /** * Construct a stream cipher filter. - * @param cipher_obj a cipher object to use + * @param cipher a cipher object to use */ - StreamCipher_Filter(StreamCipher* cipher_obj); + StreamCipher_Filter(StreamCipher* cipher); /** * Construct a stream cipher filter. - * @param cipher_obj a cipher object to use + * @param cipher a cipher object to use * @param key the key to use inside this filter */ - StreamCipher_Filter(StreamCipher* cipher_obj, const SymmetricKey& key); + StreamCipher_Filter(StreamCipher* cipher, const SymmetricKey& key); /** * Construct a stream cipher filter. @@ -85,11 +85,9 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter * @param key the key to use inside this filter */ StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key); - - ~StreamCipher_Filter() { delete cipher; } private: secure_vector<byte> buffer; - StreamCipher* cipher; + std::unique_ptr<StreamCipher> m_cipher; }; /** @@ -98,10 +96,10 @@ class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter class BOTAN_DLL Hash_Filter : public Filter { public: - void write(const byte input[], size_t len) { hash->update(input, len); } + void write(const byte input[], size_t len) { m_hash->update(input, len); } void end_msg(); - std::string name() const { return hash->name(); } + std::string name() const { return m_hash->name(); } /** * Construct a hash filter. @@ -111,8 +109,8 @@ class BOTAN_DLL Hash_Filter : public Filter * hash. Otherwise, specify a smaller value here so that the * output of the hash algorithm will be cut off. */ - Hash_Filter(HashFunction* hash_fun, size_t len = 0) : - OUTPUT_LENGTH(len), hash(hash_fun) {} + Hash_Filter(HashFunction* hash, size_t len = 0) : + OUTPUT_LENGTH(len), m_hash(hash) {} /** * Construct a hash filter. @@ -124,10 +122,9 @@ class BOTAN_DLL Hash_Filter : public Filter */ Hash_Filter(const std::string& request, size_t len = 0); - ~Hash_Filter() { delete hash; } private: const size_t OUTPUT_LENGTH; - HashFunction* hash; + std::unique_ptr<HashFunction> m_hash; }; /** @@ -136,48 +133,50 @@ class BOTAN_DLL Hash_Filter : public Filter class BOTAN_DLL MAC_Filter : public Keyed_Filter { public: - void write(const byte input[], size_t len) { mac->update(input, len); } + void write(const byte input[], size_t len) { m_mac->update(input, len); } void end_msg(); - std::string name() const { return mac->name(); } + std::string name() const { return m_mac->name(); } /** * Set the key of this filter. * @param key the key to set */ - void set_key(const SymmetricKey& key) { mac->set_key(key); } + void set_key(const SymmetricKey& key) { m_mac->set_key(key); } - Key_Length_Specification key_spec() const override { return mac->key_spec(); } + Key_Length_Specification key_spec() const override { return m_mac->key_spec(); } /** * Construct a MAC filter. The MAC key will be left empty. - * @param mac_obj the MAC to use + * @param mac the MAC to use * @param out_len the output length of this filter. Leave the default * value 0 if you want to use the full output of the * MAC. Otherwise, specify a smaller value here so that the * output of the MAC will be cut off. */ - MAC_Filter(MessageAuthenticationCode* mac_obj, - size_t out_len = 0) : OUTPUT_LENGTH(out_len) + MAC_Filter(MessageAuthenticationCode* mac, + size_t out_len = 0) : + OUTPUT_LENGTH(out_len), + m_mac(mac) { - mac = mac_obj; } /** * Construct a MAC filter. - * @param mac_obj the MAC to use + * @param mac the MAC to use * @param key the MAC key to use * @param out_len the output length of this filter. Leave the default * value 0 if you want to use the full output of the * MAC. Otherwise, specify a smaller value here so that the * output of the MAC will be cut off. */ - MAC_Filter(MessageAuthenticationCode* mac_obj, + MAC_Filter(MessageAuthenticationCode* mac, const SymmetricKey& key, - size_t out_len = 0) : OUTPUT_LENGTH(out_len) + size_t out_len = 0) : + OUTPUT_LENGTH(out_len), + m_mac(mac) { - mac = mac_obj; - mac->set_key(key); + m_mac->set_key(key); } /** @@ -201,11 +200,9 @@ class BOTAN_DLL MAC_Filter : public Keyed_Filter */ MAC_Filter(const std::string& mac, const SymmetricKey& key, size_t len = 0); - - ~MAC_Filter() { delete mac; } private: const size_t OUTPUT_LENGTH; - MessageAuthenticationCode* mac; + std::unique_ptr<MessageAuthenticationCode> m_mac; }; } diff --git a/src/lib/selftest/selftest.cpp b/src/lib/selftest/selftest.cpp index 87111b292..1a84677c7 100644 --- a/src/lib/selftest/selftest.cpp +++ b/src/lib/selftest/selftest.cpp @@ -95,20 +95,16 @@ algorithm_kat_detailed(const SCAN_Name& algo_name, else if(const BlockCipher* proto = af.prototype_block_cipher(algo, provider)) { - Keyed_Filter* enc = get_cipher_mode(proto, ENCRYPTION, - algo_name.cipher_mode(), - algo_name.cipher_mode_pad()); + std::unique_ptr<Keyed_Filter> enc(get_cipher_mode(proto, ENCRYPTION, + algo_name.cipher_mode(), + algo_name.cipher_mode_pad())); - Keyed_Filter* dec = get_cipher_mode(proto, DECRYPTION, - algo_name.cipher_mode(), - algo_name.cipher_mode_pad()); + std::unique_ptr<Keyed_Filter> dec(get_cipher_mode(proto, DECRYPTION, + algo_name.cipher_mode(), + algo_name.cipher_mode_pad())); if(!enc || !dec) - { - delete enc; - delete dec; continue; - } enc->set_key(key); @@ -128,17 +124,17 @@ algorithm_kat_detailed(const SCAN_Name& algo_name, if(!ad.empty()) { - if(AEAD_Filter* enc_aead = dynamic_cast<AEAD_Filter*>(enc)) + if(AEAD_Filter* enc_aead = dynamic_cast<AEAD_Filter*>(enc.get())) { enc_aead->set_associated_data(&ad[0], ad.size()); - if(AEAD_Filter* dec_aead = dynamic_cast<AEAD_Filter*>(dec)) + if(AEAD_Filter* dec_aead = dynamic_cast<AEAD_Filter*>(dec.get())) dec_aead->set_associated_data(&ad[0], ad.size()); } } - all_results[provider + " (encrypt)"] = test_filter_kat(enc, input, output); - all_results[provider + " (decrypt)"] = test_filter_kat(dec, output, input); + all_results[provider + " (encrypt)"] = test_filter_kat(enc.release(), input, output); + all_results[provider + " (decrypt)"] = test_filter_kat(dec.release(), output, input); } } |