From 8699aa5df9a0c4f21fe8540fea6e4f903690e18d Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 21 Jun 2024 13:57:57 +0700 Subject: Refactoring in Asn1.Misc --- crypto/src/asn1/misc/CAST5CBCParameters.cs | 74 ++++++++++++++++-------------- crypto/src/asn1/misc/IDEACBCPar.cs | 58 ++++++++++++----------- 2 files changed, 70 insertions(+), 62 deletions(-) (limited to 'crypto') diff --git a/crypto/src/asn1/misc/CAST5CBCParameters.cs b/crypto/src/asn1/misc/CAST5CBCParameters.cs index 3adafe05f..1dc3dbe39 100644 --- a/crypto/src/asn1/misc/CAST5CBCParameters.cs +++ b/crypto/src/asn1/misc/CAST5CBCParameters.cs @@ -1,6 +1,5 @@ using System; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Misc @@ -8,51 +7,48 @@ namespace Org.BouncyCastle.Asn1.Misc public class Cast5CbcParameters : Asn1Encodable { - private readonly DerInteger keyLength; - private readonly Asn1OctetString iv; - - public static Cast5CbcParameters GetInstance( - object o) + public static Cast5CbcParameters GetInstance(object o) { - if (o is Cast5CbcParameters) - { - return (Cast5CbcParameters) o; - } - - if (o is Asn1Sequence) - { - return new Cast5CbcParameters((Asn1Sequence) o); - } - - throw new ArgumentException("unknown object in Cast5CbcParameters factory"); + if (o == null) + return null; + if (o is Cast5CbcParameters cast5CbcParameters) + return cast5CbcParameters; + return new Cast5CbcParameters(Asn1Sequence.GetInstance(o)); } - public Cast5CbcParameters( - byte[] iv, - int keyLength) + public static Cast5CbcParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) { - this.iv = new DerOctetString(iv); - this.keyLength = new DerInteger(keyLength); + return new Cast5CbcParameters(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); } - private Cast5CbcParameters( - Asn1Sequence seq) + private readonly Asn1OctetString m_iv; + private readonly DerInteger m_keyLength; + + private Cast5CbcParameters(Asn1Sequence seq) { - if (seq.Count != 2) - throw new ArgumentException("Wrong number of elements in sequence", "seq"); + int count = seq.Count, pos = 0; + if (count < 1 || count > 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_iv = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional) + ?? new DerOctetString(new byte[8]); + m_keyLength = DerInteger.GetInstance(seq[pos++]); - iv = (Asn1OctetString) seq[0]; - keyLength = (DerInteger) seq[1]; + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - public Asn1OctetString IV => iv; + public Cast5CbcParameters(byte[] iv, int keyLength) + { + m_iv = new DerOctetString(iv ?? new byte[8]); + m_keyLength = new DerInteger(keyLength); + } + + public Asn1OctetString IV => m_iv; - public byte[] GetIV() => Arrays.Clone(iv.GetOctets()); + public byte[] GetIV() => Arrays.Clone(m_iv.GetOctets()); - public int KeyLength - { - get { return keyLength.IntValueExact; } - } + public int KeyLength => m_keyLength.IntValueExact; /** * Produce an object suitable for an Asn1OutputStream. @@ -67,7 +63,15 @@ namespace Org.BouncyCastle.Asn1.Misc */ public override Asn1Object ToAsn1Object() { - return new DerSequence(iv, keyLength); + return IsDefaultIV(m_iv) + ? new DerSequence(m_keyLength) + : new DerSequence(m_iv, m_keyLength); + } + + private static bool IsDefaultIV(Asn1OctetString iv) + { + return iv.GetOctetsLength() == 8 + && Arrays.AreAllZeroes(iv.GetOctets(), 0, 8); } } } diff --git a/crypto/src/asn1/misc/IDEACBCPar.cs b/crypto/src/asn1/misc/IDEACBCPar.cs index 9b73f5729..21eabea52 100644 --- a/crypto/src/asn1/misc/IDEACBCPar.cs +++ b/crypto/src/asn1/misc/IDEACBCPar.cs @@ -1,6 +1,5 @@ using System; -using Org.BouncyCastle.Asn1; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Asn1.Misc @@ -8,42 +7,47 @@ namespace Org.BouncyCastle.Asn1.Misc public class IdeaCbcPar : Asn1Encodable { - internal Asn1OctetString iv; + public static IdeaCbcPar GetInstance(object o) + { + if (o == null) + return null; + if (o is IdeaCbcPar ideaCbcPar) + return ideaCbcPar; + return new IdeaCbcPar(Asn1Sequence.GetInstance(o)); + } + + public static IdeaCbcPar GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return new IdeaCbcPar(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private readonly Asn1OctetString m_iv; - public static IdeaCbcPar GetInstance( - object o) + private IdeaCbcPar(Asn1Sequence seq) { - if (o is IdeaCbcPar) - { - return (IdeaCbcPar) o; - } + int count = seq.Count, pos = 0; + if (count < 0 || count > 1) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); - if (o is Asn1Sequence) - { - return new IdeaCbcPar((Asn1Sequence) o); - } + m_iv = Asn1Utilities.ReadOptional(seq, ref pos, Asn1OctetString.GetOptional); - throw new ArgumentException("unknown object in IDEACBCPar factory"); + if (pos != count) + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); } - public IdeaCbcPar( - byte[] iv) + public IdeaCbcPar() + : this(iv: null) { - this.iv = new DerOctetString(iv); } - private IdeaCbcPar( - Asn1Sequence seq) + public IdeaCbcPar(byte[] iv) { - if (seq.Count == 1) - { - iv = (Asn1OctetString) seq[0]; - } + m_iv = iv == null ? null : new DerOctetString(iv); } - public Asn1OctetString IV => iv; + public Asn1OctetString IV => m_iv; - public byte[] GetIV() => Arrays.Clone(iv.GetOctets()); + public byte[] GetIV() => Arrays.Clone(m_iv?.GetOctets()); /** * Produce an object suitable for an Asn1OutputStream. @@ -55,9 +59,9 @@ namespace Org.BouncyCastle.Asn1.Misc */ public override Asn1Object ToAsn1Object() { - return iv != null - ? new DerSequence(iv) - : DerSequence.Empty; + return m_iv == null + ? DerSequence.Empty + : new DerSequence(m_iv); } } } -- cgit 1.4.1