mirror of
https://github.com/AGWA/git-crypt.git
synced 2025-12-05 20:40:05 -08:00
Ensure memsets of sensitive memory aren't optimized away
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
4
key.cpp
4
key.cpp
@@ -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)
|
||||
|
||||
11
util.cpp
11
util.cpp
@@ -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 ()
|
||||
|
||||
1
util.hpp
1
util.hpp
@@ -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*);
|
||||
|
||||
Reference in New Issue
Block a user