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/CcmParameters.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/CcmParameters.cs')
-rw-r--r-- | crypto/src/asn1/cms/CcmParameters.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/crypto/src/asn1/cms/CcmParameters.cs b/crypto/src/asn1/cms/CcmParameters.cs new file mode 100644 index 000000000..c9588adca --- /dev/null +++ b/crypto/src/asn1/cms/CcmParameters.cs @@ -0,0 +1,66 @@ +using System; + +using Org.BouncyCastle.Utilities; + +namespace Org.BouncyCastle.Asn1.Cms +{ + public class CcmParameters + : Asn1Encodable + { + private const int DefaultIcvLen = 12; + + private readonly byte[] m_nonce; + private readonly int m_icvLen; + + public static CcmParameters GetInstance(object obj) + { + if (obj == null) + return null; + if (obj is CcmParameters ccmParameters) + return ccmParameters; + return new CcmParameters(Asn1Sequence.GetInstance(obj)); + } + + public static CcmParameters GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) + { + return GetInstance(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); + } + + private CcmParameters(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 CcmParameters(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)); + } + } +} |