Skip to content

Commit

Permalink
SNOW-1524245 Set initialisation vector length for Gcm encryption to 1…
Browse files Browse the repository at this point in the history
…2 bytes (#1056)
  • Loading branch information
sfc-gh-knozderko authored Nov 8, 2024
1 parent f27eb2a commit 05da00a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class GcmEncryptionProviderTest
{
private const string PlainText = "there is no rose without thorns";
private static readonly byte[] s_plainTextBytes = Encoding.UTF8.GetBytes(PlainText);
private static readonly byte[] s_qsmkBytes = TestDataGenarator.NextBytes(GcmEncryptionProvider.BlockSizeInBytes);
private static readonly byte[] s_qsmkBytes = TestDataGenarator.NextBytes(GcmEncryptionProvider.TagSizeInBytes);
private static readonly string s_qsmk = Convert.ToBase64String(s_qsmkBytes);
private static readonly string s_queryId = Guid.NewGuid().ToString();
private const long SmkId = 1234L;
Expand Down
13 changes: 7 additions & 6 deletions Snowflake.Data/Core/FileTransfer/GcmEncryptionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace Snowflake.Data.Core.FileTransfer
{
internal class GcmEncryptionProvider
{
private const int AesBlockSize = 128;
internal const int BlockSizeInBytes = AesBlockSize / 8;
private const int TagSizeInBits = 128;
internal const int TagSizeInBytes = TagSizeInBits / 8;
private const int InitVectorSizeInBytes = 12;
private const string AesGcmNoPaddingCipher = "AES/GCM/NoPadding";

private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<GcmEncryptionProvider>();
Expand Down Expand Up @@ -57,8 +58,8 @@ public static Stream Encrypt(
int masterKeySize = decodedMasterKey.Length;
s_logger.Debug($"Master key size : {masterKeySize}");

var contentIV = new byte[BlockSizeInBytes];
var keyIV = new byte[BlockSizeInBytes];
var contentIV = new byte[InitVectorSizeInBytes];
var keyIV = new byte[InitVectorSizeInBytes];
var fileKeyBytes = new byte[masterKeySize]; // we choose a random fileKey to encrypt it with qsmk key with GCM
s_random.NextBytes(contentIV);
s_random.NextBytes(keyIV);
Expand Down Expand Up @@ -179,8 +180,8 @@ private static IBufferedCipher BuildAesGcmNoPaddingCipher(bool forEncryption, by
var cipher = CipherUtilities.GetCipher(AesGcmNoPaddingCipher);
KeyParameter keyParameter = new KeyParameter(keyBytes);
var keyParameterAead = aadData == null
? new AeadParameters(keyParameter, AesBlockSize, initialisationVector)
: new AeadParameters(keyParameter, AesBlockSize, initialisationVector, aadData);
? new AeadParameters(keyParameter, TagSizeInBits, initialisationVector)
: new AeadParameters(keyParameter, TagSizeInBits, initialisationVector, aadData);
cipher.Init(forEncryption, keyParameterAead);
return cipher;
}
Expand Down

0 comments on commit 05da00a

Please sign in to comment.