private static void Test4(IBlockCrypto crypto, string hex1, string hex2) { Assert.AreEqual(@"AES-CBC", crypto.Name); Assert.AreEqual(16, crypto.BlockSize); Span <byte> h1 = hex1.FromHex(); Span <byte> h2 = hex2.FromHex(); Span <byte> o1 = stackalloc byte[64]; crypto.Encrypt4(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); crypto.Reset(); crypto.Encrypt4(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); crypto.Reset(); crypto.Decrypt(h2, o1); crypto.Decrypt(h2.Slice(16), o1.Slice(16)); crypto.Decrypt(h2.Slice(32), o1.Slice(32)); crypto.Decrypt(h2.Slice(48), o1.Slice(48)); Assert.IsTrue(o1.SequenceEqual(h1)); crypto.Dispose(); }
private void TestDecrypt(IBlockCrypto crypto, Span <byte> origin) { Span <byte> o = stackalloc byte[origin.Length]; for (var i = 0; i < Max; ++i) { crypto.Decrypt(origin, o); } crypto.Dispose(); }
public void Dispose() { _crypto.Dispose(); _gHash.Dispose(); ArrayPool <byte> .Shared.Return(_buffer); ArrayPool <byte> .Shared.Return(_tagBuffer); ArrayPool <byte> .Shared.Return(_counterBlock); }
private static void Test4(IBlockCrypto crypto, string hex1, string hex2) { Assert.AreEqual(@"AES", crypto.Name); Assert.AreEqual(16, crypto.BlockSize); Span <byte> h1 = hex1.FromHex(); Span <byte> h2 = hex2.FromHex(); Span <byte> o1 = stackalloc byte[crypto.BlockSize * 4]; crypto.Encrypt4(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); crypto.Encrypt4(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); crypto.Dispose(); }
private static void Test(IBlockCrypto crypto, string hex1, string hex2, string hex3) { Assert.AreEqual(@"SM4", crypto.Name); Assert.AreEqual(16, crypto.BlockSize); Span <byte> h1 = hex1.FromHex(); Span <byte> h2 = hex2.FromHex(); Span <byte> h3 = hex3.FromHex(); Span <byte> o1 = new byte[crypto.BlockSize]; crypto.Encrypt(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); crypto.Encrypt(h1, o1); Assert.IsTrue(o1.SequenceEqual(h2)); Span <byte> t = h1; for (int i = 0; i < 1000000; ++i) { crypto.Encrypt(t, o1); t = o1; } Assert.IsTrue(t.SequenceEqual(h3)); crypto.Decrypt(h2, o1); Assert.IsTrue(o1.SequenceEqual(h1)); crypto.Decrypt(h2, o1); Assert.IsTrue(o1.SequenceEqual(h1)); t = h3; for (int i = 0; i < 1000000; ++i) { crypto.Decrypt(t, o1); t = o1; } Assert.IsTrue(t.SequenceEqual(h1)); crypto.Dispose(); }
private static void TestN(int n, IBlockCrypto crypto, ReadOnlySpan <byte> key) { using SM4Crypto sf = new(key); ReadOnlySpan <byte> source = RandomNumberGenerator.GetBytes(n * sf.BlockSize); Span <byte> expected = stackalloc byte[source.Length]; for (int i = 0; i < n; ++i) { sf.Encrypt(source.Slice(i * sf.BlockSize, sf.BlockSize), expected.Slice(i * sf.BlockSize, sf.BlockSize)); } Assert.AreEqual(@"SM4", crypto.Name); Assert.AreEqual(n * sf.BlockSize, crypto.BlockSize); Span <byte> destination = stackalloc byte[source.Length]; crypto.Encrypt(source, destination); Assert.IsTrue(expected.SequenceEqual(destination)); crypto.Dispose(); }