summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-07-17 23:27:26 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-07-17 23:27:26 +0700
commit40695be9c25db0223881aadf28baa21097555f94 (patch)
tree4167ebc034d4f0e298edb90c7234502fe6449aac /crypto/test
parentAdd net6.0 to target frameworks (diff)
downloadBouncyCastle.NET-ed25519-40695be9c25db0223881aadf28baa21097555f94.tar.xz
Add AesX86Engine using Aes intrinsics
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/src/crypto/test/AesX86Test.cs85
-rw-r--r--crypto/test/src/crypto/test/CipherTest.cs157
2 files changed, 156 insertions, 86 deletions
diff --git a/crypto/test/src/crypto/test/AesX86Test.cs b/crypto/test/src/crypto/test/AesX86Test.cs
new file mode 100644
index 000000000..379a31a0e
--- /dev/null
+++ b/crypto/test/src/crypto/test/AesX86Test.cs
@@ -0,0 +1,85 @@
+#if NET5_0_OR_GREATER
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto.Engines;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Utilities.Encoders;
+using Org.BouncyCastle.Utilities.Test;
+
+namespace Org.BouncyCastle.Crypto.Tests
+{
+    [TestFixture, TestOf(typeof(AesX86Engine))]
+    public class AesX86Test
+        : CipherTest
+    {
+        private static SimpleTest[] CreateTests() => new SimpleTest[]{
+            new BlockCipherVectorTest(0, new AesX86Engine(), new KeyParameter(Hex.Decode("80000000000000000000000000000000")), "00000000000000000000000000000000", "0EDD33D3C621E546455BD8BA1418BEC8"),
+            new BlockCipherVectorTest(1, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000080")), "00000000000000000000000000000000", "172AEAB3D507678ECAF455C12587ADB7"),
+            new BlockCipherMonteCarloTest(2, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000000")), "00000000000000000000000000000000", "C34C052CC0DA8D73451AFE5F03BE297F"),
+            new BlockCipherMonteCarloTest(3, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("5F060D3716B345C253F6749ABAC10917")), "355F697E8B868B65B25A04E18D782AFA", "ACC863637868E3E068D2FD6E3508454A"),
+            new BlockCipherVectorTest(4, new AesX86Engine(), new KeyParameter(Hex.Decode("000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "6CD02513E8D4DC986B4AFE087A60BD0C"),
+            new BlockCipherMonteCarloTest(5, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("AAFE47EE82411A2BF3F6752AE8D7831138F041560631B114")), "F3F6752AE8D7831138F041560631B114", "77BA00ED5412DFF27C8ED91F3C376172"),
+            new BlockCipherVectorTest(6, new AesX86Engine(), new KeyParameter(Hex.Decode("0000000000000000000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "DDC6BF790C15760D8D9AEB6F9A75FD4E"),
+            new BlockCipherMonteCarloTest(7, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("28E79E2AFC5F7745FCCABE2F6257C2EF4C4EDFB37324814ED4137C288711A386")), "C737317FE0846F132B23C8C2A672CE22", "E58B82BFBA53C0040DC610C642121168"),
+            new BlockCipherVectorTest(8, new AesX86Engine(), new KeyParameter(Hex.Decode("80000000000000000000000000000000")), "00000000000000000000000000000000", "0EDD33D3C621E546455BD8BA1418BEC8"),
+            new BlockCipherVectorTest(9, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000080")), "00000000000000000000000000000000", "172AEAB3D507678ECAF455C12587ADB7"),
+            new BlockCipherMonteCarloTest(10, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000000")), "00000000000000000000000000000000", "C34C052CC0DA8D73451AFE5F03BE297F"),
+            new BlockCipherMonteCarloTest(11, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("5F060D3716B345C253F6749ABAC10917")), "355F697E8B868B65B25A04E18D782AFA", "ACC863637868E3E068D2FD6E3508454A"),
+            new BlockCipherVectorTest(12, new AesX86Engine(), new KeyParameter(Hex.Decode("000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "6CD02513E8D4DC986B4AFE087A60BD0C"),
+            new BlockCipherMonteCarloTest(13, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("AAFE47EE82411A2BF3F6752AE8D7831138F041560631B114")), "F3F6752AE8D7831138F041560631B114", "77BA00ED5412DFF27C8ED91F3C376172"),
+            new BlockCipherVectorTest(14, new AesX86Engine(), new KeyParameter(Hex.Decode("0000000000000000000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "DDC6BF790C15760D8D9AEB6F9A75FD4E"),
+            new BlockCipherMonteCarloTest(15, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("28E79E2AFC5F7745FCCABE2F6257C2EF4C4EDFB37324814ED4137C288711A386")), "C737317FE0846F132B23C8C2A672CE22", "E58B82BFBA53C0040DC610C642121168"),
+            new BlockCipherVectorTest(16, new AesX86Engine(), new KeyParameter(Hex.Decode("80000000000000000000000000000000")), "00000000000000000000000000000000", "0EDD33D3C621E546455BD8BA1418BEC8"),
+            new BlockCipherVectorTest(17, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000080")), "00000000000000000000000000000000", "172AEAB3D507678ECAF455C12587ADB7"),
+            new BlockCipherMonteCarloTest(18, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("00000000000000000000000000000000")), "00000000000000000000000000000000", "C34C052CC0DA8D73451AFE5F03BE297F"),
+            new BlockCipherMonteCarloTest(19, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("5F060D3716B345C253F6749ABAC10917")), "355F697E8B868B65B25A04E18D782AFA", "ACC863637868E3E068D2FD6E3508454A"),
+            new BlockCipherVectorTest(20, new AesX86Engine(), new KeyParameter(Hex.Decode("000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "6CD02513E8D4DC986B4AFE087A60BD0C"),
+            new BlockCipherMonteCarloTest(21, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("AAFE47EE82411A2BF3F6752AE8D7831138F041560631B114")), "F3F6752AE8D7831138F041560631B114", "77BA00ED5412DFF27C8ED91F3C376172"),
+            new BlockCipherVectorTest(22, new AesX86Engine(), new KeyParameter(Hex.Decode("0000000000000000000000000000000000000000000000000000000000000000")), "80000000000000000000000000000000", "DDC6BF790C15760D8D9AEB6F9A75FD4E"),
+            new BlockCipherMonteCarloTest(23, 10000, new AesX86Engine(), new KeyParameter(Hex.Decode("28E79E2AFC5F7745FCCABE2F6257C2EF4C4EDFB37324814ED4137C288711A386")), "C737317FE0846F132B23C8C2A672CE22", "E58B82BFBA53C0040DC610C642121168")
+        };
+
+        [OneTimeSetUp]
+        public static void OneTimeSetup()
+        {
+            if (!AesX86Engine.IsSupported)
+            {
+                Assert.Ignore();
+            }
+        }
+
+        public override string Name => "AesX86";
+
+        public AesX86Test()
+            : base()
+        {
+        }
+
+        public override ITestResult Perform()
+        {
+            if (AesX86Engine.IsSupported)
+            {
+                ITestResult result = base.Perform();
+                if (!result.IsSuccessful())
+                    return result;
+            }
+
+            return new SimpleTestResult(true, Name + ": Okay");
+        }
+
+        public override void PerformTest()
+        {
+            RunTests(CreateTests());
+            RunEngineChecks(new AesX86Engine(), new KeyParameter(new byte[16]));
+        }
+
+        [Test]
+        public void TestFunction()
+        {
+            string resultText = Perform().ToString();
+
+            Assert.AreEqual(Name + ": Okay", resultText);
+        }
+    }
+}
+#endif
diff --git a/crypto/test/src/crypto/test/CipherTest.cs b/crypto/test/src/crypto/test/CipherTest.cs
index d9c085d44..3d380514c 100644
--- a/crypto/test/src/crypto/test/CipherTest.cs
+++ b/crypto/test/src/crypto/test/CipherTest.cs
@@ -10,108 +10,93 @@ namespace Org.BouncyCastle.Crypto.Tests
 	public abstract class CipherTest
 		: SimpleTest
 	{
-		private SimpleTest[]      _tests;
-		private IBlockCipher _engine;
-		private KeyParameter _validKey;
-
-//		protected CipherTest(
-//			SimpleTest[]	tests)
-//		{
-//			_tests = tests;
-//		}
-
-		protected CipherTest(
-			SimpleTest[]	tests,
-			IBlockCipher	engine,
-			KeyParameter	validKey)
+		private readonly SimpleTest[] m_tests;
+		private readonly IBlockCipher m_engine;
+		private readonly KeyParameter m_validKey;
+
+		protected CipherTest()
+		{
+            m_tests = null;
+            m_engine = null;
+            m_validKey = null;
+        }
+
+        protected CipherTest(SimpleTest[] tests, IBlockCipher engine, KeyParameter validKey)
 		{
-			_tests = tests;
-			_engine = engine;
-			_validKey = validKey;
+            m_tests = tests;
+			m_engine = engine;
+			m_validKey = validKey;
 		}
 
 		public override void PerformTest()
 		{
-			for (int i = 0; i != _tests.Length; i++)
+			if (m_tests != null)
 			{
-				_tests[i].PerformTest();
-			}
+                RunTests(m_tests);
+            }
 
-			if (_engine != null)
+            if (m_engine != null)
 			{
-				//
-				// state tests
-				//
-				byte[] buf = new byte[_engine.GetBlockSize()];
-
-				try
-				{
-					_engine.ProcessBlock(buf, 0, buf, 0);
-
-					Fail("failed initialisation check");
-				}
-				catch (InvalidOperationException)
-				{
-					// expected
-				}
-
-				bufferSizeCheck((_engine));
-			}
+                RunEngineChecks(m_engine, m_validKey);
+            }
 		}
 
-		private void bufferSizeCheck(
-			IBlockCipher engine)
-		{
-			byte[] correctBuf = new byte[engine.GetBlockSize()];
-			byte[] shortBuf = new byte[correctBuf.Length / 2];
-
-			engine.Init(true, _validKey);
-
-			try
-			{
-				engine.ProcessBlock(shortBuf, 0, correctBuf, 0);
-
-				Fail("failed short input check");
-			}
-			catch (DataLengthException)
-			{
-				// expected
-			}
+        protected void RunEngineChecks(IBlockCipher engine, KeyParameter validKey)
+        {
+            byte[] buf = new byte[engine.GetBlockSize()];
+            ExpectInvalidOperationException(engine, buf, buf, "failed initialisation check");
 
-			try
-			{
-				engine.ProcessBlock(correctBuf, 0, shortBuf, 0);
+            CheckDataLengthExceptions(engine, validKey);
+        }
 
-				Fail("failed short output check");
-			}
-			catch (DataLengthException)
-			{
-				// expected
-			}
+        protected void RunTests(SimpleTest[] tests)
+        {
+            foreach (var test in tests)
+            {
+                test.PerformTest();
+            }
+        }
 
-			engine.Init(false, _validKey);
+        private void CheckDataLengthExceptions(IBlockCipher engine, ICipherParameters parameters)
+		{
+			byte[] correctBuf = new byte[engine.GetBlockSize()];
+			byte[] shortBuf = new byte[correctBuf.Length / 2];
 
-			try
-			{
-				engine.ProcessBlock(shortBuf, 0, correctBuf, 0);
+            engine.Init(true, parameters);
 
-				Fail("failed short input check");
-			}
-			catch (DataLengthException)
-			{
-				// expected
-			}
+			ExpectDataLengthException(engine, shortBuf, correctBuf, "failed short input check");
+            ExpectDataLengthException(engine, correctBuf, shortBuf, "failed short output check");
 
-			try
-			{
-				engine.ProcessBlock(correctBuf, 0, shortBuf, 0);
+			engine.Init(false, parameters);
 
-				Fail("failed short output check");
-			}
-			catch (DataLengthException)
-			{
-				// expected
-			}
+            ExpectDataLengthException(engine, shortBuf, correctBuf, "failed short input check");
+            ExpectDataLengthException(engine, correctBuf, shortBuf, "failed short output check");
 		}
-	}
+
+        private void ExpectDataLengthException(IBlockCipher engine, byte[] input, byte[] output, string message)
+		{
+            try
+            {
+                engine.ProcessBlock(input, 0, output, 0);
+                Fail(message);
+            }
+            catch (DataLengthException)
+            {
+                // expected
+            }
+        }
+
+        private void ExpectInvalidOperationException(IBlockCipher engine, byte[] input, byte[] output, string message)
+        {
+            try
+            {
+                engine.ProcessBlock(input, 0, output, 0);
+                Fail(message);
+            }
+            catch (InvalidOperationException)
+            {
+                // expected
+            }
+        }
+    }
 }