forked from ppy/osu-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafeEncryptionProvider.cs
More file actions
489 lines (402 loc) · 15.5 KB
/
SafeEncryptionProvider.cs
File metadata and controls
489 lines (402 loc) · 15.5 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
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
using StreamFormatDecryptor;
namespace StreamFormatDecryptor;
public class SafeEncryptionProvider
{
private static readonly bool TraceAlgorithmTwo = false;
private uint[] k;
private byte[] kB;
private const uint d = 0x9e3779b9;
private const uint r = 32;
public fEnum.EncryptionMethod m = fEnum.EncryptionMethod.Four;
#region Local array converter (Byte <-> Unsigned Integer)
public static byte[] ConvertUIntArrayToByteArray(uint[] input)
{
byte[] output = new byte[input.Length * 4];
Buffer.BlockCopy(input, 0, output, 0, output.Length);
return output;
}
public static uint[] ConvertByteArrayToUIntArray(byte[] input)
{
if (input.Length % 4 != 0)
throw new ArgumentException("Byte array length must be multiple of 4");
uint[] output = new uint[input.Length / 4];
Buffer.BlockCopy(input, 0, output, 0, input.Length);
return output;
}
#endregion
// Key has to be 4 words long and can't be null
public void Init(uint[] pkey, fEnum.EncryptionMethod EM)
{
if (EM == fEnum.EncryptionMethod.Four)
throw new ArgumentException("1"); //Encryption method can't be none
if (pkey.Length != 4)
throw new ArgumentException("2"); //Encryption key has to be 4 words long
k = pkey;
Console.WriteLine("Key: " + string.Join(" ", pkey));
m = EM;
Console.WriteLine("Encryption method: " + EM);
kB = ConvertUIntArrayToByteArray(pkey);
Console.WriteLine("Key bytes: " + string.Join(" ", kB));
}
private void CheckKey()
{
if (m == fEnum.EncryptionMethod.Four)
throw new ArgumentException("Encryption method has to be set first");
}
#region Simple Bytes Encrypt/Decrypt
private void SimpleEncryptBytesSafe(byte[] buf, int offset, int length)
{
byte prevE = 0; // previous encrypted
for (int i = 0; i < length; i++)
{
int bufIdx = offset + i;
buf[bufIdx] = unchecked((byte)((buf[bufIdx] + (kB[i % 16] >> 2)) % 256));
buf[bufIdx] ^= RotateLeft(kB[15 - i % 16], (byte)((prevE + length - i) % 7));
buf[bufIdx] = RotateRight(buf[bufIdx], (byte)((~(uint)(prevE)) % 7));
prevE = buf[bufIdx];
}
}
private void SimpleDecryptBytesSafe(byte[] buf, int offset, int length)
{
byte prevE = 0; // previous encrypted
for (int i = 0; i < length; i++)
{
int bufIdx = offset + i;
byte tmpE = buf[bufIdx];
buf[bufIdx] = RotateLeft(buf[bufIdx], (byte)((~(uint)(prevE)) % 7));
buf[bufIdx] ^= RotateLeft(kB[15 - i % 16], (byte)((prevE + length - i) % 7));
buf[bufIdx] = unchecked((byte)((buf[bufIdx] - (kB[i % 16] >> 2) + 256) % 256));
prevE = tmpE;
}
}
#endregion
#region Rotation
private static byte RotateLeft(byte val, byte n)
{
return (byte)((val << n) | (val >> (8 - n)));
}
private static byte RotateRight(byte val, byte n)
{
return (byte)((val >> n) | (val << (8 - n)));
}
#endregion
#region Encryption ONE (Disabled)
/**
private void EncryptDecryptOneSafe(byte[] bufferArr, int bufferIdx, int bufferLen, bool isEncrypted)
{
uint fullWordCount = unchecked((uint)bufferLen / 8);
uint leftover = (uint)(bufferLen % 8); //remaining of fullWordCount
uint[] intWordArrB = ConvertByteArrayToUIntArray(bufferArr);
intWordArrB -= 2;
intWordArrO -= 2;
if (isEncrypted)
for (int wordCount = 0; wordCount < fullWordCount; wordCount++)
EncryptWordOne(intWordArrB += 2, intWordArrO += 2);
else
for (int wordCount = 0; wordCount < fullWordCount; wordCount++)
DecryptWordOne(intWordArrB += 2, intWordArrO += 2);
if (leftover == 0) return; // no leftover for me? get lost :c
byte[] bufferEnd = bufferArr + bufferLen;
byte[] byteWordArrB2 = bufferEnd - leftover;
byte[] byteWordArrO2 = ConvertUIntArrayToByteArray(resultArr + bufferLen - leftover); ;
// copy leftoverBuffer[] -> result[]
Array.Copy(byteWordArrB2, byteWordArrO2, leftover);
// deal with leftover
if (isEncrypted)
SimpleEncryptBytesSafe(byteWordArrO2 - leftover, 0, unchecked((int)leftover));
else
SimpleDecryptBytesSafe(byteWordArrO2 - leftover, 0, unchecked((int)leftover));
}
**/
#region Sub-Functions (Encrypt/Decrypt)
/**
private void EncryptWordOne(uint[] v uint[] o)
{
uint i;
uint v0 = v[0];
uint v1 = v[1];
uint sum = 0;
for (i = 0; i < r; i++)
{
//todo: cache sum + k for better speed
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += d;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
}
o[0] = v0;
o[1] = v1;
}
private void DecryptWordOne(uint[] v , uint[] o )
{
uint i;
uint v0 = v[0];
uint v1 = v[1];
uint sum = unchecked(d * r);
for (i = 0; i < r; i++)
{
//todo: cache sum + k for better speed
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
sum -= d;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
}
o[0] = v0;
o[1] = v1;
}
**/
#endregion
#endregion
#region Encryption TWO
public const uint NMax = 16;
public const uint NMaxBytes = NMax * 4;
private void EncryptDecryptTwoSafe(byte[] bufferArr, bool encrypt, int count, int offset)
{
uint fullWordCount = unchecked((uint)count / NMaxBytes);
uint leftover = unchecked((uint)count) % NMaxBytes;
uint rounds = 6 + 52 / NMax;
if (TraceAlgorithmTwo)
{
Console.WriteLine("\n=== CRYPTO ALGORITHM TWO ===");
Console.WriteLine("Full word count: " + fullWordCount);
Console.WriteLine("Leftover: " + leftover);
Console.WriteLine("Rounds: " + rounds);
Console.WriteLine("Set max n to: " + NMax);
Console.WriteLine($"Starting {(encrypt ? "encryption" : "decryption")}...");
}
byte[] bufferCut = new byte[fullWordCount * NMaxBytes];
Buffer.BlockCopy(bufferArr, offset, bufferCut, 0, (int)(fullWordCount * NMaxBytes));
uint[] bufferCutWords = ConvertByteArrayToUIntArray(bufferCut);
if (encrypt)
for (uint wordCount = 0; wordCount < fullWordCount; wordCount++)
{
EncryptWordsTwoSafe(bufferCutWords, (int)(wordCount * NMax), NMax);
}
else //copy pasta because we dont want to waste time on a cmp each iteration
for (uint wordCount = 0; wordCount < fullWordCount; wordCount++)
{
DecryptWordsTwoSafe(bufferCutWords, (int)(wordCount * NMax), NMax);
}
byte[] bufferProcessed = ConvertUIntArrayToByteArray(bufferCutWords);
Buffer.BlockCopy(bufferProcessed, 0, bufferArr, offset, (int)(fullWordCount * NMaxBytes));
uint n_leftover = leftover / 4;
byte[] leftoverBuffer = new byte[n_leftover * 4];
Buffer.BlockCopy(bufferArr, (int)(offset + fullWordCount * NMaxBytes), leftoverBuffer, 0, (int)n_leftover * 4);
uint[] leftoverBufferWords = ConvertByteArrayToUIntArray(leftoverBuffer);
if (n_leftover > 1)
{
if (TraceAlgorithmTwo)
{
Console.WriteLine($"Starting leftover {(encrypt ? "encryption" : "decryption")}...");
}
if (encrypt)
EncryptWordsTwoSafe(leftoverBufferWords, 0, n_leftover);
else
DecryptWordsTwoSafe(leftoverBufferWords, 0, n_leftover);
leftover -= n_leftover * 4;
if (leftover == 0)
{
if (TraceAlgorithmTwo)
{
Console.WriteLine("Leftover is 0, no further byte pass required.");
Console.WriteLine("============================");
}
// Copy processed words back to main buffer before returning
byte[] leftoverBufferProcessedWords = ConvertUIntArrayToByteArray(leftoverBufferWords);
Buffer.BlockCopy(leftoverBufferProcessedWords, 0, bufferArr, (int)(offset + fullWordCount * NMaxBytes), (int)n_leftover * 4);
return;
}
}
byte[] leftoverBufferProcessed = ConvertUIntArrayToByteArray(leftoverBufferWords);
Buffer.BlockCopy(leftoverBufferProcessed, 0, bufferArr, (int)(offset + fullWordCount * NMaxBytes), (int)n_leftover * 4);
if (TraceAlgorithmTwo)
{
Console.WriteLine($"Starting final (simple) {(encrypt ? "encryption" : "decryption")}...");
}
if (encrypt)
SimpleEncryptBytesSafe(bufferArr, (int)(count - leftover) + offset, (int)leftover);
else
SimpleDecryptBytesSafe(bufferArr, (int)(count - leftover) + offset, (int)leftover);
if (TraceAlgorithmTwo)
{
Console.WriteLine($"Final {(encrypt ? "encryption" : "decryption")} done.");
}
}
#region Sub-Functions (Encrypt/Decrypt)
private void EncryptWordsTwoSafe(uint[] v, int offset, uint n)
{
uint y, z, sum;
uint p, e;
uint rounds = 6 + 52 / n;
sum = 0;
z = v[n - 1 + offset];
do
{
sum += d;
e = (sum >> 2) & 3;
for (p = 0; p < n - 1; p++)
{
y = v[p + 1 + offset];
z = v[p + offset] += ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
}
y = v[offset];
z = v[n - 1 + offset] += ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
} while (--rounds > 0);
}
private void DecryptWordsTwoSafe(uint[] v, int offset, uint n)
{
if (v == null)
throw new ArgumentNullException(nameof(v));
if (k == null)
throw new InvalidOperationException("Encryption key not initialized. Call Init method first.");
uint y, z, sum;
uint p, e;
uint rounds = 6 + 52 / n;
sum = rounds * d;
y = v[offset];
do
{
e = (sum >> 2) & 3;
for (p = n - 1; p > 0; p--)
{
z = v[p - 1 + offset];
y = v[p + offset] -= ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
}
z = v[n - 1 + offset];
y = v[offset] -= ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
} while ((sum -= d) != 0);
}
#endregion
#endregion
#region Encryption HOMEBREW
private void EncryptDecryptHomebrew(byte[] bufferArr, int offset,int bufferLen, bool isEncrypted)
{
if (isEncrypted)
SimpleEncryptBytesSafe(bufferArr,offset, bufferLen);
else
SimpleDecryptBytesSafe(bufferArr,offset, bufferLen);
}
#endregion
#region Encrypt/Decrypt Main
private void EncryptDecryptSafe(byte[] bufferArr, byte[] outputArr, int bufStart, int outputStart, int bufferLength, bool encrypt)
{
switch (m)
{
case fEnum.EncryptionMethod.One:
EncryptDecryptOneSafe(bufferArr, outputArr, bufStart, outputStart, bufferLength, encrypt);
break;
case fEnum.EncryptionMethod.Two:
// EncryptDecryptTwoSafe handles its own word conversion and simple cipher steps
EncryptDecryptTwoSafe(bufferArr, encrypt, bufferLength, bufStart);
break;
case fEnum.EncryptionMethod.Three:
EncryptDecryptHomebrew(bufferArr, bufStart, bufferLength, encrypt);
break;
case fEnum.EncryptionMethod.Four:
//checkKey();
break;
}
}
private void EncryptDecryptOneSafe(byte[] bufferArr, byte[] outputArr, int bufStart, int outputStart, int bufferLength, bool encrypt)
{
uint fullWordCount = unchecked((uint)bufferLength / 8);
// We need to work with a segment of the array.
// For simplicity, we can copy to a temporary array if not already aligned.
byte[] inputSegment = new byte[fullWordCount * 8];
Buffer.BlockCopy(bufferArr, bufStart, inputSegment, 0, inputSegment.Length);
uint[] words = ConvertByteArrayToUIntArray(inputSegment);
uint[] outputWords = new uint[words.Length];
if (encrypt)
{
for (int i = 0; i < fullWordCount; i++)
EncryptWordOneSafe(words, outputWords, i * 2);
}
else
{
for (int i = 0; i < fullWordCount; i++)
DecryptWordOneSafe(words, outputWords, i * 2);
}
byte[] processedBytes = ConvertUIntArrayToByteArray(outputWords);
Buffer.BlockCopy(processedBytes, 0, outputArr ?? bufferArr, outputArr != null ? outputStart : bufStart, (int)(fullWordCount * 8));
}
private void EncryptWordOneSafe(uint[] v, uint[] o, int offset)
{
uint i;
uint v0 = v[offset];
uint v1 = v[offset + 1];
uint sum = 0;
for (i = 0; i < r; i++)
{
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += d;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
}
o[offset] = v0;
o[offset + 1] = v1;
}
private void DecryptWordOneSafe(uint[] v, uint[] o, int offset)
{
uint i;
uint v0 = v[offset];
uint v1 = v[offset + 1];
uint sum = unchecked(d * r);
for (i = 0; i < r; i++)
{
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
sum -= d;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
}
o[offset] = v0;
o[offset + 1] = v1;
}
public void EncryptDecrypt(byte[] buffer, byte[] output, int bufStart, int outputStart, int count,
bool encrypt)
{
EncryptDecryptSafe(buffer, output, bufStart, outputStart, count, encrypt);
}
#endregion
#region Encrypt/Decrypt Methods
#region Decrypt
/**
* Will be decrypted from and to the buffer.
* Fastest if buffer size is a multiple of 8
**/
public void Decrypt(byte[] buffer)
{
EncryptDecrypt(buffer, null, 0, 0, buffer.Length, false);
}
public void Decrypt(byte[] buffer, int start, int count)
{
EncryptDecrypt(buffer, null, start, 0, count, false);
}
public void Decrypt(byte[] buffer, byte[] output)
{
EncryptDecrypt(buffer, output, 0, 0, buffer.Length, false);
}
public void Decrypt(byte[] buffer, byte[] output, int bufStart, int outStart, int count)
{
EncryptDecrypt(buffer, output, bufStart, outStart, count, false);
}
#endregion
#region Encrypt
/**
* Will be encrypted from and to the buffer.
* Fastest if buffer size is a multiple of 8
**/
public void Encrypt(byte[] buffer)
{
EncryptDecrypt(buffer, null, 0, 0, buffer.Length, true);
}
public void Encrypt(byte[] buffer, int start, int count)
{
EncryptDecrypt(buffer, null, start, 0, count, true);
}
public void Encrypt(byte[] buffer, byte[] output)
{
EncryptDecrypt(buffer, output, 0, 0, buffer.Length, true);
}
public void Encrypt(byte[] buffer, byte[] output, int bufStart, int outStart, int count)
{
EncryptDecrypt(buffer, output, bufStart, outStart, count, true);
}
#endregion
#endregion
}