From ad05b5af90b1c3bd687829d62f7ef5195c0a4406 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Thu, 18 May 2023 18:16:57 +0700 Subject: Refactor AsconTest and SparkleTest --- crypto/test/src/crypto/test/AsconTest.cs | 39 +++++++++++++++++++++++++++--- crypto/test/src/crypto/test/SparkleTest.cs | 23 +++++++++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) (limited to 'crypto') diff --git a/crypto/test/src/crypto/test/AsconTest.cs b/crypto/test/src/crypto/test/AsconTest.cs index b649cfa86..835665ad0 100644 --- a/crypto/test/src/crypto/test/AsconTest.cs +++ b/crypto/test/src/crypto/test/AsconTest.cs @@ -586,6 +586,17 @@ namespace Org.BouncyCastle.Crypto.Tests { //expected } + ascon.Init(true, param); + try + { + int need = ascon.GetUpdateOutputSize(64); + ascon.ProcessBytes(new byte[64], 0, 64, new byte[need], 1); + Assert.Fail("output for ProcessBytes is too short"); + } + catch (OutputLengthException) + { + //expected + } try { ascon.DoFinal(new byte[2], 2); @@ -695,6 +706,7 @@ namespace Org.BouncyCastle.Crypto.Tests offset = ascon.ProcessBytes(m7, 0, split, c9, 0); offset += ascon.ProcessBytes(m7, split, m7.Length - split, c9, offset); offset += ascon.DoFinal(c9, offset); + if (!Arrays.AreEqual(c7, c8) || !Arrays.AreEqual(c7, c9)) { Assert.Fail("Splitting input of plaintext should output the same ciphertext"); @@ -825,6 +837,7 @@ namespace Org.BouncyCastle.Crypto.Tests private static void ImplTestVectorsDigest(AsconDigest.AsconParameters asconParameters, string filename) { + Random random = new Random(); var ascon = CreateDigest(asconParameters); var map = new Dictionary(); using (var src = new StreamReader( @@ -840,10 +853,20 @@ namespace Org.BouncyCastle.Crypto.Tests byte[] expected = Hex.Decode(map["MD"]); map.Clear(); - ascon.BlockUpdate(ptByte, 0, ptByte.Length); byte[] hash = new byte[ascon.GetDigestSize()]; + + ascon.BlockUpdate(ptByte, 0, ptByte.Length); ascon.DoFinal(hash, 0); Assert.True(Arrays.AreEqual(expected, hash)); + + if (ptByte.Length > 1) + { + int split = random.Next(1, ptByte.Length); + ascon.BlockUpdate(ptByte, 0, split); + ascon.BlockUpdate(ptByte, split, ptByte.Length - split); + ascon.DoFinal(hash, 0); + Assert.IsTrue(Arrays.AreEqual(expected, hash)); + } } else { @@ -865,7 +888,6 @@ namespace Org.BouncyCastle.Crypto.Tests Random random = new Random(); var asconEngine = CreateEngine(asconParameters); var buf = new Dictionary(); - //TestSampler sampler = new TestSampler(); using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("crypto.ascon.LWC_AEAD_KAT_" + filename + ".txt"))) { Dictionary map = new Dictionary(); @@ -929,6 +951,7 @@ namespace Org.BouncyCastle.Crypto.Tests private static void ImplTestVectorsXof(AsconXof.AsconParameters asconParameters, string filename) { + Random random = new Random(); var ascon = CreateXof(asconParameters); var buf = new Dictionary(); using (var src = new StreamReader( @@ -945,10 +968,20 @@ namespace Org.BouncyCastle.Crypto.Tests byte[] expected = Hex.Decode(map["MD"]); map.Clear(); - ascon.BlockUpdate(ptByte, 0, ptByte.Length); byte[] hash = new byte[ascon.GetDigestSize()]; + + ascon.BlockUpdate(ptByte, 0, ptByte.Length); ascon.DoFinal(hash, 0); Assert.True(Arrays.AreEqual(expected, hash)); + + if (ptByte.Length > 1) + { + int split = random.Next(1, ptByte.Length); + ascon.BlockUpdate(ptByte, 0, split); + ascon.BlockUpdate(ptByte, split, ptByte.Length - split); + ascon.DoFinal(hash, 0); + Assert.IsTrue(Arrays.AreEqual(expected, hash)); + } } else { diff --git a/crypto/test/src/crypto/test/SparkleTest.cs b/crypto/test/src/crypto/test/SparkleTest.cs index 712223a86..425aee2c2 100644 --- a/crypto/test/src/crypto/test/SparkleTest.cs +++ b/crypto/test/src/crypto/test/SparkleTest.cs @@ -366,7 +366,7 @@ namespace Org.BouncyCastle.Crypto.Tests private static void ImplTestExceptionsDigest(SparkleDigest.SparkleParameters sparkleParameters) { - var sparkle = new SparkleDigest(sparkleParameters); + var sparkle = CreateDigest(sparkleParameters); try { @@ -383,7 +383,7 @@ namespace Org.BouncyCastle.Crypto.Tests sparkle.DoFinal(new byte[sparkle.GetDigestSize() - 1], 2); Assert.Fail(sparkle.AlgorithmName + ": output for Dofinal is too short"); } - catch (DataLengthException) + catch (OutputLengthException) { //expected } @@ -391,7 +391,7 @@ namespace Org.BouncyCastle.Crypto.Tests private void ImplTestExceptionsEngine(SparkleEngine.SparkleParameters sparkleParameters) { - var sparkle = new SparkleEngine(sparkleParameters); + var sparkle = CreateEngine(sparkleParameters); int keysize = sparkle.GetKeyBytesSize(), ivsize = sparkle.GetIVBytesSize(); int offset; @@ -548,6 +548,17 @@ namespace Org.BouncyCastle.Crypto.Tests { //expected } + sparkle.Init(true, param); + try + { + int need = sparkle.GetUpdateOutputSize(64); + sparkle.ProcessBytes(new byte[64], 0, 64, new byte[need], 1); + Assert.Fail("output for ProcessBytes is too short"); + } + catch (OutputLengthException) + { + //expected + } try { sparkle.DoFinal(new byte[2], 2); @@ -639,10 +650,9 @@ namespace Org.BouncyCastle.Crypto.Tests byte[] c7 = new byte[sparkle.GetOutputSize(m7.Length)]; byte[] c8 = new byte[c7.Length]; byte[] c9 = new byte[c7.Length]; - sparkle.Init(true, param); sparkle.ProcessAadBytes(aad2, 0, aad2.Length); offset = sparkle.ProcessBytes(m7, 0, m7.Length, c7, 0); - sparkle.DoFinal(c7, offset); + offset += sparkle.DoFinal(c7, offset); // TODO Maybe use a different IV for this sparkle.Init(true, param); @@ -658,6 +668,7 @@ namespace Org.BouncyCastle.Crypto.Tests offset = sparkle.ProcessBytes(m7, 0, split, c9, 0); offset += sparkle.ProcessBytes(m7, split, m7.Length - split, c9, offset); offset += sparkle.DoFinal(c9, offset); + if (!Arrays.AreEqual(c7, c8) || !Arrays.AreEqual(c7, c9)) { Assert.Fail("Splitting input of plaintext should output the same ciphertext"); @@ -780,7 +791,7 @@ namespace Org.BouncyCastle.Crypto.Tests if (ptByte.Length > 1) { - int split = random.Next(1, ptByte.Length - 1); + int split = random.Next(1, ptByte.Length); sparkle.BlockUpdate(ptByte, 0, split); sparkle.BlockUpdate(ptByte, split, ptByte.Length - split); sparkle.DoFinal(hash, 0); -- cgit 1.4.1