dont init cipher when using native

This commit is contained in:
Robin Rolf 2024-03-18 14:05:20 +00:00
parent 4de68fe8f8
commit c529ef5030

View File

@ -351,21 +351,9 @@ private ArraySegment<byte> Encrypt(ArraySegment<byte> plaintext)
} }
// Need to make the nonce unique again before encrypting another message // Need to make the nonce unique again before encrypting another message
UpdateNonce(); UpdateNonce();
// Re-initialize the cipher with our cached parameters int outSize = plaintext.Count + MacSizeBytes;
Cipher.Init(true, _cipherParametersEncrypt);
// Calculate the expected output size, this should always be input size + mac size
int outSize = Cipher.GetOutputSize(plaintext.Count);
#if UNITY_EDITOR
// expecting the outSize to be input size + MacSize
if (outSize != plaintext.Count + MacSizeBytes)
{
throw new Exception($"Encrypt: Unexpected output size (Expected {plaintext.Count + MacSizeBytes}, got {outSize}");
}
#endif
// Resize the static buffer to fit // Resize the static buffer to fit
EnsureSize(ref _tmpCryptBuffer, outSize); EnsureSize(ref _tmpCryptBuffer, outSize);
if (AesGCMEncryptionNative.IsSupported) if (AesGCMEncryptionNative.IsSupported)
{ {
ArraySegment<byte> nativeRes = AesGCMEncryptionNative.Encrypt(_keyRaw, _nonce, plaintext, new ArraySegment<byte>(_tmpCryptBuffer)); ArraySegment<byte> nativeRes = AesGCMEncryptionNative.Encrypt(_keyRaw, _nonce, plaintext, new ArraySegment<byte>(_tmpCryptBuffer));
@ -377,6 +365,20 @@ private ArraySegment<byte> Encrypt(ArraySegment<byte> plaintext)
return nativeRes; return nativeRes;
} }
// Re-initialize the cipher with our cached parameters
Cipher.Init(true, _cipherParametersEncrypt);
#if UNITY_EDITOR
// Sanity check!
// Calculate the expected output size, this should always be input size + mac size
int calcOutSize = Cipher.GetOutputSize(plaintext.Count);
// expecting the outSize to be input size + MacSize
if (calcOutSize != outSize)
{
throw new Exception($"Encrypt: Unexpected output size (Expected {outSize}, got {calcOutSize}");
}
#endif
int resultLen; int resultLen;
try try
{ {
@ -411,18 +413,9 @@ private ArraySegment<byte> Decrypt(ArraySegment<byte> ciphertext)
// Invalid // Invalid
return new ArraySegment<byte>(); return new ArraySegment<byte>();
} }
// Re-initialize the cipher with our cached parameters
Cipher.Init(false, _cipherParametersDecrypt);
// Calculate the expected output size, this should always be input size - mac size
int outSize = Cipher.GetOutputSize(ciphertext.Count); int outSize = ciphertext.Count - MacSizeBytes;
#if UNITY_EDITOR
// expecting the outSize to be input size - MacSize
if (outSize != ciphertext.Count - MacSizeBytes)
{
throw new Exception($"Decrypt: Unexpected output size (Expected {ciphertext.Count - MacSizeBytes}, got {outSize}");
}
#endif
// Resize the static buffer to fit // Resize the static buffer to fit
EnsureSize(ref _tmpCryptBuffer, outSize); EnsureSize(ref _tmpCryptBuffer, outSize);
if (AesGCMEncryptionNative.IsSupported) if (AesGCMEncryptionNative.IsSupported)
@ -435,6 +428,21 @@ private ArraySegment<byte> Decrypt(ArraySegment<byte> ciphertext)
} }
return nativeRes; return nativeRes;
} }
// Re-initialize the cipher with our cached parameters
Cipher.Init(false, _cipherParametersDecrypt);
#if UNITY_EDITOR
// Sanity check!
// Calculate the expected output size, this should always be input size - mac size
int calcOutSize = Cipher.GetOutputSize(ciphertext.Count);
// expecting the outSize to be input size - MacSize
if (outSize != calcOutSize)
{
throw new Exception($"Decrypt: Unexpected output size (Expected {ciphertext.Count - MacSizeBytes}, got {outSize}");
}
#endif
int resultLen; int resultLen;
try try
{ {