forked from etotheipi/BitcoinArmory
/
SecureBinaryData.h
166 lines (141 loc) · 5.46 KB
/
SecureBinaryData.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE-ATI or http://www.gnu.org/licenses/agpl.html //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef SECUREBINARYDATA_H_
#define SECUREBINARYDATA_H_
#include "BinaryData.h"
// This is used to attempt to keep keying material out of swap
// I am stealing this from bitcoin 0.4.0 src, serialize.h
#if defined(_MSC_VER) || defined(__MINGW32__)
// Note that VirtualLock does not provide this as a guarantee on Windows,
// but, in practice, memory that has been VirtualLock'd almost never gets written to
// the pagefile except in rare circumstances where memory is extremely low.
#include <windows.h>
#include "leveldb_windows_port\win32_posix\mman.h"
//#define mlock(p, n) VirtualLock((p), (n));
//#define munlock(p, n) VirtualUnlock((p), (n));
#else
#include <sys/mman.h>
#include <limits.h>
/* This comes from limits.h if it's not defined there set a sane default */
#ifndef PAGESIZE
#include <unistd.h>
#define PAGESIZE sysconf(_SC_PAGESIZE)
#endif
#define mlock(a,b) \
mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
(((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
#define munlock(a,b) \
munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\
(((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1))))
#endif
////////////////////////////////////////////////////////////////////////////////
// Make sure that all crypto information is handled with page-locked data,
// and overwritten when it's destructor is called. For simplicity, we will
// use this data type for all crypto data, even for data values that aren't
// really sensitive. We can use the SecureBinaryData(bdObj) to convert our
// regular strings/BinaryData objects to secure objects
//
class SecureBinaryData : public BinaryData
{
public:
// We want regular BinaryData, but page-locked and secure destruction
SecureBinaryData(void) : BinaryData()
{
lockData();
}
SecureBinaryData(size_t sz) : BinaryData(sz)
{
lockData();
}
SecureBinaryData(BinaryData const & data) : BinaryData(data)
{
lockData();
}
SecureBinaryData(uint8_t const * inData, size_t sz) : BinaryData(inData, sz)
{
lockData();
}
SecureBinaryData(uint8_t const * d0, uint8_t const * d1) : BinaryData(d0, d1)
{
lockData();
}
SecureBinaryData(BinaryDataRef const & bdRef) : BinaryData(bdRef)
{
lockData();
}
~SecureBinaryData(void) { destroy(); }
SecureBinaryData(SecureBinaryData&& mv) : BinaryData()
{
data_ = move(mv.data_);
}
// These methods are definitely inherited, but SWIG needs them here if they
// are to be used from python
uint8_t const * getPtr(void) const { return BinaryData::getPtr(); }
uint8_t * getPtr(void) { return BinaryData::getPtr(); }
size_t getSize(void) const { return BinaryData::getSize(); }
SecureBinaryData copy(void) const { return SecureBinaryData(getPtr(), getSize()); }
std::string toHexStr(bool BE = false) const { return BinaryData::toHexStr(BE); }
std::string toBinStr(void) const { return BinaryData::toBinStr(); }
SecureBinaryData(SecureBinaryData const & sbd2) :
BinaryData(sbd2.getPtr(), sbd2.getSize()) {
lockData();
}
void resize(size_t sz) { BinaryData::resize(sz); lockData(); }
void reserve(size_t sz) { BinaryData::reserve(sz); lockData(); }
BinaryData getRawCopy(void) const { return BinaryData(getPtr(), getSize()); }
BinaryDataRef getRawRef(void) const { return BinaryDataRef(getPtr(), getSize()); }
SecureBinaryData copySwapEndian(size_t pos1 = 0, size_t pos2 = 0) const;
SecureBinaryData & append(const SecureBinaryData & sbd2);
SecureBinaryData & operator=(SecureBinaryData const & sbd2);
SecureBinaryData operator+(const SecureBinaryData & sbd2) const;
//uint8_t const & operator[](size_t i) const {return BinaryData::operator[](i);}
bool operator==(SecureBinaryData const & sbd2) const;
SecureBinaryData getHash256(void) const;
SecureBinaryData getHash160(void) const;
void lockData(void)
{
if (getSize() > 0)
mlock(getPtr(), getSize());
}
void destroy(void)
{
if (getSize() > 0)
{
fill(0x00);
munlock(getPtr(), getSize());
}
resize(0);
}
void XOR(const BinaryDataRef& rhs)
{
if (getSize() > rhs.getSize())
throw std::runtime_error("invalid rhs length");
for (unsigned i = 0; i < getSize(); i++)
{
auto val = getPtr() + i;
*val ^= *(rhs.getPtr() + i);
}
}
static SecureBinaryData fromString(const std::string& str)
{
if (str.empty())
return {};
SecureBinaryData sbd(str.size());
memcpy(sbd.getPtr(), str.data(), str.size());
return sbd;
}
static SecureBinaryData fromStringView(const std::string_view& strv)
{
if (strv.empty())
return {};
SecureBinaryData sbd(strv.size());
memcpy(sbd.getPtr(), strv.data(), strv.size());
return sbd;
}
};
#endif