def test_nonlinear_function9(): for key_byte in range(256): data = bytearray(16) key = bytearray(15) key.append(key_byte) sbox = bytearray() for byte in range(256): data[-1] = byte nonlinear_function9(data, key) sbox.append(data[-2]) print find_best_differential(sbox) print calculate_linearity(sbox)
def test_function(): sbox = bytearray(256) for x in range(256): temp = x * x sbox[x] = (temp >> 8) ^ (temp & 255) print "Best differential: ", find_best_differential(sbox) print "Linearity: ", calculate_linearity(sbox)
def test_generate_key(): # sbox = [] # for byte in range(256): # data = bytearray(16) # data[-2] = byte # sbox.append(generate_key(data)) # print bytearray(sbox) # print find_best_differential(sbox) sbox = [] for byte in range(256): data = bytearray(16) data[-1] = byte prf(data, generate_key(data)) sbox.append(data[-1]) print find_best_differential(sbox) print calculate_linearity(sbox)
def test_speck(): for key in (pow(251, x, 257) % 256 for x in range(256)): for left in range(256): sbox = bytearray() for right in range(256): left, right = speck_round(left, right, key) sbox.append(left & 255) differentials = find_best_differential(sbox) linearity = calculate_linearity(sbox) print differentials print linearity
def test_nonlinear_function8(): #for _rotation in range(20): # print "\nRotation: ", _rotation _rotation = 5 for key in (pow(251, x, 257) % 256 for x in range(1, 16)): sbox = bytearray() for right in range(256): left = 0 left, right = nonlinear_function8(left, right, key, _rotation=_rotation) sbox.append(left >> 56) differentials = find_best_differential(sbox) linearity = calculate_linearity(sbox) print differentials print linearity
def test_prp_s_box(): import collections s_box = collections.defaultdict(bytearray) rounds = 4 for ending in range(256): data = bytearray(16) data[-1] = ending for round in range(rounds): prp(data, xor_sum(data)) s_box[round].append(data[-2]) from differential import find_best_differential from linear import calculate_linearity for round in range(rounds): _s_box = s_box[round] print "Best differential after {} rounds: ".format(round + 1), find_best_differential(_s_box) print "Linearity after {} rounds: ".format(round + 1), calculate_linearity(_s_box)
def test_prp_s_box(): import collections s_box = collections.defaultdict(bytearray) rounds = 4 for ending in range(256): data = bytearray(16) data[-1] = ending for round in range(rounds): prp(data, xor_sum(data)) s_box[round].append(data[-2]) from differential import find_best_differential from linear import calculate_linearity for round in range(rounds): _s_box = s_box[round] print "Best differential after {} rounds: ".format( round + 1), find_best_differential(_s_box) print "Linearity after {} rounds: ".format( round + 1), calculate_linearity(_s_box)
import os from differential import find_best_differential from linear import calculate_linearity def shuffle(array, key): for index in reversed(range(1, len(array))): other_index = key[index] & (index - 1) array[index], array[other_index] = array[other_index], array[index] for x in range(16): sbox = bytearray(range(256)) key = bytearray(os.urandom(256)) shuffle(sbox, key) key = bytearray(os.urandom(256)) shuffle(sbox, key) shuffle(sbox, key) print find_best_differential(sbox) print calculate_linearity(sbox)
def cryptanalyze_sbox_worst_case(sbox, differential_types=STANDARD_DIFFERENTIAL): differentials = differential.find_best_differential( sbox, differential_types) linearity = linear.calculate_linearity(sbox, log(len(sbox), 2)) return differentials, linearity
def test_random_sbox(): for x in range(16): sbox = bytearray(os.urandom(256)) print find_best_differential(sbox) print calculate_linearity(sbox)
def cryptanalyze_sbox_worst_case(sbox, differential_types=STANDARD_DIFFERENTIAL): differentials = differential.find_best_differential(sbox, differential_types) linearity = linear.calculate_linearity(sbox, log(len(sbox), 2)) return differentials, linearity