forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetEncryption.cpp
More file actions
244 lines (198 loc) · 7.23 KB
/
AssetEncryption.cpp
File metadata and controls
244 lines (198 loc) · 7.23 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016, goatpig //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "AssetEncryption.h"
#include "Assets.h"
#include "make_unique.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//// KeyDerivationFunction
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
KeyDerivationFunction::~KeyDerivationFunction()
{}
////////////////////////////////////////////////////////////////////////////////
BinaryData KeyDerivationFunction_Romix::computeID() const
{
BinaryWriter bw;
bw.put_BinaryData(salt_);
bw.put_uint32_t(iterations_);
bw.put_uint32_t(memTarget_);
return BtcUtils::getHash256(bw.getData());
}
////////////////////////////////////////////////////////////////////////////////
BinaryData KeyDerivationFunction_Romix::initialize()
{
KdfRomix kdf;
kdf.computeKdfParams(0);
iterations_ = kdf.getNumIterations();
memTarget_ = kdf.getMemoryReqtBytes();
return kdf.getSalt();
}
////////////////////////////////////////////////////////////////////////////////
SecureBinaryData KeyDerivationFunction_Romix::deriveKey(
const SecureBinaryData& rawKey) const
{
KdfRomix kdfObj(memTarget_, iterations_, salt_);
return move(kdfObj.DeriveKey(rawKey));
}
////////////////////////////////////////////////////////////////////////////////
shared_ptr<KeyDerivationFunction> KeyDerivationFunction::deserialize(
const BinaryDataRef& data)
{
BinaryRefReader brr(data);
//check size
auto totalLen = brr.get_var_int();
if (totalLen != brr.getSizeRemaining())
throw runtime_error("invalid serialized kdf size");
//return ptr
shared_ptr<KeyDerivationFunction> kdfPtr = nullptr;
//check prefix
auto prefix = brr.get_uint16_t();
switch (prefix)
{
case KDF_ROMIX_PREFIX:
{
//iterations
auto iterations = brr.get_uint32_t();
//memTarget
auto memTarget = brr.get_uint32_t();
//salt
auto len = brr.get_var_int();
SecureBinaryData salt(move(brr.get_BinaryData(len)));
kdfPtr = make_shared<KeyDerivationFunction_Romix>(
iterations, memTarget, salt);
break;
}
default:
throw runtime_error("unexpected kdf prefix");
}
return kdfPtr;
}
////////////////////////////////////////////////////////////////////////////////
BinaryData KeyDerivationFunction_Romix::serialize() const
{
BinaryWriter bw;
bw.put_uint16_t(KDF_ROMIX_PREFIX);
bw.put_uint32_t(iterations_);
bw.put_uint32_t(memTarget_);
bw.put_var_int(salt_.getSize());
bw.put_BinaryData(salt_);
BinaryWriter finalBw;
finalBw.put_var_int(bw.getSize());
finalBw.put_BinaryDataRef(bw.getDataRef());
return finalBw.getData();
}
////////////////////////////////////////////////////////////////////////////////
const BinaryData& KeyDerivationFunction_Romix::getId(void) const
{
if (id_.getSize() == 0)
id_ = move(computeID());
return id_;
}
////////////////////////////////////////////////////////////////////////////////
bool KeyDerivationFunction_Romix::isSame(KeyDerivationFunction* const kdf) const
{
auto kdfromix = dynamic_cast<KeyDerivationFunction_Romix*>(kdf);
if (kdfromix == nullptr)
return false;
return iterations_ == kdfromix->iterations_ &&
memTarget_ == kdfromix->memTarget_ &&
salt_ == kdfromix->salt_;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//// Cypher
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Cypher::~Cypher()
{}
////////////////////////////////////////////////////////////////////////////////
unique_ptr<Cypher> Cypher::deserialize(BinaryRefReader& brr)
{
unique_ptr<Cypher> cypher;
auto prefix = brr.get_uint8_t();
if (prefix != CYPHER_BYTE)
throw runtime_error("invalid serialized cypher prefix");
auto type = brr.get_uint8_t();
auto len = brr.get_var_int();
auto&& kdfId = brr.get_BinaryData(len);
len = brr.get_var_int();
auto&& encryptionKeyId = brr.get_BinaryData(len);
len = brr.get_var_int();
auto&& iv = SecureBinaryData(brr.get_BinaryDataRef(len));
switch (type)
{
case CypherType_AES:
{
cypher = move(make_unique<Cypher_AES>(
kdfId, encryptionKeyId, iv));
break;
}
default:
throw CypherException("unexpected cypher type");
}
return move(cypher);
}
////////////////////////////////////////////////////////////////////////////////
BinaryData Cypher_AES::serialize() const
{
BinaryWriter bw;
bw.put_uint8_t(CYPHER_BYTE);
bw.put_uint8_t(getType());
bw.put_var_int(kdfId_.getSize());
bw.put_BinaryData(kdfId_);
bw.put_var_int(encryptionKeyId_.getSize());
bw.put_BinaryData(encryptionKeyId_);
bw.put_var_int(iv_.getSize());
bw.put_BinaryData(iv_);
return bw.getData();
}
////////////////////////////////////////////////////////////////////////////////
unique_ptr<Cypher> Cypher_AES::getCopy() const
{
return make_unique<Cypher_AES>(kdfId_, encryptionKeyId_);
}
////////////////////////////////////////////////////////////////////////////////
unique_ptr<Cypher> Cypher_AES::getCopy(const BinaryData& keyId) const
{
return make_unique<Cypher_AES>(kdfId_, keyId);
}
////////////////////////////////////////////////////////////////////////////////
SecureBinaryData Cypher_AES::encrypt(const SecureBinaryData& key,
const SecureBinaryData& data) const
{
CryptoAES aes_cypher;
return aes_cypher.EncryptCBC(data, key, iv_);
}
////////////////////////////////////////////////////////////////////////////////
SecureBinaryData Cypher_AES::encrypt(DecryptedEncryptionKey* const key,
const BinaryData& kdfId, const SecureBinaryData& data) const
{
if (key == nullptr)
throw runtime_error("empty ptr");
auto& encryptionKey = key->getDerivedKey(kdfId);
CryptoAES aes_cypher;
return aes_cypher.EncryptCBC(data, encryptionKey, iv_);
}
////////////////////////////////////////////////////////////////////////////////
SecureBinaryData Cypher_AES::decrypt(const SecureBinaryData& key,
const SecureBinaryData& data) const
{
CryptoAES aes_cypher;
return aes_cypher.DecryptCBC(data, key, iv_);
}
////////////////////////////////////////////////////////////////////////////////
bool Cypher_AES::isSame(Cypher* const cypher) const
{
auto cypher_aes = dynamic_cast<Cypher_AES*>(cypher);
if (cypher_aes == nullptr)
return false;
return kdfId_ == cypher_aes->kdfId_ &&
encryptionKeyId_ == cypher_aes->encryptionKeyId_ &&
iv_ == cypher_aes->iv_;
}