summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-09 20:45:17 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-09 20:45:17 +0700
commit9d4dde60ecdcfabf38eaa98c483b15263e0b99e5 (patch)
treedf27be68a5e95d77c5cdc49b5d154581aa8fa605
parentAdd missing file (diff)
downloadBouncyCastle.NET-ed25519-9d4dde60ecdcfabf38eaa98c483b15263e0b99e5.tar.xz
Add GetMaxResultLength method
-rw-r--r--crypto/src/crmf/PKMacBuilder.cs16
-rw-r--r--crypto/src/crypto/IBlockResult.cs12
-rw-r--r--crypto/src/crypto/SimpleBlockResult.cs25
-rw-r--r--crypto/src/crypto/operators/DefaultSignatureResult.cs16
4 files changed, 32 insertions, 37 deletions
diff --git a/crypto/src/crmf/PKMacBuilder.cs b/crypto/src/crmf/PKMacBuilder.cs
index ae9baa3d0..7261a9daf 100644
--- a/crypto/src/crmf/PKMacBuilder.cs
+++ b/crypto/src/crmf/PKMacBuilder.cs
@@ -60,7 +60,7 @@ namespace Org.BouncyCastle.Crmf
         }
     }
 
-    internal class DefaultPKMacResult
+    internal sealed class DefaultPKMacResult
         : IBlockResult
     {
         private readonly IMac mac;
@@ -77,21 +77,19 @@ namespace Org.BouncyCastle.Crmf
             return res;
         }
 
-        public int Collect(byte[] sig, int sigOff)
+        public int Collect(byte[] buf, int off)
         {
-            byte[] signature = Collect();
-            signature.CopyTo(sig, sigOff);
-            return signature.Length;
+            return mac.DoFinal(buf, off);
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        public int Collect(Span<byte> destination)
+        public int Collect(Span<byte> output)
         {
-            byte[] result = Collect();
-            result.CopyTo(destination);
-            return result.Length;
+            return mac.DoFinal(output);
         }
 #endif
+
+        public int GetMaxResultLength() => mac.GetMacSize();
     }
 
     public class PKMacBuilder
diff --git a/crypto/src/crypto/IBlockResult.cs b/crypto/src/crypto/IBlockResult.cs
index 3ed96a7c1..f3b73e59f 100644
--- a/crypto/src/crypto/IBlockResult.cs
+++ b/crypto/src/crypto/IBlockResult.cs
@@ -18,17 +18,19 @@ namespace Org.BouncyCastle.Crypto
         /// Store the final result of the operation by copying it into the destination array.
         /// </summary>
         /// <returns>The number of bytes copied into destination.</returns>
-        /// <param name="destination">The byte array to copy the result into.</param>
-        /// <param name="offset">The offset into destination to start copying the result at.</param>
-        int Collect(byte[] destination, int offset);
+        /// <param name="buf">The byte array to copy the result into.</param>
+        /// <param name="off">The offset into destination to start copying the result at.</param>
+        int Collect(byte[] buf, int off);
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         /// <summary>
         /// Store the final result of the operation by copying it into the destination span.
         /// </summary>
         /// <returns>The number of bytes copied into destination.</returns>
-        /// <param name="destination">The span to copy the result into.</param>
-        int Collect(Span<byte> destination);
+        /// <param name="output">The span to copy the result into.</param>
+        int Collect(Span<byte> output);
 #endif
+
+        int GetMaxResultLength();
     }
 }
diff --git a/crypto/src/crypto/SimpleBlockResult.cs b/crypto/src/crypto/SimpleBlockResult.cs
index 35432c2b3..3f1a814e8 100644
--- a/crypto/src/crypto/SimpleBlockResult.cs
+++ b/crypto/src/crypto/SimpleBlockResult.cs
@@ -5,7 +5,7 @@ namespace Org.BouncyCastle.Crypto
     /// <summary>
     /// A simple block result object which just carries a byte array.
     /// </summary>
-    public class SimpleBlockResult
+    public sealed class SimpleBlockResult
         : IBlockResult
     {
         private readonly byte[] result;
@@ -20,15 +20,6 @@ namespace Org.BouncyCastle.Crypto
         }
 
         /// <summary>
-        /// Return the number of bytes in the result
-        /// </summary>
-        /// <value>The length of the result in bytes.</value>
-        public int Length
-        {
-            get { return result.Length; }
-        }
-
-        /// <summary>
         /// Return the final result of the operation.
         /// </summary>
         /// <returns>A block of bytes, representing the result of an operation.</returns>
@@ -41,22 +32,24 @@ namespace Org.BouncyCastle.Crypto
         /// Store the final result of the operation by copying it into the destination array.
         /// </summary>
         /// <returns>The number of bytes copied into destination.</returns>
-        /// <param name="destination">The byte array to copy the result into.</param>
-        /// <param name="offset">The offset into destination to start copying the result at.</param>
-        public int Collect(byte[] destination, int offset)
+        /// <param name="buf">The byte array to copy the result into.</param>
+        /// <param name="off">The offset into destination to start copying the result at.</param>
+        public int Collect(byte[] buf, int off)
         {
-            Array.Copy(result, 0, destination, offset, result.Length);
+            Array.Copy(result, 0, buf, off, result.Length);
 
             return result.Length;
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        public int Collect(Span<byte> destination)
+        public int Collect(Span<byte> output)
         {
-            result.CopyTo(destination);
+            result.CopyTo(output);
 
             return result.Length;
         }
 #endif
+
+        public int GetMaxResultLength() => result.Length;
     }
 }
diff --git a/crypto/src/crypto/operators/DefaultSignatureResult.cs b/crypto/src/crypto/operators/DefaultSignatureResult.cs
index a236838d6..cbbc04d20 100644
--- a/crypto/src/crypto/operators/DefaultSignatureResult.cs
+++ b/crypto/src/crypto/operators/DefaultSignatureResult.cs
@@ -2,7 +2,7 @@
 
 namespace Org.BouncyCastle.Crypto.Operators
 {
-    public class DefaultSignatureResult
+    public sealed class DefaultSignatureResult
         : IBlockResult
     {
         private readonly ISigner mSigner;
@@ -17,20 +17,22 @@ namespace Org.BouncyCastle.Crypto.Operators
             return mSigner.GenerateSignature();
         }
 
-        public int Collect(byte[] sig, int sigOff)
+        public int Collect(byte[] buf, int off)
         {
             byte[] signature = Collect();
-            signature.CopyTo(sig, sigOff);
+            signature.CopyTo(buf, off);
             return signature.Length;
         }
 
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
-        public int Collect(Span<byte> destination)
+        public int Collect(Span<byte> output)
         {
-            byte[] result = Collect();
-            result.CopyTo(destination);
-            return result.Length;
+            byte[] signature = Collect();
+            signature.CopyTo(output);
+            return signature.Length;
         }
 #endif
+
+        public int GetMaxResultLength() => mSigner.GetMaxSignatureSize();
     }
 }