private void FillBuffer() { m_current_block_length = m_view.Read(m_current_block_position, m_current_block, 0, (uint)BlockLength); for (int offset = 0; offset < m_current_block_length; offset += 0x10) { m_enc.DecryptBlock(m_current_block_position + offset, m_current_block, offset); } }
public void DecryptIntermediate128BitKey() // working 11/20/2018 11:35 AM { byte[] P = new byte[] { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 }; byte[] K = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; byte[] result = Camellia.DecryptBlock(P, K); Assert.IsTrue(compareArrays(result, new byte[16] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, out int FirstDiff)); }
public void DecryptIntermediate192BitKey() // working 11/20/2018 11:35 AM { byte[] P = new byte[16] { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 }; byte[] K = new byte[24] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; byte[] result = Camellia.DecryptBlock(P, K); Assert.IsTrue(compareArrays(result, new byte[16] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, out int FirstDiff)); }
public void DecryptIntermediate256BitKey() // working 11/20/2018 11:35 AM { byte[] P = new byte[16] { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 }; byte[] K = new byte[32] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; byte[] result = Camellia.DecryptBlock(P, K); Assert.IsTrue(compareArrays(result, new byte[16] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, out int FirstDiff)); }
private static int ReadEncrypted(ArcView.Frame view, Camellia enc, long offset, byte[] buffer, int index, int length) { int offset_pad = (int)offset & 0xF; int aligned_len = (offset_pad + length + 0xF) & ~0xF; byte[] aligned_buf; int block = 0; if (aligned_len == length) { aligned_buf = buffer; block = index; } else { aligned_buf = new byte[aligned_len]; } int read = view.Read(offset - offset_pad, aligned_buf, block, (uint)aligned_len); if (read < offset_pad) { return(0); } for (int block_count = aligned_len / 0x10; block_count > 0; --block_count) { enc.DecryptBlock(offset, aligned_buf, block); block += 0x10; offset += 0x10; } if (aligned_buf != buffer) { Buffer.BlockCopy(aligned_buf, offset_pad, buffer, index, length); } return(Math.Min(length, read - offset_pad)); }
public void DecryptBlock(long block_offset, byte[] buffer, int index) { m_enc.DecryptBlock(block_offset, buffer, index); }
private static int ReadEncrypted(ArcView.Frame view, Camellia enc, long offset, byte[] buffer, int index, int length) { int offset_pad = (int)offset & 0xF; int aligned_len = (offset_pad + length + 0xF) & ~0xF; byte[] aligned_buf; int block = 0; if (aligned_len == length) { aligned_buf = buffer; block = index; } else { aligned_buf = new byte[aligned_len]; } int read = view.Read (offset - offset_pad, aligned_buf, block, (uint)aligned_len); if (read < offset_pad) return 0; for (int block_count = aligned_len / 0x10; block_count > 0; --block_count) { enc.DecryptBlock (offset, aligned_buf, block); block += 0x10; offset += 0x10; } if (aligned_buf != buffer) Buffer.BlockCopy (aligned_buf, offset_pad, buffer, index, length); return Math.Min (length, read-offset_pad); }
public void Decrypt_Exception_InvalidKeyLength() // working 11/20/2018 11:35 AM { Camellia.DecryptBlock(new byte[16], new byte[1]); Assert.Fail("Did not throw exception"); }
public void Decrypt_Exception_NullKey() // working 11/20/2018 11:35 AM { Camellia.DecryptBlock(new byte[16], null); Assert.Fail("Did not throw exception"); }
static void Main(string[] args) { if (!Camellia.RunSelfTests()) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** SELFTEST FAILED****"); } else { Console.WriteLine("SELF TEST OK!"); } byte[] P = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; byte[] K1 = new byte[16] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; byte[] K2 = new byte[24] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; byte[] K3 = new byte[32] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }; byte[] result; int FirstDiff; result = Camellia.EncryptBlock(P, K1); if (!compareArrays(result, new byte[] { 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73, 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43 }, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 1 FAILED****"); } else { Console.WriteLine("FULL ENCRYPTION TEST 1 OK!"); } result = Camellia.DecryptBlock(result, K1); if (!compareArrays(result, P, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 1 FAILED****"); } else { Console.WriteLine("FULL DECRYPTION TEST 1 OK!"); } result = Camellia.EncryptBlock(P, K2); if (!compareArrays(result, new byte[] { 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8, 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9 }, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 2 FAILED ****"); } else { Console.WriteLine("FULL ENCRYPTION TEST 2 OK!"); } result = Camellia.DecryptBlock(result, K2); if (!compareArrays(result, P, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 2 FAILED****"); } else { Console.WriteLine("FULL DECRYPTION TEST 2 OK!"); } result = Camellia.EncryptBlock(P, K3); if (!compareArrays(result, new byte[] { 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c, 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09 }, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 3 FAILED ****"); } else { Console.WriteLine("FULL ENCRYPTION TEST 3 OK!"); } result = Camellia.DecryptBlock(result, K3); if (!compareArrays(result, P, out FirstDiff)) { //throw new Exception("FAILED TEST"); Console.WriteLine("*** TEST 3 FAILED****"); } else { Console.WriteLine("FULL DECRYPTION TEST 3 OK!"); } Console.WriteLine("Press ENTER to quit..."); Console.ReadLine(); }