// Copyright (c) 2010 Gareth Lennox (garethl@dwakn.com)
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of Gareth Lennox nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System.IO;
using System.Security.Cryptography;
using libhac.XTSSharp;
namespace libhac
{
///
/// Xts sector-based
///
public class AesCtrStream : SectorStream
{
///
/// The default sector size
///
public const int DefaultSectorSize = 16;
private readonly long _counterOffset;
private readonly byte[] _tempBuffer;
private readonly Aes _aes;
private CounterModeCryptoTransform _decryptor;
///
/// Creates a new stream
///
/// The base stream
/// The decryption key
/// Offset to add to the counter
public AesCtrStream(Stream baseStream, byte[] key, long counterOffset = 0)
: this(baseStream, key, 0, baseStream.Length, counterOffset) { }
///
/// Creates a new stream
///
/// The base stream
/// The decryption key
/// Offset to start at in the input stream
/// The length of the created stream
/// Offset to add to the counter
public AesCtrStream(Stream baseStream, byte[] key, long offset, long length, long counterOffset)
: base(baseStream, 0x10, offset)
{
_counterOffset = counterOffset;
Length = length;
_tempBuffer = new byte[0x10];
_aes = Aes.Create();
if (_aes == null) throw new CryptographicException("Unable to create AES object");
_aes.Key = key;
_aes.Mode = CipherMode.ECB;
_aes.Padding = PaddingMode.None;
_decryptor = CreateDecryptor();
}
private CounterModeCryptoTransform CreateDecryptor()
{
var dec = new CounterModeCryptoTransform(_aes, _aes.Key, new byte[0x10]);
dec.UpdateCounter(_counterOffset + Position);
return dec;
}
///
/// Releases the unmanaged resources used by the and optionally releases the managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_decryptor?.Dispose();
}
public override void Flush()
{
throw new System.NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value)
{
throw new System.NotImplementedException();
}
///
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
///
/// An array of bytes. This method copies bytes from to the current stream.
/// The zero-based byte offset in at which to begin copying bytes to the current stream.
/// The number of bytes to be written to the current stream.
public override void Write(byte[] buffer, int offset, int count)
{
throw new System.NotImplementedException();
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length { get; }
public override long Position
{
get => base.Position;
set
{
base.Position = value;
_decryptor.UpdateCounter(_counterOffset + base.Position);
}
}
///
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
///
/// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
/// An array of bytes. When this method returns, the buffer contains the specified byte array with the values between and ( + - 1) replaced by the bytes read from the current source.
/// The zero-based byte offset in at which to begin storing the data read from the current stream.
/// The maximum number of bytes to be read from the current stream.
public override int Read(byte[] buffer, int offset, int count)
{
ValidateSize(count);
//read the sector from the base stream
var ret = base.Read(_tempBuffer, 0, count);
if (ret == 0)
return 0;
if (_decryptor == null)
_decryptor = CreateDecryptor();
//decrypt the sector
var retV = _decryptor.TransformBlock(_tempBuffer, 0, ret, buffer, offset);
//Console.WriteLine("Decrypting sector {0} == {1} bytes", currentSector, retV);
return retV;
}
}
}