summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorDavid Hook <dgh@cryptoworkshop.com>2022-07-13 06:26:36 +1000
committerDavid Hook <dgh@cryptoworkshop.com>2022-07-13 06:26:36 +1000
commitc990f6a071a7b09d7d4750542817bf1d74186268 (patch)
tree5c2e0e8b941c2bdb2b3f53d6a9f52b9c7aa34621 /crypto/test
parentDetached picnic signature (diff)
downloadBouncyCastle.NET-ed25519-c990f6a071a7b09d7d4750542817bf1d74186268.tar.xz
initial Haraka install
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/crypto/test/Haraka256DigestTest.cs196
-rw-r--r--crypto/test/src/crypto/test/Haraka512DigestTest.cs192
2 files changed, 388 insertions, 0 deletions
diff --git a/crypto/test/src/crypto/test/Haraka256DigestTest.cs b/crypto/test/src/crypto/test/Haraka256DigestTest.cs
new file mode 100644
index 000000000..7b8154d4c
--- /dev/null
+++ b/crypto/test/src/crypto/test/Haraka256DigestTest.cs
@@ -0,0 +1,196 @@
+using System;
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace BouncyCastle.Crypto.Tests
+{
+    public class Haraka256DigestTest : SimpleTest
+    {
+        public override string Name
+        {
+            get { return "Haraka 256"; }
+        }
+
+        public void TestKnownVector()
+        {
+            byte[] input = new byte[32];
+            for (int t = 0; t < input.Length; t++)
+            {
+                input[t] = (byte)t;
+            }
+
+            // From Appendix B, Haraka-256 v2, https://eprint.iacr.org/2016/098.pdf
+            byte[] expected256 = Hex.Decode("8027ccb87949774b78d0545fb72bf70c695c2a0923cbd47bba1159efbf2b2c1c");
+
+            Haraka256Digest haraka = new Haraka256Digest();
+            haraka.Update(input, 0, input.Length);
+            byte[] output = new byte[haraka.GetDigestSize()];
+            haraka.DoFinal(output, 0);
+            Assert.IsTrue(Arrays.AreEqual(expected256, output));
+        }
+
+
+        public void TestInputTooShort()
+        {
+            try
+            {
+                Haraka256Digest haraka = new Haraka256Digest();
+                byte[] input = new byte[31];
+                haraka.Update(input, 0, input.Length);
+                haraka.DoFinal(null, 0);
+                Assert.Fail("fail on input not 32 bytes.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "input must be exactly 32 bytes"));
+            }
+        }
+
+        public void TestInputTooLong()
+        {
+            try
+            {
+                Haraka256Digest haraka = new Haraka256Digest();
+                byte[] input = new byte[33];
+                haraka.Update(input, 0, input.Length);
+                haraka.DoFinal(null, 0);
+                Assert.Fail("fail on input not 32 bytes.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "total input cannot be more than 32 bytes"));
+            }
+        }
+
+        public void TestOutput()
+        {
+
+            //
+            // Buffer too short.
+            //
+            try
+            {
+                Haraka256Digest harakaCipher = new Haraka256Digest();
+                byte[] input = new byte[32];
+                harakaCipher.Update(input, 0, input.Length);
+                byte[] output = new byte[31];
+                harakaCipher.DoFinal(output, 0);
+                Assert.Fail("Output too short for digest result.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "output too short to receive digest"));
+            }
+
+            //
+            // Offset puts end past length of buffer.
+            //
+            try
+            {
+                Haraka256Digest harakaCipher = new Haraka256Digest();
+                byte[] input = new byte[32];
+                harakaCipher.Update(input, 0, input.Length);
+                byte[] output = new byte[48];
+                harakaCipher.DoFinal(output, 17);
+                Assert.Fail("Output too short for digest result.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "output too short to receive digest"));
+            }
+
+            //
+            // Offset output..
+            //
+            try
+            {
+                byte[] input = new byte[32];
+                for (int t = 0; t < input.Length; t++)
+                {
+                    input[t] = (byte)t;
+                }
+
+                byte[] expected256 = Hex.Decode("000000008027ccb87949774b78d0545fb72bf70c695c2a0923cbd47bba1159efbf2b2c1c");
+
+                Haraka256Digest harakaCipher = new Haraka256Digest();
+                harakaCipher.Update(input, 0, input.Length);
+                byte[] output = new byte[harakaCipher.GetDigestSize() + 4];
+                harakaCipher.DoFinal(output, 4);
+                Assert.IsTrue(Arrays.AreEqual(expected256, output));
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "output too short to receive digest"));
+            }
+        }
+
+        void TestMonty()
+        {
+            int c = 0;
+            string[][] vectors = new string[][]{new string[]
+            {
+                "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F",
+                "e78599d7163ab58f1c90f0171c6fc4e852eb4b8cc29a4af63194fd9977c1de84"
+            },
+            new string[]{
+                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+                "c4cebda63c00c4cd312f36ea92afd4b0f6048507c5b367326ef9d8fdd2d5c09a"
+            }
+            };
+
+            for (int i = 0; i != vectors.Length; i++)
+            {
+                //
+                // 1000 rounds of digest application, where alternative outputs are copied over alternate halves of the input.
+                //
+                string[] vector = vectors[i];
+
+                byte[] expected = Hex.Decode(vector[1]);
+
+                // Load initial message.
+
+                Haraka256Digest haraka = new Haraka256Digest();
+                byte[] result = Hex.Decode(vector[0]);
+                for (int t = 0; t < 1000; t++)
+                {
+                    haraka.Update(result, 0, result.Length);
+                    haraka.DoFinal(result, 0);
+                }
+                Assert.IsTrue(Arrays.AreEqual(expected, result));
+
+                //
+                // Deliberately introduce incorrect value.
+                //
+
+                result[0] ^= 1;
+                Assert.IsTrue(!Arrays.AreEqual(expected, result));
+                c++;
+            }
+        }
+
+        private bool Contains(string message, string sub)
+        {
+            return message.IndexOf(sub) >= 0;
+        }
+
+        public override void PerformTest()
+        {
+            TestKnownVector();
+            TestInputTooLong();
+            TestInputTooShort();
+            TestOutput();
+            TestMonty();
+        }
+
+        //public static void Main()
+        //{
+        //    //runTest(new Haraka256DigestTest());
+        //    var t = new Haraka256DigestTest();
+        //    t.PerformTest();
+        //}
+    }
+}
\ No newline at end of file
diff --git a/crypto/test/src/crypto/test/Haraka512DigestTest.cs b/crypto/test/src/crypto/test/Haraka512DigestTest.cs
new file mode 100644
index 000000000..634b20724
--- /dev/null
+++ b/crypto/test/src/crypto/test/Haraka512DigestTest.cs
@@ -0,0 +1,192 @@
+using System;
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto.Digests;
+using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace BouncyCastle.Crypto.Tests
+{
+    public class Haraka512DigestTest : SimpleTest
+    {
+        public override string Name
+        {
+            get { return "Haraka 512"; }
+        }
+
+        public void TestKnownVector()
+        {
+            byte[] input = new byte[64];
+            for (int t = 0; t < input.Length; t++)
+            {
+                input[t] = (byte)t;
+            }
+
+            // From Appendix B, Haraka-512 v2, https://eprint.iacr.org/2016/098.pdf
+            byte[] expected512 = Hex.Decode("be7f723b4e80a99813b292287f306f625a6d57331cae5f34dd9277b0945be2aa");
+
+            Haraka512Digest haraka = new Haraka512Digest();
+            haraka.Update(input, 0, input.Length);
+            byte[] output = new byte[haraka.GetDigestSize()];
+            haraka.DoFinal(output, 0);
+            Assert.IsTrue(Arrays.AreEqual(expected512, output));
+        }
+
+        public void TestInputTooShort()
+        {
+            try
+            {
+                Haraka512Digest haraka = new Haraka512Digest();
+                byte[] input = new byte[63];
+                haraka.Update(input, 0, input.Length);
+                haraka.DoFinal(null, 0);
+                Assert.Fail("fail on input not 64 bytes.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "input must be exactly 64 bytes"));
+            }
+        }
+
+        public void TestInputTooLong()
+        {
+            try
+            {
+                Haraka512Digest haraka = new Haraka512Digest();
+                byte[] input = new byte[65];
+                haraka.Update(input, 0, input.Length);
+                haraka.DoFinal(null, 0);
+                Assert.Fail("fail on input not 64 bytes.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "total input cannot be more than 64 bytes"));
+            }
+        }
+
+        public void TestOutput()
+        {
+            //
+            // Buffer too short.
+            //
+            try
+            {
+                Haraka512Digest haraka = new Haraka512Digest();
+                byte[] input = new byte[64];
+                haraka.Update(input, 0, input.Length);
+                byte[] output = new byte[31];
+                haraka.DoFinal(output, 0);
+                Assert.Fail("Output too short for digest result.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "output too short to receive digest"));
+            }
+
+            //
+            // Offset puts end past length of buffer.
+            //
+            try
+            {
+                Haraka512Digest haraka = new Haraka512Digest();
+                byte[] input = new byte[64];
+                haraka.Update(input, 0, input.Length);
+                byte[] output = new byte[48];
+                haraka.DoFinal(output, 17);
+                Assert.Fail("Output too short for digest result.");
+            }
+            catch (ArgumentException e)
+            {
+                Assert.IsTrue(Contains(e.Message, "output too short to receive digest"));
+            }
+
+            //
+            // Offset output..
+            //
+            {
+                byte[] input = new byte[64];
+                for (int t = 0; t < input.Length; t++)
+                {
+                    input[t] = (byte)t;
+                }
+
+                byte[] expected512 = Hex.Decode("00000000be7f723b4e80a99813b292287f306f625a6d57331cae5f34dd9277b0945be2aa");
+
+                Haraka512Digest haraka = new Haraka512Digest();
+                haraka.Update(input, 0, input.Length);
+                byte[] output = new byte[haraka.GetDigestSize() + 4];
+                haraka.DoFinal(output, 4);
+                Assert.IsTrue(Arrays.AreEqual(expected512, output));
+            }
+
+        }
+
+        void TestMonty()
+        {
+            int c = 0;
+            string[][] vectors = new string[][]{
+            new string[]{
+                "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F",
+                "ABE210FE673F3B28E70E5100C476D82F61A7E2BDB3D8423FB0A15E5DE3D3A4DE"
+            },
+            new string[]{
+                "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+                "5F5ECB52C61F5036C96BE555D2E18C520AB3ED093954700C283A322D14DBFE02"
+            }
+        };
+
+            for (int i = 0; i != vectors.Length; i++)
+            {
+                //
+                // 1000 rounds of digest application, where alternative outputs are copied over alternate halves of the input.
+                //
+                string[] vector = vectors[i];
+
+                byte[] expected = Hex.Decode(vector[1]);
+
+                // Load initial message.
+                byte[] input = Hex.Decode(vector[0]);
+                Haraka512Digest haraka = new Haraka512Digest();
+                byte[] result = new byte[haraka.GetDigestSize()];
+                for (int t = 0; t < 1000; t++)
+                {
+                    haraka.Update(input, 0, input.Length);
+                    haraka.DoFinal(result, 0);
+
+                    if ((t & 0x01) == 1)
+                    {
+                        Array.Copy(result, 0, input, 0, result.Length);
+                    }
+                    else
+                    {
+                        Array.Copy(result, 0, input, result.Length, result.Length);
+                    }
+                }
+                Assert.IsTrue(Arrays.AreEqual(expected, result));
+
+                //
+                // Deliberately introduce incorrect value.
+                //
+
+                result[0] ^= 1;
+                Assert.IsTrue(!Arrays.AreEqual(expected, result));
+                c++;
+            }
+        }
+
+        private bool Contains(string message, string sub)
+        {
+            return message.IndexOf(sub) >= 0;
+        }
+
+        public override void PerformTest()
+        {
+            TestKnownVector();
+            TestInputTooLong();
+            TestInputTooShort();
+            TestOutput();
+            TestMonty();
+        }
+    }
+}
\ No newline at end of file