From b4763830b493e40673dd6552f9f21fa44528b999 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 15 Feb 2023 01:47:29 +0700 Subject: Separate Ascon Hash, XOF --- crypto/test/src/crypto/test/AsconTest.cs | 87 ++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 11 deletions(-) (limited to 'crypto/test') diff --git a/crypto/test/src/crypto/test/AsconTest.cs b/crypto/test/src/crypto/test/AsconTest.cs index 95dd9c210..59e5aaa6d 100644 --- a/crypto/test/src/crypto/test/AsconTest.cs +++ b/crypto/test/src/crypto/test/AsconTest.cs @@ -24,13 +24,13 @@ namespace Org.BouncyCastle.Crypto.Tests { ImplTestVectorsHash(AsconDigest.AsconParameters.AsconHashA, "asconhasha"); ImplTestVectorsHash(AsconDigest.AsconParameters.AsconHash, "asconhash"); - ImplTestVectorsHash(AsconDigest.AsconParameters.AsconXof, "asconxof"); - ImplTestVectorsHash(AsconDigest.AsconParameters.AsconXofA, "asconxofa"); + ImplTestVectorsXof(AsconXof.AsconParameters.AsconXof, "asconxof"); + ImplTestVectorsXof(AsconXof.AsconParameters.AsconXofA, "asconxofa"); ImplTestExceptions(new AsconDigest(AsconDigest.AsconParameters.AsconHashA), 32); ImplTestExceptions(new AsconDigest(AsconDigest.AsconParameters.AsconHash), 32); - ImplTestExceptions(new AsconDigest(AsconDigest.AsconParameters.AsconXof), 32); - ImplTestExceptions(new AsconDigest(AsconDigest.AsconParameters.AsconXofA), 32); + ImplTestExceptions(new AsconXof(AsconXof.AsconParameters.AsconXof), 32); + ImplTestExceptions(new AsconXof(AsconXof.AsconParameters.AsconXofA), 32); AsconEngine asconEngine = new AsconEngine(AsconEngine.AsconParameters.ascon80pq); ImplTestExceptions(asconEngine); @@ -439,11 +439,12 @@ namespace Org.BouncyCastle.Crypto.Tests "GetOutputSize of " + asconEngine.AlgorithmName + " is incorrect for decryption"); } - private void ImplTestVectorsHash(AsconDigest.AsconParameters AsconParameters, string filename) + private void ImplTestVectorsHash(AsconDigest.AsconParameters asconParameters, string filename) { - AsconDigest Ascon = new AsconDigest(AsconParameters); + AsconDigest ascon = new AsconDigest(asconParameters); var buf = new Dictionary(); - using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("crypto.ascon."+filename+"_LWC_HASH_KAT_256.txt"))) + using (var src = new StreamReader( + SimpleTest.GetTestDataAsStream("crypto.ascon." + filename + "_LWC_HASH_KAT_256.txt"))) { Dictionary map = new Dictionary(); string line; @@ -456,11 +457,50 @@ 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.DoFinal(hash, 0); + ascon.BlockUpdate(ptByte, 0, ptByte.Length); + byte[] hash = new byte[ascon.GetDigestSize()]; + ascon.DoFinal(hash, 0); Assert.True(Arrays.AreEqual(expected, hash)); - Ascon.Reset(); + ascon.Reset(); + } + else + { + if (data.Length >= 3) + { + map[data[0].Trim()] = data[2].Trim(); + } + else + { + map[data[0].Trim()] = ""; + } + } + } + } + } + + private void ImplTestVectorsXof(AsconXof.AsconParameters asconParameters, string filename) + { + AsconXof ascon = new AsconXof(asconParameters); + var buf = new Dictionary(); + using (var src = new StreamReader( + SimpleTest.GetTestDataAsStream("crypto.ascon." + filename + "_LWC_HASH_KAT_256.txt"))) + { + Dictionary map = new Dictionary(); + string line; + while ((line = src.ReadLine()) != null) + { + var data = line.Split(' '); + if (data.Length == 1) + { + byte[] ptByte = Hex.Decode(map["Msg"]); + byte[] expected = Hex.Decode(map["MD"]); + map.Clear(); + + ascon.BlockUpdate(ptByte, 0, ptByte.Length); + byte[] hash = new byte[ascon.GetDigestSize()]; + ascon.DoFinal(hash, 0); + Assert.True(Arrays.AreEqual(expected, hash)); + ascon.Reset(); } else { @@ -501,5 +541,30 @@ namespace Org.BouncyCastle.Crypto.Tests //expected } } + + private void ImplTestExceptions(AsconXof asconXof, int digestSize) + { + Assert.AreEqual(digestSize, asconXof.GetDigestSize(), + asconXof.AlgorithmName + ": digest size is not correct"); + + try + { + asconXof.BlockUpdate(new byte[1], 1, 1); + Assert.Fail(asconXof.AlgorithmName + ": input for BlockUpdate is too short"); + } + catch (DataLengthException) + { + //expected + } + try + { + asconXof.DoFinal(new byte[digestSize - 1], 2); + Assert.Fail(asconXof.AlgorithmName + ": output for DoFinal is too short"); + } + catch (OutputLengthException) + { + //expected + } + } } } -- cgit 1.4.1