diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-16 14:18:27 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-02-16 14:18:27 +0700 |
commit | a4f880422d022680673d6902bf6f5530cc544091 (patch) | |
tree | bfc5048253e0876780965b5af04121c3cc761105 /crypto/src/asn1/cms/GcmParameters.cs | |
parent | Update copyright (diff) | |
download | BouncyCastle.NET-ed25519-a4f880422d022680673d6902bf6f5530cc544091.tar.xz |
ParameterUtilities support for CCM, GCM parameters
- see https://github.com/bcgit/bc-csharp/issues/354
Diffstat (limited to 'crypto/src/asn1/cms/GcmParameters.cs')
-rw-r--r-- | crypto/src/asn1/cms/GcmParameters.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/crypto/src/asn1/cms/GcmParameters.cs b/crypto/src/asn1/cms/GcmParameters.cs new file mode 100644 index 000000000..bc86d27fa --- /dev/null +++ b/crypto/src/asn1/cms/GcmParameters.cs @@ -0,0 +1,66 @@ +using System; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Asn1.Cms +{ + public class GcmParameters + : Asn1Encodable + { + private const int DefaultIcvLen = 12; + + private readonly byte[] m_nonce; + private readonly int m_icvLen; + + public static GcmParameters GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is GcmParameters gcmParameters) + return gcmParameters; + return new GcmParameters(Asn1Sequence.GetInstance(obj)); + } + + public static GcmParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private GcmParameters(Asn1Sequence seq) + { + int count = seq.Count; + if (count < 1 || count > 2) + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); + + m_nonce = Asn1OctetString.GetInstance(seq[0]).GetOctets(); + + if (count > 1) + { + m_icvLen = DerInteger.GetInstance(seq[1]).IntValueExact; + } + else + { + m_icvLen = DefaultIcvLen; + } + } + + public GcmParameters(byte[] nonce, int icvLen) + { + m_nonce = Arrays.Clone(nonce); + m_icvLen = icvLen; + } + + public byte[] GetNonce() => Arrays.Clone(m_nonce); + + public int IcvLen => m_icvLen; + + public override Asn1Object ToAsn1Object() + { + var nonce = new DerOctetString(m_nonce); + + return m_icvLen == DefaultIcvLen + ? new DerSequence(nonce) + : new DerSequence(nonce, new DerInteger(m_icvLen)); + } + } +} |