-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecure.cpp
More file actions
431 lines (346 loc) · 10.6 KB
/
Copy pathSecure.cpp
File metadata and controls
431 lines (346 loc) · 10.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <random>
#include <fstream>
#include <iostream>
#ifdef SIMPLECRYPT
#include <QDataStream>
#include <QCryptographicHash>
#endif
#ifdef OBFUSCATION
#include <QDate>
#include <QTime>
#endif
#include <QSharedPointer>
#include "Secure.h"
#ifdef CRYPTOPP
static const std::vector<uint8_t> salt{'d', '5', 'y', 'N', '+', '/', '?', '/', ')', 'e', 'j', 'z', 'O', '9', 'Q'};
#endif
#ifdef OBFUSCATION
static const std::vector<uint8_t> salt{
0x2e, 0x24, 0x2c, 0x21, 0x2e, 0x78, 0x59, 0x31,
0x4a, 0x58, 0x24, 0x66, 0x78, 0x42, 0x7e, 0x4c,
0x77, 0x79, 0x2e, 0x53, 0x30, 0x27, 0x7b, 0x78,
0x57, 0x75, 0x44, 0x24, 0x7d, 0x43, 0x3f, 0x79
};
#endif
#if 0
static void print_hex(const uint8_t* data, size_t len);
#endif
//------------------------------------------------
// Factory methods
secure_ptr_t Secure::create(const QString& passphrase, const QString& cipher, const QString& hash)
{
#ifdef CRYPTOPP
QByteArray key_buffer;
if (!passphrase.isEmpty())
{
std::ifstream ifs(passphrase.toLatin1(), std::ios::in | std::ios::binary | std::ios::ate);
if (ifs.good())
{
assert(ifs.is_open());
ifs.seekg(0, std::ios::end);
std::ifstream::pos_type file_size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
key_buffer.resize(int(salt.size()) + int(file_size));
ifs.read(reinterpret_cast<char*>(key_buffer.data()) + salt.size(), file_size);
ifs.close();
::memcpy(key_buffer.data(), salt.data(), salt.size());
auto secure_ptr{secure_ptr_t(new Secure(cipher))};
secure_ptr->set_key(key_buffer, hash);
return secure_ptr;
}
else
{
// treat it as a passphrase
auto buffer{passphrase.toUtf8()};
key_buffer.resize(int(salt.size()) + int(buffer.size()));
::memcpy(key_buffer.data(), salt.data(), salt.size());
::memcpy(key_buffer.data() + salt.size(), buffer.constData(), size_t(buffer.size()));
auto secure_ptr{secure_ptr_t(new Secure(cipher))};
secure_ptr->set_key(key_buffer, hash);
return secure_ptr;
}
}
// if they are creating a Secure instance, it stands to reason the want
// SOME kind of security. therefore, if the instance does not have a
// passphrase, then at least the the salt value will serve to provide
// SOME protection.
key_buffer.resize(int(salt.size()));
::memcpy(key_buffer.data(), salt.data(), salt.size());
auto secure_ptr{secure_ptr_t(new Secure(cipher))};
secure_ptr->set_key(key_buffer, hash);
#endif
#if defined (SIMPLECRYPT) || defined (OBFUSCATION)
Q_UNUSED(passphrase)
Q_UNUSED(cipher)
Q_UNUSED(hash)
auto secure_ptr{secure_ptr_t(new Secure())};
// create the internal SimpleCrypt instance with the passphrase
secure_ptr->set_key(passphrase.toUtf8());
#endif
return secure_ptr;
}
//------------------------------------------------
// Instance methods
#ifdef CRYPTOPP
Secure::Secure(Cipher cipher, QObject* parent) : QObject(parent), m_cipher(cipher)
{
init();
}
#endif
Secure::Secure(const QString& cipher, QObject* parent) : QObject(parent)
{
#ifdef CRYPTOPP
auto lc{cipher.toLower()};
if (lc == "aes")
m_cipher = Cipher::aes;
#endif
#if defined (SIMPLECRYPT) || defined (OBFUSCATION)
Q_UNUSED(cipher)
#endif
init();
}
Secure::~Secure()
{
close();
}
void Secure::init()
{
#ifdef CRYPTOPP
::memset(m_key, 0x00, MaxKeySize);
::memset(m_iv, 0x00, CryptoPP::AES::BLOCKSIZE);
#endif
}
void Secure::close()
{
#ifdef CRYPTOPP
if (m_cfbEncryption.get())
m_cfbEncryption.reset();
if (m_cfbDecryption.get())
m_cfbDecryption.reset();
#endif
#ifdef SIMPLECRYPT
if(m_simplecrypt.get())
m_simplecrypt.reset();
#endif
}
#ifdef CRYPTOPP
bool Secure::set_key(const QByteArray& passphrase, Hash hash)
{
close();
// the initialization vector (IV) must be as random as tolerable,
// but also--since we are using different cryptographic providers
// on different platforms--deterministically generated so it starts
// the same on all platforms, otherwise, decryption across
// cryptographic providers WILL NOT WORK!
std::minstd_rand rand_generator(0xC0FFEE);
for (uint8_t& crypto_val : m_iv)
crypto_val = uint8_t(rand_generator() >> 16);
// hash the passphrase into the key
if (hash == Hash::sha256)
{
CryptoPP::SHA256 cpp_hash;
CryptoPP::ArraySource as(
reinterpret_cast<const CryptoPP::byte*>(passphrase.constData()),
size_t(passphrase.size()),
true,
new CryptoPP::HashFilter(cpp_hash, new CryptoPP::ArraySink(&m_key[0], MaxKeySize)));
}
else
assert(false && "Only SHA256 is currently supported!");
return true;
}
#endif
#ifdef SIMPLECRYPT
quint64 Secure::sixty_four_hash(const QString& str)
{
QByteArray hash =
QCryptographicHash::hash(QByteArray::fromRawData(reinterpret_cast<const char*>(str.utf16()), str.length() * 2), QCryptographicHash::Md5);
Q_ASSERT(hash.size() == 16);
QDataStream stream(hash);
quint64 a, b;
stream >> a >> b;
return a ^ b;
};
#endif
#ifdef OBFUSCATION
QByteArray Secure::newKey(int pepper, int start, int end)
{
Q_UNUSED(start)
Q_UNUSED(end)
std::vector<uint8_t> local_salt;
if((pepper & 0x1) == 1) // odd?
{
// walk-swap the bytes
for(size_t i = 1;i < salt.size(); i += 2)
{
local_salt.push_back(salt[i]);
local_salt.push_back(salt[i-1]);
}
}
else // rotate
{
// auto total_rotations = julian / salt.size();
auto offset = pepper % salt.size();
for(size_t count = 0; count < salt.size(); ++count)
{
local_salt.push_back(salt[offset]);
if(++offset == salt.size())
offset = 0;
}
}
assert(local_salt.size() == salt.size());
QByteArray key;
size_t offset = 0;
for(int i = 0;i < passphrase.length(); ++i)
{
key.push_back(passphrase[i] ^ local_salt[offset]);
if(++offset == local_salt.size())
offset = 0;
}
return key;
}
QByteArray Secure::newDoyKey()
{
QDate now = QDate::currentDate();
return newKey(now.dayOfYear(), 1, 366);
}
QByteArray Secure::newHourKey()
{
QTime now = QTime::currentTime();
return newKey(now.hour() + 1, 1, 24);
}
QByteArray Secure::newMonthKey()
{
QDate now = QDate::currentDate();
return newKey(now.month(), 1, 12);
}
#endif
bool Secure::set_key(const QByteArray& passphrase, const QString& hash_str)
{
#ifdef CRYPTOPP
auto local_hash{Hash::sha256};
auto hs{hash_str.toLower()};
if (hs == "sha256")
local_hash = Hash::sha256;
return set_key(passphrase, local_hash);
#endif
#ifdef SIMPLECRYPT
Q_UNUSED(hash_str)
auto init{sixty_four_hash(passphrase)};
m_simplecrypt = simplecrypt_ptr_t(new SimpleCrypt(init));
return true;
#endif
#ifdef OBFUSCATION
Q_UNUSED(hash_str)
this->passphrase = passphrase;
return true;
#endif
}
QByteArray Secure::encrypt(const QByteArray& in_buffer, bool& success)
{
#ifdef CRYPTOPP
return encrypt(reinterpret_cast<const uint8_t*>(in_buffer.constData()), uint32_t(in_buffer.size()), success);
#endif
#ifdef SIMPLECRYPT
auto encrypted{m_simplecrypt->encryptToString(QString(in_buffer))};
success = true;
return encrypted.toUtf8();
#endif
#ifdef OBFUSCATION
QByteArray buffer(in_buffer.length(), 0);
auto key = newDoyKey();
int offset = 0;
for(int i = 0;i < in_buffer.length(); ++i)
{
buffer[i] = in_buffer[i] ^ key[offset];
if(++offset == key.size())
offset = 0;
}
success = true;
return buffer;
#endif
}
#ifdef CRYPTOPP
QByteArray Secure::encrypt(const uint8_t* p_data, uint32_t in_size, bool& success)
{
success = true;
QByteArray out_buffer;
out_buffer.resize(static_cast<int>(in_size));
try
{
if (!m_cfbEncryption)
m_cfbEncryption.reset(new CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption);
m_cfbEncryption->SetKeyWithIV(&m_key[0], sizeof(m_key), &m_iv[0]);
m_cfbEncryption->ProcessData(reinterpret_cast<CryptoPP::byte*>(out_buffer.data()), p_data, in_size);
}
catch (const CryptoPP::Exception& e)
{
std::cerr << e.what() << std::endl;
success = false;
}
assert(success);
return out_buffer;
}
#endif
QByteArray Secure::decrypt(const QByteArray& in_buffer, bool& success)
{
success = true;
#ifdef CRYPTOPP
QByteArray out_buffer;
out_buffer.resize(in_buffer.size());
try
{
if (!m_cfbDecryption)
m_cfbDecryption.reset(new CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption);
m_cfbDecryption->SetKeyWithIV(&m_key[0], sizeof(m_key), &m_iv[0]);
m_cfbDecryption->ProcessData(
reinterpret_cast<CryptoPP::byte*>(out_buffer.data()), reinterpret_cast<const CryptoPP::byte*>(in_buffer.constData()), static_cast<size_t>(in_buffer.size()));
}
catch (const CryptoPP::Exception& e)
{
std::cerr << e.what() << std::endl;
success = false;
}
assert(success);
return out_buffer;
#endif
#ifdef SIMPLECRYPT
auto decrypted{m_simplecrypt->decryptToString(QString(in_buffer))};
success = true;
return decrypted.toUtf8();
#endif
#ifdef OBFUSCATION
QByteArray buffer(in_buffer.length(), 0);
auto key = newDoyKey();
int offset = 0;
for(int i = 0;i < in_buffer.length(); ++i)
{
buffer[i] = in_buffer[i] ^ key[offset];
if(++offset == key.size())
offset = 0;
}
success = true;
return buffer;
#endif
}
#if 0
#ifdef CRYPTOPP
// function to help with debugging encryption/decryption data
void print_hex(const uint8_t* data, size_t len)
{
// display the hash for comparison (debugging)
std::string encoded;
CryptoPP::HexEncoder encoder;
encoder.Put(reinterpret_cast<const CryptoPP::byte*>(data), len);
encoder.MessageEnd();
auto size{encoder.MaxRetrievable()};
if (size)
{
encoded.resize(size);
encoder.Get((CryptoPP::byte*)&encoded[0], encoded.size());
std::cout << encoded << std::endl;
std::cout.flush();
}
}
#endif
#endif