summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-11-09 20:42:52 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-11-09 20:42:52 +0700
commit6dc5aeefdc7d52d7bc2fefdcd2095c8f5b80f200 (patch)
tree45f80f9ee64e11f7ede0c1527f84627e56762f7d
parentSpan API for IDsaEncoding (diff)
downloadBouncyCastle.NET-ed25519-6dc5aeefdc7d52d7bc2fefdcd2095c8f5b80f200.tar.xz
Add GetMaxSignatureSize method
-rw-r--r--crypto/src/crypto/ISigner.cs2
-rw-r--r--crypto/src/crypto/signers/DsaDigestSigner.cs4
-rw-r--r--crypto/src/crypto/signers/Ed25519Signer.cs2
-rw-r--r--crypto/src/crypto/signers/Ed25519ctxSigner.cs2
-rw-r--r--crypto/src/crypto/signers/Ed25519phSigner.cs2
-rw-r--r--crypto/src/crypto/signers/Ed448Signer.cs2
-rw-r--r--crypto/src/crypto/signers/Ed448phSigner.cs2
-rw-r--r--crypto/src/crypto/signers/GOST3410DigestSigner.cs2
-rw-r--r--crypto/src/crypto/signers/GenericSigner.cs2
-rw-r--r--crypto/src/crypto/signers/Iso9796d2PssSigner.cs46
-rw-r--r--crypto/src/crypto/signers/Iso9796d2Signer.cs46
-rw-r--r--crypto/src/crypto/signers/PssSigner.cs16
-rw-r--r--crypto/src/crypto/signers/RsaDigestSigner.cs2
-rw-r--r--crypto/src/crypto/signers/SM2Signer.cs48
-rw-r--r--crypto/src/crypto/signers/X931Signer.cs21
15 files changed, 114 insertions, 85 deletions
diff --git a/crypto/src/crypto/ISigner.cs b/crypto/src/crypto/ISigner.cs
index 668e5e4cd..238ed5176 100644
--- a/crypto/src/crypto/ISigner.cs
+++ b/crypto/src/crypto/ISigner.cs
@@ -28,6 +28,8 @@ namespace Org.BouncyCastle.Crypto
         void BlockUpdate(ReadOnlySpan<byte> input);
 #endif
 
+        int GetMaxSignatureSize();
+
         /// <summary>Generate a signature for the message we've been loaded with using the key we were initialised with.
         /// </summary>
         /// <returns>A byte array containing the signature for the message.</returns>
diff --git a/crypto/src/crypto/signers/DsaDigestSigner.cs b/crypto/src/crypto/signers/DsaDigestSigner.cs
index e8c2487ba..f546785bd 100644
--- a/crypto/src/crypto/signers/DsaDigestSigner.cs
+++ b/crypto/src/crypto/signers/DsaDigestSigner.cs
@@ -73,7 +73,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 		}
 #endif
 
-		public virtual byte[] GenerateSignature()
+        public virtual int GetMaxSignatureSize() => encoding.GetMaxEncodingSize(GetOrder());
+
+        public virtual byte[] GenerateSignature()
 		{
 			if (!forSigning)
 				throw new InvalidOperationException("DSADigestSigner not initialised for signature generation.");
diff --git a/crypto/src/crypto/signers/Ed25519Signer.cs b/crypto/src/crypto/signers/Ed25519Signer.cs
index 59dc1bec5..79a2b1202 100644
--- a/crypto/src/crypto/signers/Ed25519Signer.cs
+++ b/crypto/src/crypto/signers/Ed25519Signer.cs
@@ -59,6 +59,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => Ed25519.SignatureSize;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning || null == privateKey)
diff --git a/crypto/src/crypto/signers/Ed25519ctxSigner.cs b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
index 4ccca8f22..90deb84ef 100644
--- a/crypto/src/crypto/signers/Ed25519ctxSigner.cs
+++ b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
@@ -62,6 +62,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => Ed25519.SignatureSize;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning || null == privateKey)
diff --git a/crypto/src/crypto/signers/Ed25519phSigner.cs b/crypto/src/crypto/signers/Ed25519phSigner.cs
index 800447143..c0fb04ddf 100644
--- a/crypto/src/crypto/signers/Ed25519phSigner.cs
+++ b/crypto/src/crypto/signers/Ed25519phSigner.cs
@@ -62,6 +62,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => Ed25519.SignatureSize;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning || null == privateKey)
diff --git a/crypto/src/crypto/signers/Ed448Signer.cs b/crypto/src/crypto/signers/Ed448Signer.cs
index 3a7def690..647a4b451 100644
--- a/crypto/src/crypto/signers/Ed448Signer.cs
+++ b/crypto/src/crypto/signers/Ed448Signer.cs
@@ -62,6 +62,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => Ed448.SignatureSize;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning || null == privateKey)
diff --git a/crypto/src/crypto/signers/Ed448phSigner.cs b/crypto/src/crypto/signers/Ed448phSigner.cs
index 7ff9cfbbe..197ac1aaa 100644
--- a/crypto/src/crypto/signers/Ed448phSigner.cs
+++ b/crypto/src/crypto/signers/Ed448phSigner.cs
@@ -62,6 +62,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => Ed448.SignatureSize;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning || null == privateKey)
diff --git a/crypto/src/crypto/signers/GOST3410DigestSigner.cs b/crypto/src/crypto/signers/GOST3410DigestSigner.cs
index 9564e43d3..63e65986b 100644
--- a/crypto/src/crypto/signers/GOST3410DigestSigner.cs
+++ b/crypto/src/crypto/signers/GOST3410DigestSigner.cs
@@ -77,6 +77,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => size;
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning)
diff --git a/crypto/src/crypto/signers/GenericSigner.cs b/crypto/src/crypto/signers/GenericSigner.cs
index e0ff685ae..2a416eeb7 100644
--- a/crypto/src/crypto/signers/GenericSigner.cs
+++ b/crypto/src/crypto/signers/GenericSigner.cs
@@ -76,6 +76,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => engine.GetOutputBlockSize();
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning)
diff --git a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
index 72afabf4c..ce7130538 100644
--- a/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
+++ b/crypto/src/crypto/signers/Iso9796d2PssSigner.cs
@@ -325,28 +325,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
-        /// <summary> reset the internal state</summary>
-        public virtual void Reset()
-        {
-            digest.Reset();
-            messageLength = 0;
-            if (mBuf != null)
-            {
-                ClearBlock(mBuf);
-            }
-            if (recoveredMessage != null)
-            {
-                ClearBlock(recoveredMessage);
-                recoveredMessage = null;
-            }
-            fullMessage = false;
-            if (preSig != null)
-            {
-                preSig = null;
-                ClearBlock(preBlock);
-                preBlock = null;
-            }
-        }
+        public virtual int GetMaxSignatureSize() => cipher.GetOutputBlockSize();
 
         /// <summary> Generate a signature for the loaded message using the key we were
         /// initialised with.
@@ -535,6 +514,29 @@ namespace Org.BouncyCastle.Crypto.Signers
             return true;
         }
 
+        /// <summary> reset the internal state</summary>
+        public virtual void Reset()
+        {
+            digest.Reset();
+            messageLength = 0;
+            if (mBuf != null)
+            {
+                ClearBlock(mBuf);
+            }
+            if (recoveredMessage != null)
+            {
+                ClearBlock(recoveredMessage);
+                recoveredMessage = null;
+            }
+            fullMessage = false;
+            if (preSig != null)
+            {
+                preSig = null;
+                ClearBlock(preBlock);
+                preBlock = null;
+            }
+        }
+
         /// <summary>
         /// Return true if the full message was recoveredMessage.
         /// </summary>
diff --git a/crypto/src/crypto/signers/Iso9796d2Signer.cs b/crypto/src/crypto/signers/Iso9796d2Signer.cs
index ea1dc3f18..3a1ffeb93 100644
--- a/crypto/src/crypto/signers/Iso9796d2Signer.cs
+++ b/crypto/src/crypto/signers/Iso9796d2Signer.cs
@@ -267,28 +267,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
-        /// <summary> reset the internal state</summary>
-        public virtual void Reset()
-        {
-            digest.Reset();
-            messageLength = 0;
-            ClearBlock(mBuf);
-
-            if (recoveredMessage != null)
-            {
-                ClearBlock(recoveredMessage);
-            }
-
-            recoveredMessage = null;
-            fullMessage = false;
-
-            if (preSig != null)
-            {
-                preSig = null;
-                ClearBlock(preBlock);
-                preBlock = null;
-            }
-        }
+        public virtual int GetMaxSignatureSize() => cipher.GetOutputBlockSize();
 
         /// <summary> Generate a signature for the loaded message using the key we were
         /// initialised with.
@@ -527,6 +506,29 @@ namespace Org.BouncyCastle.Crypto.Signers
             return true;
         }
 
+        /// <summary> reset the internal state</summary>
+        public virtual void Reset()
+        {
+            digest.Reset();
+            messageLength = 0;
+            ClearBlock(mBuf);
+
+            if (recoveredMessage != null)
+            {
+                ClearBlock(recoveredMessage);
+            }
+
+            recoveredMessage = null;
+            fullMessage = false;
+
+            if (preSig != null)
+            {
+                preSig = null;
+                ClearBlock(preBlock);
+                preBlock = null;
+            }
+        }
+
         private bool ReturnFalse(byte[] block)
         {
             messageLength = 0;
diff --git a/crypto/src/crypto/signers/PssSigner.cs b/crypto/src/crypto/signers/PssSigner.cs
index df73a7472..69f9e96e4 100644
--- a/crypto/src/crypto/signers/PssSigner.cs
+++ b/crypto/src/crypto/signers/PssSigner.cs
@@ -211,12 +211,9 @@ namespace Org.BouncyCastle.Crypto.Signers
 		}
 #endif
 
-		public virtual void Reset()
-		{
-			contentDigest1.Reset();
-		}
+        public virtual int GetMaxSignatureSize() => cipher.GetOutputBlockSize();
 
-		public virtual byte[] GenerateSignature()
+        public virtual byte[] GenerateSignature()
 		{
 			if (contentDigest1.GetDigestSize() != hLen)
 				throw new InvalidOperationException();
@@ -333,8 +330,13 @@ namespace Org.BouncyCastle.Crypto.Signers
 			return true;
 		}
 
-		/// <summary> int to octet string.</summary>
-		private void ItoOSP(
+        public virtual void Reset()
+        {
+            contentDigest1.Reset();
+        }
+
+        /// <summary> int to octet string.</summary>
+        private void ItoOSP(
 			int		i,
 			byte[]	sp)
 		{
diff --git a/crypto/src/crypto/signers/RsaDigestSigner.cs b/crypto/src/crypto/signers/RsaDigestSigner.cs
index 75b3a24b9..296e4b016 100644
--- a/crypto/src/crypto/signers/RsaDigestSigner.cs
+++ b/crypto/src/crypto/signers/RsaDigestSigner.cs
@@ -139,6 +139,8 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
+        public virtual int GetMaxSignatureSize() => rsaEngine.GetOutputBlockSize();
+
         public virtual byte[] GenerateSignature()
         {
             if (!forSigning)
diff --git a/crypto/src/crypto/signers/SM2Signer.cs b/crypto/src/crypto/signers/SM2Signer.cs
index 07b41bd30..60fae3264 100644
--- a/crypto/src/crypto/signers/SM2Signer.cs
+++ b/crypto/src/crypto/signers/SM2Signer.cs
@@ -116,29 +116,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
-        public virtual bool VerifySignature(byte[] signature)
-        {
-            try
-            {
-                BigInteger[] rs = encoding.Decode(ecParams.N, signature);
-
-                return VerifySignature(rs[0], rs[1]);
-            }
-            catch (Exception)
-            {
-            }
-
-            return false;
-        }
-
-        public virtual void Reset()
-        {
-            if (z != null)
-            {
-                digest.Reset();
-                digest.BlockUpdate(z, 0, z.Length);
-            }
-        }
+        public virtual int GetMaxSignatureSize() => encoding.GetMaxEncodingSize(ecParams.N);
 
         public virtual byte[] GenerateSignature()
         {
@@ -188,6 +166,30 @@ namespace Org.BouncyCastle.Crypto.Signers
             }
         }
 
+        public virtual bool VerifySignature(byte[] signature)
+        {
+            try
+            {
+                BigInteger[] rs = encoding.Decode(ecParams.N, signature);
+
+                return VerifySignature(rs[0], rs[1]);
+            }
+            catch (Exception)
+            {
+            }
+
+            return false;
+        }
+
+        public virtual void Reset()
+        {
+            if (z != null)
+            {
+                digest.Reset();
+                digest.BlockUpdate(z, 0, z.Length);
+            }
+        }
+
         private bool VerifySignature(BigInteger r, BigInteger s)
         {
             BigInteger n = ecParams.N;
diff --git a/crypto/src/crypto/signers/X931Signer.cs b/crypto/src/crypto/signers/X931Signer.cs
index 40255c40c..e40ad88a7 100644
--- a/crypto/src/crypto/signers/X931Signer.cs
+++ b/crypto/src/crypto/signers/X931Signer.cs
@@ -3,7 +3,6 @@
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Utilities;
-using Org.BouncyCastle.Utilities.Zlib;
 
 namespace Org.BouncyCastle.Crypto.Signers
 {
@@ -54,11 +53,6 @@ namespace Org.BouncyCastle.Crypto.Signers
             }
         }
 
-        public virtual string AlgorithmName
-        {
-            get { return digest.AlgorithmName + "with" + cipher.AlgorithmName + "/X9.31"; }
-        }
-
         /**
          * Constructor for a signer with an explicit digest trailer.
          *
@@ -70,6 +64,11 @@ namespace Org.BouncyCastle.Crypto.Signers
         {
         }
 
+        public virtual string AlgorithmName
+        {
+            get { return digest.AlgorithmName + "with" + cipher.AlgorithmName + "/X9.31"; }
+        }
+
         public virtual void Init(bool forSigning, ICipherParameters parameters)
         {
             kParam = (RsaKeyParameters)parameters;
@@ -100,10 +99,7 @@ namespace Org.BouncyCastle.Crypto.Signers
         }
 #endif
 
-        public virtual void Reset()
-        {
-            digest.Reset();
-        }
+        public virtual int GetMaxSignatureSize() => BigIntegers.GetUnsignedByteLength(kParam.Modulus);
 
         public virtual byte[] GenerateSignature()
         {
@@ -195,5 +191,10 @@ namespace Org.BouncyCastle.Crypto.Signers
 
             return rv;
         }
+
+        public virtual void Reset()
+        {
+            digest.Reset();
+        }
     }
 }