def test_shift_rows(): ''' shift rows in state array sa to return new state array ''' stateArr = [[newBV(0xbe), newBV(0x83), newBV(0x2c), newBV(0xc8)], \ [newBV(0xd4), newBV(0x3b), newBV(0x86), newBV(0xc0)], \ [newBV(0x0a), newBV(0xe1), newBV(0xd4), newBV(0x4d)], \ [newBV(0xda), newBV(0x64), newBV(0xf2), newBV(0xfe)]] expected = [[newBV(0xbe), newBV(0x3b), newBV(0xd4), newBV(0xfe)], \ [newBV(0xd4), newBV(0xe1), newBV(0xf2), newBV(0xc8)], \ [newBV(0x0a), newBV(0x64), newBV(0x2c), newBV(0xc0)], \ [newBV(0xda), newBV(0x83), newBV(0x86), newBV(0x4d)]] actual = AES.shift_rows(stateArr) assert actual == expected stateArr = [[newBV(0x87), newBV(0xec), newBV(0x4a), newBV(0x8c)], \ [newBV(0xf2), newBV(0x6e), newBV(0xc3), newBV(0xd8)], \ [newBV(0x4d), newBV(0x4c), newBV(0x46), newBV(0x95)], \ [newBV(0x97), newBV(0x90), newBV(0xe7), newBV(0xa6)]] expected = [[newBV(0x87), newBV(0x6e), newBV(0x46), newBV(0xa6)], \ [newBV(0xf2), newBV(0x4c), newBV(0xe7), newBV(0x8c)], \ [newBV(0x4d), newBV(0x90), newBV(0x4a), newBV(0xd8)], \ [newBV(0x97), newBV(0xec), newBV(0xc3), newBV(0x95)]] actual = AES.shift_rows(stateArr) assert actual == expected stateArr = [[newBV(0xe9), newBV(0x09), newBV(0x89), newBV(0x72)], \ [newBV(0xcb), newBV(0x31), newBV(0x07), newBV(0x5f)], \ [newBV(0x3d), newBV(0x32), newBV(0x7d), newBV(0x94)], \ [newBV(0xaf), newBV(0x2e), newBV(0x2c), newBV(0xb5)]] expected = [[newBV(0xe9), newBV(0x31), newBV(0x7d), newBV(0xb5)], \ [newBV(0xcb), newBV(0x32), newBV(0x2c), newBV(0x72)], \ [newBV(0x3d), newBV(0x2e), newBV(0x89), newBV(0x5f)], \ [newBV(0xaf), newBV(0x09), newBV(0x07), newBV(0x94)]] actual = AES.shift_rows(stateArr) assert actual == expected
def test_shift_rows(): """ Test ShiftRows. """ nb = 16 state = [x + 1 for x in range(-1, 15)] state_ref = [x + 1 for x in range(-1, 15)] state_ref[4:8] = [5, 6, 7, 4] state_ref[8:12] = [10, 11, 8, 9] state_ref[12:16] = [15, 12, 13, 14] state_ref = state_ref aes.shift_rows(state, 4) assert (state == state_ref)
d1 = aes.xor_bytes(d0, d1) d1 = aes.bytes2matrix(d1) aes.inv_mix_columns(d1) aes.inv_shift_rows(d1) aes.inv_sub_bytes(d1) d1 = aes.matrix2bytes(d1) return d1 mc_table = {} for i in range(16): for c in range(1, 256): s = bytearray(16) s[i] = c s = aes.bytes2matrix(s) aes.shift_rows(s) aes.mix_columns(s) s = aes.matrix2bytes(s) for p in range(16): if s[p] != 0: assert (p, s[p]) not in mc_table mc_table[(i, p, s[p])] = c sub_table = defaultdict(list) for i in range(256): d = aes.sbox[i] ^ aes.sbox[i ^ 1] sub_table[d].append(i) remote = Telnet('oracle2.2020.ctfcompetition.com', 1337)
def test_shift_rows_3(self): sa = aes.init_state_array( aes.key_bv('2dfb02343f6d12dd09337ec75b36e3f0')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '2d6d7ef03f33e334093602dd5bfb12c7',\ "Test shift rows from FIPS-197 C.1 round[4]")
def test_shift_rows_2(self): sa = aes.init_state_array( aes.key_bv('3b59cb73fcd90ee05774222dc067fb68')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '3bd92268fc74fb735767cbe0c0590e2d',\ "Test shift rows from FIPS-197 C.1 round[3]")
def test_shift_rows_1(self): sa = aes.init_state_array( aes.key_bv('a761ca9b97be8b45d8ad1a611fc97369')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ 'a7be1a6997ad739bd8c9ca451f618b61',\ "Test shift rows from FIPS-197 C.1 round[2]")
def test_shift_rows_0(self): sa = aes.init_state_array( aes.key_bv('63cab7040953d051cd60e0e7ba70e18c')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '6353e08c0960e104cd70b751bacad0e7',\ "Test shift rows from FIPS-197 C.1 round[1]")
def test_shift_rows_3(self): sa=aes.init_state_array(aes.key_bv('2dfb02343f6d12dd09337ec75b36e3f0')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '2d6d7ef03f33e334093602dd5bfb12c7',\ "Test shift rows from FIPS-197 C.1 round[4]")
def test_shift_rows_2(self): sa=aes.init_state_array(aes.key_bv('3b59cb73fcd90ee05774222dc067fb68')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '3bd92268fc74fb735767cbe0c0590e2d',\ "Test shift rows from FIPS-197 C.1 round[3]")
def test_shift_rows_1(self): sa=aes.init_state_array(aes.key_bv('a761ca9b97be8b45d8ad1a611fc97369')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ 'a7be1a6997ad739bd8c9ca451f618b61',\ "Test shift rows from FIPS-197 C.1 round[2]")
def test_shift_rows_0(self): sa=aes.init_state_array(aes.key_bv('63cab7040953d051cd60e0e7ba70e18c')) self.assertEqual(aes.state_str(aes.shift_rows(sa)),\ '6353e08c0960e104cd70b751bacad0e7',\ "Test shift rows from FIPS-197 C.1 round[1]")