Skip to content

Commit

Permalink
fix bug in decoding, add tests for empty aad
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Oct 22, 2024
1 parent 4f4bc6b commit 1aebc4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
41 changes: 39 additions & 2 deletions Snowflake.Data.Tests/UnitTests/GcmEncryptionProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class GcmEncryptionProviderTest
private const string InvalidAad = "invalid additional information";
private static readonly byte[] s_invalidAadBytes = Encoding.UTF8.GetBytes(InvalidAad);
private static readonly string s_invalidAadBase64 = Convert.ToBase64String(s_invalidAadBytes);
private static readonly string s_emptyAad = string.Empty;
private static readonly byte[] s_emptyAadBytes = Encoding.UTF8.GetBytes(s_emptyAad);
private static readonly string s_emptyAadBase64 = Convert.ToBase64String(s_emptyAadBytes);
private static readonly PutGetEncryptionMaterial s_encryptionMaterial = new PutGetEncryptionMaterial
{
queryStageMasterKey = s_qsmk,
Expand All @@ -48,8 +51,8 @@ public void TestEncryptAndDecryptWithoutAad()
// act
using (var encryptedStream = GcmEncryptionProvider.Encrypt(
s_encryptionMaterial,
encryptionMetadata,
s_fileTransferConfiguration,// this is output parameter
encryptionMetadata, // this is output parameter
s_fileTransferConfiguration,
new MemoryStream(s_plainTextBytes),
null,
null))
Expand All @@ -73,6 +76,40 @@ public void TestEncryptAndDecryptWithoutAad()
}
}

[Test]
public void TestEncryptAndDecryptWithEmptyAad()
{
// arrange
SFEncryptionMetadata encryptionMetadata = new SFEncryptionMetadata();

// act
using (var encryptedStream = GcmEncryptionProvider.Encrypt(
s_encryptionMaterial,
encryptionMetadata, // this is output parameter
s_fileTransferConfiguration,
new MemoryStream(s_plainTextBytes),
s_emptyAadBytes,
s_emptyAadBytes))
{
var encryptedContent = ExtractContentBytes(encryptedStream);

// assert
Assert.NotNull(encryptionMetadata.key);
Assert.NotNull(encryptionMetadata.iv);
Assert.NotNull(encryptionMetadata.matDesc);
Assert.AreEqual(s_emptyAadBase64, encryptionMetadata.keyAad);
Assert.AreEqual(s_emptyAadBase64, encryptionMetadata.aad);

// act
using (var decryptedStream = GcmEncryptionProvider.Decrypt(new MemoryStream(encryptedContent), s_encryptionMaterial, encryptionMetadata, s_fileTransferConfiguration))
{
// assert
var decryptedText = ExtractContent(decryptedStream);
CollectionAssert.AreEqual(s_plainTextBytes, decryptedText);
}
}
}

[Test]
public void TestEncryptAndDecryptWithAad()
{
Expand Down
7 changes: 5 additions & 2 deletions Snowflake.Data/Core/FileTransfer/GcmEncryptionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ private static Stream DecryptContent(Stream inputStream, byte[] fileKeyBytes, by
{
cipherStream.Write(buffer, 0, bytesRead);
}

cipherStream.Flush(); // we cannot close or dispose cipherStream because closing cipherStream would close target stream
contentCipher.DoFinal(); // in case of decrypting we ignore the result which has to be empty
var lastBytes = contentCipher.DoFinal();
if (lastBytes != null && lastBytes.Length > 0)
{
targetStream.Write(lastBytes, 0, lastBytes.Length);
}
return targetStream;
}
catch (Exception)
Expand Down

0 comments on commit 1aebc4b

Please sign in to comment.