Skip to content

Commit c529ef5

Browse files
committed
dont init cipher when using native
1 parent 4de68fe commit c529ef5

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

Assets/Mirror/Transports/Encryption/EncryptedConnection.cs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,9 @@ private ArraySegment<byte> Encrypt(ArraySegment<byte> plaintext)
351351
}
352352
// Need to make the nonce unique again before encrypting another message
353353
UpdateNonce();
354-
// Re-initialize the cipher with our cached parameters
355-
Cipher.Init(true, _cipherParametersEncrypt);
356-
357-
// Calculate the expected output size, this should always be input size + mac size
358-
int outSize = Cipher.GetOutputSize(plaintext.Count);
359-
#if UNITY_EDITOR
360-
// expecting the outSize to be input size + MacSize
361-
if (outSize != plaintext.Count + MacSizeBytes)
362-
{
363-
throw new Exception($"Encrypt: Unexpected output size (Expected {plaintext.Count + MacSizeBytes}, got {outSize}");
364-
}
365-
#endif
354+
int outSize = plaintext.Count + MacSizeBytes;
366355
// Resize the static buffer to fit
367356
EnsureSize(ref _tmpCryptBuffer, outSize);
368-
369357
if (AesGCMEncryptionNative.IsSupported)
370358
{
371359
ArraySegment<byte> nativeRes = AesGCMEncryptionNative.Encrypt(_keyRaw, _nonce, plaintext, new ArraySegment<byte>(_tmpCryptBuffer));
@@ -377,6 +365,20 @@ private ArraySegment<byte> Encrypt(ArraySegment<byte> plaintext)
377365
return nativeRes;
378366
}
379367

368+
// Re-initialize the cipher with our cached parameters
369+
Cipher.Init(true, _cipherParametersEncrypt);
370+
371+
#if UNITY_EDITOR
372+
// Sanity check!
373+
// Calculate the expected output size, this should always be input size + mac size
374+
int calcOutSize = Cipher.GetOutputSize(plaintext.Count);
375+
// expecting the outSize to be input size + MacSize
376+
if (calcOutSize != outSize)
377+
{
378+
throw new Exception($"Encrypt: Unexpected output size (Expected {outSize}, got {calcOutSize}");
379+
}
380+
#endif
381+
380382
int resultLen;
381383
try
382384
{
@@ -411,18 +413,9 @@ private ArraySegment<byte> Decrypt(ArraySegment<byte> ciphertext)
411413
// Invalid
412414
return new ArraySegment<byte>();
413415
}
414-
// Re-initialize the cipher with our cached parameters
415-
Cipher.Init(false, _cipherParametersDecrypt);
416416

417-
// Calculate the expected output size, this should always be input size - mac size
418-
int outSize = Cipher.GetOutputSize(ciphertext.Count);
419-
#if UNITY_EDITOR
420-
// expecting the outSize to be input size - MacSize
421-
if (outSize != ciphertext.Count - MacSizeBytes)
422-
{
423-
throw new Exception($"Decrypt: Unexpected output size (Expected {ciphertext.Count - MacSizeBytes}, got {outSize}");
424-
}
425-
#endif
417+
418+
int outSize = ciphertext.Count - MacSizeBytes;
426419
// Resize the static buffer to fit
427420
EnsureSize(ref _tmpCryptBuffer, outSize);
428421
if (AesGCMEncryptionNative.IsSupported)
@@ -435,6 +428,21 @@ private ArraySegment<byte> Decrypt(ArraySegment<byte> ciphertext)
435428
}
436429
return nativeRes;
437430
}
431+
432+
// Re-initialize the cipher with our cached parameters
433+
Cipher.Init(false, _cipherParametersDecrypt);
434+
435+
#if UNITY_EDITOR
436+
// Sanity check!
437+
// Calculate the expected output size, this should always be input size - mac size
438+
int calcOutSize = Cipher.GetOutputSize(ciphertext.Count);
439+
// expecting the outSize to be input size - MacSize
440+
if (outSize != calcOutSize)
441+
{
442+
throw new Exception($"Decrypt: Unexpected output size (Expected {ciphertext.Count - MacSizeBytes}, got {outSize}");
443+
}
444+
#endif
445+
438446
int resultLen;
439447
try
440448
{

0 commit comments

Comments
 (0)