diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-22 18:32:28 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-07-22 18:32:28 +0700 |
commit | 44a91fea962afb5b6a1f88567e196f13397ef572 (patch) | |
tree | 08d5295aa38572898a20bd0dc32ed2b3bd28ab52 /crypto/test | |
parent | Add explanatory comment for "wrong AES OIDs" (diff) | |
download | BouncyCastle.NET-ed25519-44a91fea962afb5b6a1f88567e196f13397ef572.tar.xz |
Add Rfc5649WrapEngine
Diffstat (limited to 'crypto/test')
-rw-r--r-- | crypto/test/src/test/AESTest.cs | 98 | ||||
-rw-r--r-- | crypto/test/src/test/AriaTest.cs | 428 | ||||
-rw-r--r-- | crypto/test/src/test/BaseBlockCipherTest.cs | 27 |
3 files changed, 531 insertions, 22 deletions
diff --git a/crypto/test/src/test/AESTest.cs b/crypto/test/src/test/AESTest.cs index a0545239e..932f959b3 100644 --- a/crypto/test/src/test/AESTest.cs +++ b/crypto/test/src/test/AESTest.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Text; using NUnit.Framework; @@ -10,6 +9,7 @@ using Org.BouncyCastle.Crypto.IO; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities.Encoders; +using Org.BouncyCastle.Utilities.Test; namespace Org.BouncyCastle.Tests { @@ -44,7 +44,7 @@ namespace Org.BouncyCastle.Tests { for (int i = 0; i != cipherTests.Length; i += 4) { - doCipherTest(int.Parse(cipherTests[i]), + DoCipherTest(int.Parse(cipherTests[i]), Hex.Decode(cipherTests[i + 1]), Hex.Decode(cipherTests[i + 2]), Hex.Decode(cipherTests[i + 3])); @@ -97,7 +97,27 @@ namespace Org.BouncyCastle.Tests wrapTest(1, "AESWrap", kek1, in1, out1); } - [Test] + [Test] + public void TestWrapRfc3211() + { + byte[] kek2 = Hex.Decode("000102030405060708090a0b0c0d0e0f"); + byte[] in2 = Hex.Decode("00112233445566778899aabbccddeeff"); + byte[] out2 = Hex.Decode("7c8798dfc802553b3f00bb4315e3a087322725c92398b9c112c74d0925c63b61"); + + wrapTest(2, "AESRFC3211WRAP", kek2, kek2, FixedSecureRandom.From(Hex.Decode("9688df2af1b7b1ac9688df2a")), in2, out2); + } + + [Test] + public void TestWrapRfc5649() + { + byte[] kek3 = Hex.Decode("5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8"); + byte[] in3 = Hex.Decode("c37b7e6492584340bed12207808941155068f738"); + byte[] out3 = Hex.Decode("138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a"); + + wrapTest(3, "AESWrapPad", kek3, in3, out3); + } + + [Test] public void TestWrapOids() { string[] wrapOids = @@ -110,11 +130,20 @@ namespace Org.BouncyCastle.Tests wrapOidTest(wrapOids, "AESWrap"); } - private void doCipherTest( - int strength, - byte[] keyBytes, - byte[] input, - byte[] output) + [Test] + public void TestWrapPadOids() + { + string[] wrapPadOids = + { + NistObjectIdentifiers.IdAes128WrapPad.Id, + NistObjectIdentifiers.IdAes192WrapPad.Id, + NistObjectIdentifiers.IdAes256WrapPad.Id + }; + + wrapOidTest(wrapPadOids, "AESWrapPad"); + } + + private void DoCipherTest(int strength, byte[] keyBytes, byte[] input, byte[] output) { KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", keyBytes); @@ -343,15 +372,60 @@ namespace Org.BouncyCastle.Tests } } + [Test] + public void TestOcb() + { + byte[] K = Hex.Decode("000102030405060708090A0B0C0D0E0F"); + byte[] P = Hex.Decode("000102030405060708090A0B0C0D0E0F"); + byte[] N = Hex.Decode("000102030405060708090A0B"); + string T = "4CBB3E4BD6B456AF"; + byte[] C = Hex.Decode("BEA5E8798DBE7110031C144DA0B2612213CC8B747807121A" + T); + + KeyParameter key = ParameterUtilities.CreateKeyParameter("AES", K); + IBufferedCipher inCipher = CipherUtilities.GetCipher("AES/OCB/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("AES/OCB/NoPadding"); + + inCipher.Init(true, new ParametersWithIV(key, N)); + + byte[] enc = inCipher.DoFinal(P); + if (!AreEqual(enc, C)) + { + Fail("ciphertext doesn't match in OCB"); + } + + outCipher.Init(false, new ParametersWithIV(key, N)); + + byte[] dec = outCipher.DoFinal(C); + if (!AreEqual(dec, P)) + { + Fail("plaintext doesn't match in OCB"); + } + + try + { + inCipher = CipherUtilities.GetCipher("AES/OCB/PKCS5Padding"); + + Fail("bad padding missed in OCB"); + } + catch (SecurityUtilityException) + { + // expected + } + } + public override void PerformTest() { TestCiphers(); TestWrap(); - TestOids(); + TestWrapRfc3211(); + TestWrapRfc5649(); + TestOids(); TestWrapOids(); - TestEax(); + TestWrapPadOids(); + TestEax(); TestCcm(); TestGcm(); - } - } + TestOcb(); + } + } } diff --git a/crypto/test/src/test/AriaTest.cs b/crypto/test/src/test/AriaTest.cs new file mode 100644 index 000000000..3e95ae947 --- /dev/null +++ b/crypto/test/src/test/AriaTest.cs @@ -0,0 +1,428 @@ +using System; +using System.IO; + +using NUnit.Framework; + +using Org.BouncyCastle.Asn1.Nsri; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.IO; +using Org.BouncyCastle.Crypto.Parameters; +using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities.Encoders; +using Org.BouncyCastle.Utilities.Test; + +namespace Org.BouncyCastle.Tests +{ + /// <remarks>Basic test class for the ARIA cipher vectors from FIPS-197</remarks> + [TestFixture] + public class AriaTest + : BaseBlockCipherTest + { + internal static readonly string[] cipherTests = + { + "128", + "000102030405060708090a0b0c0d0e0f", + "00112233445566778899aabbccddeeff", + "d718fbd6ab644c739da95f3be6451778", + "192", + "000102030405060708090a0b0c0d0e0f1011121314151617", + "00112233445566778899aabbccddeeff", + "26449c1805dbe7aa25a468ce263a9e79", + "256", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "00112233445566778899aabbccddeeff", + "f92bd7c79fb72e2f2b8f80c1972d24fc" + }; + + public AriaTest() + : base("ARIA") + { + } + + [Test] + public void TestCiphers() + { + for (int i = 0; i != cipherTests.Length; i += 4) + { + DoCipherTest(int.Parse(cipherTests[i]), + Hex.Decode(cipherTests[i + 1]), + Hex.Decode(cipherTests[i + 2]), + Hex.Decode(cipherTests[i + 3])); + } + } + + [Test] + public void TestOids() + { + string[] oids = { + NsriObjectIdentifiers.id_aria128_ecb.Id, + NsriObjectIdentifiers.id_aria128_cbc.Id, + NsriObjectIdentifiers.id_aria128_ofb.Id, + NsriObjectIdentifiers.id_aria128_cfb.Id, + NsriObjectIdentifiers.id_aria192_ecb.Id, + NsriObjectIdentifiers.id_aria192_cbc.Id, + NsriObjectIdentifiers.id_aria192_ofb.Id, + NsriObjectIdentifiers.id_aria192_cfb.Id, + NsriObjectIdentifiers.id_aria256_ecb.Id, + NsriObjectIdentifiers.id_aria256_cbc.Id, + NsriObjectIdentifiers.id_aria256_ofb.Id, + NsriObjectIdentifiers.id_aria256_cfb.Id + }; + + string[] names = { + "ARIA/ECB/PKCS7Padding", + "ARIA/CBC/PKCS7Padding", + "ARIA/OFB/NoPadding", + "ARIA/CFB/NoPadding", + "ARIA/ECB/PKCS7Padding", + "ARIA/CBC/PKCS7Padding", + "ARIA/OFB/NoPadding", + "ARIA/CFB/NoPadding", + "ARIA/ECB/PKCS7Padding", + "ARIA/CBC/PKCS7Padding", + "ARIA/OFB/NoPadding", + "ARIA/CFB/NoPadding" + }; + + oidTest(oids, names, 4); + } + + [Test] + public void TestWrap() + { + byte[] kek1 = Hex.Decode("000102030405060708090a0b0c0d0e0f"); + byte[] in1 = Hex.Decode("00112233445566778899aabbccddeeff"); + byte[] out1 = Hex.Decode("a93f148d4909d85f1aae656909879275ae597b3acf9d60db"); + + wrapTest(1, "ARIAWrap", kek1, in1, out1); + } + + [Test] + public void TestWrapRfc3211() + { + byte[] kek2 = Hex.Decode("000102030405060708090a0b0c0d0e0f"); + byte[] in2 = Hex.Decode("00112233445566778899aabbccddeeff"); + byte[] out2 = Hex.Decode("9b2d3cac0acf9d4bde7c1bdb0313fbef931f025acc77bf57d3d1cabc88b514d0"); + + wrapTest(2, "ARIARFC3211WRAP", kek2, kek2, FixedSecureRandom.From(Hex.Decode("9688df2af1b7b1ac9688df2a")), in2, out2); + } + + [Test] + public void TestWrapRfc5649() + { + byte[] kek3 = Hex.Decode("000102030405060708090a0b0c0d0e0f"); + byte[] in3 = Hex.Decode("00112233445566778899aabbccddeeff"); + byte[] out3 = Hex.Decode("ac0e22699a036ced63adeb75f4946f82dc98ad8af43b24d5"); + + wrapTest(3, "ARIAWrapPad", kek3, in3, out3); + } + + [Test] + public void TestWrapOids() + { + string[] wrapOids = + { + NsriObjectIdentifiers.id_aria128_kw.Id, + NsriObjectIdentifiers.id_aria192_kw.Id, + NsriObjectIdentifiers.id_aria256_kw.Id + }; + + wrapOidTest(wrapOids, "ARIAWrap"); + } + + [Test] + public void TestWrapPadOids() + { + string[] wrapPadOids = + { + NsriObjectIdentifiers.id_aria128_kwp.Id, + NsriObjectIdentifiers.id_aria192_kwp.Id, + NsriObjectIdentifiers.id_aria256_kwp.Id + }; + + wrapOidTest(wrapPadOids, "ARIAWrapPad"); + } + + private void DoCipherTest(int strength, byte[] keyBytes, byte[] input, byte[] output) + { + KeyParameter key = ParameterUtilities.CreateKeyParameter("ARIA", keyBytes); + + IBufferedCipher inCipher = CipherUtilities.GetCipher("ARIA/ECB/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("ARIA/ECB/NoPadding"); + + try + { + outCipher.Init(true, key); + } + catch (Exception e) + { + Fail("ARIA failed initialisation - " + e, e); + } + + try + { + inCipher.Init(false, key); + } + catch (Exception e) + { + Fail("ARIA failed initialisation - " + e, e); + } + + // + // encryption pass + // + MemoryStream bOut = new MemoryStream(); + + CipherStream cOut = new CipherStream(bOut, null, outCipher); + + try + { + for (int i = 0; i != input.Length / 2; i++) + { + cOut.WriteByte(input[i]); + } + cOut.Write(input, input.Length / 2, input.Length - input.Length / 2); + cOut.Close(); + } + catch (IOException e) + { + Fail("ARIA failed encryption - " + e, e); + } + + byte[] bytes = bOut.ToArray(); + + if (!AreEqual(bytes, output)) + { + Fail("ARIA failed encryption - expected " + + Hex.ToHexString(output) + " got " + + Hex.ToHexString(bytes)); + } + + // + // decryption pass + // + MemoryStream bIn = new MemoryStream(bytes, false); + + CipherStream cIn = new CipherStream(bIn, inCipher, null); + + try + { +// DataInputStream dIn = new DataInputStream(cIn); + BinaryReader dIn = new BinaryReader(cIn); + + bytes = new byte[input.Length]; + + for (int i = 0; i != input.Length / 2; i++) + { +// bytes[i] = (byte)dIn.read(); + bytes[i] = dIn.ReadByte(); + } + + int remaining = bytes.Length - input.Length / 2; +// dIn.readFully(bytes, input.Length / 2, remaining); + byte[] extra = dIn.ReadBytes(remaining); + if (extra.Length < remaining) + throw new EndOfStreamException(); + extra.CopyTo(bytes, input.Length / 2); + } + catch (Exception e) + { + Fail("ARIA failed encryption - " + e, e); + } + + if (!AreEqual(bytes, input)) + { + Fail("ARIA failed decryption - expected " + + Hex.ToHexString(input) + " got " + + Hex.ToHexString(bytes)); + } + } + + [Test] + public void TestEax() + { + byte[] K = Hex.Decode("233952DEE4D5ED5F9B9C6D6FF80FF478"); + byte[] N = Hex.Decode("62EC67F9C3A4A407FCB2A8C49031A8B3"); + byte[] P = Hex.Decode("68656c6c6f20776f726c642121"); + byte[] C = Hex.Decode("85fe63d6cfb872d2420e65425c074dfad6fe752e03"); + + KeyParameter key = ParameterUtilities.CreateKeyParameter("ARIA", K); + IBufferedCipher inCipher = CipherUtilities.GetCipher("ARIA/EAX/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("ARIA/EAX/NoPadding"); + + inCipher.Init(true, new ParametersWithIV(key, N)); + + byte[] enc = inCipher.DoFinal(P); + if (!AreEqual(enc, C)) + { + Fail("ciphertext doesn't match in EAX"); + } + + outCipher.Init(false, new ParametersWithIV(key, N)); + + byte[] dec = outCipher.DoFinal(C); + if (!AreEqual(dec, P)) + { + Fail("plaintext doesn't match in EAX"); + } + + try + { + inCipher = CipherUtilities.GetCipher("ARIA/EAX/PKCS5Padding"); + + Fail("bad padding missed in EAX"); + } + catch (SecurityUtilityException) + { + // expected + } + } + + [Test] + public void TestCcm() + { + byte[] K = Hex.Decode("404142434445464748494a4b4c4d4e4f"); + byte[] N = Hex.Decode("10111213141516"); + byte[] P = Hex.Decode("68656c6c6f20776f726c642121"); + byte[] C = Hex.Decode("0af625ff69cd9dbe65fae181d654717eb7a0263bcd"); + + KeyParameter key = ParameterUtilities.CreateKeyParameter("ARIA", K); + + IBufferedCipher inCipher = CipherUtilities.GetCipher("ARIA/CCM/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("ARIA/CCM/NoPadding"); + + inCipher.Init(true, new ParametersWithIV(key, N)); + + byte[] enc = inCipher.DoFinal(P); + if (!AreEqual(enc, C)) + { + Fail("ciphertext doesn't match in CCM"); + } + + outCipher.Init(false, new ParametersWithIV(key, N)); + + byte[] dec = outCipher.DoFinal(C); + if (!AreEqual(dec, P)) + { + Fail("plaintext doesn't match in CCM"); + } + + try + { + inCipher = CipherUtilities.GetCipher("ARIA/CCM/PKCS5Padding"); + + Fail("bad padding missed in CCM"); + } + catch (SecurityUtilityException) + { + // expected + } + } + + [Test] + public void TestGcm() + { + // Test Case 15 from McGrew/Viega + byte[] K = Hex.Decode( + "feffe9928665731c6d6a8f9467308308" + + "feffe9928665731c6d6a8f9467308308"); + byte[] P = Hex.Decode( + "d9313225f88406e5a55909c5aff5269a" + + "86a7a9531534f7da2e4c303d8a318a72" + + "1c3c0c95956809532fcf0e2449a6b525" + + "b16aedf5aa0de657ba637b391aafd255"); + byte[] N = Hex.Decode("cafebabefacedbaddecaf888"); + string T = "c8f245c8619ca9ba7d6d9545e7f48214"; + byte[] C = Hex.Decode( + "c3aa0e01a4f8b5dfdb25d0f1c78c275e516114080e2be7a7f7bffd4504b19a8552f80ad5b55f3d911725489629996d398d5ed6f077e22924c5b8ebe20a219693" + + T); + + KeyParameter key = ParameterUtilities.CreateKeyParameter("ARIA", K); + IBufferedCipher inCipher = CipherUtilities.GetCipher("ARIA/GCM/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("ARIA/GCM/NoPadding"); + + inCipher.Init(true, new ParametersWithIV(key, N)); + + byte[] enc = inCipher.DoFinal(P); + if (!AreEqual(enc, C)) + { + Fail("ciphertext doesn't match in GCM"); + } + + outCipher.Init(false, new ParametersWithIV(key, N)); + + byte[] dec = outCipher.DoFinal(C); + if (!AreEqual(dec, P)) + { + Fail("plaintext doesn't match in GCM"); + } + + try + { + inCipher = CipherUtilities.GetCipher("ARIA/GCM/PKCS5Padding"); + + Fail("bad padding missed in GCM"); + } + catch (SecurityUtilityException) + { + // expected + } + } + + [Test] + public void TestOcb() + { + byte[] K = Hex.Decode("000102030405060708090A0B0C0D0E0F"); + byte[] P = Hex.Decode("000102030405060708090A0B0C0D0E0F"); + byte[] N = Hex.Decode("000102030405060708090A0B"); + string T = "0027ce4f3aaeec75"; + byte[] C = Hex.Decode("7bcae9eac9f1f54704a630e309099a87f53a1c1559de1b3b" + T); + + KeyParameter key = ParameterUtilities.CreateKeyParameter("ARIA", K); + IBufferedCipher inCipher = CipherUtilities.GetCipher("ARIA/OCB/NoPadding"); + IBufferedCipher outCipher = CipherUtilities.GetCipher("ARIA/OCB/NoPadding"); + + inCipher.Init(true, new ParametersWithIV(key, N)); + + byte[] enc = inCipher.DoFinal(P); + if (!AreEqual(enc, C)) + { + Fail("ciphertext doesn't match in OCB"); + } + + outCipher.Init(false, new ParametersWithIV(key, N)); + + byte[] dec = outCipher.DoFinal(C); + if (!AreEqual(dec, P)) + { + Fail("plaintext doesn't match in OCB"); + } + + try + { + inCipher = CipherUtilities.GetCipher("ARIA/OCB/PKCS5Padding"); + + Fail("bad padding missed in OCB"); + } + catch (SecurityUtilityException) + { + // expected + } + } + + public override void PerformTest() + { + TestCiphers(); + TestWrap(); + TestWrapRfc3211(); + TestWrapRfc5649(); + TestOids(); + TestWrapOids(); + TestWrapPadOids(); + TestEax(); + TestCcm(); + TestGcm(); + TestOcb(); + } + } +} diff --git a/crypto/test/src/test/BaseBlockCipherTest.cs b/crypto/test/src/test/BaseBlockCipherTest.cs index 2997126f6..2381d92b0 100644 --- a/crypto/test/src/test/BaseBlockCipherTest.cs +++ b/crypto/test/src/test/BaseBlockCipherTest.cs @@ -94,23 +94,30 @@ namespace Org.BouncyCastle.Tests } } - protected void wrapTest( - int id, - string wrappingAlgorithm, - byte[] kek, - byte[] inBytes, - byte[] outBytes) + protected void wrapTest(int id, string wrappingAlgorithm, byte[] kek, byte[] inBytes, byte[] outBytes) { + wrapTest(id, wrappingAlgorithm, kek, null, null, inBytes, outBytes); + } + + protected void wrapTest(int id, string wrappingAlgorithm, byte[] kek, byte[] iv, SecureRandom rand, + byte[] inBytes, byte[] outBytes) + { IWrapper wrapper = WrapperUtilities.GetWrapper(wrappingAlgorithm); - wrapper.Init(true, ParameterUtilities.CreateKeyParameter(algorithm, kek)); + ICipherParameters cp = ParameterUtilities.CreateKeyParameter(algorithm, kek); + if (iv != null) + { + cp = new ParametersWithIV(cp, iv); + } + + wrapper.Init(true, ParameterUtilities.WithRandom(cp, rand)); try { byte[] cText = wrapper.Wrap(inBytes, 0, inBytes.Length); if (!AreEqual(cText, outBytes)) { - Fail("failed wrap test " + id + " expected " + Fail("failed wrap test " + id + " expected " + Hex.ToHexString(outBytes) + " got " + Hex.ToHexString(cText)); } @@ -124,7 +131,7 @@ namespace Org.BouncyCastle.Tests Fail("failed wrap test exception " + e.ToString(), e); } - wrapper.Init(false, ParameterUtilities.CreateKeyParameter(algorithm, kek)); + wrapper.Init(false, cp); try { @@ -132,7 +139,7 @@ namespace Org.BouncyCastle.Tests if (!AreEqual(pTextBytes, inBytes)) { - Fail("failed unwrap test " + id + " expected " + Fail("failed unwrap test " + id + " expected " + Hex.ToHexString(inBytes) + " got " + Hex.ToHexString(pTextBytes)); } |