forked from mcraiha/CSharp-AES-CTR-NetStandard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSAES-CTR.cs
471 lines (400 loc) · 16 KB
/
CSAES-CTR.cs
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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace CS_AES_CTR
{
/// <summary>
/// Class that can be used for AES CTR encryption / decryption
/// </summary>
public sealed class AES_CTR : IDisposable
{
/// <summary>
/// What are allowed key lengths in bytes (128, 192 and 256 bits)
/// </summary>
/// <value></value>
public static readonly int[] allowedKeyLengths = new int[3] { 16, 24, 32 };
/// <summary>
/// What is allowed initial counter length in bytes
/// </summary>
public const int allowedCounterLength = 16;
/// <summary>
/// Only allowed Initialization vector length in bytes
/// </summary>
private const int ivLength = 16;
/// <summary>
/// How many bytes are processed at time
/// </summary>
private const int processBytesAtTime = 16;
/// <summary>
/// Internal counter
/// </summary>
private readonly byte[] counter = new byte[allowedCounterLength];
/// <summary>
/// Internal transformer for doing encrypt/decrypt transforming
/// </summary>
private readonly ICryptoTransform counterEncryptor;
/// <summary>
/// Determines if the objects in this class have been disposed of. Set to true by the Dispose() method.
/// </summary>
private bool isDisposed;
/// <summary>
/// Changes counter behaviour according endianess.
/// </summary>
private readonly bool isLittleEndian;
#if NET6_0_OR_GREATER
/// <summary>
/// AES_CTR constructor
/// </summary>
/// <param name="key">Key as readonlyspan. (128, 192 or 256 bits)</param>
/// <param name="initialCounter">Initial counter as readonlyspan. 16 bytes</param>
/// <param name="littleEndian">Is initial counter little endian (default false)</param>
/// <returns></returns>
public AES_CTR(ReadOnlySpan<byte> key, ReadOnlySpan<byte> initialCounter, bool littleEndian = false) : this(key.ToArray(), initialCounter.ToArray(), littleEndian)
{
}
#endif // NET6_0_OR_GREATER
/// <summary>
/// AES_CTR constructor
/// </summary>
/// <param name="key">Key as byte array. (128, 192 or 256 bits)</param>
/// <param name="initialCounter">Initial counter as byte array. 16 bytes</param>
/// <param name="littleEndian">Is initial counter little endian (default false)</param>
public AES_CTR(byte[] key, byte[] initialCounter, bool littleEndian = false)
{
if (key == null)
{
throw new ArgumentNullException("Key is null");
}
if (!Array.Exists(allowedKeyLengths, allowed => allowed == key.Length))
{
throw new ArgumentException($"Key length must be {allowedKeyLengths[0]}, {allowedKeyLengths[1]} or {allowedKeyLengths[2]} bytes. Actual: {key.Length}");
}
if (initialCounter == null)
{
throw new ArgumentNullException("Initial counter is null");
}
if (allowedCounterLength != initialCounter.Length)
{
throw new ArgumentException($"Initial counter must be {allowedCounterLength} bytes");
}
this.isDisposed = false;
SymmetricAlgorithm aes = new AesManaged { Mode = CipherMode.ECB, Padding = PaddingMode.None };
// Create copy of initial counter since state is kept during the lifetime of AES_CTR
Buffer.BlockCopy(initialCounter, 0, this.counter, 0, allowedCounterLength);
this.isLittleEndian = littleEndian;
// Initialization vector is always full of zero bytes in CTR mode
var zeroIv = new byte[ivLength];
this.counterEncryptor = aes.CreateEncryptor(key, zeroIv);
}
#region Encrypt
/// <summary>
/// Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="output">Output byte array, must have enough bytes</param>
/// <param name="input">Input byte array</param>
/// <param name="numBytes">Number of bytes to encrypt</param>
public void EncryptBytes(byte[] output, byte[] input, int numBytes)
{
this.WorkBytes(output, input, numBytes);
}
/// <summary>
/// Encrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
/// </summary>
/// <param name="output">Output stream</param>
/// <param name="input">Input stream</param>
/// <param name="howManyBytesToProcessAtTime">How many bytes to read and write at time, default is 1024</param>
public void EncryptStream(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
this.WorkStreams(output, input, howManyBytesToProcessAtTime);
}
/// <summary>
/// Async encrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
/// </summary>
/// <param name="output">Output stream</param>
/// <param name="input">Input stream</param>
/// <param name="howManyBytesToProcessAtTime">How many bytes to read and write at time, default is 1024</param>
/// <returns></returns>
public async Task EncryptStreamAsync(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
await this.WorkStreamsAsync(output, input, howManyBytesToProcessAtTime);
}
/// <summary>
/// Encrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="output">Output byte array, must have enough bytes</param>
/// <param name="input">Input byte array</param>
public void EncryptBytes(byte[] output, byte[] input)
{
this.WorkBytes(output, input, input.Length);
}
/// <summary>
/// Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="input">Input byte array</param>
/// <param name="numBytes">Number of bytes to encrypt</param>
/// <returns>Byte array that contains encrypted bytes</returns>
public byte[] EncryptBytes(byte[] input, int numBytes)
{
byte[] returnArray = new byte[numBytes];
this.WorkBytes(returnArray, input, numBytes);
return returnArray;
}
/// <summary>
/// Encrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="input">Input byte array</param>
/// <returns>Byte array that contains encrypted bytes</returns>
public byte[] EncryptBytes(byte[] input)
{
byte[] returnArray = new byte[input.Length];
this.WorkBytes(returnArray, input, input.Length);
return returnArray;
}
/// <summary>
/// Encrypt string as UTF8 byte array, returns byte array that is allocated by method.
/// </summary>
/// <remarks>Here you can NOT swap encrypt and decrypt methods, because of bytes-string transform</remarks>
/// <param name="input">Input string</param>
/// <returns>Byte array that contains encrypted bytes</returns>
public byte[] EncryptString(string input)
{
byte[] utf8Bytes = System.Text.Encoding.UTF8.GetBytes(input);
byte[] returnArray = new byte[utf8Bytes.Length];
this.WorkBytes(returnArray, utf8Bytes, utf8Bytes.Length);
return returnArray;
}
#endregion // Encrypt
#region Decrypt
/// <summary>
/// Decrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="output">Output byte array, must have enough bytes</param>
/// <param name="input">Input byte array</param>
/// <param name="numBytes">Number of bytes to encrypt</param>
public void DecryptBytes(byte[] output, byte[] input, int numBytes)
{
this.WorkBytes(output, input, numBytes);
}
/// <summary>
/// Decrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
/// </summary>
/// <param name="output">Output stream</param>
/// <param name="input">Input stream</param>
/// <param name="howManyBytesToProcessAtTime">How many bytes to read and write at time, default is 1024</param>
public void DecryptStream(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
this.WorkStreams(output, input, howManyBytesToProcessAtTime);
}
/// <summary>
/// Async decrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
/// </summary>
/// <param name="output">Output stream</param>
/// <param name="input">Input stream</param>
/// <param name="howManyBytesToProcessAtTime">How many bytes to read and write at time, default is 1024</param>
/// <returns></returns>
public async Task DecryptStreamAsync(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
await this.WorkStreamsAsync(output, input, howManyBytesToProcessAtTime);
}
/// <summary>
/// Decrypt arbitrary-length byte array (input), writing the resulting byte array to preallocated output buffer.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="output">Output byte array, must have enough bytes</param>
/// <param name="input">Input byte array</param>
public void DecryptBytes(byte[] output, byte[] input)
{
this.WorkBytes(output, input, input.Length);
}
/// <summary>
/// Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="input">Input byte array</param>
/// <param name="numBytes">Number of bytes to encrypt</param>
/// <returns>Byte array that contains decrypted bytes</returns>
public byte[] DecryptBytes(byte[] input, int numBytes)
{
byte[] returnArray = new byte[numBytes];
this.WorkBytes(returnArray, input, numBytes);
return returnArray;
}
/// <summary>
/// Decrypt arbitrary-length byte array (input), writing the resulting byte array that is allocated by method.
/// </summary>
/// <remarks>Since this is symmetric operation, it doesn't really matter if you use Encrypt or Decrypt method</remarks>
/// <param name="input">Input byte array</param>
/// <returns>Byte array that contains decrypted bytes</returns>
public byte[] DecryptBytes(byte[] input)
{
byte[] returnArray = new byte[input.Length];
this.WorkBytes(returnArray, input, input.Length);
return returnArray;
}
/// <summary>
/// Decrypt UTF8 byte array to string.
/// </summary>
/// <remarks>Here you can NOT swap encrypt and decrypt methods, because of bytes-string transform</remarks>
/// <param name="input">Byte array</param>
/// <returns>Byte array that contains encrypted bytes</returns>
public string DecryptUTF8ByteArray(byte[] input)
{
byte[] tempArray = new byte[input.Length];
this.WorkBytes(tempArray, input, input.Length);
return System.Text.Encoding.UTF8.GetString(tempArray);
}
#endregion // Decrypt
/// <summary>
/// Decrypt / Encrypt arbitrary-length byte stream (input), writing the resulting bytes to another stream (output)
/// </summary>
/// <param name="output">Output stream</param>
/// <param name="input">Input stream</param>
/// <param name="howManyBytesToProcessAtTime">How many bytes to read and write at time, default is 1024</param>
private void WorkStreams(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
int readBytes;
byte[] inputBuffer = new byte[howManyBytesToProcessAtTime];
byte[] outputBuffer = new byte[howManyBytesToProcessAtTime];
while ((readBytes = input.Read(inputBuffer, 0, howManyBytesToProcessAtTime)) > 0)
{
// Encrypt or decrypt
WorkBytes(output: outputBuffer, input: inputBuffer, numBytes: readBytes);
// Write buffer
output.Write(outputBuffer, 0, readBytes);
}
}
private async Task WorkStreamsAsync(Stream output, Stream input, int howManyBytesToProcessAtTime = 1024)
{
byte[] readBytesBuffer = new byte[howManyBytesToProcessAtTime];
byte[] writeBytesBuffer = new byte[howManyBytesToProcessAtTime];
int howManyBytesWereRead = await input.ReadAsync(readBytesBuffer, 0, howManyBytesToProcessAtTime);
while (howManyBytesWereRead > 0)
{
// Encrypt or decrypt
WorkBytes(output: writeBytesBuffer, input: readBytesBuffer, numBytes: howManyBytesWereRead);
// Write
await output.WriteAsync(writeBytesBuffer, 0, howManyBytesWereRead);
// Read more
howManyBytesWereRead = await input.ReadAsync(readBytesBuffer, 0, howManyBytesToProcessAtTime);
}
}
private void WorkBytes(byte[] output, byte[] input, int numBytes)
{
// Check parameters
if (input == null)
{
throw new ArgumentNullException("input", "Input cannot be null");
}
if (output == null)
{
throw new ArgumentNullException("output", "Output cannot be null");
}
if (numBytes < 0 || numBytes > input.Length)
{
throw new ArgumentOutOfRangeException("numBytes", "The number of bytes to read must be between [0..input.Length]");
}
if (output.Length < numBytes)
{
throw new ArgumentOutOfRangeException("output", $"Output byte array should be able to take at least {numBytes}");
}
if (isDisposed)
{
throw new ObjectDisposedException("state", "AES_CTR has already been disposed");
}
int offset = 0;
var tmp = new byte[allowedCounterLength];
while (numBytes > 0)
{
// Generate new XOR mask for next processBytesAtTime
counterEncryptor.TransformBlock(counter, 0, allowedCounterLength, tmp, 0);
// Increase counter (basically this increases the last index first and continues to one before that if 255 -> 0, better solution would be to use uint128, but it does not exist yet)
if (this.isLittleEndian)
{
// LittleEndian
for (int i = 0; i < allowedCounterLength; i++)
{
if (++counter[i] != 0)
{
break;
}
}
}
else
{
// BigEndian
for (int i = allowedCounterLength - 1; i >= 0; i--)
{
if (++counter[i] != 0)
{
break;
}
}
}
// Last bytes
if (numBytes <= processBytesAtTime)
{
for (int i = 0; i < numBytes; i++)
{
output[i + offset] = (byte) (input[i + offset] ^ tmp[i]);
}
return;
}
for (int i = 0; i < processBytesAtTime; i++ )
{
output[i + offset] = (byte) (input[i + offset] ^ tmp[i]);
}
numBytes -= processBytesAtTime;
offset += processBytesAtTime;
}
}
#region Destructor and Disposer
/// <summary>
/// Clear and dispose of the internal variables. The finalizer is only called if Dispose() was never called on this cipher.
/// </summary>
~AES_CTR()
{
Dispose(false);
}
/// <summary>
/// Clear and dispose of the internal state. Also request the GC not to call the finalizer, because all cleanup has been taken care of.
/// </summary>
public void Dispose()
{
Dispose(true);
/*
* The Garbage Collector does not need to invoke the finalizer because Dispose(bool) has already done all the cleanup needed.
*/
GC.SuppressFinalize(this);
}
/// <summary>
/// This method should only be invoked from Dispose() or the finalizer. This handles the actual cleanup of the resources.
/// </summary>
/// <param name="disposing">
/// Should be true if called by Dispose(); false if called by the finalizer
/// </param>
private void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
/* Cleanup managed objects by calling their Dispose() methods */
if (this.counterEncryptor != null)
{
this.counterEncryptor.Dispose();
}
}
/* Cleanup here */
Array.Clear(this.counter, 0, allowedCounterLength);
}
isDisposed = true;
}
#endregion // Destructor and Disposer
}
}