Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow EtM_DecryptTransform block level seeking via SetCurrentChunkNumber() #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions EtM_Transforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public class EtM_DecryptTransform : ICryptoTransform
byte[] key;
uint currentChunkNumber;
ArraySegment<byte>? salt;
ArraySegment<byte>? saltWithPrepend;

public uint CurrentChunkNumber { get { return this.currentChunkNumber; } }

Expand All @@ -140,6 +141,14 @@ public EtM_DecryptTransform(byte[] key, ArraySegment<byte>? salt = null, bool au
this.IsAuthenticateOnly = authenticateOnly;
}

public void SetCurrentChunkNumber(uint chunk)
{
if (chunk > EtM_Transform_Constants.INITIAL_CHUNK_NUMBER && !saltWithPrepend.HasValue)
throw new Exception("First chunk must be read first.");

this.currentChunkNumber = chunk;
}

public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
int partialBlockSize = inputCount % EtM_Transform_Constants.OUTPUT_BLOCK_SIZE;
Expand All @@ -154,6 +163,8 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
var authenticateOnly = this.IsAuthenticateOnly;
for (; i < fullBlockSize; i += EtM_Transform_Constants.OUTPUT_BLOCK_SIZE, j += EtM_Transform_Constants.INPUT_BLOCK_SIZE)
{
var isFirstChunk = this.currentChunkNumber == EtM_Transform_Constants.INITIAL_CHUNK_NUMBER;

var outputSegment = new ArraySegment<byte>?(new ArraySegment<byte>(outputBuffer, outputOffset + j, EtM_Transform_Constants.INPUT_BLOCK_SIZE));
var cipherText = new ArraySegment<byte>(inputBuffer, inputOffset + i, EtM_Transform_Constants.OUTPUT_BLOCK_SIZE);

Expand All @@ -162,7 +173,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
if (!EtM_CTR.Authenticate(
masterKey: this.key,
ciphertext: cipherText,
salt: this.salt,
salt: isFirstChunk ? this.salt : this.saltWithPrepend,
counter: this.currentChunkNumber))
outputSegment = null;
}
Expand All @@ -172,8 +183,9 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
masterKey: this.key,
ciphertext: cipherText,
outputSegment: ref outputSegment,
salt: this.salt,
salt: isFirstChunk ? this.salt : this.saltWithPrepend,
counter: this.currentChunkNumber);

}

if (outputSegment == null)
Expand All @@ -182,9 +194,11 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
throw new CryptographicException("Decryption failed for block " + this.currentChunkNumber.ToString() + ".");
}

if (this.currentChunkNumber == EtM_Transform_Constants.INITIAL_CHUNK_NUMBER)
if (isFirstChunk && !saltWithPrepend.HasValue)
{
EtM_EncryptTransform.PrependSaltWith1stBlockContext(ref this.salt, inputBuffer, inputOffset);
if (salt.HasValue)
saltWithPrepend = new ArraySegment<byte>(salt.Value.Array);
EtM_EncryptTransform.PrependSaltWith1stBlockContext(ref this.saltWithPrepend, inputBuffer, inputOffset);
}

checked { ++this.currentChunkNumber; }
Expand All @@ -204,6 +218,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input

byte[] outputBuffer = null;
var cipherText = new ArraySegment<byte>(inputBuffer, inputOffset, inputCount);
var isFirstChunk = this.currentChunkNumber == EtM_Transform_Constants.INITIAL_CHUNK_NUMBER;

if (this.IsAuthenticateOnly)
{
Expand All @@ -219,10 +234,10 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
outputBuffer = EtM_CTR.Decrypt(
masterKey: this.key,
ciphertext: cipherText,
salt: this.salt,
salt: isFirstChunk ? this.salt : this.saltWithPrepend,
counter: this.currentChunkNumber);
}
this.Dispose();

if (outputBuffer == null)
throw new CryptographicException("Decryption failed for block " + this.currentChunkNumber.ToString() + ".");

Expand Down