2017-04-12 02:07:38 +02:00
|
|
|
|
using System;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
using System.IO;
|
2022-05-22 03:55:22 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
|
2017-04-15 08:28:34 +02:00
|
|
|
|
namespace Teknik.Utilities.Cryptography
|
2017-03-20 08:11:22 +01:00
|
|
|
|
{
|
2017-04-15 08:28:34 +02:00
|
|
|
|
public class AesCounterStream : Stream
|
2017-03-20 08:11:22 +01:00
|
|
|
|
{
|
|
|
|
|
private Stream _Inner;
|
2017-04-12 07:01:42 +02:00
|
|
|
|
private CounterModeCryptoTransform _Cipher;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
|
2017-04-12 09:10:39 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs Encryption or Decryption on a stream with the given Key and IV
|
|
|
|
|
///
|
|
|
|
|
/// Cipher is AES-256 in CTR mode with no padding
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream"></param>
|
|
|
|
|
/// <param name="encrypt"></param>
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
/// <param name="iv"></param>
|
2022-05-28 20:00:15 +02:00
|
|
|
|
public AesCounterStream(Stream stream, bool encrypt, PooledArray key, PooledArray iv)
|
2017-03-20 08:11:22 +01:00
|
|
|
|
{
|
|
|
|
|
_Inner = stream;
|
2017-04-12 02:07:38 +02:00
|
|
|
|
|
|
|
|
|
// Create the Aes Cipher
|
2022-05-23 03:52:04 +02:00
|
|
|
|
using AesCounterMode aes = new AesCounterMode(iv);
|
2017-04-12 02:07:38 +02:00
|
|
|
|
if (encrypt)
|
|
|
|
|
{
|
2022-05-28 20:00:15 +02:00
|
|
|
|
_Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(key.Array, iv.Array); // Encrypt
|
2017-04-12 02:07:38 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-28 20:00:15 +02:00
|
|
|
|
_Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(key.Array, iv.Array); // Decrypt
|
2017-04-12 02:07:38 +02:00
|
|
|
|
}
|
2017-04-12 07:01:42 +02:00
|
|
|
|
|
|
|
|
|
// Sync the counter
|
|
|
|
|
SyncCounter();
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 03:55:22 +02:00
|
|
|
|
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null && CanRead)
|
|
|
|
|
{
|
|
|
|
|
int processed = 0;
|
|
|
|
|
|
|
|
|
|
// Read the data from the stream
|
2022-05-26 08:30:14 +02:00
|
|
|
|
int bytesRead = await _Inner.ReadAsync(buffer).ConfigureAwait(false);
|
2022-05-22 03:55:22 +02:00
|
|
|
|
if (bytesRead > 0)
|
|
|
|
|
{
|
|
|
|
|
// Process the read buffer
|
|
|
|
|
processed = _Cipher.TransformBlock(buffer.Span, 0, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do we have more?
|
|
|
|
|
if (processed < bytesRead)
|
|
|
|
|
{
|
|
|
|
|
// Finalize the cipher
|
|
|
|
|
var finalProcessed = _Cipher.TransformFinalBlock(buffer.Span, processed, bytesRead);
|
|
|
|
|
if (finalProcessed > 0)
|
|
|
|
|
processed += finalProcessed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processed;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 08:11:22 +01:00
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null && CanRead)
|
|
|
|
|
{
|
2022-05-23 03:52:04 +02:00
|
|
|
|
Span<byte> readBuf = buffer;
|
2017-04-12 07:01:42 +02:00
|
|
|
|
int processed = 0;
|
2017-04-12 02:07:38 +02:00
|
|
|
|
|
2017-04-12 07:01:42 +02:00
|
|
|
|
// Read the data from the stream
|
2022-05-23 03:52:04 +02:00
|
|
|
|
int bytesRead = _Inner.Read(readBuf.Slice(offset, count));
|
2017-04-12 07:01:42 +02:00
|
|
|
|
if (bytesRead > 0)
|
2017-04-12 02:07:38 +02:00
|
|
|
|
{
|
2017-04-12 09:10:39 +02:00
|
|
|
|
// Process the read buffer
|
2022-05-23 03:52:04 +02:00
|
|
|
|
processed = _Cipher.TransformBlock(readBuf, offset, bytesRead);
|
2017-04-12 02:07:38 +02:00
|
|
|
|
}
|
2017-04-11 02:15:18 +02:00
|
|
|
|
|
2017-04-12 07:01:42 +02:00
|
|
|
|
// Do we have more?
|
|
|
|
|
if (processed < bytesRead)
|
2017-03-20 08:11:22 +01:00
|
|
|
|
{
|
|
|
|
|
// Finalize the cipher
|
2022-05-23 03:52:04 +02:00
|
|
|
|
var finalProcessed = _Cipher.TransformFinalBlock(readBuf, processed + offset, bytesRead);
|
2022-05-22 03:55:22 +02:00
|
|
|
|
if (finalProcessed > 0)
|
|
|
|
|
processed += finalProcessed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return processed;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Read(Span<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null && CanRead)
|
|
|
|
|
{
|
|
|
|
|
int processed = 0;
|
|
|
|
|
|
|
|
|
|
// Read the data from the stream
|
|
|
|
|
int bytesRead = _Inner.Read(buffer);
|
|
|
|
|
if (bytesRead > 0)
|
|
|
|
|
{
|
|
|
|
|
// Process the read buffer
|
|
|
|
|
processed = _Cipher.TransformBlock(buffer, 0, bytesRead);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do we have more?
|
|
|
|
|
if (processed < bytesRead)
|
|
|
|
|
{
|
|
|
|
|
// Finalize the cipher
|
|
|
|
|
var finalProcessed = _Cipher.TransformFinalBlock(buffer, processed, bytesRead);
|
|
|
|
|
if (finalProcessed > 0)
|
|
|
|
|
processed += finalProcessed;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-12 07:01:42 +02:00
|
|
|
|
return processed;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null && CanWrite)
|
|
|
|
|
{
|
|
|
|
|
// Process the cipher
|
2022-05-26 08:30:14 +02:00
|
|
|
|
Span<byte> output = buffer;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
|
2017-04-12 09:10:39 +02:00
|
|
|
|
// Process the buffer
|
2022-05-26 08:30:14 +02:00
|
|
|
|
int processed = _Cipher.TransformBlock(output, offset, count);
|
2017-04-12 09:10:39 +02:00
|
|
|
|
|
|
|
|
|
// Do we have more?
|
|
|
|
|
if (processed < count)
|
|
|
|
|
{
|
|
|
|
|
// Finalize the cipher
|
2022-05-26 08:30:14 +02:00
|
|
|
|
var finalProcessed = _Cipher.TransformFinalBlock(output, processed + offset, count);
|
2022-05-22 03:55:22 +02:00
|
|
|
|
if (finalProcessed > 0)
|
|
|
|
|
processed += finalProcessed;
|
2017-04-12 09:10:39 +02:00
|
|
|
|
}
|
2022-05-26 08:30:14 +02:00
|
|
|
|
_Inner.Write(output);
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanRead
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
return _Inner.CanRead;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanSeek
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
return _Inner.CanSeek;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
return _Inner.CanWrite;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long Length
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
return _Inner.Length;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
return _Inner.Position;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
_Inner.Position = value;
|
2017-04-12 07:01:42 +02:00
|
|
|
|
|
|
|
|
|
// Sync the counter
|
|
|
|
|
SyncCounter();
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Flush()
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
_Inner.Flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
2017-04-12 07:01:42 +02:00
|
|
|
|
long newPos = _Inner.Seek(offset, origin);
|
|
|
|
|
|
|
|
|
|
// Sync the counter
|
|
|
|
|
SyncCounter();
|
|
|
|
|
|
|
|
|
|
return newPos;
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
{
|
|
|
|
|
if (_Inner != null)
|
|
|
|
|
{
|
|
|
|
|
_Inner.SetLength(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-12 07:01:42 +02:00
|
|
|
|
|
2022-05-22 03:55:22 +02:00
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
_Inner.Dispose();
|
2022-05-28 08:03:57 +02:00
|
|
|
|
_Cipher.Dispose();
|
2022-05-22 03:55:22 +02:00
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-12 07:01:42 +02:00
|
|
|
|
private void SyncCounter()
|
|
|
|
|
{
|
|
|
|
|
if (_Cipher != null)
|
|
|
|
|
{
|
|
|
|
|
// Calculate the counter iterations and position needed
|
|
|
|
|
int iterations = (int)Math.Floor(_Inner.Position / (decimal)_Cipher.InputBlockSize);
|
|
|
|
|
int counterPos = (int)(_Inner.Position % _Cipher.InputBlockSize);
|
|
|
|
|
|
|
|
|
|
// Are we out of sync with the cipher?
|
2017-04-12 09:10:39 +02:00
|
|
|
|
if (_Cipher.Iterations != iterations + 1 || _Cipher.CounterPosition != counterPos)
|
2017-04-12 07:01:42 +02:00
|
|
|
|
{
|
|
|
|
|
// Reset the current counter
|
|
|
|
|
_Cipher.ResetCounter();
|
|
|
|
|
|
|
|
|
|
// Iterate the counter to the current position
|
|
|
|
|
for (int i = 0; i < iterations; i++)
|
|
|
|
|
{
|
|
|
|
|
_Cipher.IncrementCounter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Encrypt the counter
|
|
|
|
|
_Cipher.EncryptCounter();
|
|
|
|
|
|
|
|
|
|
// Set the current position of the counter
|
|
|
|
|
_Cipher.CounterPosition = counterPos;
|
|
|
|
|
|
|
|
|
|
// Increment the counter for the next time
|
|
|
|
|
_Cipher.IncrementCounter();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-20 08:11:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|