Make certain AES streams seekable

Make AES streams where the initial counter is supplied seekable by storing the initial counter value
This commit is contained in:
Alex Barney 2018-08-31 17:53:57 -05:00
parent 4101c48cf4
commit 3d8d3992eb
2 changed files with 18 additions and 5 deletions

View file

@ -29,11 +29,21 @@ namespace LibHac
/// </summary>
/// <param name="baseStream">The base stream</param>
/// <param name="key">The decryption key</param>
/// <param name="counter">The intial counter</param>
/// <param name="counter">The initial counter</param>
public Aes128CtrStream(Stream baseStream, byte[] key, byte[] counter)
: base(baseStream, BlockSize)
{
_counterOffset = 0;
// Make the stream seekable by remembering the initial counter value
if (counter != null)
{
for (int i = 0; i < 8; i++)
{
_counterOffset |= (long)counter[0xF - i] << (4 + i * 8);
}
}
Length = baseStream.Length;
_tempBuffer = new byte[CryptChunkSize];
@ -70,11 +80,11 @@ namespace LibHac
private void UpdateCounter(long offset)
{
offset >>= 4;
ulong off = (ulong)offset >> 4;
for (uint j = 0; j < 0x8; j++)
{
Counter[0x10 - j - 1] = (byte)(offset & 0xFF);
offset >>= 8;
Counter[0x10 - j - 1] = (byte)(off & 0xFF);
off >>= 8;
}
}

View file

@ -31,7 +31,10 @@ namespace LibHac.Streams
_currentSector = sectorNum;
long startPos = sectorNum * _bufferSize;
if (_baseStream.Position != startPos)
{
_baseStream.Position = startPos;
}
_readBytes = _baseStream.Read(_buffer, 0, _bufferSize);
}