예제 #1
0
        public void ComputeInverseSboxTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var tt    = SjclAes.ComputeTrippleTable(dt);
            var sbox  = SjclAes.ComputeSboxTable(dt, tt);
            var table = SjclAes.ComputeInverseSboxTable(sbox);

            // Test data is generated with SJCL sources
            Assert.AreEqual(256, table.Length);

            Assert.AreEqual(0x52, table[0x00]);
            Assert.AreEqual(0x09, table[0x01]);
            Assert.AreEqual(0x6a, table[0x02]);
            Assert.AreEqual(0x6b, table[0x7f]);
            Assert.AreEqual(0x3a, table[0x80]);
            Assert.AreEqual(0x0c, table[0xfe]);
            Assert.AreEqual(0x7d, table[0xff]);

            // Every value should be exactly once
            Array.Sort(table);
            for (var i = 0; i < 256; ++i)
            {
                Assert.AreEqual(i, table[i]);
            }
        }
예제 #2
0
        public void ComputeInverseSboxTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var tt    = SjclAes.ComputeTrippleTable(dt);
            var sbox  = SjclAes.ComputeSboxTable(dt, tt);
            var table = SjclAes.ComputeInverseSboxTable(sbox);

            // Test data is generated with SJCL sources
            Assert.That(table.Length, Is.EqualTo(256));

            Assert.That(table[0x00], Is.EqualTo(0x52));
            Assert.That(table[0x01], Is.EqualTo(0x09));
            Assert.That(table[0x02], Is.EqualTo(0x6a));
            Assert.That(table[0x7f], Is.EqualTo(0x6b));
            Assert.That(table[0x80], Is.EqualTo(0x3a));
            Assert.That(table[0xfe], Is.EqualTo(0x0c));
            Assert.That(table[0xff], Is.EqualTo(0x7d));

            // Every value should be exactly once
            Array.Sort(table);
            for (var i = 0; i < 256; ++i)
            {
                Assert.That(table[i], Is.EqualTo(i));
            }
        }
예제 #3
0
        public void ScheduleEncryptionKey_throws_on_incorrect_input_key_length()
        {
            var dt   = SjclAes.ComputeDoubleTable();
            var sbox = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));

            foreach (var i in new[] { 0, 1, 2, 3, 4, 15, 17, 23, 25, 31, 33, 1024 })
            {
                var e = Assert.Throws <ArgumentException>(() => SjclAes.ScheduleEncryptionKey(new byte[i], sbox));
                Assert.AreEqual(string.Format("Invalid key length: {0}\r\nParameter name: key", i), e.Message);
            }
        }
예제 #4
0
        public void ScheduleEncryptionKey_returns_correct_result()
        {
            var dt   = SjclAes.ComputeDoubleTable();
            var sbox = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));

            foreach (var i in KeyTestCases)
            {
                var key = SjclAes.ScheduleEncryptionKey(i.Key, sbox);
                Assert.AreEqual(i.EncryptionKey, key);
            }
        }
예제 #5
0
        public void ScheduleEncryptionKey_throws_on_incorrect_input_key_length()
        {
            var dt   = SjclAes.ComputeDoubleTable();
            var sbox = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));

            foreach (var i in new[] { 0, 1, 2, 3, 4, 15, 17, 23, 25, 31, 33, 1024 })
            {
                Assert.That(() => SjclAes.ScheduleEncryptionKey(new byte[i], sbox),
                            Throws.TypeOf <ArgumentException>()
                            .And.Message.StartsWith("Invalid key length"));
            }
        }
예제 #6
0
        public void ScheduleDecryptionKey_returns_correct_result()
        {
            var dt     = SjclAes.ComputeDoubleTable();
            var sbox   = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));
            var decode = SjclAes.ComputeDecodeTable(dt, sbox);

            foreach (var i in KeyTestCases)
            {
                var encKey = SjclAes.ScheduleEncryptionKey(i.Key, sbox);
                var key    = SjclAes.ScheduleDecryptionKey(encKey, sbox, decode);
                Assert.That(key, Is.EqualTo(i.DecryptionKey));
            }
        }
예제 #7
0
        public void ComputeTrippleTable_returns_correct_result()
        {
            var table = SjclAes.ComputeTrippleTable(SjclAes.ComputeDoubleTable());

            // Test data is generated with SJCL sources
            Assert.AreEqual(256, table.Length);

            Assert.AreEqual(0x00, table[0x00]);
            Assert.AreEqual(0xf6, table[0x01]);
            Assert.AreEqual(0xf7, table[0x02]);
            Assert.AreEqual(0xdc, table[0x7f]);
            Assert.AreEqual(0x89, table[0x80]);
            Assert.AreEqual(0xa3, table[0xfe]);
            Assert.AreEqual(0x55, table[0xff]);
        }
예제 #8
0
        public void ComputeTrippleTable_returns_correct_result()
        {
            var table = SjclAes.ComputeTrippleTable(SjclAes.ComputeDoubleTable());

            // Test data is generated with SJCL sources
            Assert.That(table.Length, Is.EqualTo(256));

            Assert.That(table[0x00], Is.EqualTo(0x00));
            Assert.That(table[0x01], Is.EqualTo(0xf6));
            Assert.That(table[0x02], Is.EqualTo(0xf7));
            Assert.That(table[0x7f], Is.EqualTo(0xdc));
            Assert.That(table[0x80], Is.EqualTo(0x89));
            Assert.That(table[0xfe], Is.EqualTo(0xa3));
            Assert.That(table[0xff], Is.EqualTo(0x55));
        }
예제 #9
0
        public void ComputeDecodeTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var sbox  = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));
            var table = SjclAes.ComputeDecodeTable(dt, sbox);

            // Test data is generated with SJCL sources
            Assert.AreEqual(4, table.GetLength(0));
            Assert.AreEqual(256, table.GetLength(1));

            // 0
            Assert.AreEqual(0x51f4a750, table[0, 0x00]);
            Assert.AreEqual(0x7e416553, table[0, 0x01]);
            Assert.AreEqual(0x1a17a4c3, table[0, 0x02]);
            Assert.AreEqual(0x141ea9c8, table[0, 0x7f]);
            Assert.AreEqual(0x57f11985, table[0, 0x80]);
            Assert.AreEqual(0x486c5c74, table[0, 0xfe]);
            Assert.AreEqual(0xd0b85742, table[0, 0xff]);

            // 1
            Assert.AreEqual(0x5051f4a7, table[1, 0x00]);
            Assert.AreEqual(0x537e4165, table[1, 0x01]);
            Assert.AreEqual(0xc31a17a4, table[1, 0x02]);
            Assert.AreEqual(0xc8141ea9, table[1, 0x7f]);
            Assert.AreEqual(0x8557f119, table[1, 0x80]);
            Assert.AreEqual(0x74486c5c, table[1, 0xfe]);
            Assert.AreEqual(0x42d0b857, table[1, 0xff]);

            // 2
            Assert.AreEqual(0xa75051f4, table[2, 0x00]);
            Assert.AreEqual(0x65537e41, table[2, 0x01]);
            Assert.AreEqual(0xa4c31a17, table[2, 0x02]);
            Assert.AreEqual(0xa9c8141e, table[2, 0x7f]);
            Assert.AreEqual(0x198557f1, table[2, 0x80]);
            Assert.AreEqual(0x5c74486c, table[2, 0xfe]);
            Assert.AreEqual(0x5742d0b8, table[2, 0xff]);

            // 3
            Assert.AreEqual(0xf4a75051, table[3, 0x00]);
            Assert.AreEqual(0x4165537e, table[3, 0x01]);
            Assert.AreEqual(0x17a4c31a, table[3, 0x02]);
            Assert.AreEqual(0x1ea9c814, table[3, 0x7f]);
            Assert.AreEqual(0xf1198557, table[3, 0x80]);
            Assert.AreEqual(0x6c5c7448, table[3, 0xfe]);
            Assert.AreEqual(0xb85742d0, table[3, 0xff]);
        }
예제 #10
0
        public void ComputeEncodeTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var sbox  = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));
            var table = SjclAes.ComputeEncodeTable(dt, sbox);

            // Test data is generated with SJCL sources
            Assert.AreEqual(4, table.GetLength(0));
            Assert.AreEqual(256, table.GetLength(1));

            // 0
            Assert.AreEqual(0xc66363a5, table[0, 0x00]);
            Assert.AreEqual(0xf87c7c84, table[0, 0x01]);
            Assert.AreEqual(0xee777799, table[0, 0x02]);
            Assert.AreEqual(0xbfd2d26d, table[0, 0x7f]);
            Assert.AreEqual(0x81cdcd4c, table[0, 0x80]);
            Assert.AreEqual(0x6dbbbbd6, table[0, 0xfe]);
            Assert.AreEqual(0x2c16163a, table[0, 0xff]);

            // 1
            Assert.AreEqual(0xa5c66363, table[1, 0x00]);
            Assert.AreEqual(0x84f87c7c, table[1, 0x01]);
            Assert.AreEqual(0x99ee7777, table[1, 0x02]);
            Assert.AreEqual(0x6dbfd2d2, table[1, 0x7f]);
            Assert.AreEqual(0x4c81cdcd, table[1, 0x80]);
            Assert.AreEqual(0xd66dbbbb, table[1, 0xfe]);
            Assert.AreEqual(0x3a2c1616, table[1, 0xff]);

            // 2
            Assert.AreEqual(0x63a5c663, table[2, 0x00]);
            Assert.AreEqual(0x7c84f87c, table[2, 0x01]);
            Assert.AreEqual(0x7799ee77, table[2, 0x02]);
            Assert.AreEqual(0xd26dbfd2, table[2, 0x7f]);
            Assert.AreEqual(0xcd4c81cd, table[2, 0x80]);
            Assert.AreEqual(0xbbd66dbb, table[2, 0xfe]);
            Assert.AreEqual(0x163a2c16, table[2, 0xff]);

            // 3
            Assert.AreEqual(0x6363a5c6, table[3, 0x00]);
            Assert.AreEqual(0x7c7c84f8, table[3, 0x01]);
            Assert.AreEqual(0x777799ee, table[3, 0x02]);
            Assert.AreEqual(0xd2d26dbf, table[3, 0x7f]);
            Assert.AreEqual(0xcdcd4c81, table[3, 0x80]);
            Assert.AreEqual(0xbbbbd66d, table[3, 0xfe]);
            Assert.AreEqual(0x16163a2c, table[3, 0xff]);
        }
예제 #11
0
        public void ComputeDecodeTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var sbox  = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));
            var table = SjclAes.ComputeDecodeTable(dt, sbox);

            // Test data is generated with SJCL sources
            Assert.That(table.GetLength(0), Is.EqualTo(4));
            Assert.That(table.GetLength(1), Is.EqualTo(256));

            // 0
            Assert.That(table[0, 0x00], Is.EqualTo(0x51f4a750));
            Assert.That(table[0, 0x01], Is.EqualTo(0x7e416553));
            Assert.That(table[0, 0x02], Is.EqualTo(0x1a17a4c3));
            Assert.That(table[0, 0x7f], Is.EqualTo(0x141ea9c8));
            Assert.That(table[0, 0x80], Is.EqualTo(0x57f11985));
            Assert.That(table[0, 0xfe], Is.EqualTo(0x486c5c74));
            Assert.That(table[0, 0xff], Is.EqualTo(0xd0b85742));

            // 1
            Assert.That(table[1, 0x00], Is.EqualTo(0x5051f4a7));
            Assert.That(table[1, 0x01], Is.EqualTo(0x537e4165));
            Assert.That(table[1, 0x02], Is.EqualTo(0xc31a17a4));
            Assert.That(table[1, 0x7f], Is.EqualTo(0xc8141ea9));
            Assert.That(table[1, 0x80], Is.EqualTo(0x8557f119));
            Assert.That(table[1, 0xfe], Is.EqualTo(0x74486c5c));
            Assert.That(table[1, 0xff], Is.EqualTo(0x42d0b857));

            // 2
            Assert.That(table[2, 0x00], Is.EqualTo(0xa75051f4));
            Assert.That(table[2, 0x01], Is.EqualTo(0x65537e41));
            Assert.That(table[2, 0x02], Is.EqualTo(0xa4c31a17));
            Assert.That(table[2, 0x7f], Is.EqualTo(0xa9c8141e));
            Assert.That(table[2, 0x80], Is.EqualTo(0x198557f1));
            Assert.That(table[2, 0xfe], Is.EqualTo(0x5c74486c));
            Assert.That(table[2, 0xff], Is.EqualTo(0x5742d0b8));

            // 3
            Assert.That(table[3, 0x00], Is.EqualTo(0xf4a75051));
            Assert.That(table[3, 0x01], Is.EqualTo(0x4165537e));
            Assert.That(table[3, 0x02], Is.EqualTo(0x17a4c31a));
            Assert.That(table[3, 0x7f], Is.EqualTo(0x1ea9c814));
            Assert.That(table[3, 0x80], Is.EqualTo(0xf1198557));
            Assert.That(table[3, 0xfe], Is.EqualTo(0x6c5c7448));
            Assert.That(table[3, 0xff], Is.EqualTo(0xb85742d0));
        }
예제 #12
0
        public void ComputeEncodeTable_returns_correct_result()
        {
            var dt    = SjclAes.ComputeDoubleTable();
            var sbox  = SjclAes.ComputeSboxTable(dt, SjclAes.ComputeTrippleTable(dt));
            var table = SjclAes.ComputeEncodeTable(dt, sbox);

            // Test data is generated with SJCL sources
            Assert.That(table.GetLength(0), Is.EqualTo(4));
            Assert.That(table.GetLength(1), Is.EqualTo(256));

            // 0
            Assert.That(table[0, 0x00], Is.EqualTo(0xc66363a5));
            Assert.That(table[0, 0x01], Is.EqualTo(0xf87c7c84));
            Assert.That(table[0, 0x02], Is.EqualTo(0xee777799));
            Assert.That(table[0, 0x7f], Is.EqualTo(0xbfd2d26d));
            Assert.That(table[0, 0x80], Is.EqualTo(0x81cdcd4c));
            Assert.That(table[0, 0xfe], Is.EqualTo(0x6dbbbbd6));
            Assert.That(table[0, 0xff], Is.EqualTo(0x2c16163a));

            // 1
            Assert.That(table[1, 0x00], Is.EqualTo(0xa5c66363));
            Assert.That(table[1, 0x01], Is.EqualTo(0x84f87c7c));
            Assert.That(table[1, 0x02], Is.EqualTo(0x99ee7777));
            Assert.That(table[1, 0x7f], Is.EqualTo(0x6dbfd2d2));
            Assert.That(table[1, 0x80], Is.EqualTo(0x4c81cdcd));
            Assert.That(table[1, 0xfe], Is.EqualTo(0xd66dbbbb));
            Assert.That(table[1, 0xff], Is.EqualTo(0x3a2c1616));

            // 2
            Assert.That(table[2, 0x00], Is.EqualTo(0x63a5c663));
            Assert.That(table[2, 0x01], Is.EqualTo(0x7c84f87c));
            Assert.That(table[2, 0x02], Is.EqualTo(0x7799ee77));
            Assert.That(table[2, 0x7f], Is.EqualTo(0xd26dbfd2));
            Assert.That(table[2, 0x80], Is.EqualTo(0xcd4c81cd));
            Assert.That(table[2, 0xfe], Is.EqualTo(0xbbd66dbb));
            Assert.That(table[2, 0xff], Is.EqualTo(0x163a2c16));

            // 3
            Assert.That(table[3, 0x00], Is.EqualTo(0x6363a5c6));
            Assert.That(table[3, 0x01], Is.EqualTo(0x7c7c84f8));
            Assert.That(table[3, 0x02], Is.EqualTo(0x777799ee));
            Assert.That(table[3, 0x7f], Is.EqualTo(0xd2d26dbf));
            Assert.That(table[3, 0x80], Is.EqualTo(0xcdcd4c81));
            Assert.That(table[3, 0xfe], Is.EqualTo(0xbbbbd66d));
            Assert.That(table[3, 0xff], Is.EqualTo(0x16163a2c));
        }