-
Notifications
You must be signed in to change notification settings - Fork 25
/
crypto.cc
535 lines (439 loc) · 11.6 KB
/
crypto.cc
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
* bcm2-utils
* Copyright (C) 2016 Joseph C. Lehner <[email protected]>
*
* bcm2-utils is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bcm2-utils is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with bcm2-utils. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <stdexcept>
#include <cstring>
#include "crypto.h"
#include "util.h"
#if defined(__APPLE__)
#include <CommonCrypto/CommonCrypto.h>
#include <CommonCrypto/CommonDigest.h>
#ifndef MD5_CTX
#define MD5_CTX CC_MD5_CTX
#define MD5_Init(ctx) CC_MD5_Init(ctx)
#define MD5_Update(ctx, data, len) CC_MD5_Update(ctx, data, len)
#define MD5_Final(md, ctx) CC_MD5_Final(md, ctx)
#endif
#define BCM2UTILS_USE_COMMON_CRYPTO
#elif defined(_WIN32)
#include <wincrypt.h>
#include <system_error>
#define BCM2UTILS_USE_WINCRYPT
#else
#include <openssl/aes.h>
#include <openssl/md5.h>
#include <openssl/des.h>
#define BCM2UTILS_USE_OPENSSL
#endif
using namespace std;
using namespace bcm2dump;
namespace bcm2utils {
namespace {
inline const unsigned char* data(const string& buf)
{
return reinterpret_cast<const unsigned char*>(buf.data());
}
#ifndef BCM2UTILS_USE_COMMON_CRYPTO
inline unsigned char* data(string& buf)
{
return reinterpret_cast<unsigned char*>(&buf[0]);
}
#endif
void check_keysize(const string& key, size_t size, const string& name)
{
if (key.size() != size) {
throw invalid_argument("invalid key size for algorithm " + name);
}
}
#ifdef BCM2UTILS_USE_OPENSSL
inline const_DES_cblock* to_ccblock(const uint8_t* buf)
{
return (const_DES_cblock*)buf;
}
inline DES_cblock* to_cblock(uint8_t* buf)
{
return (DES_cblock*)buf;
}
inline const_DES_cblock* to_ccblock(const string& buf, size_t offset)
{
return (const_DES_cblock*)&buf[offset];
}
AES_KEY make_aes_key(const string& key, bool encrypt)
{
AES_KEY ret;
int err;
if (!encrypt) {
err = AES_set_decrypt_key(data(key), key.size() * 8, &ret);
} else {
err = AES_set_encrypt_key(data(key), key.size() * 8, &ret);
}
if (err) {
throw runtime_error("failed to set AES key: error " + to_string(err));
}
return ret;
}
#endif
#if defined(BCM2UTILS_USE_WINCRYPT) || defined(BCM2UTILS_USE_COMMON_CRYPTO)
#ifdef BCM2UTILS_USE_WINCRYPT
struct enctype
{
const ALG_ID algo;
const size_t keysize;
const size_t blocksize;
const bool ecb;
};
const enctype et_aes_256_ecb = { CALG_AES_256, 32, 16, true };
const enctype et_aes_128_cbc = { CALG_AES_128, 16, 16, false };
const enctype et_3des_ecb = { CALG_3DES, 24, 8, true };
const enctype et_des_ecb = { CALG_DES, 8, 8, true };
#else
struct enctype
{
const CCAlgorithm algo;
const size_t keysize;
const size_t blocksize;
const bool ecb;
};
const enctype et_aes_256_ecb = { kCCAlgorithmAES128, 32, 16, true };
const enctype et_aes_128_cbc = { kCCAlgorithmAES128, 16, 16, false };
const enctype et_3des_ecb = { kCCAlgorithm3DES, 24, 16, true };
const enctype et_des_ecb = { kCCAlgorithmDES, 8, 8, true };
#endif
void check_keysize(const string& key, const enctype& et)
{
if (key.size() != et.keysize + (et.ecb ? 0 : et.blocksize)) {
throw invalid_argument("invalid key size for algorithm");
}
}
#endif
#ifdef BCM2UTILS_USE_WINCRYPT
class wincrypt_context
{
public:
wincrypt_context()
: handle(0)
{
BOOL ok = CryptAcquireContext(
&handle,
nullptr,
#ifndef BCM2CFG_WINXP
MS_ENH_RSA_AES_PROV,
#else
MS_ENH_RSA_AES_PROV_XP,
#endif
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT);
if (!ok) {
throw winapi_error("CryptAcquireContext");
}
}
~wincrypt_context()
{
if (handle) {
CryptReleaseContext(handle, 0);
}
}
HCRYPTPROV handle;
};
class wincrypt_hash
{
public:
wincrypt_hash(ALG_ID algo)
: m_hash(0)
{
if (!CryptCreateHash(m_ctx.handle, algo, 0, 0, &m_hash)) {
throw winapi_error("CryptCreateHash");
}
}
~wincrypt_hash()
{
if (m_hash) {
CryptDestroyHash(m_hash);
}
}
HCRYPTHASH get() const
{ return m_hash; }
private:
wincrypt_context m_ctx;
HCRYPTHASH m_hash;
};
class wincrypt_key
{
public:
wincrypt_key(const enctype& et, const string& key)
: m_key(0)
{
// THIS API IS LUDICROUS!!!
struct {
BLOBHEADER hdr;
DWORD keysize;
BYTE key[32]; // max key size
} blob;
if (et.keysize > sizeof(blob.key)) {
throw runtime_error("key size out of range");
}
check_keysize(key, et);
blob.keysize = et.keysize;
blob.hdr = {
.bType = PLAINTEXTKEYBLOB,
.bVersion = CUR_BLOB_VERSION,
.reserved = 0,
.aiKeyAlg = et.algo
};
memcpy(blob.key, key.data(), et.keysize);
if (!CryptImportKey(m_ctx.handle, reinterpret_cast<BYTE*>(&blob), sizeof(blob), 0, 0, &m_key)) {
throw winapi_error("CryptImportKey");
}
DWORD mode = et.ecb ? CRYPT_MODE_ECB : CRYPT_MODE_CBC;
set_param(KP_MODE, &mode);
if (!et.ecb) {
set_param(KP_IV, key.data() + et.keysize);
}
}
~wincrypt_key()
{
if (m_key) {
CryptDestroyKey(m_key);
}
}
HCRYPTKEY get() const
{ return m_key; }
private:
template<class T> void set_param(DWORD param, const T* data)
{
if (!CryptSetKeyParam(m_key, param, reinterpret_cast<const BYTE*>(data), 0)) {
throw winapi_error("CryptSetKeyParam");
}
}
wincrypt_context m_ctx;
HCRYPTKEY m_key;
};
#endif
#if defined(BCM2UTILS_USE_COMMON_CRYPTO)
string crypt_generic(const enctype& et, const string& ibuf, const string& key, bool encrypt)
{
check_keysize(key, et);
size_t moved;
size_t len = align_left(ibuf.size(), et.blocksize);
string obuf(len, '\0');
CCCryptorStatus ret = CCCrypt(
encrypt ? kCCEncrypt : kCCDecrypt,
et.algo,
et.ecb ? kCCOptionECBMode : 0,
key.data(), et.keysize,
et.ecb ? nullptr : key.data() + et.keysize,
ibuf.data(), len,
&obuf[0], len,
&moved);
if (ret != kCCSuccess) {
throw runtime_error("CCCrypt: error " + to_string(ret));
} else if (moved != len) {
throw runtime_error("CCCrypt: expected length " + to_string(len) + ", got " + to_string(moved));
}
if (len < ibuf.size()) {
obuf += ibuf.substr(len);
}
return obuf;
}
#elif defined(BCM2UTILS_USE_WINCRYPT)
string crypt_generic(const enctype& et, const string& ibuf, const string& key, bool encrypt)
{
wincrypt_key ckey(et, key);
DWORD len = align_left(ibuf.size(), et.blocksize);
string obuf = ibuf;
// since we want to avoid wincrypt's padding, pass FALSE to both
// crypt functions, even though it's technically the final (and only) chunk
BOOL ok;
if (encrypt) {
ok = CryptEncrypt(ckey.get(), 0, FALSE, 0, reinterpret_cast<unsigned char*>(&obuf[0]), &len, len);
} else {
ok = CryptDecrypt(ckey.get(), 0, FALSE, 0, reinterpret_cast<unsigned char*>(&obuf[0]), &len);
}
if (!ok) {
throw winapi_error(encrypt ? "CryptEncrypt" : "CryptDecrypt");
}
// no need to deal with the remaining data, since we copied ibuf to obuf
return obuf;
}
#elif defined(BCM2UTILS_USE_OPENSSL)
template<size_t BlockSize, class F> string crypt_generic_ecb(const string& ibuf, const F& crypter)
{
string obuf = ibuf;
for (size_t i = 0; (i + BlockSize - 1) < ibuf.size(); i += BlockSize) {
crypter(data(ibuf) + i, reinterpret_cast<uint8_t*>(&obuf[i]));
}
return obuf;
}
#endif
}
string hash_md5(const string& buf)
{
string md5(16, '\0');
#ifndef BCM2UTILS_USE_WINCRYPT
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data(buf), buf.size());
MD5_Final(reinterpret_cast<unsigned char*>(&md5[0]), &ctx);
return md5;
#else
wincrypt_hash hash(CALG_MD5);
if (!CryptHashData(hash.get(), data(buf), buf.size(), 0)) {
throw winapi_error("CryptHashData");
}
DWORD size = md5.size();
if (!CryptGetHashParam(hash.get(), HP_HASHVAL, reinterpret_cast<unsigned char*>(&md5[0]), &size, 0)) {
throw winapi_error("CryptGetHashParam");
}
return md5;
#endif
}
string crypt_3des_ecb(const string& ibuf, const string& key, bool encrypt)
{
#if defined(BCM2UTILS_USE_OPENSSL)
check_keysize(key, 24, "3des-ecb");
DES_key_schedule ks[3];
for (int i = 0; i < 3; ++i) {
DES_set_key_unchecked(to_ccblock(key, i * 8), &ks[i]);
}
return crypt_generic_ecb<8>(ibuf, [&ks, &encrypt](const uint8_t *iblock, uint8_t *oblock) {
DES_ecb3_encrypt(to_ccblock(iblock), to_cblock(oblock), &ks[0], &ks[1], &ks[2],
encrypt ? DES_ENCRYPT : DES_DECRYPT);
});
#else
return crypt_generic(et_3des_ecb, ibuf, key, encrypt);
#endif
}
string crypt_des_ecb(const string& ibuf, const string& key, bool encrypt)
{
#if defined(BCM2UTILS_USE_OPENSSL)
check_keysize(key, 8, "des-ecb");
DES_key_schedule ks;
DES_set_key_unchecked(to_ccblock(key, 0), &ks);
return crypt_generic_ecb<8>(ibuf, [&ks, &encrypt](const uint8_t *iblock, uint8_t *oblock) {
DES_ecb_encrypt(to_ccblock(iblock), to_cblock(oblock), &ks,
encrypt ? DES_ENCRYPT : DES_DECRYPT);
});
#else
return crypt_generic(et_des_ecb, ibuf, key, encrypt);
#endif
}
string crypt_aes_256_ecb(const string& ibuf, const string& key, bool encrypt)
{
#if defined(BCM2UTILS_USE_OPENSSL)
check_keysize(key, 32, "aes-256-ecb");
AES_KEY aes = make_aes_key(key, encrypt);
return crypt_generic_ecb<16>(ibuf, [&aes, &encrypt](const uint8_t *iblock, uint8_t *oblock) {
if (encrypt) {
AES_encrypt(iblock, oblock, &aes);
} else {
AES_decrypt(iblock, oblock, &aes);
}
});
#else
return crypt_generic(et_aes_256_ecb, ibuf, key, encrypt);
#endif
}
#ifdef BCM2UTILS_USE_OPENSSL
namespace {
template<size_t BITS> string crypt_aes_cbc(const string& ibuf, const string& key_and_iv, bool encrypt)
{
// first the key, then the iv
check_keysize(key_and_iv, (BITS / 8) + 16, "aes-" + to_string(BITS) + "-cbc");
string key = key_and_iv.substr(0, BITS / 8);
string iv = key_and_iv.substr(BITS / 8);
AES_KEY aes = make_aes_key(key, encrypt);
string obuf = ibuf;
AES_cbc_encrypt(data(ibuf), data(obuf), ibuf.size(), &aes, data(iv), encrypt);
return obuf;
}
}
string crypt_aes_128_cbc(const string& ibuf, const string& key_and_iv, bool encrypt)
{
return crypt_aes_cbc<128>(ibuf, key_and_iv, encrypt);
}
#else
string crypt_aes_128_cbc(const string& ibuf, const string& key_and_iv, bool encrypt)
{
return crypt_generic(et_aes_128_cbc, ibuf, key_and_iv, encrypt);
}
#endif
namespace {
uint32_t srand_motorola;
int32_t rand_motorola()
{
uint32_t result, next = srand_motorola;
next *= 0x41c64e6d;
next += 0x3039;
result = next & 0xffe00000;
next *= 0x41c64e6d;
next += 0x3039;
result += (next & 0xfffc0000) >> 11;
next *= 0x41c64e6d;
next += 0x3039;
result = (result + (next >> 25)) & 0x7fffffff;
srand_motorola = next;
return result;
}
}
// this is some snakeoily shit right here!
string crypt_motorola(string buf, const string& key)
{
check_keysize(key, 1, "motorola");
srand_motorola = key[0] & 0xff;
for (size_t i = 0; i < buf.size(); ++i) {
double r = rand_motorola();
int x = ((r / 0x7fffffff) * 255) + 1;
buf[i] ^= x;
}
return buf;
}
void swap16(string& buf)
{
for (size_t i = 0; (i + 1) < buf.size(); i += 2) {
swap(buf[i], buf[i + 1]);
}
}
// ditto!
string crypt_sub_16x16(string buf, bool encrypt)
{
if (encrypt) {
swap16(buf);
}
for (size_t i = 0; i < (buf.size() / 16) * 16; i += 2) {
unsigned k = i & 0xff;
if (encrypt) {
buf[i] = (buf[i] + k) & 0xff;
} else {
buf[i] = (buf[i] - k) & 0xff;
}
}
if (!encrypt) {
swap16(buf);
}
return buf;
}
string crypt_xor_char(string buf, const string& key)
{
check_keysize(key, 1, "xor");
for (size_t i = 0; i < buf.size(); ++i) {
buf[i] ^= (key[0] & 0xff);
}
return buf;
}
}