-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.cpp
507 lines (429 loc) · 12.2 KB
/
Encryption.cpp
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
#include "Encryption.hpp"
uint8_t Encryption::random()
{
static std::uniform_int_distribution<uint16_t> distribution(0, 0xFFFF);
static std::mt19937 rng;
static bool seeded = false;
if (!seeded)
{
auto now = std::chrono::system_clock::now().time_since_epoch();
auto t = std::chrono::duration_cast<std::chrono::milliseconds>(now).count();
t = t % 0xffffffff;
rng.seed((unsigned int)t);
seeded = true;
}
return (uint8_t)(distribution(rng) % 0xFF);
}
uint8_t* Encryption::Header::generateBytes()
{
if (this->size < 30 || this->size > 285)
return nullptr;
uint8_t* bytes = (uint8_t*)malloc(this->size);
if (!bytes)
return nullptr;
for (size_t i = 0; i < this->size; i++)
bytes[i] = random();
bytes[2] = this->endPadding;
bytes[12] = NULL;
bytes[20] = this->byteOffset;
bytes[23] = NULL;
bytes[24] = (uint8_t)(this->size - (size_t)30); // will not overflow, see check above
bytes[26 + bytes[24]] = this->byteModulo;
bytes[28 + bytes[24]] = NULL;
bytes[4] = 'P';
bytes[5] = 'A';
bytes[6] = 'R';
bytes[7] = 'T';
bytes[8] = 'I';
bytes[9] = 'C';
bytes[10] = 'L';
bytes[11] = 'E';
bytes[14] = 'C';
bytes[15] = 'H';
bytes[16] = 'U';
bytes[17] = 'R';
bytes[18] = 'C';
bytes[19] = 'H';
return bytes;
};
Encryption::Header Encryption::generateHeader()
{
Header head{};
head.byteModulo = random();
head.endPadding = random();
head.byteOffset = random();
head.nRandomBytes = random();
head.size = 30 + head.nRandomBytes;
head.isValid = true;
return head;
}
Encryption::Header Encryption::parseHeader(uint8_t* bytes, size_t nBytes)
{
Header head{};
if (nBytes < minHeaderSize)
{
head.isValid = false;
head.parseError = "too small";
return head;
}
bool PARTICLECHURCH =
bytes[4] == 'P' &&
bytes[5] == 'A' &&
bytes[6] == 'R' &&
bytes[7] == 'T' &&
bytes[8] == 'I' &&
bytes[9] == 'C' &&
bytes[10] == 'L' &&
bytes[11] == 'E' &&
bytes[14] == 'C' &&
bytes[15] == 'H' &&
bytes[16] == 'U' &&
bytes[17] == 'R' &&
bytes[18] == 'C' &&
bytes[19] == 'H';
if (!PARTICLECHURCH)
{
head.isValid = false;
head.parseError = "signature not found";
return head;
}
if (bytes[12] || bytes[23])
{
head.isValid = false;
head.parseError = "nonzero NULLS";
return head;
}
head.nRandomBytes = bytes[24];
head.size = 25 + head.nRandomBytes + 5;
if (nBytes < head.size)
{
head.isValid = false;
head.parseError = "too small";
return head;
}
head.endPadding = bytes[2];
head.byteOffset = bytes[20];
head.byteModulo = bytes[26 + head.nRandomBytes];
head.isValid = true;
head.parseError = "none";
return head;
}
void Encryption::encryptChunk(Header& header, size_t chunkIndex, uint8_t* decrypted/* size = 252 */, uint8_t* encrypted/* size = 265 */)
{
uint8_t byteRotation = 1 + (encrypted[0] = random()) % 251;
uint8_t bitRotation = (encrypted[1] = random());
uint8_t cutLocation = 10 + (encrypted[2] = random()) % 200;
uint8_t inversion = 10 + (encrypted[3] = random()) % 200;
for (uint8_t i = 0; i < 252; i++)
{
uint8_t r = ((uint16_t)byteRotation + (uint16_t)i) % 252;
int16_t p = r - cutLocation;
while (p < 0) p += 252;
uint8_t br = 1 + ((size_t)i + (size_t)bitRotation + chunkIndex) % 7; // [1, 7]
uint8_t v = (decrypted[i] >> br) | (decrypted[i] << (8 - br));
if (!(inversion % 2) || !(inversion % 3) || !(inversion % 5))
{
uint8_t add = ((uint16_t)header.byteOffset + (uint16_t)chunkIndex) % std::max(header.byteModulo, inversion);
v = ((uint16_t)v + (uint16_t)add) % 256;
}
encrypted[4 + p] = v;
}
}
void Encryption::decryptChunk(Header& header, size_t chunkIndex, uint8_t* encrypted/* size = 256 */, uint8_t* decrypted/* size = 252 */)
{
uint8_t byteRotation = 1 + encrypted[0] % 251;
uint8_t bitRotation = encrypted[1];
uint8_t cutLocation = 10 + encrypted[2] % 200;
uint8_t inversion = 10 + encrypted[3] % 200;
for (uint8_t i = 0; i < 252; i++)
{
uint8_t r = ((uint16_t)byteRotation + (uint16_t)i) % 252;
int16_t p = r - cutLocation;
while (p < 0) p += 252;
uint8_t br = 1 + ((size_t)i + (size_t)bitRotation + chunkIndex) % 7; // [1, 7]
uint8_t v = encrypted[4 + p];
if (!(inversion % 2) || !(inversion % 3) || !(inversion % 5))
{
uint8_t add = ((uint16_t)header.byteOffset + (uint16_t)chunkIndex) % std::max(header.byteModulo, inversion);
add = (256 - add);
v = ((uint16_t)v + (uint16_t)add) % 256;
}
decrypted[i] = (v << br) | (v >> (8 - br));
}
}
bool Encryption::encryptFile(const char* fileInput, const char* fileOutput)
{
if (debug)
{
std::cout << "========== Encrypting ==========" << std::endl;
std::cout << "Input: (decrypted) " << fileInput << std::endl;
std::cout << "Output: (encrypted) " << fileOutput << std::endl;
}
std::ifstream inFile;
inFile.open(fileInput, std::ios::in | std::ios::binary);
if (!inFile.is_open())
{
if (debug)
std::cout << "Failed to open input file" << std::endl;
return false;
}
inFile.seekg(0, std::ios::end);
uint64_t inFileSize = inFile.tellg(); // overflows files with size > 16,777,216 terabytes, i think we're good
inFile.clear();
inFile.seekg(0);
if (debug)
std::cout << "Input file size: " << inFileSize << " bytes" << std::endl;
std::ofstream outFile;
outFile.open(fileOutput, std::ios::out | std::ios::binary);
if (!outFile.is_open())
{
if (debug)
std::cout << "Failed to open output file" << std::endl;
inFile.close();
return false;
}
if (debug)
std::cout << "Successfully opened files, now encrypting..." << std::endl;
Header head = generateHeader();
head.endPadding = 252 - (inFileSize % 252);
uint8_t* headerBytes = head.generateBytes();
if (!headerBytes)
{
if (debug)
std::cout << "Failed to generate header bytes" << std::endl;
return false;
}
if (debug)
std::cout <<
"Generated header {byteModulo = " << (int)head.byteModulo <<
", byteOffset = " << (int)head.byteOffset <<
", endPadding = " << (int)head.endPadding <<
", nRandomBytes = " << (int)head.nRandomBytes << "}, writing to output file..." << std::endl;
if (!outFile.write((char*)headerBytes, head.size))
{
if (debug)
std::cout << "Failed to write header to file" << std::endl;
return false;
}
free(headerBytes);
uint8_t* inBuffer = (uint8_t*)malloc(252);
if (!inBuffer)
{
if (debug)
std::cout << "Failed to malloc 252 bytes for inBuffer" << std::endl;
return false;
}
uint8_t* outBuffer = (uint8_t*)malloc(256);
if (!outBuffer)
{
if (debug)
std::cout << "Failed to malloc 256 bytes for outBuffer" << std::endl;
return false;
}
size_t chunkIndex = 0;
size_t nChunks = (size_t)ceil((double)inFileSize / 252.0);
while (inFile.read((char*)inBuffer, 252))
{
if (debug)
std::cout << "Encrypting chunk " << chunkIndex << "/" << nChunks << std::endl;
encryptChunk(head, chunkIndex, inBuffer, outBuffer);
if (!outFile.write((char*)outBuffer, 256))
{
if (debug)
std::cout << "Failed to write 256 bytes to output file" << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return false;
}
chunkIndex++;
}
if ((252 - inFile.gcount()) != head.endPadding)
{
if (debug)
std::cout << "endPadding discrepancy" << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return false;
}
if (head.endPadding > 0)
{
if (debug)
std::cout << "Padding last chunk..." << std::endl;
for (int i = 252 - 1; i >= head.endPadding; i--)
{
inBuffer[i] = inBuffer[i - head.endPadding];
}
for (int i = 0; i < head.endPadding; i++)
{
inBuffer[i] = 'V';// random();
}
// now encrypt as normal
if (debug)
std::cout << "Encrypting chunk " << chunkIndex << "/" << nChunks << std::endl;
encryptChunk(head, chunkIndex, inBuffer, outBuffer);
if (!outFile.write((char*)outBuffer, 256))
{
if (debug)
std::cout << "Failed to write 256 bytes to output file" << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return false;
}
}
if (debug)
std::cout << "Done! Wrote " << (uint64_t)256 * (uint64_t)nChunks + (uint64_t)head.size << " bytes to " << fileOutput << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return true;
}
bool Encryption::decryptFile(const char* fileInput, const char* fileOutput)
{
if (debug)
{
std::cout << "========== Decrypting ==========" << std::endl;
std::cout << "Input: (encrypted) " << fileInput << std::endl;
std::cout << "Output: (decrypted) " << fileOutput << std::endl;
}
std::ifstream inFile;
inFile.open(fileInput, std::ios::in | std::ios::binary);
if (!inFile.is_open())
{
if (debug)
std::cout << "Failed to open input file" << std::endl;
return false;
}
inFile.seekg(0, std::ios::end);
uint64_t inFileSize = inFile.tellg(); // overflows files with size > 16,777,216 terabytes, i think we're good
inFile.clear();
inFile.seekg(0);
if (debug)
std::cout << "Input file size: " << inFileSize << " bytes" << std::endl;
if (inFileSize < minHeaderSize)
{
if (debug)
std::cout << "inFile invalid - too small" << std::endl;
inFile.close();
return false;
}
size_t headerSearchSize = (size_t)std::min(inFileSize, (uint64_t)maxHeaderSize);
uint8_t* headerBytes = (uint8_t*)malloc(headerSearchSize);
if (!headerBytes)
{
if (debug)
std::cout << "failed to allocate header bytes" << std::endl;
inFile.close();
return false;
}
if (!inFile.read((char*)headerBytes, headerSearchSize))
{
if (debug)
std::cout << "failed to read header bytes in inFile" << std::endl;
inFile.close();
free(headerBytes);
return false;
}
Header head = parseHeader(headerBytes, headerSearchSize);
free(headerBytes);
if (!head.isValid)
{
if (debug)
std::cout << "header is invalid" << std::endl;
inFile.close();
return false;
}
if (debug)
std::cout <<
"Parsed header {byteModulo = " << (int)head.byteModulo <<
", byteOffset = " << (int)head.byteOffset <<
", endPadding = " << (int)head.endPadding <<
", nRandomBytes = " << (int)head.nRandomBytes << "}, writing to output file..." << std::endl;
if ((inFileSize - head.size) % 256)
{
if (debug)
std::cout << "inFileSize is not valid, expected a multiple of 256" << std::endl;
inFile.close();
return false;
}
inFile.clear();
inFile.seekg(head.size);
if (debug)
std::cout << "now opening output file" << std::endl;
std::ofstream outFile;
outFile.open(fileOutput, std::ios::out | std::ios::binary);
if (!outFile.is_open())
{
if (debug)
std::cout << "Failed to open output file" << std::endl;
inFile.close();
return false;
}
if (debug)
std::cout << "Successfully opened files, now decrypting..." << std::endl;
uint8_t* inBuffer = (uint8_t*)malloc(256);
if (!inBuffer)
{
if (debug)
std::cout << "Failed to malloc 256 bytes for inBuffer" << std::endl;
return false;
}
uint8_t* outBuffer = (uint8_t*)malloc(252);
if (!outBuffer)
{
if (debug)
std::cout << "Failed to malloc 252 bytes for outBuffer" << std::endl;
return false;
}
size_t chunkIndex = 0;
size_t nChunks = (size_t)((inFileSize - head.size) / 256);
while (inFile.read((char*)inBuffer, 256))
{
if (debug)
std::cout << "Decrypting chunk " << chunkIndex << "/" << nChunks << std::endl;
decryptChunk(head, chunkIndex, inBuffer, outBuffer);
bool isLastChunk = (chunkIndex + 1) >= nChunks;
if (isLastChunk)
{
if (debug)
std::cout << "removing padding from last chunk..." << std::endl;
for (int i = 0; i < 252 - head.endPadding; i++)
{
outBuffer[i] = outBuffer[i + head.endPadding];
}
}
if (!outFile.write((char*)outBuffer, isLastChunk ? 252 - head.endPadding : 252))
{
if (debug)
std::cout << "Failed to write 252 bytes to output file" << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return false;
}
chunkIndex++;
}
if (debug)
std::cout << "Done! Wrote " << (uint64_t)252 * (uint64_t)nChunks - (uint64_t)head.endPadding << " bytes to " << fileOutput << std::endl;
outFile.close();
inFile.close();
free(inBuffer);
free(outBuffer);
return true;
}
uint64_t Encryption::getDecryptedSize(Header header, uint64_t encryptedSize)
{
if (encryptedSize <= header.size) return 0;
encryptedSize -= header.size;
uint64_t nChunks = encryptedSize / 256; // encryptedSize *should* be a multiple of 256
encryptedSize = nChunks * 252;
if (encryptedSize < header.endPadding) return 0;
return encryptedSize - header.endPadding;
}