def test_nonlinear_function7(): for key in (pow(251, x, 257) % 256 for x in range(1, 256)): sbox = bytearray() for right in range(256): left = 0 left, right = nonlinear_function7(left, right, key) sbox.append((right >> 8) ^ (right & 255)) print find_best_differential(sbox)
def test_nonlinear_function7(): for key in (pow(251, x, 257) % 256 for x in range(1, 256)): sbox = bytearray() for right in range(256): left = 0 left, right = nonlinear_function7(left, right, key) sbox.append((right >> 8) ^ (right & 255)) print find_best_differential(sbox)
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_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_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_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_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_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)
def test_nonlinear_function5(): cycle = find_cycle_length(nonlinear_function5, 1) print len(cycle), set(range(256)).difference(cycle), cycle sbox = bytearray(nonlinear_function4(index) for index in range(256)) print find_best_differential(sbox)
def test_function(sbox, function, *args, **kwargs): cycle = find_cycle_length(function, *args, **kwargs) print len(cycle), sorted(cycle) print find_best_differential(sbox)
def test_nonlinear_function2(): cycle = find_cycle_length(nonlinear_function2, (0, 131)) print len(cycle), cycle sbox = dict((byte, nonlinear_function2((byte, 131))[0]) for byte in range(256)) find_best_differential(sbox)
def test_nonlinear_function(): cycle = find_cycle_length(nonlinear_function, 235, 131) print len(cycle), sorted(cycle) sbox = bytearray(nonlinear_function(byte, 131) for byte in range(256)) print find_best_differential(sbox)
def test_nonlinear_function5(): cycle = find_cycle_length(nonlinear_function5, 1) print len(cycle), set(range(256)).difference(cycle), cycle sbox = bytearray(nonlinear_function4(index) for index in range(256)) print find_best_differential(sbox)
def test_nonlinear_function(): cycle = find_cycle_length(nonlinear_function, 235, 131) print len(cycle), sorted(cycle) sbox = bytearray(nonlinear_function(byte, 131) for byte in range(256)) print find_best_differential(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_nonlinear_function2(): cycle = find_cycle_length(nonlinear_function2, (0, 131)) print len(cycle), cycle sbox = dict((byte, nonlinear_function2((byte, 131))[0]) for byte in range(256)) find_best_differential(sbox)
def test_function(sbox, function, *args, **kwargs): cycle = find_cycle_length(function, *args, **kwargs) print len(cycle), sorted(cycle) print find_best_differential(sbox)
def test_nonlinear_function6(): import os for key in range(1, 256): sbox = bytearray(nonlinear_function6(right, [pow(251, x, 257) % 256 for x in range(key, key + 5)]) for right in range(256)) print find_best_differential(sbox)
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)
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 test_random_sbox(): for x in range(16): sbox = bytearray(os.urandom(256)) print find_best_differential(sbox) print calculate_linearity(sbox)
def test_nonlinear_function6(): import os for key in range(1, 256): sbox = bytearray(nonlinear_function6(right, [pow(251, x, 257) % 256 for x in range(key, key + 5)]) for right in range(256)) print find_best_differential(sbox)
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