From f0004636e1ab9d4dd2b617f8405e264cf8bbaf52 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 17 May 2023 01:52:14 +0700 Subject: Refactoring in Tls.Crypto.Impl.BC --- crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs | 14 +++++++++----- crypto/src/tls/crypto/impl/bc/BcTlsAeadCipherImpl.cs | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs b/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs index dc5672814..3ed6b3365 100644 --- a/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs +++ b/crypto/src/tls/crypto/impl/bc/BcChaCha20Poly1305.cs @@ -4,6 +4,7 @@ using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Macs; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Utilities; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Tls.Crypto.Impl.BC { @@ -76,9 +77,12 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public int DoFinal(byte[] additionalData, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) { - if (additionalData != null) + if (!Arrays.IsNullOrEmpty(additionalData)) { - this.m_additionalDataLength += additionalData.Length; + if (m_additionalDataLength != 0) + throw new InvalidOperationException(); + + m_additionalDataLength = additionalData.Length; UpdateMac(additionalData, 0, additionalData.Length); } @@ -97,13 +101,13 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC m_cipher.Init(m_isEncrypting, new ParametersWithIV(null, nonce)); InitMac(); - if (additionalData == null) + if (Arrays.IsNullOrEmpty(additionalData)) { - this.m_additionalDataLength = 0; + m_additionalDataLength = 0; } else { - this.m_additionalDataLength = additionalData.Length; + m_additionalDataLength = additionalData.Length; UpdateMac(additionalData, 0, additionalData.Length); } } diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsAeadCipherImpl.cs b/crypto/src/tls/crypto/impl/bc/BcTlsAeadCipherImpl.cs index 7dfa737d9..4ffe52b02 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsAeadCipherImpl.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsAeadCipherImpl.cs @@ -3,6 +3,7 @@ using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Tls.Crypto.Impl.BC { @@ -61,7 +62,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public virtual int DoFinal(byte[] additionalData, byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) { - if (additionalData != null) + if (!Arrays.IsNullOrEmpty(additionalData)) { m_cipher.ProcessAadBytes(additionalData, 0, additionalData.Length); } -- cgit 1.4.1