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)); } }
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]); } }
public void Decrypt_returns_correct_value() { foreach (var i in AesTestCases) { var plaintext = new SjclAes(i.Key).Decrypt(i.Ciphertext); Assert.AreEqual(i.Plaintext, plaintext); } }
public void Encrypt_returns_correct_value() { foreach (var i in AesTestCases) { var ciphertext = new SjclAes(i.Key).Encrypt(i.Plaintext); Assert.That(ciphertext, Is.EqualTo(i.Ciphertext)); } }
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); } }
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)); } }
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")); } }
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); } }
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); } }
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); } }
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")); } }
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")); } }
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)); } }
public void ComputeDoubleTable_returns_correct_result() { var table = SjclAes.ComputeDoubleTable(); // Test data is generated with SJCL sources Assert.AreEqual(256, table.Length); Assert.AreEqual(0x00, table[0x00]); Assert.AreEqual(0x02, table[0x01]); Assert.AreEqual(0x04, table[0x02]); Assert.AreEqual(0xfe, table[0x7f]); Assert.AreEqual(0x1b, table[0x80]); Assert.AreEqual(0xe7, table[0xfe]); Assert.AreEqual(0xe5, table[0xff]); }
public void ComputeDoubleTable_returns_correct_result() { var table = 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(0x02)); Assert.That(table[0x02], Is.EqualTo(0x04)); Assert.That(table[0x7f], Is.EqualTo(0xfe)); Assert.That(table[0x80], Is.EqualTo(0x1b)); Assert.That(table[0xfe], Is.EqualTo(0xe7)); Assert.That(table[0xff], Is.EqualTo(0xe5)); }
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)); }
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]); }
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]); }
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)); }
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)); }
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]); }
public void ToQuads_converts_words_to_quads() { Assert.AreEqual( new SjclQuad[0], SjclAes.ToQuads(new uint[0])); Assert.AreEqual( new SjclQuad[] { new SjclQuad(1, 2, 3, 4) }, SjclAes.ToQuads(new uint[] { 1, 2, 3, 4 })); Assert.AreEqual( new SjclQuad[] { new SjclQuad(1, 2, 3, 4), new SjclQuad(5, 6, 7, 8) }, SjclAes.ToQuads(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8 })); Assert.AreEqual( new SjclQuad[] { new SjclQuad(1, 2, 3, 4), new SjclQuad(5, 6, 7, 8), new SjclQuad(9, 10, 11, 12) }, SjclAes.ToQuads(new uint[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 })); }
public void Decrypt_throws_on_mismatching_tag() { foreach (var i in Rfc3610TestCases) { var aes = new SjclAes(i.Key); // Change ciphertext var ciphertext = (byte[])i.Ciphertext.Clone(); ++ciphertext[ciphertext.Length / 2]; VerifyCmmMismatchThrown(aes, ciphertext, i.Iv, i.Adata, i.TagLength); // Change iv var iv = (byte[])i.Iv.Clone(); ++iv[iv.Length / 2]; VerifyCmmMismatchThrown(aes, i.Ciphertext, iv, i.Adata, i.TagLength); // Change adata var adata = (byte[])i.Adata.Clone(); ++adata[adata.Length / 2]; VerifyCmmMismatchThrown(aes, i.Ciphertext, i.Iv, adata, i.TagLength); } }
// // 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")); }
public void ToQuads_throws_on_length_that_is_not_multiple_of_4() { Assert.That(() => SjclAes.ToQuads(new uint[] { 1, 2, 3 }), Throws.TypeOf <ArgumentException>() .And.Message.StartsWith("Length must be a multiple of 4")); }
public void ToQuads_throws_on_length_that_is_not_multiple_of_4() { SjclAes.ToQuads(new uint[] { 1, 2, 3 }); }
// // 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); }