aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2018-09-18 17:32:11 +0200
committerRiccardo Spagni <ric@spagni.net>2018-09-18 17:32:11 +0200
commit580497d5f93df0c6f1358e087e8c97079fd7dfa0 (patch)
tree911c5ad29c5a8dce201a74f774f8956fff176cc6 /contrib/epee
parentMerge pull request #4302 (diff)
parentwallet: use wipeable_string in more places where a secret is used (diff)
downloadmonero-580497d5f93df0c6f1358e087e8c97079fd7dfa0.tar.xz
Merge pull request #4306
56b50faa wallet: use wipeable_string in more places where a secret is used (moneromooo-monero) 07ec748c wipeable_string: add hex_to_pod function (moneromooo-monero)
Diffstat (limited to 'contrib/epee')
-rw-r--r--contrib/epee/include/hex.h1
-rw-r--r--contrib/epee/include/wipeable_string.h19
-rw-r--r--contrib/epee/src/wipeable_string.cpp3
3 files changed, 21 insertions, 2 deletions
diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h
index 02600c320..901c666a9 100644
--- a/contrib/epee/include/hex.h
+++ b/contrib/epee/include/hex.h
@@ -44,6 +44,7 @@ namespace epee
static std::string string(const span<const std::uint8_t> src);
//! \return A epee::wipeable_string containing hex of `src`.
static epee::wipeable_string wipeable_string(const span<const std::uint8_t> src);
+ template<typename T> static epee::wipeable_string wipeable_string(const T &pod) { return wipeable_string(span<const uint8_t>((const uint8_t*)&pod, sizeof(pod))); }
//! \return An array containing hex of `src`.
template<std::size_t N>
diff --git a/contrib/epee/include/wipeable_string.h b/contrib/epee/include/wipeable_string.h
index 4cebe5fdf..31854fe2e 100644
--- a/contrib/epee/include/wipeable_string.h
+++ b/contrib/epee/include/wipeable_string.h
@@ -28,10 +28,11 @@
#pragma once
-#include <boost/optional/optional_fwd.hpp>
+#include <boost/optional/optional.hpp>
#include <stddef.h>
#include <vector>
#include <string>
+#include "memwipe.h"
#include "fnv1.h"
namespace epee
@@ -65,6 +66,8 @@ namespace epee
void trim();
void split(std::vector<wipeable_string> &fields) const;
boost::optional<wipeable_string> parse_hexstr() const;
+ template<typename T> inline bool hex_to_pod(T &pod) const;
+ template<typename T> inline bool hex_to_pod(tools::scrubbed<T> &pod) const { return hex_to_pod(unwrap(pod)); }
void resize(size_t sz);
void reserve(size_t sz);
void clear();
@@ -79,6 +82,20 @@ namespace epee
private:
std::vector<char> buffer;
};
+
+ template<typename T> inline bool wipeable_string::hex_to_pod(T &pod) const
+ {
+ static_assert(std::is_pod<T>::value, "expected pod type");
+ if (size() != sizeof(T) * 2)
+ return false;
+ boost::optional<epee::wipeable_string> blob = parse_hexstr();
+ if (!blob)
+ return false;
+ if (blob->size() != sizeof(T))
+ return false;
+ pod = *(const T*)blob->data();
+ return true;
+ }
}
namespace std
diff --git a/contrib/epee/src/wipeable_string.cpp b/contrib/epee/src/wipeable_string.cpp
index 7c9722765..69f92e106 100644
--- a/contrib/epee/src/wipeable_string.cpp
+++ b/contrib/epee/src/wipeable_string.cpp
@@ -32,6 +32,8 @@
#include "misc_log_ex.h"
#include "wipeable_string.h"
+static constexpr const char hex[] = u8"0123456789abcdef";
+
namespace
{
int atolower(int c)
@@ -197,7 +199,6 @@ boost::optional<epee::wipeable_string> wipeable_string::parse_hexstr() const
const size_t len = size();
const char *d = data();
res->grow(0, len / 2);
- static constexpr const char hex[] = u8"0123456789abcdef";
for (size_t i = 0; i < len; i += 2)
{
char c = atolower(d[i]);