diff --git a/crypto-openssl.cpp b/crypto-openssl.cpp index 6ae8293..48d7af5 100644 --- a/crypto-openssl.cpp +++ b/crypto-openssl.cpp @@ -48,8 +48,8 @@ struct Aes_impl { }; Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key) +: impl(new Aes_impl) { - impl = new Aes_impl; if (AES_set_encrypt_key(raw_key, KEY_LEN * 8, &(impl->key)) != 0) { throw Crypto_error("Aes_ctr_encryptor::Aes_ctr_encryptor", "AES_set_encrypt_key failed"); } @@ -57,7 +57,8 @@ Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key) Aes_ecb_encryptor::~Aes_ecb_encryptor () { - delete impl; + // Note: Explicit destructor necessary because class contains an auto_ptr + // which contains an incomplete type when the auto_ptr is declared. } void Aes_ecb_encryptor::encrypt(const unsigned char* plain, unsigned char* cipher) @@ -70,15 +71,17 @@ struct Hmac_impl { }; Hmac_sha1_state::Hmac_sha1_state (const unsigned char* key, size_t key_len) +: impl(new Hmac_impl) { - impl = new Hmac_impl; HMAC_Init(&(impl->ctx), key, key_len, EVP_sha1()); } Hmac_sha1_state::~Hmac_sha1_state () { + // Note: Explicit destructor necessary because class contains an auto_ptr + // which contains an incomplete type when the auto_ptr is declared. + HMAC_cleanup(&(impl->ctx)); - delete impl; } void Hmac_sha1_state::add (const unsigned char* buffer, size_t buffer_len) diff --git a/crypto.hpp b/crypto.hpp index ae6a14c..4eedc07 100644 --- a/crypto.hpp +++ b/crypto.hpp @@ -36,6 +36,7 @@ #include #include #include +#include void init_crypto (); @@ -56,11 +57,7 @@ public: }; private: - Aes_impl* impl; - - // disallow copy/assignment: - Aes_ecb_encryptor (const Aes_ecb_encryptor&); - Aes_ecb_encryptor& operator= (const Aes_ecb_encryptor&); + std::auto_ptr impl; public: Aes_ecb_encryptor (const unsigned char* key); @@ -104,11 +101,7 @@ public: }; private: - Hmac_impl* impl; - - // disallow copy/assignment: - Hmac_sha1_state (const Hmac_sha1_state&) { } - Hmac_sha1_state& operator= (const Hmac_sha1_state&) { return *this; } + std::auto_ptr impl; public: Hmac_sha1_state (const unsigned char* key, size_t key_len);