From ac9e78b9c9bea20a4419187be7f7ba35beadb1a1 Mon Sep 17 00:00:00 2001 From: gefeili Date: Wed, 28 Dec 2022 14:25:10 +1030 Subject: Initial push of Elephant v2 --- crypto/test/src/crypto/test/ElephantTest.cs | 92 +++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 crypto/test/src/crypto/test/ElephantTest.cs (limited to 'crypto/test/src') diff --git a/crypto/test/src/crypto/test/ElephantTest.cs b/crypto/test/src/crypto/test/ElephantTest.cs new file mode 100644 index 000000000..ff350b338 --- /dev/null +++ b/crypto/test/src/crypto/test/ElephantTest.cs @@ -0,0 +1,92 @@ +using System; +using NUnit.Framework; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Encoders; +using Org.BouncyCastle.Utilities.Test; +using System.Collections.Generic; +using System.IO; +using Org.BouncyCastle.Crypto.Engines; + + +namespace BouncyCastle.Crypto.Tests +{ + public class ElephantTest : SimpleTest + { + public override string Name + { + get { return "Photon-Beetle"; } + } + + [Test] + public override void PerformTest() + { + testVectors(ElephantEngine.ElephantParameters.elephant160, "v160"); + testVectors(ElephantEngine.ElephantParameters.elephant176, "v176"); + testVectors(ElephantEngine.ElephantParameters.elephant200, "v200"); + } + + private void testVectors(ElephantEngine.ElephantParameters pbp, String filename) + { + ElephantEngine Elephant = new ElephantEngine(pbp); + ICipherParameters param; + var buf = new Dictionary(); + //TestSampler sampler = new TestSampler(); + using (var src = new StreamReader(SimpleTest.GetTestDataAsStream("crypto.elephant." + filename + "_LWC_AEAD_KAT_128_96.txt"))) + { + string line; + string[] data; + byte[] ptByte, adByte; + byte[] rv; + Dictionary map = new Dictionary(); + while ((line = src.ReadLine()) != null) + { + data = line.Split(' '); + if (data.Length == 1) + { + //if (!map["Count"].Equals("2")) + //{ + // continue; + //} + param = new ParametersWithIV(new KeyParameter(Hex.Decode(map["Key"])), Hex.Decode(map["Nonce"])); + Elephant.Init(true, param); + adByte = Hex.Decode(map["AD"]); + Elephant.ProcessAadBytes(adByte, 0, adByte.Length); + ptByte = Hex.Decode(map["PT"]); + rv = new byte[Elephant.GetOutputSize(ptByte.Length)]; + Elephant.ProcessBytes(ptByte, 0, ptByte.Length, rv, 0); + Elephant.DoFinal(rv, ptByte.Length); + //foreach (byte b in Hex.Decode(map["CT"])) + //{ + // Console.Write(b.ToString("X2")); + //} + //Console.WriteLine(); + //foreach (byte b in rv) + //{ + // Console.Write(b.ToString("X2")); + //} + //Console.WriteLine(); + Assert.True(Arrays.AreEqual(rv, Hex.Decode(map["CT"]))); + //Console.WriteLine(map["Count"] + " pass"); + map.Clear(); + Elephant.Reset(); + } + else + { + if (data.Length >= 3) + { + map[data[0].Trim()] = data[2].Trim(); + } + else + { + map[data[0].Trim()] = ""; + } + + } + } + } + } + } +} + -- cgit 1.4.1