示例#1
0
def test_key_expansion_raise_exception_if_col_in_or_col_out_is_not_int():
    with pytest.raises(TypeError):
        key = np.random.randint(0, 255, (16, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in='foo')
    with pytest.raises(TypeError):
        key = np.random.randint(0, 255, (24, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=0, col_out='foo')
示例#2
0
def test_key_expansion_raise_exception_if_col_in_or_col_out_is_negative():
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, (16, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=-2, col_out=40)
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, (24, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=0, col_out=-12)
示例#3
0
def test_key_expansion_bwd_256(aes_datas):
    master = np.random.randint(0, 255, (32,), dtype='uint8')
    round_keys = aes.key_expansion(key_cols=master, col_in=0, col_out=60)[0]
    for i in range(7, 0, -1):
        key_cols = round_keys[(i - 1) * 32: i * 32]
        master = aes.key_expansion(key_cols=key_cols, col_in=int(((i - 1) * 32) / 4), col_out=0)
        res = master[0].reshape((-1, 4))
        exp = round_keys[:i * 32].reshape((-1, 4))
        for i, w in enumerate(res):
            assert w.tolist() == exp[i].tolist()
    round_keys = aes_datas['256_key_schedule_output'].reshape(15 * 16)
    for i in range(7, 0, -1):
        key_cols = round_keys[(i - 1) * 32: i * 32]
        master = aes.key_expansion(key_cols=key_cols, col_in=int(((i - 1) * 32) / 4), col_out=0)
        assert np.array_equal(master[0], round_keys[:i * 32])
示例#4
0
def test_key_expansion_bwd_192(aes_data):
    round_keys = aes_data['192_key_schedule_output'].reshape(13 * 16)
    for i in range(8, 0, -1):
        key_cols = round_keys[16 + (i - 1) * 24:16 + i * 24]
        master = aes.key_expansion(key_cols=key_cols,
                                   col_in=int((16 + (i - 1) * 24) / 4),
                                   col_out=0)
        assert np.array_equal(master[0], round_keys[:16 + i * 24])
示例#5
0
def test_key_expansion_bwd_starting_from_any_intermediate_state(
        key_expansion_params):
    key = key_expansion_params['key']
    cols_size = key_expansion_params['cols_size']
    rounds = range(key_expansion_params['rounds'].stop - 1,
                   key_expansion_params['rounds'].start - 1, -1)
    full_schedule = aes.key_schedule(key).reshape((-1, 4))
    _in = len(full_schedule) - cols_size
    key_base = full_schedule[_in:].reshape((cols_size * 4, ))
    for intermediate_round in rounds:
        _out = _in - intermediate_round * 4
        schedule_part_1 = aes.key_expansion(key_base, col_in=_in,
                                            col_out=_out).reshape((-1, 4))
        assert np.array_equal(schedule_part_1, full_schedule[_out:])
        sched_part_2 = aes.key_expansion(
            schedule_part_1[:cols_size].reshape(-1), col_in=_out,
            col_out=0).reshape((-1, 4))
        expected = full_schedule[:_out + cols_size]
        for i, v in enumerate(sched_part_2):
            assert np.array_equal(v, expected[i])
示例#6
0
def test_key_expansion_fwd_starting_from_any_intermediate_state(
        key_expansion_params):
    key = key_expansion_params['key']
    rounds = key_expansion_params['rounds']
    cols_size = key_expansion_params['cols_size']
    full_schedule = aes.key_schedule(key).reshape((-1, 4))
    for intermediate_round in rounds:
        schedule_part_1 = aes.key_expansion(
            key, col_out=cols_size + intermediate_round * 4).reshape((-1, 4))

        assert np.array_equal(
            schedule_part_1,
            full_schedule[:cols_size + intermediate_round * 4])
        sched_part_2 = aes.key_expansion(
            schedule_part_1[len(schedule_part_1) - cols_size:].reshape(-1),
            col_in=intermediate_round * 4,
        ).reshape((-1, 4))
        expected = full_schedule[intermediate_round * 4:]

        for i, v in enumerate(sched_part_2):
            assert np.array_equal(v, expected[i])
示例#7
0
def test_key_expansion_raise_exception_if_key_isnt_array():
    with pytest.raises(TypeError):
        aes.key_expansion(key_cols='foo')
    with pytest.raises(TypeError):
        aes.key_expansion(key_cols=123456465)
    with pytest.raises(TypeError):
        aes.key_expansion(key_cols={'shape': 12})
示例#8
0
def test_key_expansion_raise_exception_if_col_out_is_greater_than_max_cols():
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, (16, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=0, col_out=50)
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, (24, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=0, col_out=60)
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, (32, ), dtype='uint8')
        aes.key_expansion(key_cols=key, col_in=0, col_out=66)
示例#9
0
def test_key_expansion_raise_exception_if_key_hasnt_a_valid_size():
    with pytest.raises(ValueError):
        key = np.random.randint(0, 255, size=(8, ), dtype='uint8')
        aes.key_expansion(key_cols=key)
示例#10
0
def test_key_expansion_raise_exception_if_key_isnt_int8_array():
    with pytest.raises(ValueError):
        key = np.random.random_sample((16, ))
        aes.key_expansion(key_cols=key)
示例#11
0
def test_key_expansion_fwd_256(aes_data):
    master = aes_data['256_key']
    keys = aes.key_expansion(master, col_in=0)
    assert np.array_equal(aes_data['256_key_schedule_output'].reshape(15 * 16),
                          keys[0])
示例#12
0
def test_key_expansion_bwd_128(aes_data):
    round_keys = aes_data['128_key_schedule_output'].reshape(11 * 16)
    for i in range(0, 11):
        key_cols = round_keys[i * 16:(i + 1) * 16]
        master = aes.key_expansion(key_cols=key_cols, col_in=i * 4, col_out=0)
        assert np.array_equal(master[0], round_keys[:(i + 1) * 16])
示例#13
0
def test_key_expansion_fwd_192(aes_datas):
    master = aes_datas['192_key']
    keys = aes.key_expansion(master, col_in=0)
    assert np.array_equal(aes_datas['192_key_schedule_output'].reshape(13 * 16), keys[0])