diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-19 12:59:36 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-19 12:59:36 +0700 |
commit | 0072cdce6f29ac642846905268cfc2a7ba23b19e (patch) | |
tree | 31b179dcaa676d75c42977eeda1969e6e545932c | |
parent | Picnic: deferred static initializers (diff) | |
download | BouncyCastle.NET-ed25519-0072cdce6f29ac642846905268cfc2a7ba23b19e.tar.xz |
Add test coverage for AesEngine_X86.ProcessFourBlocks
-rw-r--r-- | crypto/test/src/crypto/test/AesX86Test.cs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/crypto/test/src/crypto/test/AesX86Test.cs b/crypto/test/src/crypto/test/AesX86Test.cs index fc5a92545..9ebc663bc 100644 --- a/crypto/test/src/crypto/test/AesX86Test.cs +++ b/crypto/test/src/crypto/test/AesX86Test.cs @@ -1,10 +1,13 @@ // NOTE: .NET Core 3.1 is tested against our .NET Standard 2.0 assembly. //#if NETCOREAPP3_0_OR_GREATER #if NET6_0_OR_GREATER +using System; + using NUnit.Framework; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.Encoders; using Org.BouncyCastle.Utilities.Test; @@ -41,6 +44,8 @@ namespace Org.BouncyCastle.Crypto.Tests new BlockCipherMonteCarloTest(23, 10000, new AesEngine_X86(), new KeyParameter(Hex.Decode("28E79E2AFC5F7745FCCABE2F6257C2EF4C4EDFB37324814ED4137C288711A386")), "C737317FE0846F132B23C8C2A672CE22", "E58B82BFBA53C0040DC610C642121168") }; + private static readonly SecureRandom Random = new SecureRandom(); + [OneTimeSetUp] public static void OneTimeSetup() { @@ -82,6 +87,68 @@ namespace Org.BouncyCastle.Crypto.Tests Assert.AreEqual(Name + ": Okay", resultText); } + + [Test] + public void TestFourBlocksDecrypt128() + { + ImplTestFourBlocks(false, 128); + } + + [Test] + public void TestFourBlocksDecrypt192() + { + ImplTestFourBlocks(false, 192); + } + + [Test] + public void TestFourBlocksDecrypt256() + { + ImplTestFourBlocks(false, 256); + } + + [Test] + public void TestFourBlocksEncrypt128() + { + ImplTestFourBlocks(true, 128); + } + + [Test] + public void TestFourBlocksEncrypt192() + { + ImplTestFourBlocks(true, 192); + } + + [Test] + public void TestFourBlocksEncrypt256() + { + ImplTestFourBlocks(true, 256); + } + + private static void ImplTestFourBlocks(bool forEncryption, int keySize) + { + Span<byte> key = stackalloc byte[keySize / 8]; + Span<byte> data = stackalloc byte[64]; + Span<byte> fourBlockOutput = stackalloc byte[64]; + Span<byte> singleBlockOutput = stackalloc byte[64]; + + for (int i = 0; i < 100; ++i) + { + Random.NextBytes(key); + Random.NextBytes(data); + + var aes = new AesEngine_X86(); + aes.Init(forEncryption, new KeyParameter(key)); + + aes.ProcessFourBlocks(data, fourBlockOutput); + + for (int j = 0; j < 64; j += 16) + { + aes.ProcessBlock(data[j..], singleBlockOutput[j..]); + } + + Assert.IsTrue(fourBlockOutput.SequenceEqual(singleBlockOutput)); + } + } } } #endif |