summary refs log tree commit diff
path: root/crypto/test
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2018-09-25 21:47:28 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2018-09-25 21:47:28 +0700
commit7951b6781f03204652fd7a6ff023732b010ebd59 (patch)
treec854ba920f600ddf6aa7ed41bd06708a9e425c88 /crypto/test
parentFix ed25519 ignoring the public key offset (diff)
downloadBouncyCastle.NET-ed25519-7951b6781f03204652fd7a6ff023732b010ebd59.tar.xz
Port of SM4 from Java API
Diffstat (limited to 'crypto/test')
-rw-r--r--crypto/test/UnitTests.csproj2
-rw-r--r--crypto/test/src/crypto/test/RegressionTest.cs1
-rw-r--r--crypto/test/src/crypto/test/SM4Test.cs93
-rw-r--r--crypto/test/src/test/RegressionTest.cs1
-rw-r--r--crypto/test/src/test/SM4Test.cs149
5 files changed, 246 insertions, 0 deletions
diff --git a/crypto/test/UnitTests.csproj b/crypto/test/UnitTests.csproj
index bb046c2b6..d89cd6d56 100644
--- a/crypto/test/UnitTests.csproj
+++ b/crypto/test/UnitTests.csproj
@@ -265,6 +265,7 @@
     <Compile Include="src\crypto\test\SM2KeyExchangeTest.cs" />
     <Compile Include="src\crypto\test\SM2SignerTest.cs" />
     <Compile Include="src\crypto\test\SM3DigestTest.cs" />
+    <Compile Include="src\crypto\test\SM4Test.cs" />
     <Compile Include="src\crypto\test\SkeinDigestTest.cs" />
     <Compile Include="src\crypto\test\SkeinMacTest.cs" />
     <Compile Include="src\crypto\test\StreamCipherResetTest.cs" />
@@ -434,6 +435,7 @@
     <Compile Include="src\test\RegressionTest.cs" />
     <Compile Include="src\test\SEEDTest.cs" />
     <Compile Include="src\test\SigTest.cs" />
+    <Compile Include="src\test\SM4Test.cs" />
     <Compile Include="src\test\TestUtilities.cs" />
     <Compile Include="src\test\WrapTest.cs" />
     <Compile Include="src\test\X509CertificatePairTest.cs" />
diff --git a/crypto/test/src/crypto/test/RegressionTest.cs b/crypto/test/src/crypto/test/RegressionTest.cs
index 18aa62d97..f8b5c3c79 100644
--- a/crypto/test/src/crypto/test/RegressionTest.cs
+++ b/crypto/test/src/crypto/test/RegressionTest.cs
@@ -133,6 +133,7 @@ namespace Org.BouncyCastle.Crypto.Tests
             new SM2EngineTest(),
             new SM2KeyExchangeTest(),
             new SM2SignerTest(),
+            new SM4Test(),
             new X25519Test(),
             new X448Test(),
             new Ed25519Test(),
diff --git a/crypto/test/src/crypto/test/SM4Test.cs b/crypto/test/src/crypto/test/SM4Test.cs
new file mode 100644
index 000000000..ae2f18b00
--- /dev/null
+++ b/crypto/test/src/crypto/test/SM4Test.cs
@@ -0,0 +1,93 @@
+using System;
+
+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
+{
+    /**
+     * SM4 tester, vectors from <a href="http://eprint.iacr.org/2008/329.pdf">http://eprint.iacr.org/2008/329.pdf</a>
+     */
+	[TestFixture]
+    public class SM4Test
+        : CipherTest
+    {
+        static SimpleTest[] tests =
+        {
+            new BlockCipherVectorTest(0, new SM4Engine(),
+                new KeyParameter(Hex.Decode("0123456789abcdeffedcba9876543210")),
+                "0123456789abcdeffedcba9876543210",
+                "681edf34d206965e86b3e94f536e4246")
+        };
+
+        public SM4Test()
+            : base(tests, new SM4Engine(), new KeyParameter(new byte[16]))
+        {
+        }
+
+        public override void PerformTest()
+        {
+            base.PerformTest();
+
+            DoTest1000000();
+        }
+
+        private void DoTest1000000()
+        {
+            byte[] plain = Hex.Decode("0123456789abcdeffedcba9876543210");
+            byte[] key = Hex.Decode("0123456789abcdeffedcba9876543210");
+            byte[] cipher = Hex.Decode("595298c7c6fd271f0402f804c33d3f66");
+            byte[] buf = new byte[16];
+
+            IBlockCipher engine = new SM4Engine();
+
+            engine.Init(true, new KeyParameter(key));
+
+            Array.Copy(plain, 0, buf, 0, buf.Length);
+
+            for (int i = 0; i != 1000000; i++)
+            {
+                engine.ProcessBlock(buf, 0, buf, 0);
+            }
+
+            if (!AreEqual(cipher, buf))
+            {
+                Fail("1000000 encryption test failed");
+            }
+
+            engine.Init(false, new KeyParameter(key));
+
+            for (int i = 0; i != 1000000; i++)
+            {
+                engine.ProcessBlock(buf, 0, buf, 0);
+            }
+
+            if (!AreEqual(plain, buf))
+            {
+                Fail("1000000 decryption test failed");
+            }
+        }
+
+        public override string Name
+        {
+            get { return "SM4"; }
+        }
+
+        public static void Main(string[] args)
+        {
+            RunTest(new SM4Test());
+        }
+
+        [Test]
+        public void TestFunction()
+        {
+            string resultText = Perform().ToString();
+
+            Assert.AreEqual(Name + ": Okay", resultText);
+        }
+    }
+}
diff --git a/crypto/test/src/test/RegressionTest.cs b/crypto/test/src/test/RegressionTest.cs
index 0ffde72e4..7f4f38f00 100644
--- a/crypto/test/src/test/RegressionTest.cs
+++ b/crypto/test/src/test/RegressionTest.cs
@@ -64,6 +64,7 @@ namespace Org.BouncyCastle.Tests
 			new MqvTest(),
 			new CMacTest(),
 			new Crl5Test(),
+            new SM4Test()
 		};
 
 		public static void Main(
diff --git a/crypto/test/src/test/SM4Test.cs b/crypto/test/src/test/SM4Test.cs
new file mode 100644
index 000000000..5d36b3431
--- /dev/null
+++ b/crypto/test/src/test/SM4Test.cs
@@ -0,0 +1,149 @@
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+using Org.BouncyCastle.Crypto;
+using Org.BouncyCastle.Crypto.IO;
+using Org.BouncyCastle.Crypto.Parameters;
+using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities.Encoders;
+
+namespace Org.BouncyCastle.Tests
+{
+    /**
+     * basic test class for SM4
+     */
+	[TestFixture]
+    public class SM4Test
+        : BaseBlockCipherTest
+    {
+        internal static readonly string[] cipherTests =
+        {
+            "128",
+            "0123456789abcdeffedcba9876543210",
+            "0123456789abcdeffedcba9876543210",
+            "681edf34d206965e86b3e94f536e4246"
+        };
+
+        public SM4Test()
+            : base("SM4")
+        {
+        }
+
+        public void DoTest(
+            int         strength,
+            byte[]      keyBytes,
+            byte[]      input,
+            byte[]      output)
+        {
+            KeyParameter key = ParameterUtilities.CreateKeyParameter("SM4", keyBytes);
+
+            IBufferedCipher inCipher = CipherUtilities.GetCipher("SM4/ECB/NoPadding");
+            IBufferedCipher outCipher = CipherUtilities.GetCipher("SM4/ECB/NoPadding");
+
+            try
+            {
+                outCipher.Init(true, key);
+            }
+            catch (Exception e)
+            {
+                Fail("SM4 failed initialisation - " + e, e);
+            }
+
+            try
+            {
+                inCipher.Init(false, key);
+            }
+            catch (Exception e)
+            {
+                Fail("SM4 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("SM4 failed encryption - " + e, e);
+            }
+
+            byte[] bytes = bOut.ToArray();
+
+            if (!AreEqual(bytes, output))
+            {
+				Fail("SM4 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("SM4 failed encryption - " + e, e);
+            }
+
+            if (!AreEqual(bytes, input))
+            {
+				Fail("SM4 failed decryption - expected "
+					+ Hex.ToHexString(input) + " got "
+					+ Hex.ToHexString(bytes));
+            }
+        }
+
+        public override void PerformTest()
+        {
+            for (int i = 0; i != cipherTests.Length; i += 4)
+            {
+                DoTest(int.Parse(cipherTests[i]),
+                    Hex.Decode(cipherTests[i + 1]),
+                    Hex.Decode(cipherTests[i + 2]),
+                    Hex.Decode(cipherTests[i + 3]));
+            }
+        }
+
+        public static void Main(string[] args)
+        {
+            RunTest(new SM4Test());
+        }
+    }
+}