summary refs log tree commit diff
path: root/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 15:59:24 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 15:59:24 +0700
commitc47af79f9a7928e62b6236d2973a91eb8259b9c6 (patch)
treec371da173eab4bf38ca3c594b5ddacf97f5a230e /crypto/src/asn1/nist/KMACwithSHAKE256_params.cs
parentRefactoring in Asn1.CryptoPro (diff)
downloadBouncyCastle.NET-ed25519-c47af79f9a7928e62b6236d2973a91eb8259b9c6.tar.xz
Refactoring in Asn1.Nist
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/nist/KMACwithSHAKE256_params.cs126
1 files changed, 53 insertions, 73 deletions
diff --git a/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs
index 99f444e7f..1afb31bde 100644
--- a/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs
+++ b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs
@@ -1,5 +1,6 @@
-using Org.BouncyCastle.Utilities;
-using System;
+using System;
+
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Nist
 {
@@ -9,94 +10,73 @@ namespace Org.BouncyCastle.Asn1.Nist
     ///     customizationString  OCTET STRING DEFAULT ''H
     /// } 
     /// </summary>
-public class KMacWithShake256Params : Asn1Encodable
-{
-    private static readonly byte[] EMPTY_STRING = new byte[0];
-    private static readonly int DEF_LENGTH = 512;
-
-    private readonly int outputLength;
-    private readonly byte[] customizationString;
-
-    public KMacWithShake256Params(int outputLength)
-    {
-        this.outputLength = outputLength;
-        this.customizationString = EMPTY_STRING;
-    }
-
-    public KMacWithShake256Params(int outputLength, byte[] customizationString)
+    public class KMacWithShake256Params
+        : Asn1Encodable
     {
-        this.outputLength = outputLength;
-        this.customizationString = Arrays.Clone(customizationString);
-    }
+        private const int _DefaultOutputLength = 512;
 
-    public static KMacWithShake256Params GetInstance(object o)
-    {
-        if (o is KMacWithShake256Params)
-        {
-            return (KMacWithShake256Params)o;
-        }
-        else if (o != null)
+        public static readonly DerInteger DefaultOutputLength = new DerInteger(_DefaultOutputLength);
+        public static readonly Asn1OctetString DefaultCustomizationString = DerOctetString.Empty;
+        public static KMacWithShake256Params GetInstance(object o)
         {
+            if (o == null)
+                return null;
+            if (o is KMacWithShake256Params kMacWithShake256Params)
+                return kMacWithShake256Params;
             return new KMacWithShake256Params(Asn1Sequence.GetInstance(o));
         }
 
-        return null;
-    }
+        public static KMacWithShake256Params GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new KMacWithShake256Params(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
 
-    private KMacWithShake256Params(Asn1Sequence seq)
-    {
-        if (seq.Count > 2)
-            throw new InvalidOperationException("sequence size greater than 2");
+        public static KMacWithShake256Params GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new KMacWithShake256Params(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-        if (seq.Count == 2)
-        {
-            this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact;
-            this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[1]).GetOctets());
-        }
-        else if (seq.Count == 1)
-        {
-            if (seq[0] is DerInteger derInteger)
-            {
-                this.outputLength = derInteger.IntValueExact;
-                this.customizationString = EMPTY_STRING;
-            }
-            else
-            {
-                this.outputLength = DEF_LENGTH;
-                this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets());
-            }
-        }
-        else
+        private readonly DerInteger m_outputLength;
+        private readonly Asn1OctetString m_customizationString;
+
+        private KMacWithShake256Params(Asn1Sequence seq)
         {
-            this.outputLength = DEF_LENGTH;
-            this.customizationString = EMPTY_STRING;
-        }
-    }
+            int count = seq.Count, pos = 0;
+            if (count < 0 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-    public int OutputLength
-    {
-        get { return outputLength; }
-    }
+            m_outputLength = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional)
+                ?? DefaultOutputLength;
+            m_customizationString = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional)
+                ?? DefaultCustomizationString;
 
-    public byte[] CustomizationString
-    {
-        get { return Arrays.Clone(customizationString); }
-    }
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
+        }
 
-    public override Asn1Object ToAsn1Object()
-    {
-        Asn1EncodableVector v = new Asn1EncodableVector(2);
-        if (outputLength != DEF_LENGTH)
+        public KMacWithShake256Params(int outputLength)
         {
-            v.Add(new DerInteger(outputLength));
+            m_outputLength = new DerInteger(outputLength);
+            m_customizationString = DefaultCustomizationString;
         }
 
-        if (customizationString.Length != 0)
+        public KMacWithShake256Params(int outputLength, byte[] customizationString)
         {
-            v.Add(new DerOctetString(CustomizationString));
+            m_outputLength = new DerInteger(outputLength);
+            m_customizationString = new DerOctetString(customizationString);
         }
+        public int OutputLength => m_outputLength.IntValueExact;
+
+        public byte[] CustomizationString => Arrays.Clone(m_customizationString.GetOctets());
 
-        return new DerSequence(v);
+        public override Asn1Object ToAsn1Object()
+        {
+            Asn1EncodableVector v = new Asn1EncodableVector(2);
+            if (!m_outputLength.HasValue(_DefaultOutputLength))
+            {
+                v.Add(m_outputLength);
+            }
+            if (m_customizationString.GetOctetsLength() != 0)
+            {
+                v.Add(m_customizationString);
+            }
+            return new DerSequence(v);
+        }
     }
 }
-}