def test_shift_rows_raises_exception_if_state_is_not_a_correct_array(): with pytest.raises(ValueError): aes.shift_rows( state=np.random.randint(0, 255, (12, 12), dtype='uint8')) with pytest.raises(ValueError): aes.shift_rows( state=np.random.randint(0, 255, (12, 16), dtype='uint16'))
def test_shift_rows_returns_correct_array(aes_datas): state = aes_datas['input_state'] expected = aes_datas['expected_shift_rows'] assert np.array_equal( expected, aes.shift_rows(state=state) ) assert expected.shape == state.shape
def test_aes_decrypt_delta_r_first_rounds_with_default_arguments(): sf = scared.selection_functions.aes.decrypt.delta_r_first_rounds() assert sf.guesses.tolist() == list(range(256)) assert sf.words is Ellipsis assert sf.target_tag == 'ciphertext' assert isinstance(sf, selection_functions.SelectionFunction) data = np.random.randint(0, 255, (10, 16), dtype='uint8') expected = np.empty((10, 256, 16), dtype='uint8') for i in np.arange(256, dtype='uint8'): expected[:, i, :] = np.bitwise_xor(data, i) s = aes.inv_sub_bytes(state=expected) expected = np.bitwise_xor(aes.shift_rows(data), s.swapaxes(0, 1)).swapaxes(0, 1) assert np.array_equal(expected, sf(ciphertext=data))
def test_aes_decrypt_delta_r_first_rounds_with_alternative_args(): sf = scared.selection_functions.aes.decrypt.delta_r_first_rounds( ciphertext_tag='foo', words=slice(2, 8), guesses=np.arange(16, dtype='uint8')) assert sf.guesses.tolist() == list(range(16)) assert sf.words == slice(2, 8, None) assert sf.target_tag == 'foo' assert isinstance(sf, selection_functions.SelectionFunction) data = np.random.randint(0, 255, (10, 16), dtype='uint8') expected = np.empty((10, 16, 16), dtype='uint8') for i in np.arange(16, dtype='uint8'): expected[:, i, :] = np.bitwise_xor(data, i) s = aes.inv_sub_bytes(state=expected) expected = np.bitwise_xor(aes.shift_rows(data), s.swapaxes(0, 1)).swapaxes(0, 1) assert np.array_equal(expected[:, :, slice(2, 8)], sf(foo=data))
def test_aes_decrypt_delta_r_first_rounds_with_default_arguments(): sf = aes.selection_functions.decrypt.DeltaRFirstRounds() assert sf.guesses.tolist() == list(range(256)) assert sf.words is Ellipsis assert sf.target_tag == 'ciphertext' assert isinstance(sf, selection_functions.SelectionFunction) data = np.random.randint(0, 255, (10, 16), dtype='uint8') expected = np.empty((10, 256, 16), dtype='uint8') for i in np.arange(256, dtype='uint8'): expected[:, i, :] = np.bitwise_xor(data, i) s = aes.inv_sub_bytes(state=expected) expected = np.bitwise_xor(aes.shift_rows(data), s.swapaxes(0, 1)).swapaxes(0, 1) assert np.array_equal(expected, sf(ciphertext=data)) master_key = np.random.randint(0, 255, (16, ), dtype='uint8') expected_key = aes.key_schedule(master_key)[-1] assert np.array_equal(expected_key, sf.compute_expected_key(key=master_key)) assert sf.key_tag == 'key' assert isinstance(str(sf), str)
def test_aes_encrypt_delta_r_last_rounds_with_alternative_args(): sf = aes.selection_functions.encrypt.DeltaRLastRounds(ciphertext_tag='foo', words=slice(2, 8), guesses=_alt_guesses, key_tag='thekey') assert sf.guesses.tolist() == _alt_guesses.tolist() assert sf.words == slice(2, 8, None) assert sf.target_tag == 'foo' assert isinstance(sf, selection_functions.SelectionFunction) data = np.random.randint(0, 255, (10, 16), dtype='uint8') expected = np.empty((10, len(_alt_guesses), 16), dtype='uint8') for i, guess in enumerate(_alt_guesses): expected[:, i, :] = np.bitwise_xor(data, guess) s = aes.inv_sub_bytes(state=expected) expected = np.bitwise_xor(aes.shift_rows(data), s.swapaxes(0, 1)).swapaxes(0, 1) assert np.array_equal(expected[:, :, slice(2, 8)], sf(foo=data)) master_key = np.random.randint(0, 255, (16, ), dtype='uint8') expected_key = aes.key_schedule(master_key)[-1] assert np.array_equal(expected_key, sf.compute_expected_key(thekey=master_key)) assert isinstance(str(sf), str)
def test_shift_rows_raises_exception_if_state_is_not_array(): with pytest.raises(TypeError): aes.shift_rows(state='foo') with pytest.raises(TypeError): aes.shift_rows(state=12)