Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 4 additions & 5 deletions wrappers/csharp/src/DecryptionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public void FlushFinalBlock()
throw new NotSupportedException();
}

_finalBlockTransformed = true;

if (_inputBufferOffset > 0)
{
byte[] outputBuffer = DecryptLastChunk();
Expand All @@ -109,8 +111,6 @@ public void FlushFinalBlock()
}

Array.Clear(_inputBuffer, 0, _inputBuffer.Length);

_finalBlockTransformed = true;
}

public override void Flush()
Expand Down Expand Up @@ -188,14 +188,13 @@ private byte[] DecryptLastChunk()

long result = Native.OnlineDecryptorLastChunk(_native_ptr, _inputBuffer, (UIntPtr)_inputBufferOffset, aad, UIntPtr.Zero, outputBuffer, (UIntPtr)outputBuffer.Length);

_native_ptr = UIntPtr.Zero;

if (result < 0)
{
Utils.HandleError(result);
}

// Here, the pointer is freed, so let's set it to 0
_native_ptr = UIntPtr.Zero;

return outputBuffer;
}

Expand Down
9 changes: 4 additions & 5 deletions wrappers/csharp/src/EncryptionStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ public void FlushFinalBlock()
throw new NotSupportedException();
}

_finalBlockTransformed = true;

if (_inputBufferOffset > 0)
{
byte[] outputBuffer = EncryptLastChunk();
Expand All @@ -97,8 +99,6 @@ public void FlushFinalBlock()
}

Array.Clear(_inputBuffer, 0, _inputBuffer.Length);

_finalBlockTransformed = true;
}

public override void Flush()
Expand Down Expand Up @@ -175,14 +175,13 @@ private byte[] EncryptLastChunk()

long result = Native.OnlineEncryptorLastChunk(_native_ptr, _inputBuffer, (UIntPtr)_inputBufferOffset, aad, UIntPtr.Zero, outputBuffer, (UIntPtr)outputBuffer.Length);

_native_ptr = UIntPtr.Zero;

if (result < 0)
{
Utils.HandleError(result);
}

// Here, the pointer is freed, so let's set it to 0
_native_ptr = UIntPtr.Zero;

return outputBuffer;
}

Expand Down
36 changes: 36 additions & 0 deletions wrappers/csharp/tests/unit-tests/TestStreams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,41 @@ public void DecryptStream()
Assert.IsTrue(result.Length == 1689);
Assert.IsTrue(Utils.EncodeToBase64String(result) == TestData.Base64TestDataStream);
}

[TestMethod]
public void DecryptStreamTruncatedCiphertext()
{
const int ChunkLength = 8;
const int EncryptedChunkLength = ChunkLength + 16;

byte[] plaintext = new byte[3 * ChunkLength];
byte[] header;
byte[] ciphertext;

using (MemoryStream encrypted = new MemoryStream())
{
using (EncryptionStream ec =
new EncryptionStream(TestData.BytesTestKey, [], ChunkLength, false, 0, encrypted, true))
{
header = ec.GetHeader();

ec.Write(plaintext, 0, plaintext.Length);
ec.FlushFinalBlock();
}

ciphertext = encrypted.ToArray();
}

using MemoryStream ms = new MemoryStream();

Assert.ThrowsException<DevolutionsCryptoException>(() =>
{
using DecryptionStream dc =
new DecryptionStream(TestData.BytesTestKey, [], header, false, ms, true);

dc.Write(ciphertext, 0, ciphertext.Length - EncryptedChunkLength);
dc.FlushFinalBlock();
});
}
}
}
Loading