diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-10-26 10:21:06 +0100 |
---|---|---|
committer | Jonathan Roelofs <jonathan@codesourcery.com> | 2017-12-16 15:40:33 -0700 |
commit | 7193b89fe567327bb78f4c61c887b2e2fad2ed51 (patch) | |
tree | c3064389fdc8d4a07b78882bc77cf475cb4e8d9d /src/crypto/chacha8.h | |
parent | Merge pull request #2881 (diff) | |
download | monero-7193b89fe567327bb78f4c61c887b2e2fad2ed51.tar.xz |
Scrub keys from memory just before scope end.
Partially implements #74.
Securely erases keys from memory after they are no longer needed. Might have a
performance impact, which I haven't measured (perf measurements aren't
generally reliable on laptops).
Thanks to @stoffu for the suggestion to specialize the pod_to_hex/hex_to_pod
functions. Using overloads + SFINAE instead generalizes it so other types can
be marked as scrubbed without adding more boilerplate.
Diffstat (limited to '')
-rw-r--r-- | src/crypto/chacha8.h | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/src/crypto/chacha8.h b/src/crypto/chacha8.h index 1bf695731..dcbe6a933 100644 --- a/src/crypto/chacha8.h +++ b/src/crypto/chacha8.h @@ -49,16 +49,9 @@ namespace crypto { #if defined(__cplusplus) } -#pragma pack(push, 1) - struct chacha8_key { - uint8_t data[CHACHA8_KEY_SIZE]; - - ~chacha8_key() - { - memwipe(data, sizeof(data)); - } - }; + using chacha8_key = tools::scrubbed_arr<uint8_t, CHACHA8_KEY_SIZE>; +#pragma pack(push, 1) // MS VC 2012 doesn't interpret `class chacha8_iv` as POD in spite of [9.0.10], so it is a struct struct chacha8_iv { uint8_t data[CHACHA8_IV_SIZE]; @@ -68,15 +61,14 @@ namespace crypto { static_assert(sizeof(chacha8_key) == CHACHA8_KEY_SIZE && sizeof(chacha8_iv) == CHACHA8_IV_SIZE, "Invalid structure size"); inline void chacha8(const void* data, std::size_t length, const chacha8_key& key, const chacha8_iv& iv, char* cipher) { - chacha8(data, length, reinterpret_cast<const uint8_t*>(&key), reinterpret_cast<const uint8_t*>(&iv), cipher); + chacha8(data, length, key.data(), reinterpret_cast<const uint8_t*>(&iv), cipher); } inline void generate_chacha8_key(const void *data, size_t size, chacha8_key& key) { static_assert(sizeof(chacha8_key) <= sizeof(hash), "Size of hash must be at least that of chacha8_key"); - char pwd_hash[HASH_SIZE]; - crypto::cn_slow_hash(data, size, pwd_hash); - memcpy(&key, pwd_hash, sizeof(key)); - memwipe(pwd_hash, sizeof(pwd_hash)); + tools::scrubbed_arr<char, HASH_SIZE> pwd_hash; + crypto::cn_slow_hash(data, size, pwd_hash.data()); + memcpy(&key, pwd_hash.data(), sizeof(key)); } inline void generate_chacha8_key(std::string password, chacha8_key& key) { |