mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
167 lines
No EOL
7.3 KiB
C#
167 lines
No EOL
7.3 KiB
C#
// 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
|
|
{
|
|
/// <summary>
|
|
/// Xts sector-based
|
|
/// </summary>
|
|
public class AesCtrStream : SectorStream
|
|
{
|
|
/// <summary>
|
|
/// The default sector size
|
|
/// </summary>
|
|
public const int DefaultSectorSize = 16;
|
|
|
|
private readonly long _counterOffset;
|
|
private readonly byte[] _tempBuffer;
|
|
private readonly Aes _aes;
|
|
private CounterModeCryptoTransform _decryptor;
|
|
|
|
/// <summary>
|
|
/// Creates a new stream
|
|
/// </summary>
|
|
/// <param name="baseStream">The base stream</param>
|
|
/// <param name="key">The decryption key</param>
|
|
/// <param name="counterOffset">Offset to add to the counter</param>
|
|
public AesCtrStream(Stream baseStream, byte[] key, long counterOffset = 0)
|
|
: this(baseStream, key, 0, baseStream.Length, counterOffset) { }
|
|
|
|
/// <summary>
|
|
/// Creates a new stream
|
|
/// </summary>
|
|
/// <param name="baseStream">The base stream</param>
|
|
/// <param name="key">The decryption key</param>
|
|
/// <param name="offset">Offset to start at in the input stream</param>
|
|
/// <param name="length">The length of the created stream</param>
|
|
/// <param name="counterOffset">Offset to add to the counter</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream"/> and optionally releases the managed resources.
|
|
/// </summary>
|
|
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
|
/// </summary>
|
|
/// <param name="buffer">An array of bytes. This method copies <paramref name="count"/> bytes from <paramref name="buffer"/> to the current stream.</param>
|
|
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin copying bytes to the current stream.</param>
|
|
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
|
/// </summary>
|
|
/// <returns>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.</returns>
|
|
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset"/> and (<paramref name="offset"/> + <paramref name="count"/> - 1) replaced by the bytes read from the current source. </param>
|
|
/// <param name="offset">The zero-based byte offset in <paramref name="buffer"/> at which to begin storing the data read from the current stream.</param>
|
|
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
|
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;
|
|
}
|
|
}
|
|
} |