Exemplo n.º 1
0
def decryption_round(round_index, transformed, KHat):
    if 0 <= round_index <= ROUNDS - 2:
        to_s_box = inverse_linear_transformation(transformed)
    elif round_index == ROUNDS - 1:
        to_s_box = string_xor(transformed, KHat[ROUNDS])
    else:
        raise ValueError(f'Invalid round number {round_index}')

    to_xor = ''.join([
        sbox_inverse_func(round_index, to_s_box[ind:ind + 4])
        for ind in range(0, len(to_s_box), 4)
    ])
    output = string_xor(to_xor, KHat[round_index])
    return output
Exemplo n.º 2
0
def linear_transformation(inp):
    assert len(inp) == 128, 'input to linear transf. is not 128 bits'
    res = ''
    for array in LTtable:
        out = '0'
        for e in array:
            out = string_xor(out, inp[e])
        res += out
    return res
Exemplo n.º 3
0
def encryption_round(round_index, round_input, KHat):
    # First we xor the round input with the key
    input_xor_key = string_xor(round_input, KHat[round_index])
    # Then we apply the Sbox function on the result
    sbox_res = ''.join([
        sbox_func(round_index, input_xor_key[index:index + 4])
        for index in range(0, len(input_xor_key), 4)
    ])

    # If it is round 0 to 30 we apply the linear transformation
    # Otherwise we do bitwise xor between the sbox_res and the last key
    if 0 <= round_index <= ROUNDS - 2:
        round_output = linear_transformation(sbox_res)
    elif round_index == ROUNDS - 1:
        round_output = string_xor(sbox_res, KHat[ROUNDS])
    else:
        raise ValueError(f'round number {round_index} out of range')
    return round_output
Exemplo n.º 4
0
def inverse_linear_transformation(inp):
    assert len(inp) == 128, 'input to inverse linear transf. is not 128 bits'

    res = ''
    for array in LTInverseTable:
        out = '0'
        for array_element in array:
            out = string_xor(out, inp[array_element])
        res += out
    return res
Exemplo n.º 5
0
def xor_two_strings(s1, s2):
    bit_str1 = string_to_bitstring(s1)
    bit_str2 = string_to_bitstring(s2)
    res = string_xor(bit_str1, bit_str2)
    return bitstring_to_string(res)