def test_sub_bytes_returns_correct_array(aes_datas): state = aes_datas['input_state'] expected = aes_datas['expected_sub_bytes'] assert np.array_equal( expected, aes.sub_bytes(state=state) ) assert expected.shape == state.shape
def test_aes_encrypt_first_sub_bytes_with_default_arguments(): sf = scared.selection_functions.aes.encrypt.first_sub_bytes() assert sf.guesses.tolist() == list(range(256)) assert sf.words is Ellipsis assert sf.target_tag == 'plaintext' 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) expected = aes.sub_bytes(expected) assert np.array_equal(expected, sf(plaintext=data))
def test_aes_encrypt_first_sub_bytes_with_alternative_args(): sf = scared.selection_functions.aes.encrypt.first_sub_bytes( plaintext_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) expected = aes.sub_bytes(expected) assert np.array_equal(expected[:, :, slice(2, 8)], sf(foo=data))
def test_aes_encrypt_first_sub_bytes_with_default_arguments(): sf = aes.selection_functions.encrypt.FirstSubBytes() assert sf.guesses.tolist() == list(range(256)) assert sf.words is Ellipsis assert sf.target_tag == 'plaintext' assert sf.key_tag == 'key' 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) expected = aes.sub_bytes(expected) assert np.array_equal(expected, sf(plaintext=data)) master_key = np.random.randint(0, 255, (16, ), dtype='uint8') expected_key = aes.key_schedule(master_key)[0] assert np.array_equal(expected_key, sf.compute_expected_key(key=master_key)) assert isinstance(str(sf), str)
def test_aes_decrypt_last_sub_bytes_with_alternative_args(): sf = aes.selection_functions.decrypt.LastSubBytes(plaintext_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) expected = aes.sub_bytes(expected) 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)[0] assert np.array_equal(expected_key, sf.compute_expected_key(thekey=master_key)) assert sf.key_tag == 'thekey' assert isinstance(str(sf), str)
def test_sub_bytes_raises_exception_if_state_is_not_a_correct_array(): with pytest.raises(ValueError): aes.sub_bytes(state=np.random.randint(0, 255, (12, 12), dtype='uint8')) with pytest.raises(ValueError): aes.sub_bytes( state=np.random.randint(0, 255, (12, 16), dtype='uint16'))
def test_sub_bytes_raises_exception_if_state_is_not_array(): with pytest.raises(TypeError): aes.sub_bytes(state='foo') with pytest.raises(TypeError): aes.sub_bytes(state=12)