summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authormw <megan@cryptoworkshop.com>2020-10-29 10:22:06 +1100
committermw <megan@cryptoworkshop.com>2020-10-29 10:22:06 +1100
commit6536ed9e332d54431d00913685c02a3cf6bb287f (patch)
tree744309bd6f1c09c2268714bcabf422bb41375f0d /crypto
parentAdded CSHAKEDigest, KMac, removed unused import from NewTspTest (diff)
downloadBouncyCastle.NET-ed25519-6536ed9e332d54431d00913685c02a3cf6bb287f.tar.xz
Added KMAC Params and test
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/asn1/nist/KMACwithSHAKE128_params.cs112
-rw-r--r--crypto/src/asn1/nist/KMACwithSHAKE256_params.cs111
-rw-r--r--crypto/test/src/asn1/test/KMacParamsTest.cs84
-rw-r--r--crypto/test/src/asn1/test/RegressionTest.cs45
4 files changed, 330 insertions, 22 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);
+    }
+}
+}
diff --git a/crypto/test/src/asn1/test/KMacParamsTest.cs b/crypto/test/src/asn1/test/KMacParamsTest.cs
new file mode 100644
index 000000000..1c080f755
--- /dev/null
+++ b/crypto/test/src/asn1/test/KMacParamsTest.cs
@@ -0,0 +1,84 @@
+using NUnit.Framework;
+using Org.BouncyCastle.Asn1.Nist;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Asn1.Tests
+{
+    [TestFixture]
+    public class KMacParamsTest  :SimpleTest
+    {
+        public override string Name => "KMacParamsTest";
+
+      
+        public override void PerformTest()
+        {
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256).GetEncoded(), new DerSequence().GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512).GetEncoded(), new DerSequence().GetEncoded()));
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), new DerSequence(new DerInteger(512)).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), new DerSequence(new DerInteger(256)).GetEncoded()));
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), KMACwithSHAKE128_params.getInstance(new DerSequence(new DerInteger(512))).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), KMACwithSHAKE256_params.getInstance(new DerSequence(new DerInteger(256))).GetEncoded()));
+
+            byte[] customizationString = Strings.ToByteArray("hello, world!");
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), new DerSequence(
+                new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) }).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), new DerSequence(
+                new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) }).GetEncoded()));
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(),
+                KMACwithSHAKE128_params.getInstance(
+                    new DerSequence(new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) })).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(),
+                KMACwithSHAKE256_params.getInstance(new DerSequence(
+                new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) })).GetEncoded()));
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), new DerSequence(
+                new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), new DerSequence(
+                new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded()));
+
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(),
+                KMACwithSHAKE128_params.getInstance(
+                    new DerSequence(new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded()));
+            Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(),
+                KMACwithSHAKE256_params.getInstance(new DerSequence(
+                new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded()));
+
+            KMACwithSHAKE128_params p128 = new KMACwithSHAKE128_params(256, customizationString);
+            Assert.AreEqual(256, p128.OutputLength);
+            Assert.IsTrue(Arrays.AreEqual(customizationString, p128.CustomizationString));
+            Assert.IsTrue(p128 == KMACwithSHAKE128_params.getInstance(p128));
+
+            KMACwithSHAKE256_params p256 = new KMACwithSHAKE256_params(512, customizationString);
+            Assert.AreEqual(512, p256.OutputLength);
+            Assert.IsTrue(Arrays.AreEqual(customizationString, p256.CustomizationString));
+            Assert.IsTrue(p256 == KMACwithSHAKE256_params.getInstance(p256));
+
+            p128 = new KMACwithSHAKE128_params(512);
+            Assert.AreEqual(512, p128.OutputLength);
+            Assert.IsTrue(Arrays.AreEqual(new byte[0], p128.CustomizationString));
+
+            p256 = new KMACwithSHAKE256_params(256);
+            Assert.AreEqual(256, p256.OutputLength);
+            Assert.IsTrue(Arrays.AreEqual(new byte[0], p256.CustomizationString));
+        }
+
+        public static void Main(
+            string[] args)
+        {
+            RunTest(new KMacParamsTest());
+        }
+
+        [Test]
+        public void TestFunction()
+        {
+            string resultText = Perform().ToString();
+
+            Assert.AreEqual(resultText, Name + ": Okay", resultText);
+        }
+    }
+}
diff --git a/crypto/test/src/asn1/test/RegressionTest.cs b/crypto/test/src/asn1/test/RegressionTest.cs
index 67860ccd7..cf10c6c3b 100644
--- a/crypto/test/src/asn1/test/RegressionTest.cs
+++ b/crypto/test/src/asn1/test/RegressionTest.cs
@@ -6,20 +6,20 @@ namespace Org.BouncyCastle.Asn1.Tests
 {
     public class RegressionTest
     {
-        public static readonly ITest[] tests =
-        {
+		public static readonly ITest[] tests =
+		{
 			new AdditionalInformationSyntaxUnitTest(),
 			new AdmissionSyntaxUnitTest(),
 			new AdmissionsUnitTest(),
-            new Asn1IntegerTest(),
+			new Asn1IntegerTest(),
 			new AttributeTableUnitTest(),
-            new BiometricDataUnitTest(),
-            new BitStringTest(),
+			new BiometricDataUnitTest(),
+			new BitStringTest(),
 			new CertHashUnitTest(),
 			new CertificateTest(),
-            new CmsTest(),
-            new CommitmentTypeIndicationUnitTest(),
-            new CommitmentTypeQualifierUnitTest(),
+			new CmsTest(),
+			new CommitmentTypeIndicationUnitTest(),
+			new CommitmentTypeQualifierUnitTest(),
 			new ContentHintsUnitTest(),
 			new CscaMasterListTest(),
 			new DataGroupHashUnitTest(),
@@ -27,33 +27,33 @@ namespace Org.BouncyCastle.Asn1.Tests
 			new DerApplicationSpecificTest(),
 			new DerUtf8StringTest(),
 			new EncryptedPrivateKeyInfoTest(),
-            new EqualsAndHashCodeTest(),
+			new EqualsAndHashCodeTest(),
 			new EssCertIDv2UnitTest(),
-            new GeneralizedTimeTest(),
+			new GeneralizedTimeTest(),
 			new GeneralNameTest(),
 			new GenerationTest(),
 			new InputStreamTest(),
-            new Iso4217CurrencyCodeUnitTest(),
+			new Iso4217CurrencyCodeUnitTest(),
 			new IssuingDistributionPointUnitTest(),
 			new KeyUsageTest(),
-            new LDSSecurityObjectUnitTest(),
-            new LinkedCertificateTest(),
-            new MiscTest(),
+			new LDSSecurityObjectUnitTest(),
+			new LinkedCertificateTest(),
+			new MiscTest(),
 			new MonetaryLimitUnitTest(),
 			new MonetaryValueUnitTest(),
 			new NameOrPseudonymUnitTest(),
 			new NamingAuthorityUnitTest(),
 			new NetscapeCertTypeTest(),
-            new OcspTest(),
-            new OidTest(),
+			new OcspTest(),
+			new OidTest(),
 			new OtherCertIDUnitTest(),
 			new OtherSigningCertificateUnitTest(),
 			new ParsingTest(),
 			new PersonalDataUnitTest(),
 			new Pkcs10Test(),
-            new Pkcs12Test(),
+			new Pkcs12Test(),
 			new PkiFailureInfoTest(),
-            new PrivateKeyInfoTest(),
+			new PrivateKeyInfoTest(),
 			new ProcurationSyntaxUnitTest(),
 			new ProfessionInfoUnitTest(),
 			new QCStatementUnitTest(),
@@ -61,9 +61,9 @@ namespace Org.BouncyCastle.Asn1.Tests
 			new RequestedCertificateUnitTest(),
 			new RestrictionUnitTest(),
 			new SemanticsInformationUnitTest(),
-            new SetTest(),
-            new SignerLocationUnitTest(),
-            new SmimeTest(),
+			new SetTest(),
+			new SignerLocationUnitTest(),
+			new SmimeTest(),
 			new StringTest(),
 			new SubjectKeyIdentifierTest(),
 			new TagTest(),
@@ -72,7 +72,8 @@ namespace Org.BouncyCastle.Asn1.Tests
 			new UtcTimeTest(),
 			new X509ExtensionsTest(),
 			new X509NameTest(),
-            new X9Test(),
+			new X9Test(),
+			new KMacParamsTest()
         };
 
         public static void Main(