|
| 1 | +using NUnit.Framework; |
| 2 | +using Org.BouncyCastle.Asn1.Nist; |
| 3 | +using Org.BouncyCastle.Utilities; |
| 4 | +using Org.BouncyCastle.Utilities.Test; |
| 5 | + |
| 6 | +namespace Org.BouncyCastle.Asn1.Tests |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class KMacParamsTest :SimpleTest |
| 10 | + { |
| 11 | + public override string Name => "KMacParamsTest"; |
| 12 | + |
| 13 | + |
| 14 | + public override void PerformTest() |
| 15 | + { |
| 16 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256).GetEncoded(), new DerSequence().GetEncoded())); |
| 17 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512).GetEncoded(), new DerSequence().GetEncoded())); |
| 18 | + |
| 19 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), new DerSequence(new DerInteger(512)).GetEncoded())); |
| 20 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), new DerSequence(new DerInteger(256)).GetEncoded())); |
| 21 | + |
| 22 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512).GetEncoded(), KMACwithSHAKE128_params.getInstance(new DerSequence(new DerInteger(512))).GetEncoded())); |
| 23 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256).GetEncoded(), KMACwithSHAKE256_params.getInstance(new DerSequence(new DerInteger(256))).GetEncoded())); |
| 24 | + |
| 25 | + byte[] customizationString = Strings.ToByteArray("hello, world!"); |
| 26 | + |
| 27 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), new DerSequence( |
| 28 | + new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) }).GetEncoded())); |
| 29 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), new DerSequence( |
| 30 | + new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) }).GetEncoded())); |
| 31 | + |
| 32 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(512, customizationString).GetEncoded(), |
| 33 | + KMACwithSHAKE128_params.getInstance( |
| 34 | + new DerSequence(new Asn1Encodable[] { new DerInteger(512), new DerOctetString(customizationString) })).GetEncoded())); |
| 35 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(256, customizationString).GetEncoded(), |
| 36 | + KMACwithSHAKE256_params.getInstance(new DerSequence( |
| 37 | + new Asn1Encodable[] { new DerInteger(256), new DerOctetString(customizationString) })).GetEncoded())); |
| 38 | + |
| 39 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), new DerSequence( |
| 40 | + new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded())); |
| 41 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), new DerSequence( |
| 42 | + new Asn1Encodable[] { new DerOctetString(customizationString) }).GetEncoded())); |
| 43 | + |
| 44 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE128_params(256, customizationString).GetEncoded(), |
| 45 | + KMACwithSHAKE128_params.getInstance( |
| 46 | + new DerSequence(new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded())); |
| 47 | + Assert.IsTrue(Arrays.AreEqual(new KMACwithSHAKE256_params(512, customizationString).GetEncoded(), |
| 48 | + KMACwithSHAKE256_params.getInstance(new DerSequence( |
| 49 | + new Asn1Encodable[] { new DerOctetString(customizationString) })).GetEncoded())); |
| 50 | + |
| 51 | + KMACwithSHAKE128_params p128 = new KMACwithSHAKE128_params(256, customizationString); |
| 52 | + Assert.AreEqual(256, p128.OutputLength); |
| 53 | + Assert.IsTrue(Arrays.AreEqual(customizationString, p128.CustomizationString)); |
| 54 | + Assert.IsTrue(p128 == KMACwithSHAKE128_params.getInstance(p128)); |
| 55 | + |
| 56 | + KMACwithSHAKE256_params p256 = new KMACwithSHAKE256_params(512, customizationString); |
| 57 | + Assert.AreEqual(512, p256.OutputLength); |
| 58 | + Assert.IsTrue(Arrays.AreEqual(customizationString, p256.CustomizationString)); |
| 59 | + Assert.IsTrue(p256 == KMACwithSHAKE256_params.getInstance(p256)); |
| 60 | + |
| 61 | + p128 = new KMACwithSHAKE128_params(512); |
| 62 | + Assert.AreEqual(512, p128.OutputLength); |
| 63 | + Assert.IsTrue(Arrays.AreEqual(new byte[0], p128.CustomizationString)); |
| 64 | + |
| 65 | + p256 = new KMACwithSHAKE256_params(256); |
| 66 | + Assert.AreEqual(256, p256.OutputLength); |
| 67 | + Assert.IsTrue(Arrays.AreEqual(new byte[0], p256.CustomizationString)); |
| 68 | + } |
| 69 | + |
| 70 | + public static void Main( |
| 71 | + string[] args) |
| 72 | + { |
| 73 | + RunTest(new KMacParamsTest()); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void TestFunction() |
| 78 | + { |
| 79 | + string resultText = Perform().ToString(); |
| 80 | + |
| 81 | + Assert.AreEqual(resultText, Name + ": Okay", resultText); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments