1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
import random import z3 import string from Crypto.Util.number import bytes_to_long,long_to_bytes import libnum
'''class lfsr(): def __init__(self, init, mask, length): self.init = init self.mask = mask self.lengthmask = 2**(length+1)-1
def next(self): nextdata = (self.init << 1) & self.lengthmask i = self.init & self.mask & self.lengthmask output = 0 while i != 0: output ^= (i & 1) i = i >> 1 nextdata ^= output self.init = nextdata return output ''' class lfsr(): def __init__(self, init, mask,length): self.init = init self.mask = mask self.lengthmask = 2**(length+1)-1
def next(self): nextdata = (self.init << 1) & self.lengthmask i = self.init & self.mask & self.lengthmask lastbit = 0 while i != 0: lastbit ^= (i & 1) i = i >> 1 nextdata ^= lastbit return nextdata,lastbit
def solve_lfsr(results, index1, index2, index3, index4, index5, index6, index7, index8, length): s = z3.Solver() x = init_recovered = z3.BitVec('x', length) for result in results: relevant_bit1 = init_recovered & (1 << index1) bit1_value = z3.LShR(relevant_bit1, index1)
relevant_bit2 = init_recovered & (1 << index2) bit2_value = z3.LShR(relevant_bit2, index2)
relevant_bit3 = init_recovered & (1 << index3) bit3_value = z3.LShR(relevant_bit3, index3)
relevant_bit4 = init_recovered & (1 << index4) bit4_value = z3.LShR(relevant_bit4, index4)
relevant_bit5 = init_recovered & (1 << index5) bit5_value = z3.LShR(relevant_bit5, index5)
relevant_bit6 = init_recovered & (1 << index6) bit6_value = z3.LShR(relevant_bit6, index6)
relevant_bit7 = init_recovered & (1 << index7) bit7_value = z3.LShR(relevant_bit7, index7)
relevant_bit8 = init_recovered & (1 << index8) bit8_value = z3.LShR(relevant_bit8, index8)
s.add(bit1_value ^ bit2_value ^ bit3_value ^ bit4_value ^ bit5_value ^ bit6_value ^ bit7_value ^ bit8_value == result)
init_recovered = ((init_recovered << 1) & (2 ** (length + 1) - 1)) ^ result s.check() return hex(int(str(s.model()[x]))) if __name__ == '__main__':
'''init = int("926201d7",16) #init0 = "9201d7d5" #init = bytes_to_long(init0) tmp2 = [] for i in range(100): tmp = 0 for j in range(8): init,out = lfsr(init,0b10100100000010000000100010010100,32).next() tmp2.append((tmp << 1)^out) print tmp2'''
tmp2 = [] data = open('key').read() for i in range(100): a = '{:08b}'.format(ord(data[i])).replace('0b','') for j in range(8): tmp2.append(a[j]) print('result', solve_lfsr(tmp2, 2, 4, 7, 11, 19, 26, 29, 31, 32))
|