Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)
Esempio n. 9
0
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
Esempio n. 10
0
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
Esempio n. 11
0
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
Esempio n. 12
0
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
Esempio n. 13
0
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)
Esempio n. 14
0
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)
Esempio n. 15
0
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)
Esempio n. 16
0
def test_function(sbox, function, *args, **kwargs):
    cycle = find_cycle_length(function, *args, **kwargs)
    print len(cycle), sorted(cycle)
    print find_best_differential(sbox)    
Esempio n. 17
0
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)
Esempio n. 18
0
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)
Esempio n. 19
0
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)
Esempio n. 20
0
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)
Esempio n. 21
0
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
Esempio n. 22
0
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)
Esempio n. 23
0
def test_function(sbox, function, *args, **kwargs):
    cycle = find_cycle_length(function, *args, **kwargs)
    print len(cycle), sorted(cycle)
    print find_best_differential(sbox)    
Esempio n. 24
0
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)
Esempio n. 25
0
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)
Esempio n. 26
0
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)
    
    
Esempio n. 27
0
def test_random_sbox():
    for x in range(16):
        sbox = bytearray(os.urandom(256))
        print find_best_differential(sbox)
        print calculate_linearity(sbox)
Esempio n. 28
0
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)
Esempio n. 29
0
def test_random_sbox():
    for x in range(16):
        sbox = bytearray(os.urandom(256))
        print find_best_differential(sbox)
        print calculate_linearity(sbox)
Esempio n. 30
0
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