From 53efac102bacbbe0f7665226452337431a8a733e Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 17 Feb 2023 13:26:09 +0700 Subject: AEAD refactoring --- crypto/src/crypto/engines/AsconEngine.cs | 6 +++--- crypto/src/crypto/modes/ChaCha20Poly1305.cs | 14 +++++++++----- crypto/src/crypto/modes/GCMBlockCipher.cs | 17 +++++++++++------ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/crypto/src/crypto/engines/AsconEngine.cs b/crypto/src/crypto/engines/AsconEngine.cs index aff8499dc..870d0e57f 100644 --- a/crypto/src/crypto/engines/AsconEngine.cs +++ b/crypto/src/crypto/engines/AsconEngine.cs @@ -614,7 +614,7 @@ namespace Org.BouncyCastle.Crypto.Engines case State.EncFinal: throw new InvalidOperationException(AlgorithmName + " cannot be reused for encryption"); default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } } @@ -637,7 +637,7 @@ namespace Org.BouncyCastle.Crypto.Engines case State.EncFinal: throw new InvalidOperationException(AlgorithmName + " cannot be reused for encryption"); default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } } @@ -1027,7 +1027,7 @@ namespace Org.BouncyCastle.Crypto.Engines m_state = State.EncFinal; return; default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } ascon_aeadinit(); diff --git a/crypto/src/crypto/modes/ChaCha20Poly1305.cs b/crypto/src/crypto/modes/ChaCha20Poly1305.cs index 6330d2348..dc56f4184 100644 --- a/crypto/src/crypto/modes/ChaCha20Poly1305.cs +++ b/crypto/src/crypto/modes/ChaCha20Poly1305.cs @@ -708,9 +708,9 @@ namespace Org.BouncyCastle.Crypto.Modes case State.EncAad: break; case State.EncFinal: - throw new InvalidOperationException("ChaCha20Poly1305 cannot be reused for encryption"); + throw new InvalidOperationException(AlgorithmName + " cannot be reused for encryption"); default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } } @@ -730,9 +730,9 @@ namespace Org.BouncyCastle.Crypto.Modes case State.EncData: break; case State.EncFinal: - throw new InvalidOperationException("ChaCha20Poly1305 cannot be reused for encryption"); + throw new InvalidOperationException(AlgorithmName + " cannot be reused for encryption"); default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } } @@ -886,7 +886,7 @@ namespace Org.BouncyCastle.Crypto.Modes this.mState = State.EncFinal; return; default: - throw new InvalidOperationException(); + throw new InvalidOperationException(AlgorithmName + " needs to be initialized"); } if (resetCipher) @@ -898,7 +898,11 @@ namespace Org.BouncyCastle.Crypto.Modes if (null != mInitialAad) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + ProcessAadBytes(mInitialAad); +#else ProcessAadBytes(mInitialAad, 0, mInitialAad.Length); +#endif } } } diff --git a/crypto/src/crypto/modes/GCMBlockCipher.cs b/crypto/src/crypto/modes/GCMBlockCipher.cs index 05bb7fc21..92e4de4a9 100644 --- a/crypto/src/crypto/modes/GCMBlockCipher.cs +++ b/crypto/src/crypto/modes/GCMBlockCipher.cs @@ -234,7 +234,11 @@ namespace Org.BouncyCastle.Crypto.Modes if (initialAssociatedText != null) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + ProcessAadBytes(initialAssociatedText); +#else ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length); +#endif } } @@ -916,12 +920,13 @@ namespace Org.BouncyCastle.Crypto.Modes { initialised = false; } - else + else if (initialAssociatedText != null) { - if (initialAssociatedText != null) - { - ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length); - } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + ProcessAadBytes(initialAssociatedText); +#else + ProcessAadBytes(initialAssociatedText, 0, initialAssociatedText.Length); +#endif } } @@ -1728,7 +1733,7 @@ namespace Org.BouncyCastle.Crypto.Modes if (forEncryption) throw new InvalidOperationException("GCM cipher cannot be reused for encryption"); - throw new InvalidOperationException("GCM cipher needs to be initialised"); + throw new InvalidOperationException("GCM cipher needs to be initialized"); } } } -- cgit 1.4.1