diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-05-24 12:48:52 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-05-24 12:48:52 +0700 |
commit | 017ebcc7e78ded8046a89437dc1b56f9f1eb259f (patch) | |
tree | a34fed7d1db542e608a195129ea1e20a1a9c57bd /crypto/test | |
parent | Noekeon improvements (diff) | |
download | BouncyCastle.NET-ed25519-017ebcc7e78ded8046a89437dc1b56f9f1eb259f.tar.xz |
Add support for ARIA
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/UnitTests.csproj | 3 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/AriaTest.cs | 180 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RegressionTest.cs | 1 |
3 files changed, 183 insertions, 1 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj index cdc4d10bc..b70d94a6b 100644 --- a/crypto/test/UnitTests.csproj +++ b/crypto/test/UnitTests.csproj @@ -162,6 +162,7 @@ <Compile Include="src\crypto\test\AESTest.cs" /> <Compile Include="src\crypto\test\AESWrapTest.cs" /> <Compile Include="src\crypto\test\AllTests.cs" /> + <Compile Include="src\crypto\test\AriaTest.cs" /> <Compile Include="src\crypto\test\BCryptTest.cs" /> <Compile Include="src\crypto\test\Blake2bDigestTest.cs" /> <Compile Include="src\crypto\test\Blake2sDigestTest.cs" /> @@ -1345,4 +1346,4 @@ <ItemGroup> <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> </ItemGroup> -</Project> +</Project> \ No newline at end of file diff --git a/crypto/test/src/crypto/test/AriaTest.cs b/crypto/test/src/crypto/test/AriaTest.cs new file mode 100644 index 000000000..da92792f9 --- /dev/null +++ b/crypto/test/src/crypto/test/AriaTest.cs @@ -0,0 +1,180 @@ +using System; + +using NUnit.Framework; + +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities; +using Org.BouncyCastle.Utilities.Encoders; +using Org.BouncyCastle.Utilities.Test; + +namespace Org.BouncyCastle.Crypto.Tests +{ + [TestFixture] + public class AriaTest + : SimpleTest + { + private static readonly SecureRandom R = new SecureRandom(); + + private static readonly string[][] TEST_VECTORS_RFC5794 = { + new string[]{ + "128-Bit Key", + "000102030405060708090a0b0c0d0e0f", + "00112233445566778899aabbccddeeff", + "d718fbd6ab644c739da95f3be6451778" + }, + new string[]{ + "192-Bit Key", + "000102030405060708090a0b0c0d0e0f1011121314151617", + "00112233445566778899aabbccddeeff", + "26449c1805dbe7aa25a468ce263a9e79" + }, + new string[]{ + "256-Bit Key", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "00112233445566778899aabbccddeeff", + "f92bd7c79fb72e2f2b8f80c1972d24fc" + }, + }; + + public override string Name + { + get { return "ARIA"; } + } + + public override void PerformTest() + { + CheckTestVectors_RFC5794(); + + for (int i = 0; i < 100; ++i) + { + CheckRandomRoundtrips(); + } + + new MyAriaEngine().CheckImplementation(); + } + + private void CheckRandomRoundtrips() + { + AriaEngine ce = new AriaEngine(); + AriaEngine cd = new AriaEngine(); + + byte[] txt = new byte[ce.GetBlockSize()]; + byte[] enc = new byte[ce.GetBlockSize()]; + byte[] dec = new byte[ce.GetBlockSize()]; + + for (int keyLen = 16; keyLen <= 32; keyLen += 8) + { + byte[] K = new byte[keyLen]; + + R.NextBytes(K); + + KeyParameter key = new KeyParameter(K); + ce.Init(true, key); + cd.Init(false, key); + + R.NextBytes(txt); + + for (int i = 0; i < 100; ++i) + { + ce.ProcessBlock(txt, 0, enc, 0); + cd.ProcessBlock(enc, 0, dec, 0); + + IsTrue(Arrays.AreEqual(txt, dec)); + + Array.Copy(enc, 0, txt, 0, enc.Length); + } + } + } + + private void CheckTestVector_RFC5794(String[] tv) + { + string name = "'" + tv[0] + "'"; + + IBlockCipher c = new AriaEngine(); + int blockSize = c.GetBlockSize(); + IsTrue("Wrong block size returned from getBlockSize() for " + name, 16 == blockSize); + + KeyParameter key = new KeyParameter(Hex.Decode(tv[1])); + byte[] plaintext = Hex.Decode(tv[2]); + byte[] ciphertext = Hex.Decode(tv[3]); + + IsTrue("Unexpected plaintext length for " + name, blockSize == plaintext.Length); + IsTrue("Unexpected ciphertext length for " + name, blockSize == ciphertext.Length); + + c.Init(true, key); + + byte[] actual = new byte[blockSize]; + int num = c.ProcessBlock(plaintext, 0, actual, 0); + + IsTrue("Wrong length returned from processBlock() (encryption) for " + name, blockSize == num); + IsTrue("Incorrect ciphertext computed for " + name, Arrays.AreEqual(ciphertext, actual)); + + c.Init(false, key); + num = c.ProcessBlock(ciphertext, 0, actual, 0); + + IsTrue("Wrong length returned from processBlock() (decryption) for " + name, blockSize == num); + IsTrue("Incorrect plaintext computed for " + name, Arrays.AreEqual(plaintext, actual)); + } + + private void CheckTestVectors_RFC5794() + { + for (int i = 0; i < TEST_VECTORS_RFC5794.Length; ++i) + { + CheckTestVector_RFC5794(TEST_VECTORS_RFC5794[i]); + } + } + + public static void Main(string[] args) + { + RunTest(new AriaTest()); + } + + [Test] + public void TestFunction() + { + string resultText = Perform().ToString(); + + Assert.AreEqual(Name + ": Okay", resultText); + } + + private class MyAriaEngine + : AriaEngine + { + public void CheckImplementation() + { + CheckInvolution(); + CheckSBoxes(); + } + + private void CheckInvolution() + { + byte[] x = new byte[16], y = new byte[16]; + + for (int i = 0; i < 100; ++i) + { + R.NextBytes(x); + Array.Copy(x, 0, y, 0, 16); + A(y); + A(y); + Assert.IsTrue(Arrays.AreEqual(x, y)); + } + } + + private void CheckSBoxes() + { + for (int i = 0; i < 256; ++i) + { + byte x = (byte)i; + + Assert.IsTrue(x == SB1(SB3(x))); + Assert.IsTrue(x == SB3(SB1(x))); + + Assert.IsTrue(x == SB2(SB4(x))); + Assert.IsTrue(x == SB4(SB2(x))); + } + } + } + } +} diff --git a/crypto/test/src/crypto/test/RegressionTest.cs b/crypto/test/src/crypto/test/RegressionTest.cs index 4b7b11651..2b863d8bb 100644 --- a/crypto/test/src/crypto/test/RegressionTest.cs +++ b/crypto/test/src/crypto/test/RegressionTest.cs @@ -13,6 +13,7 @@ namespace Org.BouncyCastle.Crypto.Tests new AesLightTest(), new AesFastTest(), new AesWrapTest(), + new AriaTest(), new DesTest(), new DesEdeTest(), new ModeTest(), |