def testCipher(self): inputBytes = [0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34] exampleCypherKeyBytes = [0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c] expectedResultBytes = [0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32] inputState = aes.createState(inputBytes) expectedState = aes.createState(expectedResultBytes) keySchedule = aes.keyExpansion(exampleCypherKeyBytes) result = aes.cipher(inputState, keySchedule) self.assertEqual(result, expectedState)
def testCipher2(self): inputBytes = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff] exampleCypherKeyBytes = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f] expectedResultBytes = [0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a] inputState = aes.createState(inputBytes) expectedState = aes.createState(expectedResultBytes) keySchedule = aes.keyExpansion(exampleCypherKeyBytes) result = aes.cipher(inputState, keySchedule) self.assertEqual(result, expectedState)
def test_c3a_256_encryption(self): print "(C.3) Testing 256 cipher... ", encrypted = cipher(0x00112233445566778899aabbccddeeff, 0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f, 256) self.assertEqual(encrypted, 0x8ea2b7ca516745bfeafc49904b496089) print "Passed"
def test_c2a_196_encryption(self): print "(C.2) Testing 196 cipher... ", encrypted = cipher(0x00112233445566778899aabbccddeeff, 0x000102030405060708090a0b0c0d0e0f1011121314151617, 196) self.assertEqual(encrypted, 0xdda97ca4864cdfe06eaf70a0ec0d7191) print "Passed"
def test_c1a_128_encryption(self): print "(C.1) Testing 128 cipher... ", encrypted = cipher(0x00112233445566778899aabbccddeeff, 0x000102030405060708090a0b0c0d0e0f, 128) self.assertEqual(encrypted, 0x69c4e0d86a7b0430d8cdb78070b4c55a) print "Passed"
def test_b_128_cipher(self): print "(B) Testing 128 cipher... ", encrypted = cipher(0x3243f6a8885a308d313198a2e0370734, 0x2b7e151628aed2a6abf7158809cf4f3c, 128) self.assertEqual(encrypted, 0x3925841d02dc09fbdc118597196a0b32) print "Passed"
key = np.array([0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c], dtype=np.int8) assert((aes.subBytes(state) == sub).all()) assert((aes.shiftRows(sub) == shift).all()) assert((aes.mixColumns(shift) == mix).all()) assert((aes.addRoundKey(mix, aes.getRoundKey(aes.expandKey(key), 1)) == rounds).all()) in1 = np.array([0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34], dtype=np.int8) result = np.array([0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32], dtype=np.int8) output = aes.cipher(in1, key) assert((output == result).all()) deoutput = aes.invCipher(output, key) assert((deoutput == in1).all()) ## Appendix C tests input1 = np.array([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff], dtype=np.int8) key1 = np.array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f], dtype=np.int8) output1 = np.array([0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a], dtype=np.int8)
def testCipher(self): ciphertext = aes.cipher(self.plaintext, self.key) ciphertext2 = aes.cipher(self.plaintext2, self.key2) self.assertEqual(ciphertext.hex(), self.ciphertext.hex()) self.assertEqual(ciphertext2.hex(), self.ciphertext2.hex())