From c47af79f9a7928e62b6236d2973a91eb8259b9c6 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 1 Jul 2024 15:59:24 +0700 Subject: Refactoring in Asn1.Nist --- crypto/src/asn1/DerOctetString.cs | 4 +- crypto/src/asn1/nist/KMACwithSHAKE128_params.cs | 123 ++++++++++------------- crypto/src/asn1/nist/KMACwithSHAKE256_params.cs | 126 ++++++++++-------------- 3 files changed, 108 insertions(+), 145 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/DerOctetString.cs b/crypto/src/asn1/DerOctetString.cs index 7ea9940b3..3bce719df 100644 --- a/crypto/src/asn1/DerOctetString.cs +++ b/crypto/src/asn1/DerOctetString.cs @@ -5,7 +5,9 @@ namespace Org.BouncyCastle.Asn1 public class DerOctetString : Asn1OctetString { - /// The octets making up the octet string. + public static readonly DerOctetString Empty = new DerOctetString(EmptyOctets); + + /// The octets making up the octet string. public DerOctetString(byte[] contents) : base(contents) { diff --git a/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs index ca94b5bfe..3a03b9cb0 100644 --- a/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs +++ b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs @@ -1,6 +1,6 @@ -using Org.BouncyCastle.Utilities; -using System; +using System; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Nist { @@ -10,94 +10,75 @@ namespace Org.BouncyCastle.Asn1.Nist /// customizationString OCTET STRING DEFAULT ''H /// } /// -public class KMacWithShake128Params : Asn1Encodable -{ - private static readonly byte[] EMPTY_STRING = new byte[0]; - private static readonly int DEF_LENGTH = 256; - - private readonly int outputLength; - private readonly byte[] customizationString; - - public KMacWithShake128Params(int outputLength) + public class KMacWithShake128Params + : Asn1Encodable { - this.outputLength = outputLength; - this.customizationString = EMPTY_STRING; - } + private const int _DefaultOutputLength = 256; - public KMacWithShake128Params(int outputLength, byte[] customizationString) - { - this.outputLength = outputLength; - this.customizationString = Arrays.Clone(customizationString); - } + public static readonly DerInteger DefaultOutputLength = new DerInteger(_DefaultOutputLength); + public static readonly Asn1OctetString DefaultCustomizationString = DerOctetString.Empty; - public static KMacWithShake128Params GetInstance(object o) - { - if (o is KMacWithShake128Params) - { - return (KMacWithShake128Params)o; - } - else if (o != null) + public static KMacWithShake128Params GetInstance(object o) { + if (o == null) + return null; + if (o is KMacWithShake128Params kMacWithShake128Params) + return kMacWithShake128Params; return new KMacWithShake128Params(Asn1Sequence.GetInstance(o)); } - return null; - } + public static KMacWithShake128Params GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new KMacWithShake128Params(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); - private KMacWithShake128Params(Asn1Sequence seq) - { - if (seq.Count > 2) - throw new InvalidOperationException("sequence size greater than 2"); + public static KMacWithShake128Params GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => + new KMacWithShake128Params(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); + + private readonly DerInteger m_outputLength; + private readonly Asn1OctetString m_customizationString; - if (seq.Count == 2) + private KMacWithShake128Params(Asn1Sequence seq) { - this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact; - this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[1]).GetOctets()); + int count = seq.Count, pos = 0; + if (count < 0 || count > 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_outputLength = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional) + ?? DefaultOutputLength; + m_customizationString = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional) + ?? DefaultCustomizationString; + + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - else if (seq.Count == 1) + + public KMacWithShake128Params(int outputLength) { - 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()); - } + m_outputLength = new DerInteger(outputLength); + m_customizationString = DefaultCustomizationString; } - else + + public KMacWithShake128Params(int outputLength, byte[] customizationString) { - this.outputLength = DEF_LENGTH; - this.customizationString = EMPTY_STRING; + m_outputLength = new DerInteger(outputLength); + m_customizationString = new DerOctetString(customizationString); } - } - public int OutputLength - { - get { return outputLength; } - } + public int OutputLength => m_outputLength.IntValueExact; - public byte[] CustomizationString - { - get { return Arrays.Clone(customizationString); } - } + public byte[] CustomizationString => Arrays.Clone(m_customizationString.GetOctets()); - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector v = new Asn1EncodableVector(2); - if (outputLength != DEF_LENGTH) + public override Asn1Object ToAsn1Object() { - v.Add(new DerInteger(outputLength)); - } - - if (customizationString.Length != 0) - { - v.Add(new DerOctetString(CustomizationString)); + 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); } - - return new DerSequence(v); } } -} 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 /// } /// -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); + } } } -} -- cgit 1.4.1