Example #1
0
    public GcmCryptoModeBlock16X86(IBlockCrypto crypto, IBlockCrypto crypto16)
    {
        if (crypto.BlockSize is not BlockSize)
        {
            throw new ArgumentException($@"Crypto block size must be {BlockSize} bytes.", nameof(crypto));
        }

        if (crypto16.BlockSize is not BlockSize16)
        {
            throw new ArgumentException($@"Crypto block size must be {BlockSize16} bytes.", nameof(crypto16));
        }

        _crypto   = crypto;
        _crypto16 = crypto16;

        _buffer = ArrayPool <byte> .Shared.Rent(BlockSize16);

        _tagBuffer = ArrayPool <byte> .Shared.Rent(TagSize);

        _counterBlock = ArrayPool <byte> .Shared.Rent(BlockSize16);

        Span <byte> key = _buffer.AsSpan(0, 16);

        _crypto.Encrypt(Init, key);
        _gHash = GHashUtils.Create(key);
    }
Example #2
0
        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();
        }
 public static IStreamBlockCryptoMode Ctr(IBlockCrypto crypto, ReadOnlySpan <byte> iv)
 {
     if (Sse2.IsSupported && Ssse3.IsSupported && Sse41.IsSupported)
     {
         return(new CTR128StreamModeX86(crypto, iv));
     }
     return(new CTR128StreamMode(crypto, iv));
 }
Example #4
0
 public void Setup()
 {
     byte[] random = RandomNumberGenerator.GetBytes(4);
     _randombytes   = random.ToArray();
     _randombytes2  = random.ToArray();
     _randombytes3  = random.ToArray();
     _randombytes4  = random.ToArray();
     _randombytes16 = RandomNumberGenerator.GetBytes(16);
     _aes           = AESUtils.CreateECB(_randombytes16);
 }
Example #5
0
        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();
        }
Example #6
0
        public void Setup()
        {
            var random = Utils.RandBytes(4);

            _randombytes   = random.ToArray();
            _randombytes2  = random.ToArray();
            _randombytes3  = random.ToArray();
            _randombytes4  = random.ToArray();
            _randombytes16 = Utils.RandBytes(16).ToArray();
            _aes           = AESUtils.CreateECB(_randombytes16);
        }
Example #7
0
    public GcmCryptoMode(IBlockCrypto crypto)
    {
        if (crypto.BlockSize is not BlockSize)
        {
            throw new ArgumentException($@"Crypto block size must be {BlockSize} bytes.", nameof(crypto));
        }
        _crypto = crypto;

        _buffer = ArrayPool <byte> .Shared.Rent(BlockSize);

        _tagBuffer = ArrayPool <byte> .Shared.Rent(TagSize);

        _counterBlock = ArrayPool <byte> .Shared.Rent(BlockSize);

        _crypto.Encrypt(Init, _buffer);
        _gHash = GHashUtils.Create(_buffer);
    }
Example #8
0
        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();
        }
Example #9
0
    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();
    }
Example #10
0
    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();
    }