summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-07-27 17:42:58 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-07-27 17:42:58 +0700
commit0dca56adc504522dbc66dd8541de0c8f53644dda (patch)
treebf7751c95d0c5164c5b471e0be0503f32c5efae2 /crypto
parentRefactor CMP PKMAC verification (diff)
downloadBouncyCastle.NET-ed25519-0dca56adc504522dbc66dd8541de0c8f53644dda.tar.xz
Refactoring around stream calculators
Diffstat (limited to '')
-rw-r--r--crypto/src/crmf/PKMacBuilder.cs78
-rw-r--r--crypto/src/crypto/operators/DefaultMacCalculator.cs21
-rw-r--r--crypto/src/crypto/operators/DefaultMacResult.cs27
-rw-r--r--crypto/src/crypto/operators/DefaultSignatureCalculator.cs18
-rw-r--r--crypto/src/crypto/operators/DefaultSignatureResult.cs11
-rw-r--r--crypto/src/crypto/operators/DefaultVerifierCalculator.cs15
-rw-r--r--crypto/src/crypto/operators/DefaultVerifierResult.cs19
7 files changed, 82 insertions, 107 deletions
diff --git a/crypto/src/crmf/PKMacBuilder.cs b/crypto/src/crmf/PKMacBuilder.cs
index 6db80325d..f59ba8f35 100644
--- a/crypto/src/crmf/PKMacBuilder.cs
+++ b/crypto/src/crmf/PKMacBuilder.cs
@@ -8,90 +8,36 @@ using Org.BouncyCastle.Asn1.Oiw;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Crypto;
 using Org.BouncyCastle.Crypto.IO;
+using Org.BouncyCastle.Crypto.Operators;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crmf
 {
-    internal class PKMacStreamCalculator
-        : IStreamCalculator<DefaultPKMacResult>
-    {
-        private readonly MacSink _stream;
-
-        public PKMacStreamCalculator(IMac mac)
-        {
-            _stream = new MacSink(mac);
-        }
-
-        public Stream Stream
-        {
-            get { return _stream; }
-        }
-
-        public DefaultPKMacResult GetResult()
-        {
-            return new DefaultPKMacResult(_stream.Mac);
-        }
-    }
-
-    internal class PKMacFactory
+    internal sealed class PKMacFactory
         : IMacFactory
     {
-        protected readonly PbmParameter parameters;
-        private readonly byte[] key;
+        private readonly KeyParameter m_key;
+        private readonly PbmParameter m_parameters;
 
         public PKMacFactory(byte[] key, PbmParameter parameters)
         {
-            this.key = Arrays.Clone(key);
-            this.parameters = parameters;
+            m_key = new KeyParameter(key);
+            m_parameters = parameters;
         }
 
-        public virtual object AlgorithmDetails
-        {
-            get { return new AlgorithmIdentifier(CmpObjectIdentifiers.passwordBasedMac, parameters); }
-        }
+        public object AlgorithmDetails =>
+            new AlgorithmIdentifier(CmpObjectIdentifiers.passwordBasedMac, m_parameters);
 
-        public virtual IStreamCalculator<IBlockResult> CreateCalculator()
+        public IStreamCalculator<IBlockResult> CreateCalculator()
         {
-            IMac mac = MacUtilities.GetMac(parameters.Mac.Algorithm);
-            mac.Init(new KeyParameter(key));
-            return new PKMacStreamCalculator(mac);
+            IMac mac = MacUtilities.GetMac(m_parameters.Mac.Algorithm);
+            mac.Init(m_key);
+            return new DefaultMacCalculator(mac);
         }
     }
 
-    internal sealed class DefaultPKMacResult
-        : IBlockResult
-    {
-        private readonly IMac mac;
-
-        public DefaultPKMacResult(IMac mac)
-        {
-            this.mac = mac;
-        }
-
-        public byte[] Collect()
-        {
-            byte[] res = new byte[mac.GetMacSize()];
-            mac.DoFinal(res, 0);
-            return res;
-        }
-
-        public int Collect(byte[] buf, int off)
-        {
-            return mac.DoFinal(buf, off);
-        }
-
-#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        public int Collect(Span<byte> output)
-        {
-            return mac.DoFinal(output);
-        }
-#endif
-
-        public int GetMaxResultLength() => mac.GetMacSize();
-    }
-
     public class PKMacBuilder
     {
         private AlgorithmIdentifier owf;
diff --git a/crypto/src/crypto/operators/DefaultMacCalculator.cs b/crypto/src/crypto/operators/DefaultMacCalculator.cs
new file mode 100644
index 000000000..2137bcde5
--- /dev/null
+++ b/crypto/src/crypto/operators/DefaultMacCalculator.cs
@@ -0,0 +1,21 @@
+using System.IO;
+
+using Org.BouncyCastle.Crypto.IO;
+
+namespace Org.BouncyCastle.Crypto.Operators
+{
+    public sealed class DefaultMacCalculator
+        : IStreamCalculator<IBlockResult>
+    {
+        private readonly MacSink m_macSink;
+
+        public DefaultMacCalculator(IMac mac)
+        {
+            m_macSink = new MacSink(mac);
+        }
+
+        public Stream Stream => m_macSink;
+
+        public IBlockResult GetResult() => new DefaultMacResult(m_macSink.Mac);
+    }
+}
diff --git a/crypto/src/crypto/operators/DefaultMacResult.cs b/crypto/src/crypto/operators/DefaultMacResult.cs
new file mode 100644
index 000000000..9a45d6792
--- /dev/null
+++ b/crypto/src/crypto/operators/DefaultMacResult.cs
@@ -0,0 +1,27 @@
+using System;
+
+using Org.BouncyCastle.Security;
+
+namespace Org.BouncyCastle.Crypto.Operators
+{
+    public sealed class DefaultMacResult
+        : IBlockResult
+    {
+        private readonly IMac m_mac;
+
+        public DefaultMacResult(IMac mac)
+        {
+            m_mac = mac;
+        }
+
+        public byte[] Collect() => MacUtilities.DoFinal(m_mac);
+
+        public int Collect(byte[] buf, int off) => m_mac.DoFinal(buf, off);
+
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
+        public int Collect(Span<byte> output) => m_mac.DoFinal(output);
+#endif
+
+        public int GetMaxResultLength() => m_mac.GetMacSize();
+    }
+}
diff --git a/crypto/src/crypto/operators/DefaultSignatureCalculator.cs b/crypto/src/crypto/operators/DefaultSignatureCalculator.cs
index 851662622..0b9f151d3 100644
--- a/crypto/src/crypto/operators/DefaultSignatureCalculator.cs
+++ b/crypto/src/crypto/operators/DefaultSignatureCalculator.cs
@@ -1,28 +1,22 @@
-using System;
-using System.IO;
+using System.IO;
 
 using Org.BouncyCastle.Crypto.IO;
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
+    // TODO[api] sealed
     public class DefaultSignatureCalculator
         : IStreamCalculator<IBlockResult>
     {
-        private readonly SignerSink mSignerSink;
+        private readonly SignerSink m_signerSink;
 
         public DefaultSignatureCalculator(ISigner signer)
         {
-            this.mSignerSink = new SignerSink(signer);
+            m_signerSink = new SignerSink(signer);
         }
 
-        public Stream Stream
-        {
-            get { return mSignerSink; }
-        }
+        public Stream Stream => m_signerSink;
 
-        public IBlockResult GetResult()
-        {
-            return new DefaultSignatureResult(mSignerSink.Signer);
-        }
+        public IBlockResult GetResult() => new DefaultSignatureResult(m_signerSink.Signer);
     }
 }
diff --git a/crypto/src/crypto/operators/DefaultSignatureResult.cs b/crypto/src/crypto/operators/DefaultSignatureResult.cs
index cbbc04d20..3ac66c2b4 100644
--- a/crypto/src/crypto/operators/DefaultSignatureResult.cs
+++ b/crypto/src/crypto/operators/DefaultSignatureResult.cs
@@ -5,17 +5,14 @@ namespace Org.BouncyCastle.Crypto.Operators
     public sealed class DefaultSignatureResult
         : IBlockResult
     {
-        private readonly ISigner mSigner;
+        private readonly ISigner m_signer;
 
         public DefaultSignatureResult(ISigner signer)
         {
-            this.mSigner = signer;
+            m_signer = signer;
         }
 
-        public byte[] Collect()
-        {
-            return mSigner.GenerateSignature();
-        }
+        public byte[] Collect() => m_signer.GenerateSignature();
 
         public int Collect(byte[] buf, int off)
         {
@@ -33,6 +30,6 @@ namespace Org.BouncyCastle.Crypto.Operators
         }
 #endif
 
-        public int GetMaxResultLength() => mSigner.GetMaxSignatureSize();
+        public int GetMaxResultLength() => m_signer.GetMaxSignatureSize();
     }
 }
diff --git a/crypto/src/crypto/operators/DefaultVerifierCalculator.cs b/crypto/src/crypto/operators/DefaultVerifierCalculator.cs
index cbf4e77d6..719a3f9a4 100644
--- a/crypto/src/crypto/operators/DefaultVerifierCalculator.cs
+++ b/crypto/src/crypto/operators/DefaultVerifierCalculator.cs
@@ -5,24 +5,19 @@ using Org.BouncyCastle.Crypto.IO;
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
+    // TODO[api] sealed
     public class DefaultVerifierCalculator
         : IStreamCalculator<IVerifier>
     {
-        private readonly SignerSink mSignerSink;
+        private readonly SignerSink m_signerSink;
 
         public DefaultVerifierCalculator(ISigner signer)
         {
-            this.mSignerSink = new SignerSink(signer);
+            m_signerSink = new SignerSink(signer);
         }
 
-        public Stream Stream
-        {
-            get { return mSignerSink; }
-        }
+        public Stream Stream => m_signerSink;
 
-        public IVerifier GetResult()
-        {
-            return new DefaultVerifierResult(mSignerSink.Signer);
-        }
+        public IVerifier GetResult() => new DefaultVerifierResult(m_signerSink.Signer);
     }
 }
diff --git a/crypto/src/crypto/operators/DefaultVerifierResult.cs b/crypto/src/crypto/operators/DefaultVerifierResult.cs
index fb259c8f8..994020a46 100644
--- a/crypto/src/crypto/operators/DefaultVerifierResult.cs
+++ b/crypto/src/crypto/operators/DefaultVerifierResult.cs
@@ -4,26 +4,21 @@ using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
+    // TODO[api] sealed
     public class DefaultVerifierResult
         : IVerifier
     {
-        private readonly ISigner mSigner;
+        private readonly ISigner m_signer;
 
         public DefaultVerifierResult(ISigner signer)
         {
-            this.mSigner = signer;
+            m_signer = signer;
         }
 
-        public bool IsVerified(byte[] signature)
-        {
-            return mSigner.VerifySignature(signature);
-        }
+        public bool IsVerified(byte[] signature) => m_signer.VerifySignature(signature);
 
-        public bool IsVerified(byte[] sig, int sigOff, int sigLen)
-        {
-            byte[] signature = Arrays.CopyOfRange(sig, sigOff, sigOff + sigLen);
-
-            return IsVerified(signature);
-        }
+        // TODO[api] Use ISigner.VerifySignature(ReadOnlySpan<byte>) when available
+        public bool IsVerified(byte[] sig, int sigOff, int sigLen) =>
+            IsVerified(Arrays.CopyOfRange(sig, sigOff, sigOff + sigLen));
     }
 }