Ensure memsets of sensitive memory aren't optimized away

This commit is contained in:
Andrew Ayer
2014-07-23 19:32:30 -07:00
parent 23ff272f7d
commit 477983f4bc
5 changed files with 17 additions and 4 deletions

View File

@@ -30,6 +30,7 @@
#include "crypto.hpp"
#include "key.hpp"
#include "util.hpp"
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
@@ -61,7 +62,7 @@ Aes_ecb_encryptor::~Aes_ecb_encryptor ()
// Note: Explicit destructor necessary because class contains an auto_ptr
// which contains an incomplete type when the auto_ptr is declared.
std::memset(&impl->key, '\0', sizeof(impl->key));
explicit_memset(&impl->key, '\0', sizeof(impl->key));
}
void Aes_ecb_encryptor::encrypt(const unsigned char* plain, unsigned char* cipher)

View File

@@ -43,7 +43,7 @@ Aes_ctr_encryptor::Aes_ctr_encryptor (const unsigned char* raw_key, const unsign
Aes_ctr_encryptor::~Aes_ctr_encryptor ()
{
std::memset(pad, '\0', BLOCK_LEN);
explicit_memset(pad, '\0', BLOCK_LEN);
}
void Aes_ctr_encryptor::process (const unsigned char* in, unsigned char* out, size_t len)

View File

@@ -45,8 +45,8 @@
Key_file::Entry::Entry ()
{
version = 0;
std::memset(aes_key, 0, AES_KEY_LEN);
std::memset(hmac_key, 0, HMAC_KEY_LEN);
explicit_memset(aes_key, 0, AES_KEY_LEN);
explicit_memset(hmac_key, 0, HMAC_KEY_LEN);
}
void Key_file::Entry::load (std::istream& in)

View File

@@ -81,6 +81,17 @@ void write_be32 (std::ostream& out, uint32_t i)
out.write(reinterpret_cast<const char*>(buffer), 4);
}
void* explicit_memset (void* s, int c, std::size_t n)
{
volatile unsigned char* p = reinterpret_cast<unsigned char*>(s);
while (n--) {
*p++ = c;
}
return s;
}
static void init_std_streams_platform (); // platform-specific initialization
void init_std_streams ()

View File

@@ -70,6 +70,7 @@ uint32_t load_be32 (const unsigned char*);
void store_be32 (unsigned char*, uint32_t);
bool read_be32 (std::istream& in, uint32_t&);
void write_be32 (std::ostream& out, uint32_t);
void* explicit_memset (void* s, int c, size_t n); // memset that won't be optimized away
void init_std_streams ();
mode_t util_umask (mode_t);
int util_rename (const char*, const char*);