diff options
author | mw <megan@cryptoworkshop.com> | 2020-10-29 10:22:06 +1100 |
---|---|---|
committer | mw <megan@cryptoworkshop.com> | 2020-10-29 10:22:06 +1100 |
commit | 6536ed9e332d54431d00913685c02a3cf6bb287f (patch) | |
tree | 744309bd6f1c09c2268714bcabf422bb41375f0d /crypto/src | |
parent | Added CSHAKEDigest, KMac, removed unused import from NewTspTest (diff) | |
download | BouncyCastle.NET-ed25519-6536ed9e332d54431d00913685c02a3cf6bb287f.tar.xz |
Added KMAC Params and test
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/asn1/nist/KMACwithSHAKE128_params.cs | 112 | ||||
-rw-r--r-- | crypto/src/asn1/nist/KMACwithSHAKE256_params.cs | 111 |
2 files changed, 223 insertions, 0 deletions
diff --git a/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs new file mode 100644 index 000000000..cd73ce2bc --- /dev/null +++ b/crypto/src/asn1/nist/KMACwithSHAKE128_params.cs @@ -0,0 +1,112 @@ +using Org.BouncyCastle.Utilities; +using System; + + +namespace Org.BouncyCastle.Asn1.Nist +{ + /// <summary> + /// KMACwithSHAKE128-params ::= SEQUENCE { + /// kMACOutputLength INTEGER DEFAULT 256, -- Output length in bits + /// customizationString OCTET STRING DEFAULT ''H + /// } + /// </summary> +public class KMACwithSHAKE128_params : 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 KMACwithSHAKE128_params(int outputLength) + { + this.outputLength = outputLength; + this.customizationString = EMPTY_STRING; + } + + public KMACwithSHAKE128_params(int outputLength, byte[] customizationString) + { + this.outputLength = outputLength; + this.customizationString = Arrays.Clone(customizationString); + } + + + public static KMACwithSHAKE128_params getInstance(Object o) + { + if (o is KMACwithSHAKE128_params) + { + return (KMACwithSHAKE128_params)o; + } + else if (o != null) + { + return new KMACwithSHAKE128_params(Asn1Sequence.GetInstance(o)); + } + + return null; + } + + + private KMACwithSHAKE128_params(Asn1Sequence seq) + { + if (seq.Count > 2) + { + throw new InvalidOperationException("sequence size greater than 2"); + } + + 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) + { + this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact; + this.customizationString = EMPTY_STRING; + } + else + { + this.outputLength = DEF_LENGTH; + this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets()); + } + } + else + { + this.outputLength = DEF_LENGTH; + this.customizationString = EMPTY_STRING; + } + } + + + + public int OutputLength + { + get { return outputLength; } + } + + public byte[] CustomizationString + { + get { return Arrays.Clone(customizationString); } + } + + + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + if (outputLength != DEF_LENGTH) + { + v.Add(new DerInteger(outputLength)); + } + + if (customizationString.Length != 0) + { + v.Add(new DerOctetString(CustomizationString)); + } + + return new DerSequence(v); + } +} +} diff --git a/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs new file mode 100644 index 000000000..e70fc807d --- /dev/null +++ b/crypto/src/asn1/nist/KMACwithSHAKE256_params.cs @@ -0,0 +1,111 @@ +using Org.BouncyCastle.Utilities; +using System; + +namespace Org.BouncyCastle.Asn1.Nist +{ + /// <summary> + /// KMACwithSHAKE256-params ::= SEQUENCE { + /// kMACOutputLength INTEGER DEFAULT 512, -- Output length in bits + /// customizationString OCTET STRING DEFAULT ''H + /// } + /// </summary> +public class KMACwithSHAKE256_params : 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 KMACwithSHAKE256_params(int outputLength) + { + this.outputLength = outputLength; + this.customizationString = EMPTY_STRING; + } + + public KMACwithSHAKE256_params(int outputLength, byte[] customizationString) + { + this.outputLength = outputLength; + this.customizationString = Arrays.Clone(customizationString); + } + + + public static KMACwithSHAKE256_params getInstance(Object o) + { + if (o is KMACwithSHAKE256_params) + { + return (KMACwithSHAKE256_params)o; + } + else if (o != null) + { + return new KMACwithSHAKE256_params(Asn1Sequence.GetInstance(o)); + } + + return null; + } + + + private KMACwithSHAKE256_params(Asn1Sequence seq) + { + if (seq.Count > 2) + { + throw new InvalidOperationException("sequence size greater than 2"); + } + + 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) + { + this.outputLength = DerInteger.GetInstance(seq[0]).IntValueExact; + this.customizationString = EMPTY_STRING; + } + else + { + this.outputLength = DEF_LENGTH; + this.customizationString = Arrays.Clone(Asn1OctetString.GetInstance(seq[0]).GetOctets()); + } + } + else + { + this.outputLength = DEF_LENGTH; + this.customizationString = EMPTY_STRING; + } + } + + + + public int OutputLength + { + get { return outputLength; } + } + + public byte[] CustomizationString + { + get { return Arrays.Clone(customizationString); } + } + + + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + if (outputLength != DEF_LENGTH) + { + v.Add(new DerInteger(outputLength)); + } + + if (customizationString.Length != 0) + { + v.Add(new DerOctetString(CustomizationString)); + } + + return new DerSequence(v); + } +} +} |