summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-02 10:00:43 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-02 10:00:43 +0700
commit1471706fca485e633330087712348008dc7408bd (patch)
tree301c6e7b7069dd6a9004868fe3c6cb0eacda8e77
parentOverhaul Asn1.Cmp (diff)
downloadBouncyCastle.NET-ed25519-1471706fca485e633330087712348008dc7408bd.tar.xz
Misc. cipher cleanup
-rw-r--r--crypto/src/bcpg/BcpgInputStream.cs12
-rw-r--r--crypto/src/crypto/BufferedAeadBlockCipher.cs10
-rw-r--r--crypto/src/crypto/BufferedAeadCipher.cs5
-rw-r--r--crypto/src/crypto/BufferedStreamCipher.cs5
-rw-r--r--crypto/src/crypto/engines/RC4Engine.cs7
-rw-r--r--crypto/src/crypto/modes/CcmBlockCipher.cs10
-rw-r--r--crypto/src/crypto/modes/EAXBlockCipher.cs26
7 files changed, 21 insertions, 54 deletions
diff --git a/crypto/src/bcpg/BcpgInputStream.cs b/crypto/src/bcpg/BcpgInputStream.cs
index 44d2f9c02..03aa717d3 100644
--- a/crypto/src/bcpg/BcpgInputStream.cs
+++ b/crypto/src/bcpg/BcpgInputStream.cs
@@ -1,12 +1,11 @@
 using System;
 using System.IO;
 
-using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.Utilities.IO;
 
 namespace Org.BouncyCastle.Bcpg
 {
-	/// <remarks>Reader for PGP objects.</remarks>
+    /// <remarks>Reader for PGP objects.</remarks>
     public class BcpgInputStream
         : BaseInputStream
     {
@@ -14,13 +13,10 @@ namespace Org.BouncyCastle.Bcpg
         private bool next = false;
         private int nextB;
 
-        internal static BcpgInputStream Wrap(
-			Stream inStr)
+        internal static BcpgInputStream Wrap(Stream inStr)
         {
-            if (inStr is BcpgInputStream)
-            {
-                return (BcpgInputStream) inStr;
-            }
+            if (inStr is BcpgInputStream bcpg)
+                return bcpg;
 
             return new BcpgInputStream(inStr);
         }
diff --git a/crypto/src/crypto/BufferedAeadBlockCipher.cs b/crypto/src/crypto/BufferedAeadBlockCipher.cs
index f822e393e..b10cd25fe 100644
--- a/crypto/src/crypto/BufferedAeadBlockCipher.cs
+++ b/crypto/src/crypto/BufferedAeadBlockCipher.cs
@@ -14,14 +14,10 @@ namespace Org.BouncyCastle.Crypto
 	{
 		private readonly IAeadBlockCipher cipher;
 
-		public BufferedAeadBlockCipher(
-			IAeadBlockCipher cipher)
+		public BufferedAeadBlockCipher(IAeadBlockCipher cipher)
 		{
-			if (cipher == null)
-				throw new ArgumentNullException("cipher");
-
-			this.cipher = cipher;
-		}
+            this.cipher = cipher ?? throw new ArgumentNullException(nameof(cipher));
+        }
 
 		public override string AlgorithmName
 		{
diff --git a/crypto/src/crypto/BufferedAeadCipher.cs b/crypto/src/crypto/BufferedAeadCipher.cs
index 05bf6e25b..22f65c628 100644
--- a/crypto/src/crypto/BufferedAeadCipher.cs
+++ b/crypto/src/crypto/BufferedAeadCipher.cs
@@ -16,10 +16,7 @@ namespace Org.BouncyCastle.Crypto
 
         public BufferedAeadCipher(IAeadCipher cipher)
         {
-            if (cipher == null)
-                throw new ArgumentNullException("cipher");
-
-            this.cipher = cipher;
+            this.cipher = cipher ?? throw new ArgumentNullException(nameof(cipher));
         }
 
         public override string AlgorithmName
diff --git a/crypto/src/crypto/BufferedStreamCipher.cs b/crypto/src/crypto/BufferedStreamCipher.cs
index 8ee41c1e5..6ae51f47d 100644
--- a/crypto/src/crypto/BufferedStreamCipher.cs
+++ b/crypto/src/crypto/BufferedStreamCipher.cs
@@ -11,10 +11,7 @@ namespace Org.BouncyCastle.Crypto
 
 		public BufferedStreamCipher(IStreamCipher cipher)
 		{
-			if (cipher == null)
-				throw new ArgumentNullException("cipher");
-
-			this.m_cipher = cipher;
+			m_cipher = cipher ?? throw new ArgumentNullException(nameof(cipher));
 		}
 
 		public override string AlgorithmName
diff --git a/crypto/src/crypto/engines/RC4Engine.cs b/crypto/src/crypto/engines/RC4Engine.cs
index 5ee07c766..fe9594e10 100644
--- a/crypto/src/crypto/engines/RC4Engine.cs
+++ b/crypto/src/crypto/engines/RC4Engine.cs
@@ -68,12 +68,7 @@ namespace Org.BouncyCastle.Crypto.Engines
             return (byte)(input ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
         }
 
-        public virtual void ProcessBytes(
-            byte[]	input,
-            int		inOff,
-            int		length,
-            byte[]	output,
-            int		outOff)
+        public virtual void ProcessBytes(byte[]	input, int inOff, int length, byte[] output, int outOff)
         {
             Check.DataLength(input, inOff, length, "input buffer too short");
             Check.OutputLength(output, outOff, length, "output buffer too short");
diff --git a/crypto/src/crypto/modes/CcmBlockCipher.cs b/crypto/src/crypto/modes/CcmBlockCipher.cs
index e002c1f71..c2d3e7e76 100644
--- a/crypto/src/crypto/modes/CcmBlockCipher.cs
+++ b/crypto/src/crypto/modes/CcmBlockCipher.cs
@@ -275,7 +275,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             iv[0] = (byte)((q - 1) & 0x7);
             nonce.CopyTo(iv, 1);
 
-            IBlockCipher ctrCipher = new SicBlockCipher(cipher);
+            var ctrCipher = new SicBlockCipher(cipher);
             ctrCipher.Init(forEncryption, new ParametersWithIV(keyParam, iv));
 
             int outputLen;
@@ -375,7 +375,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             iv[0] = (byte)((q - 1) & 0x7);
             nonce.CopyTo(iv, 1);
 
-            IBlockCipher ctrCipher = new SicBlockCipher(cipher);
+            var ctrCipher = new SicBlockCipher(cipher);
             ctrCipher.Init(forEncryption, new ParametersWithIV(keyParam, iv));
 
             int outputLen;
@@ -452,8 +452,7 @@ namespace Org.BouncyCastle.Crypto.Modes
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
             return CalculateMac(data.AsSpan(dataOff, dataLen), macBlock);
 #else
-            IMac cMac = new CbcBlockCipherMac(cipher, macSize * 8);
-
+            var cMac = new CbcBlockCipherMac(cipher, macSize * 8);
             cMac.Init(keyParam);
 
             //
@@ -544,8 +543,7 @@ namespace Org.BouncyCastle.Crypto.Modes
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         private int CalculateMac(ReadOnlySpan<byte> data, Span<byte> macBlock)
         {
-            IMac cMac = new CbcBlockCipherMac(cipher, macSize * 8);
-
+            var cMac = new CbcBlockCipherMac(cipher, macSize * 8);
             cMac.Init(keyParam);
 
             //
diff --git a/crypto/src/crypto/modes/EAXBlockCipher.cs b/crypto/src/crypto/modes/EAXBlockCipher.cs
index 770c432e2..3b95cd7f2 100644
--- a/crypto/src/crypto/modes/EAXBlockCipher.cs
+++ b/crypto/src/crypto/modes/EAXBlockCipher.cs
@@ -115,9 +115,7 @@ namespace Org.BouncyCastle.Crypto.Modes
         private void InitCipher()
         {
             if (cipherInitialized)
-            {
                 return;
-            }
 
             cipherInitialized = true;
 
@@ -173,9 +171,8 @@ namespace Org.BouncyCastle.Crypto.Modes
         public virtual void ProcessAadByte(byte input)
         {
             if (cipherInitialized)
-            {
                 throw new InvalidOperationException("AAD data cannot be added after encryption/decryption processing has begun.");
-            }
+
             mac.Update(input);
         }
 
@@ -219,11 +216,11 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         public virtual int ProcessBytes(byte[] inBytes, int inOff, int len, byte[] outBytes, int outOff)
         {
-            InitCipher();
-
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
 			return ProcessBytes(inBytes.AsSpan(inOff, len), Spans.FromNullable(outBytes, outOff));
 #else
+            InitCipher();
+
             int resultLen = 0;
 
 			for (int i = 0; i != len; i++)
@@ -233,7 +230,7 @@ namespace Org.BouncyCastle.Crypto.Modes
 
             return resultLen;
 #endif
-		}
+        }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public virtual int ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
@@ -379,8 +376,7 @@ namespace Org.BouncyCastle.Crypto.Modes
 			return mac;
 		}
 
-        public virtual int GetUpdateOutputSize(
-			int len)
+        public virtual int GetUpdateOutputSize(int len)
 		{
             int totalData = len + bufOff;
             if (!forEncryption)
@@ -394,8 +390,7 @@ namespace Org.BouncyCastle.Crypto.Modes
             return totalData - totalData % blockSize;
         }
 
-		public virtual int GetOutputSize(
-			int len)
+		public virtual int GetOutputSize(int len)
 		{
             int totalData = len + bufOff;
 
@@ -489,14 +484,7 @@ namespace Org.BouncyCastle.Crypto.Modes
 
         private bool VerifyMac(byte[] mac, int off)
 		{
-            int nonEqual = 0;
-
-            for (int i = 0; i < macSize; i++)
-            {
-                nonEqual |= (macBlock[i] ^ mac[off + i]);
-            }
-
-            return nonEqual == 0;
+			return Arrays.FixedTimeEquals(macSize, mac, off, macBlock, 0);
 		}
 	}
 }