using Microsoft.VisualStudio.TestTools.UnitTesting;

using Renci.SshNet.Security.Cryptography.Ciphers;
using Renci.SshNet.Tests.Common;

using CipherMode = System.Security.Cryptography.CipherMode;

namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
{
    /// <summary>
    /// Implements 3DES cipher algorithm.
    /// </summary>
    [TestClass]
    public class TripleDesCipherTest : TestBase
    {
        // All tests below this line were generated by the script in TripleDesCipherTest.Gen.cs.txt
        [TestMethod]
        public void TripleDes_CBC_Length8_NoPad()
        {
            var input = new byte[]
            {
                0x03, 0xe1, 0xe1, 0xaa, 0xa5, 0xbc, 0xa1, 0x9f,
            };
            var key = new byte[]
            {
                0xba, 0x8c, 0x42, 0x05, 0x8b, 0x4a, 0xbf, 0x28, 0x96, 0x39, 0xec, 0x0d, 0xfc, 0x2d, 0xb2, 0x7c,
                0xe9, 0x74, 0x8e, 0x5f, 0xb9, 0xf3, 0x99, 0xce,
            };
            var iv = new byte[]
            {
                0xe1, 0x1a, 0x5c, 0x51, 0xa3, 0x1d, 0xd7, 0x1b,
            };

            // echo -n -e '\x03\xe1\xe1\xaa\xa5\xbc\xa1\x9f' | openssl enc -e -des-ede3-cbc -K BA8C42058B4ABF289639EC0DFC2DB27CE9748E5FB9F399CE -iv E11A5C51A31DD71B -nopad | hd
            var expected = new byte[]
            {
                0x0c, 0xd8, 0x26, 0xd1, 0xed, 0x41, 0x73, 0x25,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: false).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: false).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CBC_Length8_Pad()
        {
            var input = new byte[]
            {
                0x15, 0x8c, 0xad, 0xa6, 0xaf, 0x63, 0x0d, 0x8c,
            };
            var key = new byte[]
            {
                0x1a, 0xf1, 0x3a, 0x35, 0x8c, 0xca, 0x3f, 0xd6, 0x2f, 0x65, 0xc1, 0x31, 0x2d, 0x41, 0xe5, 0xc7,
                0xf3, 0x74, 0x23, 0x71, 0xed, 0x6d, 0x84, 0x79,
            };
            var iv = new byte[]
            {
                0x61, 0xd0, 0xf8, 0x6f, 0x7f, 0x0c, 0xcc, 0x86,
            };

            // echo -n -e '\x15\x8c\xad\xa6\xaf\x63\x0d\x8c' | openssl enc -e -des-ede3-cbc -K 1AF13A358CCA3FD62F65C1312D41E5C7F3742371ED6D8479 -iv 61D0F86F7F0CCC86 | hd
            var expected = new byte[]
            {
                0x91, 0xcb, 0x5c, 0xdf, 0x6d, 0xf2, 0x85, 0xbf, 0x5c, 0xf8, 0xf9, 0xbd, 0xe1, 0xc4, 0xb9, 0xab,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CBC_Length17_Pad()
        {
            var input = new byte[]
            {
                0x67, 0x02, 0x45, 0xc8, 0xb8, 0x64, 0x42, 0x17, 0xda, 0x85, 0x21, 0x3e, 0x5c, 0xa6, 0xee, 0xd4,
                0xa7,
            };
            var key = new byte[]
            {
                0xe6, 0xb3, 0x3b, 0x3f, 0x9c, 0x9e, 0xac, 0x6c, 0xc1, 0xd3, 0xbb, 0xd2, 0xd0, 0x57, 0x22, 0x99,
                0x3a, 0xc9, 0x2b, 0xfb, 0x1d, 0x0e, 0x8e, 0x31,
            };
            var iv = new byte[]
            {
                0x0c, 0x96, 0x68, 0x4c, 0x46, 0x1d, 0xbb, 0xe1,
            };

            // echo -n -e '\x67\x02\x45\xc8\xb8\x64\x42\x17\xda\x85\x21\x3e\x5c\xa6\xee\xd4\xa7' | openssl enc -e -des-ede3-cbc -K E6B33B3F9C9EAC6CC1D3BBD2D05722993AC92BFB1D0E8E31 -iv 0C96684C461DBBE1 | hd
            var expected = new byte[]
            {
                0xd1, 0x8b, 0xe6, 0x2d, 0x26, 0x99, 0xff, 0x02, 0xd8, 0x45, 0x2b, 0x21, 0x90, 0xc6, 0x85, 0xa3,
                0x58, 0xa7, 0x5a, 0xfc, 0xc7, 0x20, 0x40, 0x1f,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CBC_Length32_NoPad()
        {
            var input = new byte[]
            {
                0x23, 0xc8, 0x99, 0x59, 0x90, 0x47, 0xcb, 0x63, 0x99, 0x5b, 0xf7, 0x91, 0x87, 0x44, 0x09, 0x2e,
                0xff, 0xa4, 0x21, 0xdc, 0xc3, 0xd9, 0x89, 0xd7, 0x24, 0x0a, 0x32, 0x05, 0x36, 0x60, 0x25, 0xa4,
            };
            var key = new byte[]
            {
                0x17, 0xda, 0xaf, 0x08, 0xbe, 0xc9, 0x08, 0xf3, 0xfe, 0xc7, 0x61, 0xc2, 0x17, 0xfd, 0xaa, 0xc7,
                0x8d, 0x3a, 0x4c, 0xa2, 0xfb, 0xde, 0x1e, 0x49,
            };
            var iv = new byte[]
            {
                0x3e, 0xc1, 0x34, 0x86, 0x14, 0xc6, 0x2d, 0x39,
            };

            // echo -n -e '\x23\xc8\x99\x59\x90\x47\xcb\x63\x99\x5b\xf7\x91\x87\x44\x09\x2e\xff\xa4\x21\xdc\xc3\xd9\x89\xd7\x24\x0a\x32\x05\x36\x60\x25\xa4' | openssl enc -e -des-ede3-cbc -K 17DAAF08BEC908F3FEC761C217FDAAC78D3A4CA2FBDE1E49 -iv 3EC1348614C62D39 -nopad | hd
            var expected = new byte[]
            {
                0x75, 0x50, 0xa3, 0x30, 0xc8, 0xaf, 0x2d, 0xa8, 0x8b, 0x78, 0x07, 0x89, 0xb0, 0x82, 0xa1, 0x74,
                0xd1, 0x1c, 0x44, 0xd2, 0x1f, 0x74, 0x1b, 0xc7, 0x93, 0x17, 0x5e, 0x31, 0x31, 0x0b, 0x95, 0x50,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: false).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: false).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CBC_Length32_Pad()
        {
            var input = new byte[]
            {
                0x35, 0x52, 0x79, 0xad, 0x95, 0x01, 0x6f, 0x36, 0x9b, 0x2e, 0xde, 0xfc, 0x77, 0xc7, 0xc0, 0x27,
                0x60, 0x6b, 0x78, 0xfc, 0x13, 0x83, 0xa8, 0x38, 0xbb, 0x65, 0xca, 0xfd, 0x94, 0x82, 0xde, 0x38,
            };
            var key = new byte[]
            {
                0x99, 0x28, 0x8c, 0xc4, 0x84, 0xfd, 0x32, 0x8c, 0xca, 0x16, 0x06, 0xcc, 0x00, 0x22, 0xd2, 0x76,
                0x00, 0x0d, 0x25, 0xa9, 0x4e, 0x31, 0x25, 0xb1,
            };
            var iv = new byte[]
            {
                0xaa, 0x40, 0xf1, 0x2f, 0x36, 0x72, 0xa2, 0x18,
            };

            // echo -n -e '\x35\x52\x79\xad\x95\x01\x6f\x36\x9b\x2e\xde\xfc\x77\xc7\xc0\x27\x60\x6b\x78\xfc\x13\x83\xa8\x38\xbb\x65\xca\xfd\x94\x82\xde\x38' | openssl enc -e -des-ede3-cbc -K 99288CC484FD328CCA1606CC0022D276000D25A94E3125B1 -iv AA40F12F3672A218 | hd
            var expected = new byte[]
            {
                0x0f, 0x9a, 0xb5, 0xc9, 0x30, 0xac, 0xd0, 0x64, 0xaf, 0xdd, 0x5e, 0x0a, 0x89, 0xda, 0xe3, 0xcb,
                0x08, 0xa1, 0x82, 0x1f, 0x76, 0xd5, 0x3b, 0x59, 0x3a, 0x61, 0xff, 0x87, 0x11, 0xa9, 0x40, 0x5a,
                0x74, 0x04, 0x8c, 0x1f, 0xa4, 0xca, 0x1b, 0xf7,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CBC, pkcs7Padding: true).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CFB_Length8_NoPad()
        {
            var input = new byte[]
            {
                0x4f, 0xa6, 0x62, 0x4f, 0x3b, 0xfb, 0xa3, 0x63,
            };
            var key = new byte[]
            {
                0x38, 0xec, 0x32, 0xfd, 0x7d, 0xdb, 0x38, 0x99, 0x93, 0x53, 0xfc, 0x86, 0x5d, 0x35, 0xe9, 0x68,
                0x02, 0xda, 0x1a, 0x43, 0x0b, 0x02, 0x55, 0x57,
            };
            var iv = new byte[]
            {
                0x74, 0xed, 0x7d, 0x5a, 0xbf, 0x82, 0x3b, 0x05,
            };

            // echo -n -e '\x4f\xa6\x62\x4f\x3b\xfb\xa3\x63' | openssl enc -e -des-ede3-cfb -K 38EC32FD7DDB38999353FC865D35E96802DA1A430B025557 -iv 74ED7D5ABF823B05 -nopad | hd
            var expected = new byte[]
            {
                0x28, 0x48, 0x3f, 0xb4, 0x48, 0xce, 0x96, 0xaf,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CFB_Length17_NoPad()
        {
            var input = new byte[]
            {
                0x6a, 0xc2, 0x70, 0x62, 0xff, 0x28, 0x34, 0xce, 0x08, 0x58, 0x9c, 0xe3, 0x76, 0x1b, 0xbb, 0x1a,
                0xbc,
            };
            var key = new byte[]
            {
                0xf9, 0x4c, 0x60, 0xe1, 0x5f, 0x57, 0x35, 0x96, 0xda, 0x89, 0x8f, 0x5e, 0xde, 0xd9, 0x10, 0x17,
                0xf6, 0x1b, 0x9a, 0xc4, 0x87, 0x69, 0xda, 0xa5,
            };
            var iv = new byte[]
            {
                0x4b, 0x3b, 0xb3, 0x66, 0x71, 0xe0, 0x58, 0x31,
            };

            // echo -n -e '\x6a\xc2\x70\x62\xff\x28\x34\xce\x08\x58\x9c\xe3\x76\x1b\xbb\x1a\xbc' | openssl enc -e -des-ede3-cfb -K F94C60E15F573596DA898F5EDED91017F61B9AC48769DAA5 -iv 4B3BB36671E05831 -nopad | hd
            var expected = new byte[]
            {
                0x5a, 0x7e, 0x55, 0x4d, 0x63, 0xc1, 0x80, 0x32, 0x84, 0xdc, 0xd0, 0xa7, 0x6c, 0xea, 0x65, 0x42,
                0xc3,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }

        [TestMethod]
        public void TripleDes_CFB_Length32_NoPad()
        {
            var input = new byte[]
            {
                0x62, 0x9d, 0xc6, 0x36, 0xda, 0x23, 0x0b, 0x6b, 0x3b, 0xcb, 0x24, 0x9f, 0xa4, 0x6f, 0x29, 0x7e,
                0x8b, 0xcb, 0x7f, 0xff, 0x21, 0x56, 0x34, 0x90, 0x72, 0xba, 0x95, 0x23, 0xa3, 0xcf, 0x25, 0xfa,
            };
            var key = new byte[]
            {
                0x30, 0x5e, 0xfc, 0x40, 0x13, 0xda, 0x3d, 0xd3, 0x10, 0x2f, 0x89, 0xbc, 0x44, 0x3a, 0x01, 0xdb,
                0x11, 0x34, 0xda, 0xa5, 0x60, 0x58, 0x10, 0x0c,
            };
            var iv = new byte[]
            {
                0x69, 0x35, 0xc3, 0x1f, 0x8d, 0xe7, 0xc7, 0x6b,
            };

            // echo -n -e '\x62\x9d\xc6\x36\xda\x23\x0b\x6b\x3b\xcb\x24\x9f\xa4\x6f\x29\x7e\x8b\xcb\x7f\xff\x21\x56\x34\x90\x72\xba\x95\x23\xa3\xcf\x25\xfa' | openssl enc -e -des-ede3-cfb -K 305EFC4013DA3DD3102F89BC443A01DB1134DAA56058100C -iv 6935C31F8DE7C76B -nopad | hd
            var expected = new byte[]
            {
                0xb8, 0xcf, 0xf4, 0xf9, 0x88, 0xfd, 0x02, 0xf1, 0xb9, 0xe9, 0xf0, 0xb3, 0x1d, 0x0a, 0x9b, 0x91,
                0x30, 0x3e, 0xf7, 0xa2, 0xf6, 0xb4, 0xa5, 0xc4, 0x4d, 0x89, 0x06, 0xed, 0x55, 0xd3, 0x28, 0xd0,
            };

            var actual = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Encrypt(input);

            CollectionAssert.AreEqual(expected, actual);

            var decrypted = new TripleDesCipher(key, (byte[])iv.Clone(), CipherMode.CFB, pkcs7Padding: false).Decrypt(actual);

            CollectionAssert.AreEqual(input, decrypted);
        }
    }
}
