Esempio n. 1
0
        public void EncryptSimd_Decrypt(string value)
        {
            if (System.Runtime.Intrinsics.X86.Aes.IsSupported)
            {
                var data              = Encoding.UTF8.GetBytes(value);
                var ciphertext        = new Span <byte>(new byte[(data.Length + 16) & ~15]);
                var authenticationTag = new Span <byte>(new byte[48]);
                var plaintext         = new Span <byte>(new byte[ciphertext.Length]);
                var key         = SymmetricJwk.FromByteArray(Encoding.UTF8.GetBytes("ThisIsA128bitKey" + "ThisIsA128bitKey" + "ThisIsA128bitKey"));
                var nonce       = Encoding.UTF8.GetBytes("ThisIsAnInitVect");
                var encryptorNi = new AesCbcHmacEncryptor(EncryptionAlgorithm.A192CbcHS384, new Aes192CbcEncryptor());
                encryptorNi.Encrypt(key.AsSpan(), data, nonce, nonce, ciphertext, authenticationTag, out int tagSize);
                var  decryptor = new AesCbcHmacDecryptor(EncryptionAlgorithm.A192CbcHS384, new AesCbcDecryptor(EncryptionAlgorithm.A192CbcHS384));
                bool decrypted = decryptor.TryDecrypt(key.K, ciphertext, nonce, nonce, authenticationTag.Slice(0, tagSize), plaintext, out int bytesWritten);
                Assert.True(decrypted);
                Assert.Equal(data, plaintext.Slice(0, bytesWritten).ToArray());

                var decryptorNi = new AesCbcHmacDecryptor(EncryptionAlgorithm.A192CbcHS384, new Aes192CbcDecryptor());
                plaintext.Clear();
                decrypted = decryptorNi.TryDecrypt(key.K, ciphertext, nonce, nonce, authenticationTag.Slice(0, tagSize), plaintext, out bytesWritten);
                Assert.True(decrypted);
                Assert.Equal(data, plaintext.Slice(0, bytesWritten).ToArray());
                Assert.Equal(24, tagSize);
            }
        }
Esempio n. 2
0
        public void Decrypt_Empty()
        {
            Span <byte> data = default;
            Span <byte> authenticationTag = default;
            var         plaintext         = new byte[0];
            var         key            = SymmetricJwk.FromByteArray(Encoding.UTF8.GetBytes("ThisIsA128bitKey" + "ThisIsA128bitKey"));
            Span <byte> nonce          = default;
            Span <byte> associatedData = default;
            var         decryptor      = new AesCbcHmacDecryptor(EncryptionAlgorithm.A128CbcHS256);

            bool decrypted = decryptor.TryDecrypt(key.K, data, nonce, associatedData, authenticationTag, plaintext, out int bytesWritten);

            Assert.False(decrypted);
            Assert.Equal(0, bytesWritten);
        }
Esempio n. 3
0
        public void EncryptFast_Decrypt()
        {
            var data              = Encoding.UTF8.GetBytes("This is a test string for encryption.");
            var ciphertext        = new Span <byte>(new byte[(data.Length + 16) & ~15]);
            var authenticationTag = new Span <byte>(new byte[64]);
            var plaintext         = new Span <byte>(new byte[data.Length]);
            var key       = SymmetricJwk.GenerateKey(512);
            var nonce     = new byte[] { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
            var encryptor = new AesCbcHmacEncryptor(key, EncryptionAlgorithm.Aes256CbcHmacSha512);

            encryptor.Encrypt(data, nonce, nonce, ciphertext, authenticationTag);
            var  decryptor = new AesCbcHmacDecryptor(key, EncryptionAlgorithm.Aes256CbcHmacSha512);
            bool decrypted = decryptor.TryDecrypt(ciphertext, nonce, nonce, authenticationTag, plaintext, out int bytesWritten);

            Assert.True(decrypted);
        }
Esempio n. 4
0
        public void EncryptFast_Decrypt()
        {
            var data              = Encoding.UTF8.GetBytes("This is a test string for encryption.");
            var ciphertext        = new Span <byte>(new byte[(data.Length + 16) & ~15]);
            var authenticationTag = new Span <byte>(new byte[48]);
            var plaintext         = new Span <byte>(new byte[data.Length]);
            var key       = SymmetricJwk.GenerateKey(386);
            var nonce     = new byte[] { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
            var encryptor = new AesCbcHmacEncryptor(EncryptionAlgorithm.A192CbcHS384);

            encryptor.Encrypt(key.AsSpan(), data, nonce, nonce, ciphertext, authenticationTag, out int tagSize);
            var  decryptor = new AesCbcHmacDecryptor(EncryptionAlgorithm.A192CbcHS384);
            bool decrypted = decryptor.TryDecrypt(key.K, ciphertext, nonce, nonce, authenticationTag.Slice(0, tagSize), plaintext, out int bytesWritten);

            Assert.True(decrypted);
            Assert.Equal(24, tagSize);
        }
Esempio n. 5
0
        public void Decrypt_Empty()
        {
            if (System.Runtime.Intrinsics.X86.Aes.IsSupported)
            {
                Span <byte> data = default;
                Span <byte> authenticationTag = default;
                var         plaintext         = new byte[0];
                var         key            = SymmetricJwk.FromByteArray(Encoding.UTF8.GetBytes("ThisIsA128bitKey" + "ThisIsA128bitKey" + "ThisIsA128bitKey"));
                Span <byte> nonce          = default;
                Span <byte> associatedData = default;
                var         decryptor      = new AesCbcHmacDecryptor(EncryptionAlgorithm.A192CbcHS384);

                bool decrypted = decryptor.TryDecrypt(key.K, data, nonce, associatedData, authenticationTag, plaintext, out int bytesWritten);
                Assert.False(decrypted);
                Assert.Equal(0, bytesWritten);

                var decryptorNi = new AesCbcHmacDecryptor(EncryptionAlgorithm.A192CbcHS384, new Aes192CbcDecryptor());
                decrypted = decryptorNi.TryDecrypt(key.K, data, nonce, associatedData, authenticationTag, plaintext, out bytesWritten);
                Assert.False(decrypted);
                Assert.Equal(0, bytesWritten);
            }
        }
Esempio n. 6
0
        public void EncryptSimd_Decrypt(string value)
        {
            var data              = Encoding.UTF8.GetBytes(value);
            var ciphertext        = new Span <byte>(new byte[(data.Length + 16) & ~15]);
            var authenticationTag = new Span <byte>(new byte[64]);
            var plaintext         = new Span <byte>(new byte[ciphertext.Length]);
            var key         = new SymmetricJwk(Encoding.UTF8.GetBytes("ThisIsA128bitKey" + "ThisIsA128bitKey" + "ThisIsA128bitKey" + "ThisIsA128bitKey"));
            var nonce       = Encoding.UTF8.GetBytes("ThisIsAnInitVect");
            var encryptorNi = new AesCbcHmacEncryptor(key.K.Slice(0, 32), EncryptionAlgorithm.Aes256CbcHmacSha512, new Aes256NiCbcEncryptor(key.K.Slice(32)));

            encryptorNi.Encrypt(data, nonce, nonce, ciphertext, authenticationTag);
            var  decryptor = new AesCbcHmacDecryptor(key, EncryptionAlgorithm.Aes256CbcHmacSha512);
            bool decrypted = decryptor.TryDecrypt(ciphertext, nonce, nonce, authenticationTag, plaintext, out int bytesWritten);

            Assert.True(decrypted);
            Assert.Equal(data, plaintext.Slice(0, bytesWritten).ToArray());

            var decryptorNi = new AesCbcHmacDecryptor(key.K.Slice(0, 32), EncryptionAlgorithm.Aes256CbcHmacSha512, new Aes256NiCbcDecryptor(key.K.Slice(32)));

            plaintext.Clear();
            decrypted = decryptorNi.TryDecrypt(ciphertext, nonce, nonce, authenticationTag, plaintext, out bytesWritten);
            Assert.True(decrypted);
            Assert.Equal(data, plaintext.Slice(0, bytesWritten).ToArray());
        }