Exemple #1
0
def test_decrypt_raises_exception_if_step_is_incorrect(aes_data):
    key_128 = aes_data['128_key']
    plain = aes_data['plaintext']

    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_128, after_step=-1)
    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_128, after_step=4)
Exemple #2
0
def test_decrypt_raises_exception_if_improper_round_or_step_type(aes_data):
    key = aes_data['128_key']
    plain = aes_data['plaintext']

    with pytest.raises(TypeError):
        aes.decrypt(plain, key=key, at_round='foo')
    with pytest.raises(TypeError):
        aes.decrypt(plain, key=key, after_step='foo')
Exemple #3
0
def test_decrypt_raises_exception_if_ciphertext_and_keys_multiple_are_incompatible():
    with pytest.raises(ValueError):
        aes.decrypt(
            ciphertext=np.random.randint(0, 255, (10, 16), dtype='uint8'),
            key=np.random.randint(0, 255, (9, 16), dtype='uint8')
        )

    with pytest.raises(ValueError):
        aes.decrypt(
            ciphertext=np.random.randint(0, 255, (2, 10, 16), dtype='uint8'),
            key=np.random.randint(0, 255, (16), dtype='uint8')
        )
Exemple #4
0
def test_decrypt_stop_at_intermediate_value(aes_datas):
    int_values = aes_datas['128_decrypt_intermediate_outputs']
    key = aes_datas['128_key']
    cipher = aes_datas['128_ciphertext']
    for _round, vals in enumerate(int_values):
        for step, expected in enumerate(vals):
            value = aes.decrypt(ciphertext=cipher, key=key, at_round=_round, after_step=step)
            assert np.array_equal(expected, value)
Exemple #5
0
def test_decrypt_raises_exception_if_round_is_negative_or_too_high(aes_data):
    key_128 = aes_data['128_key']
    key_192 = aes_data['192_key']
    key_256 = aes_data['256_key']
    plain = aes_data['plaintext']

    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_128, at_round=-1)
    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_128, at_round=12)
    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_192, at_round=14)
    with pytest.raises(ValueError):
        aes.decrypt(plain, key=key_256, at_round=17)
Exemple #6
0
def test_simple_decrypt_with_128_key(aes_data):
    key = aes_data['128_key']
    cipher = aes_data['128_ciphertext']
    expected_plain = aes_data['plaintext']
    plain = aes.decrypt(key=key, ciphertext=cipher)
    assert np.array_equal(expected_plain, plain)
Exemple #7
0
def test_decrypt_raises_exception_if_ciphertext_or_key_is_not_a_byte_array_of_appropriate_length(
):
    with pytest.raises(TypeError):
        aes.decrypt(ciphertext='foo',
                    key=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(TypeError):
        aes.decrypt(ciphertext=12,
                    key=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(ValueError):
        aes.decrypt(ciphertext=np.random.randint(0, 255, (12), dtype='uint8'),
                    key=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(ValueError):
        aes.decrypt(ciphertext=np.random.randint(0, 255, (16), dtype='uint16'),
                    key=np.random.randint(0, 255, (16), dtype='uint8'))

    with pytest.raises(TypeError):
        aes.decrypt(key='foo',
                    ciphertext=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(TypeError):
        aes.decrypt(key=12,
                    ciphertext=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(ValueError):
        aes.decrypt(key=np.random.randint(0, 255, (12), dtype='uint8'),
                    ciphertext=np.random.randint(0, 255, (16), dtype='uint8'))
    with pytest.raises(ValueError):
        aes.decrypt(key=np.random.randint(0, 255, (16), dtype='uint16'),
                    ciphertext=np.random.randint(0, 255, (16), dtype='uint8'))
Exemple #8
0
def test_full_decrypt(decrypt_cases):
    assert np.array_equal(
        decrypt_cases['expected'],
        aes.decrypt(ciphertext=decrypt_cases['state'],
                    key=decrypt_cases['keys']))