summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-04 02:17:04 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-04 02:17:04 +0700
commit7610f647360313aa86d89884c45ce6d334876b73 (patch)
treeb01a0b682c5064cae4b5e1f075089410edfbfdbe
parentUpdate IPAddress from bc-java (diff)
downloadBouncyCastle.NET-ed25519-7610f647360313aa86d89884c45ce6d334876b73.tar.xz
Misc. refactoring after bc-fips-csharp updates
-rw-r--r--crypto/src/asn1/esf/CertificateValues.cs23
-rw-r--r--crypto/src/asn1/esf/CrlIdentifier.cs16
-rw-r--r--crypto/src/cmp/ProtectedPkiMessage.cs10
-rw-r--r--crypto/src/cms/CMSContentInfoParser.cs3
-rw-r--r--crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs10
-rw-r--r--crypto/src/crypto/agreement/DHBasicAgreement.cs6
-rw-r--r--crypto/src/crypto/agreement/ECDHBasicAgreement.cs5
-rw-r--r--crypto/src/crypto/agreement/ECDHCBasicAgreement.cs5
-rw-r--r--crypto/src/crypto/fpe/SP80038G.cs10
-rw-r--r--crypto/src/crypto/parameters/DHPublicKeyParameters.cs40
-rw-r--r--crypto/src/math/ec/ECAlgorithms.cs5
-rw-r--r--crypto/src/pkcs/Pkcs10CertificationRequest.cs6
-rw-r--r--crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs5
-rw-r--r--crypto/src/tls/TlsProtocol.cs1
14 files changed, 61 insertions, 84 deletions
diff --git a/crypto/src/asn1/esf/CertificateValues.cs b/crypto/src/asn1/esf/CertificateValues.cs
index 8329e45f8..96088209f 100644
--- a/crypto/src/asn1/esf/CertificateValues.cs
+++ b/crypto/src/asn1/esf/CertificateValues.cs
@@ -19,20 +19,19 @@ namespace Org.BouncyCastle.Asn1.Esf
 
 		public static CertificateValues GetInstance(object obj)
 		{
-			if (obj == null)
-				return null;
-
-			if (obj is CertificateValues certificateValues)
-				return certificateValues;
-
-			if (obj is Asn1Sequence asn1Sequence)
-				return new CertificateValues(asn1Sequence);
-
-			throw new ArgumentException("Unknown object in 'CertificateValues' factory: " + Platform.GetTypeName(obj),
-				nameof(obj));
+            if (obj == null)
+                return null;
+            if (obj is CertificateValues certificateValues)
+                return certificateValues;
+            return new CertificateValues(Asn1Sequence.GetInstance(obj));
 		}
 
-		private CertificateValues(Asn1Sequence seq)
+        public static CertificateValues GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+        }
+
+        private CertificateValues(Asn1Sequence seq)
 		{
 			if (seq == null)
 				throw new ArgumentNullException(nameof(seq));
diff --git a/crypto/src/asn1/esf/CrlIdentifier.cs b/crypto/src/asn1/esf/CrlIdentifier.cs
index 29003260a..7d6225c63 100644
--- a/crypto/src/asn1/esf/CrlIdentifier.cs
+++ b/crypto/src/asn1/esf/CrlIdentifier.cs
@@ -2,7 +2,6 @@ using System;
 
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Esf
 {
@@ -28,18 +27,17 @@ namespace Org.BouncyCastle.Asn1.Esf
 		{
 			if (obj == null)
 				return null;
-
 			if (obj is CrlIdentifier crlIdentifier)
                 return crlIdentifier;
-
-			if (obj is Asn1Sequence asn1Sequence)
-				return new CrlIdentifier(asn1Sequence);
-
-			throw new ArgumentException("Unknown object in 'CrlIdentifier' factory: " + Platform.GetTypeName(obj),
-				nameof(obj));
+			return new CrlIdentifier(Asn1Sequence.GetInstance(obj));
 		}
 
-		private CrlIdentifier(Asn1Sequence seq)
+        public static CrlIdentifier GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+        }
+
+        private CrlIdentifier(Asn1Sequence seq)
 		{
 			if (seq == null)
 				throw new ArgumentNullException(nameof(seq));
diff --git a/crypto/src/cmp/ProtectedPkiMessage.cs b/crypto/src/cmp/ProtectedPkiMessage.cs
index 8bc9e4f4d..fdcdeb90d 100644
--- a/crypto/src/cmp/ProtectedPkiMessage.cs
+++ b/crypto/src/cmp/ProtectedPkiMessage.cs
@@ -111,10 +111,11 @@ namespace Org.BouncyCastle.Cmp
                 throw new InvalidOperationException("protection algorithm is not mac based");
 
             PbmParameter parameter = PbmParameter.GetInstance(m_pkiMessage.Header.ProtectionAlg.Parameters);
-
             pkMacBuilder.SetParameters(parameter);
 
-            IBlockResult result = Process(pkMacBuilder.Build(password).CreateCalculator());
+            var macFactory = pkMacBuilder.Build(password);
+
+            IBlockResult result = Process(macFactory.CreateCalculator());
 
             return Arrays.FixedTimeEquals(result.Collect(), m_pkiMessage.Protection.GetBytes());
         }
@@ -126,10 +127,11 @@ namespace Org.BouncyCastle.Cmp
                 throw new InvalidOperationException("protection algorithm is not mac based");
 
             PbmParameter parameter = PbmParameter.GetInstance(m_pkiMessage.Header.ProtectionAlg.Parameters);
-
             pkMacBuilder.SetParameters(parameter);
 
-            IBlockResult result = Process(pkMacBuilder.Build(password).CreateCalculator());
+            var macFactory = pkMacBuilder.Build(password);
+
+            IBlockResult result = Process(macFactory.CreateCalculator());
 
             return Arrays.FixedTimeEquals(result.Collect(), m_pkiMessage.Protection.GetBytes());
         }
diff --git a/crypto/src/cms/CMSContentInfoParser.cs b/crypto/src/cms/CMSContentInfoParser.cs
index 86c7f459e..034ff40b6 100644
--- a/crypto/src/cms/CMSContentInfoParser.cs
+++ b/crypto/src/cms/CMSContentInfoParser.cs
@@ -6,7 +6,8 @@ using Org.BouncyCastle.Asn1.Cms;
 
 namespace Org.BouncyCastle.Cms
 {
-	public class CmsContentInfoParser
+    // TODO Make abstract in next API revision
+    public class CmsContentInfoParser
 		: IDisposable
 	{
 		protected ContentInfoParser	contentInfo;
diff --git a/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs b/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs
index 4530b18b8..8d2ea0bac 100644
--- a/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs
+++ b/crypto/src/crmf/ProofOfPossessionSigningKeyBuilder.cs
@@ -30,7 +30,6 @@ namespace Org.BouncyCastle.Crmf
         public ProofOfPossessionSigningKeyBuilder SetSender(GeneralName name)
         {
             this._name = name;
-
             return this;
         }
 
@@ -84,18 +83,15 @@ namespace Org.BouncyCastle.Crmf
 
         private ProofOfPossessionSigningKeyBuilder ImplSetPublicKeyMac(IMacFactory fact)
         {
-            byte[] d = _pubKeyInfo.GetDerEncoded();
-
             IStreamCalculator<IBlockResult> calc = fact.CreateCalculator();
             using (var stream = calc.Stream)
             {
-                stream.Write(d, 0, d.Length);
+                _pubKeyInfo.EncodeTo(stream, Asn1Encodable.Der);
             }
 
-            this._publicKeyMAC = new PKMacValue(
-                (AlgorithmIdentifier)fact.AlgorithmDetails,
-                new DerBitString(calc.GetResult().Collect()));
+            var mac = calc.GetResult().Collect();
 
+            this._publicKeyMAC = new PKMacValue((AlgorithmIdentifier)fact.AlgorithmDetails, new DerBitString(mac));
             return this;
         }
     }
diff --git a/crypto/src/crypto/agreement/DHBasicAgreement.cs b/crypto/src/crypto/agreement/DHBasicAgreement.cs
index ca298dd27..a27d8c534 100644
--- a/crypto/src/crypto/agreement/DHBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/DHBasicAgreement.cs
@@ -26,10 +26,10 @@ namespace Org.BouncyCastle.Crypto.Agreement
                 parameters = withRandom.Parameters;
             }
 
-            if (!(parameters is DHPrivateKeyParameters))
-                throw new ArgumentException("DHEngine expects DHPrivateKeyParameters");
+            if (!(parameters is DHPrivateKeyParameters dhPrivateKeyParameters))
+                throw new ArgumentException("DHBasicAgreement expects DHPrivateKeyParameters");
 
-            this.key = (DHPrivateKeyParameters)parameters;
+            this.key = dhPrivateKeyParameters;
             this.dhParams = key.Parameters;
         }
 
diff --git a/crypto/src/crypto/agreement/ECDHBasicAgreement.cs b/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
index b3b1ab5c7..4555cdde4 100644
--- a/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/ECDHBasicAgreement.cs
@@ -33,7 +33,10 @@ namespace Org.BouncyCastle.Crypto.Agreement
                 parameters = withRandom.Parameters;
             }
 
-            this.privKey = (ECPrivateKeyParameters)parameters;
+            if (!(parameters is ECPrivateKeyParameters ecPrivateKeyParameters))
+                throw new ArgumentException("ECDHBasicAgreement expects ECPrivateKeyParameters");
+
+            this.privKey = ecPrivateKeyParameters;
         }
 
         public virtual int GetFieldSize()
diff --git a/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs b/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
index 1bcb259c6..bb4c185df 100644
--- a/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
+++ b/crypto/src/crypto/agreement/ECDHCBasicAgreement.cs
@@ -38,7 +38,10 @@ namespace Org.BouncyCastle.Crypto.Agreement
                 parameters = withRandom.Parameters;
             }
 
-            this.privKey = (ECPrivateKeyParameters)parameters;
+            if (!(parameters is ECPrivateKeyParameters ecPrivateKeyParameters))
+                throw new ArgumentException("ECDHCBasicAgreement expects ECPrivateKeyParameters");
+
+            this.privKey = ecPrivateKeyParameters;
         }
 
         public virtual int GetFieldSize()
diff --git a/crypto/src/crypto/fpe/SP80038G.cs b/crypto/src/crypto/fpe/SP80038G.cs
index c1f5f23f4..c57a34762 100644
--- a/crypto/src/crypto/fpe/SP80038G.cs
+++ b/crypto/src/crypto/fpe/SP80038G.cs
@@ -580,7 +580,7 @@ namespace Org.BouncyCastle.Crypto.Fpe
 
             for (int i = 0; i < m; ++i)
             {
-                Xor(x, i * BLOCK_SIZE, y, 0, BLOCK_SIZE);
+                Bytes.XorTo(BLOCK_SIZE, x, i * BLOCK_SIZE, y, 0);
                 c.ProcessBlock(y, 0, y, 0);
             }
 
@@ -602,14 +602,6 @@ namespace Org.BouncyCastle.Crypto.Fpe
                 throw new ArgumentException();
         }
 
-        private static void Xor(byte[] x, int xOff, byte[] y, int yOff, int len)
-        {
-            for (int i = 0; i < len; ++i)
-            {
-                y[yOff + i] ^= x[xOff + i];
-            }
-        }
-
         private static byte[] ToByte(ushort[] buf)
         {
             byte[] s = new byte[buf.Length];
diff --git a/crypto/src/crypto/parameters/DHPublicKeyParameters.cs b/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
index a72f247a5..be4a93eb6 100644
--- a/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
+++ b/crypto/src/crypto/parameters/DHPublicKeyParameters.cs
@@ -13,13 +13,13 @@ namespace Org.BouncyCastle.Crypto.Parameters
         private static BigInteger Validate(BigInteger y, DHParameters dhParams)
         {
             if (y == null)
-                throw new ArgumentNullException("y");
+                throw new ArgumentNullException(nameof(y));
 
             BigInteger p = dhParams.P;
 
             // TLS check
             if (y.CompareTo(BigInteger.Two) < 0 || y.CompareTo(p.Subtract(BigInteger.Two)) > 0)
-                throw new ArgumentException("invalid DH public key", "y");
+                throw new ArgumentException("invalid DH public key", nameof(y));
 
             BigInteger q = dhParams.Q;
 
@@ -41,56 +41,44 @@ namespace Org.BouncyCastle.Crypto.Parameters
                     return y;
             }
 
-            throw new ArgumentException("value does not appear to be in correct group", "y");
+            throw new ArgumentException("value does not appear to be in correct group", nameof(y));
         }
 
-        private readonly BigInteger y;
+        private readonly BigInteger m_y;
 
-		public DHPublicKeyParameters(
-            BigInteger		y,
-            DHParameters	parameters)
+		public DHPublicKeyParameters(BigInteger y, DHParameters	parameters)
 			: base(false, parameters)
         {
-			this.y = Validate(y, parameters);
+			m_y = Validate(y, parameters);
         }
 
-		public DHPublicKeyParameters(
-            BigInteger			y,
-            DHParameters		parameters,
-		    DerObjectIdentifier	algorithmOid)
+		public DHPublicKeyParameters(BigInteger y, DHParameters parameters, DerObjectIdentifier	algorithmOid)
 			: base(false, parameters, algorithmOid)
         {
-            this.y = Validate(y, parameters);
+            m_y = Validate(y, parameters);
         }
 
-        public virtual BigInteger Y
-        {
-            get { return y; }
-        }
+        public virtual BigInteger Y => m_y;
 
-		public override bool Equals(
-			object  obj)
+		public override bool Equals(object obj)
         {
 			if (obj == this)
 				return true;
 
-			DHPublicKeyParameters other = obj as DHPublicKeyParameters;
-
-			if (other == null)
+            if (!(obj is DHPublicKeyParameters other))
 				return false;
 
 			return Equals(other);
         }
 
-		protected bool Equals(
-			DHPublicKeyParameters other)
+		protected bool Equals(DHPublicKeyParameters other)
 		{
-			return y.Equals(other.y) && base.Equals(other);
+			return m_y.Equals(other.m_y) && base.Equals(other);
 		}
 
 		public override int GetHashCode()
         {
-            return y.GetHashCode() ^ base.GetHashCode();
+            return m_y.GetHashCode() ^ base.GetHashCode();
         }
 
         private static int Legendre(BigInteger a, BigInteger b)
diff --git a/crypto/src/math/ec/ECAlgorithms.cs b/crypto/src/math/ec/ECAlgorithms.cs
index e7a7189b7..7b04fb56e 100644
--- a/crypto/src/math/ec/ECAlgorithms.cs
+++ b/crypto/src/math/ec/ECAlgorithms.cs
@@ -584,12 +584,11 @@ namespace Org.BouncyCastle.Math.EC
 
             ECPoint R = c.Infinity;
 
-            int top = fullComb - 1; 
-            for (int i = 0; i < d; ++i)
+            for (int i = 1; i <= d; ++i)
             {
                 uint secretIndexK = 0, secretIndexL = 0;
 
-                for (int j = top - i; j >= 0; j -= d)
+                for (int j = fullComb - i; j >= 0; j -= d)
                 {
                     uint secretBitK = K[j >> 5] >> (j & 0x1F);
                     secretIndexK ^= secretBitK >> 1;
diff --git a/crypto/src/pkcs/Pkcs10CertificationRequest.cs b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
index 12151a001..9256b91f3 100644
--- a/crypto/src/pkcs/Pkcs10CertificationRequest.cs
+++ b/crypto/src/pkcs/Pkcs10CertificationRequest.cs
@@ -11,10 +11,10 @@ using Org.BouncyCastle.Asn1.TeleTrust;
 using Org.BouncyCastle.Asn1.X509;
 using Org.BouncyCastle.Asn1.X9;
 using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.Operators;
 using Org.BouncyCastle.Security;
 using Org.BouncyCastle.Utilities;
 using Org.BouncyCastle.X509;
-using Org.BouncyCastle.Crypto.Operators;
 
 namespace Org.BouncyCastle.Pkcs
 {
@@ -342,12 +342,10 @@ namespace Org.BouncyCastle.Pkcs
         {
             try
             {
-                byte[] b = reqInfo.GetDerEncoded();
-
                 IStreamCalculator<IVerifier> streamCalculator = verifier.CreateCalculator();
                 using (var stream = streamCalculator.Stream)
                 {
-                    stream.Write(b, 0, b.Length);
+                    reqInfo.EncodeTo(stream, Asn1Encodable.Der);
                 }
 
                 return streamCalculator.GetResult().IsVerified(sigBits.GetOctets());
diff --git a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs
index 23c8c7f76..38a4088d4 100644
--- a/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs
+++ b/crypto/src/pkcs/Pkcs8EncryptedPrivateKeyInfoBuilder.cs
@@ -33,11 +33,10 @@ namespace Org.BouncyCastle.Pkcs
             {
                 MemoryStream bOut = new MemoryStream();
                 ICipher cOut = encryptor.BuildCipher(bOut);
-                byte[] keyData = privateKeyInfo.GetEncoded();
 
-                using (var str = cOut.Stream)
+                using (var stream = cOut.Stream)
                 {
-                    str.Write(keyData, 0, keyData.Length);
+                    privateKeyInfo.EncodeTo(stream);
                 }
 
                 return new Pkcs8EncryptedPrivateKeyInfo(
diff --git a/crypto/src/tls/TlsProtocol.cs b/crypto/src/tls/TlsProtocol.cs
index 16bc1385e..92322e949 100644
--- a/crypto/src/tls/TlsProtocol.cs
+++ b/crypto/src/tls/TlsProtocol.cs
@@ -1007,7 +1007,6 @@ namespace Org.BouncyCastle.Tls
 #endif
         }
 
-
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public virtual void WriteApplicationData(ReadOnlySpan<byte> buffer)
         {