summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/cryptopro/ECGOST3410ParamSetParameters.cs96
-rw-r--r--crypto/src/asn1/cryptopro/GOST28147Parameters.cs52
-rw-r--r--crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs78
-rw-r--r--crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs89
4 files changed, 145 insertions, 170 deletions
diff --git a/crypto/src/asn1/cryptopro/ECGOST3410ParamSetParameters.cs b/crypto/src/asn1/cryptopro/ECGOST3410ParamSetParameters.cs
index 2c494c526..ac3a43f9d 100644
--- a/crypto/src/asn1/cryptopro/ECGOST3410ParamSetParameters.cs
+++ b/crypto/src/asn1/cryptopro/ECGOST3410ParamSetParameters.cs
@@ -1,79 +1,77 @@
 using System;
 
 using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.CryptoPro
 {
     public class ECGost3410ParamSetParameters
         : Asn1Encodable
     {
-        internal readonly DerInteger p, q, a, b, x, y;
+        public static ECGost3410ParamSetParameters GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is ECGost3410ParamSetParameters ecGost3410ParamSetParameters)
+                return ecGost3410ParamSetParameters;
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new ECGost3410ParamSetParameters(Asn1Sequence.GetInstance(obj));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
 
         public static ECGost3410ParamSetParameters GetInstance(Asn1TaggedObject obj, bool explicitly)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new ECGost3410ParamSetParameters(Asn1Sequence.GetInstance(obj, explicitly));
+#pragma warning restore CS0618 // Type or member is obsolete
         }
 
-        public static ECGost3410ParamSetParameters GetInstance(object obj)
+        public static ECGost3410ParamSetParameters GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            if (obj == null || obj is ECGost3410ParamSetParameters)
-                return (ECGost3410ParamSetParameters)obj;
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new ECGost3410ParamSetParameters(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
+
+        private readonly DerInteger m_a, m_b, m_p, m_q, m_x, m_y;
 
-            if (obj is Asn1Sequence seq)
-                return new ECGost3410ParamSetParameters(seq);
+        [Obsolete("Use 'GetInstance' instead")]
+        public ECGost3410ParamSetParameters(Asn1Sequence seq)
+        {
+            int count = seq.Count;
+            if (count != 6)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-            throw new ArgumentException("Invalid GOST3410Parameter: " + Platform.GetTypeName(obj));
+            m_a = DerInteger.GetInstance(seq[0]);
+            m_b = DerInteger.GetInstance(seq[1]);
+            m_p = DerInteger.GetInstance(seq[2]);
+            m_q = DerInteger.GetInstance(seq[3]);
+            m_x = DerInteger.GetInstance(seq[4]);
+            m_y = DerInteger.GetInstance(seq[5]);
         }
 
-        public ECGost3410ParamSetParameters(
-            BigInteger a,
-            BigInteger b,
-            BigInteger p,
-            BigInteger q,
-            int        x,
+        public ECGost3410ParamSetParameters(BigInteger a, BigInteger b, BigInteger p, BigInteger q, int x,
             BigInteger y)
         {
-            this.a = new DerInteger(a);
-            this.b = new DerInteger(b);
-            this.p = new DerInteger(p);
-            this.q = new DerInteger(q);
-            this.x = new DerInteger(x);
-            this.y = new DerInteger(y);
+            m_a = new DerInteger(a);
+            m_b = new DerInteger(b);
+            m_p = new DerInteger(p);
+            m_q = new DerInteger(q);
+            m_x = new DerInteger(x);
+            m_y = new DerInteger(y);
         }
 
-        public ECGost3410ParamSetParameters(
-            Asn1Sequence seq)
-        {
-			if (seq.Count != 6)
-				throw new ArgumentException("Wrong number of elements in sequence", "seq");
+        public BigInteger A => m_a.PositiveValue;
 
-			this.a = DerInteger.GetInstance(seq[0]);
-			this.b = DerInteger.GetInstance(seq[1]);
-			this.p = DerInteger.GetInstance(seq[2]);
-			this.q = DerInteger.GetInstance(seq[3]);
-			this.x = DerInteger.GetInstance(seq[4]);
-			this.y = DerInteger.GetInstance(seq[5]);
-        }
+        public BigInteger B => m_b.PositiveValue;
 
-		public BigInteger P
-		{
-			get { return p.PositiveValue; }
-		}
+        public BigInteger P => m_p.PositiveValue;
 
-		public BigInteger Q
-		{
-			get { return q.PositiveValue; }
-		}
+		public BigInteger Q => m_q.PositiveValue;
 
-		public BigInteger A
-		{
-			get { return a.PositiveValue; }
-		}
+        int X => m_x.IntPositiveValueExact;
 
-		public override Asn1Object ToAsn1Object()
-        {
-			return new DerSequence(a, b, p, q, x, y);
-        }
+        public BigInteger Y => m_y.PositiveValue;
+
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_a, m_b, m_p, m_q, m_x, m_y);
     }
 }
diff --git a/crypto/src/asn1/cryptopro/GOST28147Parameters.cs b/crypto/src/asn1/cryptopro/GOST28147Parameters.cs
index 9167cd4b5..ab9c58ef0 100644
--- a/crypto/src/asn1/cryptopro/GOST28147Parameters.cs
+++ b/crypto/src/asn1/cryptopro/GOST28147Parameters.cs
@@ -1,40 +1,49 @@
 using System;
 
-using Org.BouncyCastle.Utilities;
-
 namespace Org.BouncyCastle.Asn1.CryptoPro
 {
     public class Gost28147Parameters
         : Asn1Encodable
     {
-        private readonly Asn1OctetString iv;
-        private readonly DerObjectIdentifier paramSet;
-
-		public static Gost28147Parameters GetInstance(Asn1TaggedObject obj, bool explicitly)
+		public static Gost28147Parameters GetInstance(object obj)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+            if (obj == null)
+                return null;
+            if (obj is Gost28147Parameters gost28147Parameters)
+                return gost28147Parameters;
+            return new Gost28147Parameters(Asn1Sequence.GetInstance(obj));
         }
 
-		public static Gost28147Parameters GetInstance(object obj)
-        {
-            if (obj == null || obj is Gost28147Parameters)
-                return (Gost28147Parameters) obj;
+        public static Gost28147Parameters GetInstance(Asn1TaggedObject obj, bool explicitly) =>
+            new Gost28147Parameters(Asn1Sequence.GetInstance(obj, explicitly));
 
-            if (obj is Asn1Sequence seq)
-                return new Gost28147Parameters(seq);
+        public static Gost28147Parameters GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new Gost28147Parameters(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-            throw new ArgumentException("Invalid GOST3410Parameter: " + Platform.GetTypeName(obj));
-        }
+        private readonly Asn1OctetString m_iv;
+        private readonly DerObjectIdentifier m_encryptionParamSet;
 
         private Gost28147Parameters(Asn1Sequence seq)
         {
-			if (seq.Count != 2)
-				throw new ArgumentException("Wrong number of elements in sequence", "seq");
+            int count = seq.Count;
+            if (count != 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			this.iv = Asn1OctetString.GetInstance(seq[0]);
-			this.paramSet = DerObjectIdentifier.GetInstance(seq[1]);
+            // TODO Validate length of 8?
+			m_iv = Asn1OctetString.GetInstance(seq[0]);
+            m_encryptionParamSet = DerObjectIdentifier.GetInstance(seq[1]);
         }
 
+        public Gost28147Parameters(Asn1OctetString iv, DerObjectIdentifier encryptionParamSet)
+        {
+            m_iv = iv ?? throw new ArgumentNullException(nameof(iv));
+            m_encryptionParamSet = encryptionParamSet ?? throw new ArgumentNullException(nameof(encryptionParamSet));
+        }
+
+        public Asn1OctetString IV => m_iv;
+
+        public DerObjectIdentifier EncryptionParamSet => m_encryptionParamSet;
+
 		/**
          * <pre>
          * Gost28147-89-Parameters ::=
@@ -46,9 +55,6 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
          *   Gost28147-89-IV ::= OCTET STRING (SIZE (8))
          * </pre>
          */
-        public override Asn1Object ToAsn1Object()
-        {
-			return new DerSequence(iv, paramSet);
-        }
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_iv, m_encryptionParamSet);
     }
 }
diff --git a/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs b/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
index ae0cd4f83..e7c7f9d99 100644
--- a/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
+++ b/crypto/src/asn1/cryptopro/GOST3410ParamSetParameters.cs
@@ -1,74 +1,58 @@
 using System;
 
 using Org.BouncyCastle.Math;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.CryptoPro
 {
     public class Gost3410ParamSetParameters
         : Asn1Encodable
     {
-        private readonly int keySize;
-        private readonly DerInteger	p, q, a;
-
-		public static Gost3410ParamSetParameters GetInstance(Asn1TaggedObject obj, bool explicitly)
+		public static Gost3410ParamSetParameters GetInstance(object obj)
         {
-            return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+            if (obj == null)
+                return null;
+            if (obj is Gost3410ParamSetParameters gost3410ParamSetParameters)
+                return gost3410ParamSetParameters;
+            return new Gost3410ParamSetParameters(Asn1Sequence.GetInstance(obj));
         }
 
-		public static Gost3410ParamSetParameters GetInstance(object obj)
-        {
-            if (obj == null || obj is Gost3410ParamSetParameters)
-                return (Gost3410ParamSetParameters) obj;
+        public static Gost3410ParamSetParameters GetInstance(Asn1TaggedObject obj, bool explicitly) =>
+            new Gost3410ParamSetParameters(Asn1Sequence.GetInstance(obj, explicitly));
 
-			if (obj is Asn1Sequence seq)
-                return new Gost3410ParamSetParameters(seq);
+        public static Gost3410ParamSetParameters GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new Gost3410ParamSetParameters(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-            throw new ArgumentException("Invalid GOST3410Parameter: " + Platform.GetTypeName(obj));
-        }
+        private readonly int m_keySize;
+        private readonly DerInteger m_p, m_q, m_a;
 
-		public Gost3410ParamSetParameters(int keySize, BigInteger p, BigInteger q, BigInteger a)
+		private Gost3410ParamSetParameters(Asn1Sequence seq)
         {
-            this.keySize = keySize;
-            this.p = new DerInteger(p);
-            this.q = new DerInteger(q);
-            this.a = new DerInteger(a);
+            int count = seq.Count;
+            if (count != 4)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            m_keySize = DerInteger.GetInstance(seq[0]).IntValueExact;
+			m_p = DerInteger.GetInstance(seq[1]);
+            m_q = DerInteger.GetInstance(seq[2]);
+			m_a = DerInteger.GetInstance(seq[3]);
         }
 
-		private Gost3410ParamSetParameters(Asn1Sequence seq)
+        public Gost3410ParamSetParameters(int keySize, BigInteger p, BigInteger q, BigInteger a)
         {
-			if (seq.Count != 4)
-				throw new ArgumentException("Wrong number of elements in sequence", "seq");
-
-            this.keySize = DerInteger.GetInstance(seq[0]).IntValueExact;
-			this.p = DerInteger.GetInstance(seq[1]);
-            this.q = DerInteger.GetInstance(seq[2]);
-			this.a = DerInteger.GetInstance(seq[3]);
+            m_keySize = keySize;
+            m_p = new DerInteger(p);
+            m_q = new DerInteger(q);
+            m_a = new DerInteger(a);
         }
 
-		public int KeySize
-		{
-			get { return keySize; }
-		}
+        public int KeySize => m_keySize;
 
-		public BigInteger P
-		{
-			get { return p.PositiveValue; }
-		}
+		public BigInteger P => m_p.PositiveValue;
 
-		public BigInteger Q
-		{
-			get { return q.PositiveValue; }
-		}
+		public BigInteger Q => m_q.PositiveValue;
 
-		public BigInteger A
-		{
-			get { return a.PositiveValue; }
-		}
+		public BigInteger A => m_a.PositiveValue;
 
-		public override Asn1Object ToAsn1Object()
-        {
-			return new DerSequence(new DerInteger(keySize), p, q, a);
-        }
+		public override Asn1Object ToAsn1Object() => new DerSequence(new DerInteger(m_keySize), m_p, m_q, m_a);
     }
 }
diff --git a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
index 40b69428d..6deccb80f 100644
--- a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
+++ b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
@@ -5,76 +5,63 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
     public class Gost3410PublicKeyAlgParameters
         : Asn1Encodable
     {
-        private DerObjectIdentifier	publicKeyParamSet;
-        private DerObjectIdentifier	digestParamSet;
-        private DerObjectIdentifier	encryptionParamSet;
-
-		public static Gost3410PublicKeyAlgParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        public static Gost3410PublicKeyAlgParameters GetInstance(object obj)
         {
-            return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+            if (obj == null)
+                return null;
+            if (obj is Gost3410PublicKeyAlgParameters gost3410PublicKeyAlgParameters)
+                return gost3410PublicKeyAlgParameters;
+            return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(obj));
         }
 
-		public static Gost3410PublicKeyAlgParameters GetInstance(object obj)
-        {
-            if (obj == null || obj is Gost3410PublicKeyAlgParameters)
-                return (Gost3410PublicKeyAlgParameters)obj;
+        public static Gost3410PublicKeyAlgParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
 
-            return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance(obj));
-        }
+        public static Gost3410PublicKeyAlgParameters GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-		public Gost3410PublicKeyAlgParameters(
-            DerObjectIdentifier	publicKeyParamSet,
-            DerObjectIdentifier	digestParamSet)
-			: this (publicKeyParamSet, digestParamSet, null)
-        {
-        }
+        private readonly DerObjectIdentifier m_publicKeyParamSet;
+        private readonly DerObjectIdentifier m_digestParamSet;
+        private readonly DerObjectIdentifier m_encryptionParamSet;
 
-		public Gost3410PublicKeyAlgParameters(
-            DerObjectIdentifier  publicKeyParamSet,
-            DerObjectIdentifier  digestParamSet,
-            DerObjectIdentifier  encryptionParamSet)
+        private Gost3410PublicKeyAlgParameters(Asn1Sequence seq)
         {
-			if (publicKeyParamSet == null)
-				throw new ArgumentNullException("publicKeyParamSet");
-			if (digestParamSet == null)
-				throw new ArgumentNullException("digestParamSet");
+            int count = seq.Count, pos = 0;
+            if (count < 2 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            m_publicKeyParamSet = DerObjectIdentifier.GetInstance(seq[pos++]);
+            m_digestParamSet = DerObjectIdentifier.GetInstance(seq[pos++]);
+            m_encryptionParamSet = Asn1Utilities.ReadOptional(seq, ref pos, DerObjectIdentifier.GetOptional);
 
-			this.publicKeyParamSet = publicKeyParamSet;
-            this.digestParamSet = digestParamSet;
-            this.encryptionParamSet = encryptionParamSet;
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        private Gost3410PublicKeyAlgParameters(Asn1Sequence seq)
+        public Gost3410PublicKeyAlgParameters(DerObjectIdentifier publicKeyParamSet, DerObjectIdentifier digestParamSet)
+            : this(publicKeyParamSet, digestParamSet, null)
         {
-            this.publicKeyParamSet = (DerObjectIdentifier) seq[0];
-            this.digestParamSet = (DerObjectIdentifier) seq[1];
+        }
 
-			if (seq.Count > 2)
-            {
-                this.encryptionParamSet = (DerObjectIdentifier) seq[2];
-            }
+        public Gost3410PublicKeyAlgParameters(DerObjectIdentifier publicKeyParamSet, DerObjectIdentifier digestParamSet,
+            DerObjectIdentifier encryptionParamSet)
+        {
+            m_publicKeyParamSet = publicKeyParamSet ?? throw new ArgumentNullException(nameof(publicKeyParamSet));
+            m_digestParamSet = digestParamSet ?? throw new ArgumentNullException(nameof(digestParamSet));
+            m_encryptionParamSet = encryptionParamSet;
         }
 
-		public DerObjectIdentifier PublicKeyParamSet
-		{
-			get { return publicKeyParamSet; }
-		}
+        public DerObjectIdentifier PublicKeyParamSet => m_publicKeyParamSet;
 
-		public DerObjectIdentifier DigestParamSet
-		{
-			get { return digestParamSet; }
-		}
+		public DerObjectIdentifier DigestParamSet => m_digestParamSet;
 
-		public DerObjectIdentifier EncryptionParamSet
-		{
-			get { return encryptionParamSet; }
-		}
+		public DerObjectIdentifier EncryptionParamSet => m_encryptionParamSet;
 
 		public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(publicKeyParamSet, digestParamSet);
-            v.AddOptional(encryptionParamSet);
-			return new DerSequence(v);
+            return m_encryptionParamSet == null
+                ?  new DerSequence(m_publicKeyParamSet, m_digestParamSet)
+                :  new DerSequence(m_publicKeyParamSet, m_digestParamSet, m_encryptionParamSet);
         }
     }
 }