def decode(out): Add= lambda x,y : F.Add(x,y) Mul = lambda x,y: F.Multiply(x,y) Sub = lambda x,y: F.Subtract(x,y) Div = lambda x,y: divi(x,y) m = genericmatrix.GenericMatrix(size=(8,8),zeroElement=0,identityElement=1,add=Add,mul=Mul,sub=Sub,div=Div) for i in range(8): m.SetRow(i,keys[i]) output=[] for i in range(16): output.append((ord(out[2*i])-ord('f'))*16+(ord(out[2*i+1])-ord('f'))) print(output) # keys_inv=[[keys[i][j] for j in range(8)] for i in range(8)] # det=1 # for i in range(8): # det=F.Multiply(det,keys[i][i]) # print(det) # for i in range(8): # for j in range(8): # keys_inv[i][j]=div(keys[i][j],det) keys_inv=m.Inverse() print(keys_inv) output[0:8]=inv_E(output[0:8]) output[0:8]=inv_A(output[0:8],keys_inv) output[0:8]=inv_E(output[0:8]) output[0:8]=inv_A(output[0:8],keys_inv) output[0:8]=inv_E(output[0:8]) output[8:16]=inv_E(output[8:16]) output[8:16]=inv_A(output[8:16],keys_inv) output[8:16]=inv_E(output[8:16]) output[8:16]=inv_A(output[8:16],keys_inv) output[8:16]=inv_E(output[8:16]) return output
def mix_column(input_state, mode): # define GF(2^8) (i.e., GF(256)) field F = ffield.FField(8) # depending on the mode of operation get the right mix_column matrix if mode == 'E': column_matrix = genericmatrix.GenericMatrix(size=(4, 4), add=F.Add, sub=F.Subtract, mul=F.Multiply, div=F.Divide) row = [2, 3, 1, 1] for i in range(4): column_matrix.SetRow(i, row) row = rotate(row, -1) elif mode == 'D': column_matrix = genericmatrix.GenericMatrix(size=(4, 4), add=F.Add, sub=F.Subtract, mul=F.Multiply, div=F.Divide) row = [14, 11, 13, 9] for i in range(4): column_matrix.SetRow(i, row) row = rotate(row, -1) else: raise ValueError('invalid mode of operation, mode = {0}'.format(mode)) # convert input_state to input_matrix input_matrix = reshape_as_matrix(input_state) # perform matrix multiplication using * operator output_matrix = input_matrix * column_matrix # convert output_matrix to output_state output_state = reshape_as_state(output_matrix) return output_state
def reshape_as_matrix(input_state): # define GF(2^8) (i.e., GF(256)) field F = ffield.FField(8) # define a matrix in GF(256) output_matrix = genericmatrix.GenericMatrix(size=(4, 4), add=F.Add, sub=F.Subtract, mul=F.Multiply, div=F.Divide) # add the corresponding elements from the input_state to the matrix for i in range(4): output_matrix.SetRow(i, input_state[i * 4:(i * 4) + 4]) return output_matrix
def genMatrix(size: tuple, min=0, max=P): if not isinstance(size, tuple) or len(size) != 2: raise Exception('Matrix size error') v = genericmatrix.GenericMatrix(size, zeroElement=0, identityElement=1, add=lambda x, y: (x + y) % P, sub=lambda x, y: (x - y + P) % P, mul=mymul, div=lambda x, y: (x * (_extended_gcd(y, P))) % P) row, col = size for r in range(row): tempcol = [random.randint(min, max) for c in range(col)] v.SetRow(r, tempcol) return v
def invert_matrix(): field = ffield.FField(7, gen=0x83) matrix = genericmatrix.GenericMatrix(size=(8, 8), zeroElement=0, identityElement=1, add=field.Add, mul=field.Multiply, sub=field.Subtract, div=field.Divide) matrix.SetRow(0, [get_int_from_BV(i) for i in A_matrix[0]]) matrix.SetRow(1, [get_int_from_BV(i) for i in A_matrix[1]]) matrix.SetRow(2, [get_int_from_BV(i) for i in A_matrix[2]]) matrix.SetRow(3, [get_int_from_BV(i) for i in A_matrix[3]]) matrix.SetRow(4, [get_int_from_BV(i) for i in A_matrix[4]]) matrix.SetRow(5, [get_int_from_BV(i) for i in A_matrix[5]]) matrix.SetRow(6, [get_int_from_BV(i) for i in A_matrix[6]]) matrix.SetRow(7, [get_int_from_BV(i) for i in A_matrix[7]]) inverse = matrix.Inverse() for rownum in range(8): row = [bv(i) for i in inverse.GetRow(rownum)] A_Inverse.append(row)
def xor(a, b): assert (len(a) == len(b)) return "".join(["0" if a[i] == b[i] else "1" for i in range(len(a))]) """ Finite field operations """ GF = ffield.FField(5) XOR = lambda x, y: x ^ y MUL = lambda x, y: GF.Multiply(x, y) DIV = lambda x, y: GF.Multiply(x, GF.Inverse(y)) m = genericmatrix.GenericMatrix(size=(3, 3), zeroElement=0, identityElement=1, add=XOR, mul=MUL, sub=XOR, div=DIV) alpha = 2 m.SetRow(0, [1, 1, alpha]) m.SetRow(1, [1, alpha, 1]) m.SetRow(2, [alpha, 1, 1]) def round(data, key): tmp = permutation(data) tmp2 = "" for i in range(0, 45, 5): x = toInt(tmp[i:i + 5]) tmp2 += bin_format(GF.Inverse(x), 5)
Descripttion : cal x^-1 at GF(2^8) Version : from https://mp.weixin.qq.com/s/TsnPvdPOcgKguFZbhMDWQw Autor : one30: [email protected] Date : 2021-03-06 11:22:31 LastEditTime : 2021-03-06 14:31:35 FilePath : /cal_aes_x_inverse.py ''' from pyfinite import genericmatrix XOR = lambda x, y: x ^ y AND = lambda x, y: x & y DIV = lambda x, y: x m = genericmatrix.GenericMatrix(size=(8, 8), zeroElement=0, identityElement=1, add=XOR, mul=AND, sub=XOR, div=DIV) m.SetRow(0, [0, 0, 0, 1, 0, 0, 1, 0]) m.SetRow(1, [1, 1, 1, 0, 1, 0, 1, 1]) m.SetRow(2, [1, 1, 1, 0, 1, 1, 0, 1]) m.SetRow(3, [0, 1, 0, 0, 0, 0, 1, 0]) m.SetRow(4, [0, 1, 1, 1, 1, 1, 1, 0]) m.SetRow(5, [1, 0, 1, 1, 0, 0, 1, 0]) m.SetRow(6, [0, 0, 1, 0, 0, 0, 1, 0]) m.SetRow(7, [0, 0, 0, 0, 0, 1, 0, 0]) print(m) print(m.Inverse())