aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2017-10-26 10:21:06 +0100
committerJonathan Roelofs <jonathan@codesourcery.com>2017-12-16 15:40:33 -0700
commit7193b89fe567327bb78f4c61c887b2e2fad2ed51 (patch)
treec3064389fdc8d4a07b78882bc77cf475cb4e8d9d /src/crypto
parentMerge pull request #2881 (diff)
downloadmonero-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.h20
-rw-r--r--src/crypto/crypto.h7
2 files changed, 10 insertions, 17 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) {
diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h
index abdea0165..0ce5e6d7a 100644
--- a/src/crypto/crypto.h
+++ b/src/crypto/crypto.h
@@ -36,9 +36,12 @@
#include <boost/thread/lock_guard.hpp>
#include <boost/utility/value_init.hpp>
#include <boost/optional.hpp>
+#include <type_traits>
#include <vector>
#include "common/pod-class.h"
+#include "common/util.h"
+#include "common/memwipe.h"
#include "generic-ops.h"
#include "hex.h"
#include "span.h"
@@ -65,9 +68,7 @@ namespace crypto {
friend class crypto_ops;
};
- POD_CLASS secret_key: ec_scalar {
- friend class crypto_ops;
- };
+ using secret_key = tools::scrubbed<ec_scalar>;
POD_CLASS public_keyV {
std::vector<public_key> keys;