Use auto_ptr instead of explicit memory management

This commit is contained in:
Andrew Ayer
2014-07-07 22:28:07 -07:00
parent 66a2266968
commit 0210fd7541
2 changed files with 10 additions and 14 deletions

View File

@@ -48,8 +48,8 @@ struct Aes_impl {
}; };
Aes_ecb_encryptor::Aes_ecb_encryptor (const unsigned char* raw_key) 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) { 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"); 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 () 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) 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) 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_Init(&(impl->ctx), key, key_len, EVP_sha1());
} }
Hmac_sha1_state::~Hmac_sha1_state () 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)); HMAC_cleanup(&(impl->ctx));
delete impl;
} }
void Hmac_sha1_state::add (const unsigned char* buffer, size_t buffer_len) void Hmac_sha1_state::add (const unsigned char* buffer, size_t buffer_len)

View File

@@ -36,6 +36,7 @@
#include <stddef.h> #include <stddef.h>
#include <iosfwd> #include <iosfwd>
#include <string> #include <string>
#include <memory>
void init_crypto (); void init_crypto ();
@@ -56,11 +57,7 @@ public:
}; };
private: private:
Aes_impl* impl; std::auto_ptr<Aes_impl> impl;
// disallow copy/assignment:
Aes_ecb_encryptor (const Aes_ecb_encryptor&);
Aes_ecb_encryptor& operator= (const Aes_ecb_encryptor&);
public: public:
Aes_ecb_encryptor (const unsigned char* key); Aes_ecb_encryptor (const unsigned char* key);
@@ -104,11 +101,7 @@ public:
}; };
private: private:
Hmac_impl* impl; std::auto_ptr<Hmac_impl> impl;
// disallow copy/assignment:
Hmac_sha1_state (const Hmac_sha1_state&) { }
Hmac_sha1_state& operator= (const Hmac_sha1_state&) { return *this; }
public: public:
Hmac_sha1_state (const unsigned char* key, size_t key_len); Hmac_sha1_state (const unsigned char* key, size_t key_len);