Пример #1
0
 public void Decrypt_returns_correct_value()
 {
     foreach (var i in Rfc3610TestCases)
     {
         var aes       = new SjclAes(i.Key);
         var plaintext = SjclCcm.Decrypt(aes, i.Ciphertext, i.Iv, i.Adata, i.TagLength);
         Assert.That(plaintext, Is.EqualTo(i.Plaintext));
     }
 }
Пример #2
0
 public void Encrypt_returns_correct_value()
 {
     foreach (var i in Rfc3610TestCases)
     {
         var aes        = new SjclAes(i.Key);
         var ciphertext = SjclCcm.Encrypt(aes, i.Plaintext, i.Iv, i.Adata, i.TagLength);
         Assert.AreEqual(i.Ciphertext, ciphertext);
     }
 }
Пример #3
0
        public void Encrypt_throws_on_too_short_iv()
        {
            var aes = new SjclAes(new byte[16]);

            for (var i = 0; i < 7; ++i)
            {
                Assert.That(() => SjclCcm.Encrypt(aes, new byte[1], new byte[i], new byte[0], 8),
                            Throws.TypeOf <CryptoException>()
                            .And.Message.EqualTo("IV must be at least 7 bytes long"));
            }
        }
Пример #4
0
        public void Encrypt_throws_on_too_short_iv()
        {
            var aes = new SjclAes(new byte[16]);

            for (var i = 0; i < 7; ++i)
            {
                var e = Assert.Throws <CryptoException>(
                    () => SjclCcm.Encrypt(aes, new byte[1], new byte[i], new byte[0], 8));
                Assert.AreEqual("IV must be at least 7 bytes long", e.Message);
            }
        }
Пример #5
0
        public void Encrypt_throws_on_invalid_tag_length()
        {
            var testCases = new int[] { -1, 0, 1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 18, 19, 20, 1024 };

            var aes = new SjclAes(new byte[16]);

            foreach (var i in testCases)
            {
                Assert.That(() => SjclCcm.Encrypt(aes, new byte[1], new byte[16], new byte[0], i),
                            Throws.TypeOf <CryptoException>()
                            .And.Message.EqualTo("Tag must be 4, 8, 10, 12, 14 or 16 bytes long"));
            }
        }
Пример #6
0
        public void EncodeAdataLengt_returns_corrent_value()
        {
            var testCases = new Dictionary <int, string>
            {
                { 0x0001, "0001" },
                { 0x0010, "0010" },
                { 0xfefe, "fefe" },
                { 0xfeff, "fffe" + "0000feff" },
                { 0xffff, "fffe" + "0000ffff" },
                { 0x7fffffff, "fffe" + "7fffffff" },
            };

            foreach (var i in testCases)
            {
                Assert.That(SjclCcm.EncodeAdataLength(i.Key), Is.EqualTo(i.Value.DecodeHex()));
            }
        }
Пример #7
0
        public void ComputeLengthLength_returns_corrent_value()
        {
            var testCases = new Dictionary <int, int>
            {
                { 0x01, 2 },
                { 0xff, 2 },
                { 0x0100, 2 },
                { 0xffff, 2 },
                { 0x010000, 3 },
                { 0xffffff, 3 },
                { 0x01000000, 4 },
                { 0x7fffffff, 4 },
            };

            foreach (var i in testCases)
            {
                Assert.That(SjclCcm.ComputeLengthLength(i.Key), Is.EqualTo(i.Value));
            }
        }
Пример #8
0
        //
        // Helpers
        //

        private static void VerifyCmmMismatchThrown(SjclAes aes, byte[] ciphertext, byte[] iv, byte[] adata, int tagLength)
        {
            Assert.That(() => SjclCcm.Decrypt(aes, ciphertext, iv, adata, tagLength),
                        Throws.TypeOf <CryptoException>()
                        .And.Message.EqualTo("CCM tag doesn't match"));
        }
Пример #9
0
 public void EncodeAdataLengt_throws_on_negative_length()
 {
     Assert.That(() => SjclCcm.EncodeAdataLength(-1),
                 Throws.TypeOf <CryptoException>()
                 .And.Message.EqualTo("Adata length must be positive"));
 }
Пример #10
0
        //
        // Helpers
        //

        private static void VerifyCmmMismatchThrown(SjclAes aes, byte[] ciphertext, byte[] iv, byte[] adata, int tagLength)
        {
            var e = Assert.Throws <CryptoException>(() => SjclCcm.Decrypt(aes, ciphertext, iv, adata, tagLength));

            Assert.AreEqual("CCM tag doesn't match", e.Message);
        }
Пример #11
0
 public void EncodeAdataLengt_throws_on_negative_length()
 {
     SjclCcm.EncodeAdataLength(-1);
 }
Пример #12
0
 public void EncodeAdataLengt_throws_on_zero_length()
 {
     SjclCcm.EncodeAdataLength(0);
 }