def FK(word, sub_key): leftBlock = word[0:4] rightBlock = word[4:] expension_p = expension_permutation(rightBlock) helper.debug(["SubKEY", sub_key]) helper.debug(["E/P", expension_p]) for i in range(0,2) : for j in range(0,4) : expension_p[i][j] = xor(expension_p[i][j],sub_key[i*4+j]) helper.debug(["XOR(KEY1 and E/EP)", expension_p]) sboxResult = sbox(expension_p) helper.debug(["sboxResult", sboxResult]) sboxResult = helper.mask_permutation(sboxResult, [1, 3, 2, 0]) helper.debug(["LeftBloc", leftBlock]) helper.debug(["P4", sboxResult]) for j in range(0, 4): sboxResult[j] = xor(sboxResult[j], leftBlock[j]) helper.debug(["XOR P4", sboxResult]) return sboxResult + rightBlock
def PC2(key): PC2Mask = [ 13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 ] return helper.mask_permutation(key, PC2Mask)
def PC1(key): PC1Mask = [ 56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 60, 52, 44, 36, 28, 20, 12, 4, 27, 19, 11, 3 ] return helper.mask_permutation(key, PC1Mask)
def IP1(plainText): ip1Mask = [ 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25, 32, 0, 40, 8, 48, 16, 56, 24 ] return helper.mask_permutation(plainText, ip1Mask)
def expension_permutation(plainText): expension_table = [ 31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10, 11, 12, 11, 12, 13, 14, 15, 16, 15, 16, 17, 18, 19, 20, 19, 20, 21, 22, 23, 24, 23, 24, 25, 26, 27, 28, 27, 28, 29, 30, 31, 0 ] expension_mask_applied = helper.mask_permutation(plainText, expension_table) # prepare expensions to Sboxs expension = [] for i in range(0, 8): expension.insert(i, expension_mask_applied[6 * i:6 * i + 6]) return expension
def FK(word, sub_key): leftBlock = word[0:32] rightBlock = word[32:] ############# EXPENSION PERMUATION ########### expension_p = expension_permutation(rightBlock) helper.debug(["SubKEY", sub_key]) helper.debug(["E/P", expension_p]) for i in range(0, 8): for j in range(0, 6): expension_p[i][j] = xor(expension_p[i][j], sub_key[i * 6 + j]) helper.debug(["XOR(KEY1 and E/EP)", expension_p]) ############# XBOX ############### sboxResult = sbox(expension_p) helper.debug(["sboxResult", sboxResult]) ############ PERmutation P ########## sboxResultPermutation = helper.mask_permutation(sboxResult, p) helper.debug(["LeftBloc", leftBlock]) helper.debug(["P", sboxResultPermutation]) ############ XOR ############ for j in range(0, 32): sboxResultPermutation[j] = xor(sboxResultPermutation[j], leftBlock[j]) helper.debug(["XOR LEFT and P", sboxResultPermutation]) return rightBlock + sboxResultPermutation
def IP(plainText): return helper.mask_permutation(plainText, [1, 5, 2, 0, 3, 7, 4, 6])
def IP_1(plainText): return helper.mask_permutation(plainText, [3, 0, 2, 4, 6, 1, 7, 5])
def p10_first_permutation(p10key): P10mask = [2, 4, 1, 6, 3, 9, 0, 8, 7, 5] return helper.mask_permutation(p10key, P10mask)
def P10_to_P8(key): P8mask = [5, 2, 6, 3, 7, 4, 9, 8] return helper.mask_permutation(key, P8mask)
def expension_permutation (plainText) : expension =[] expension.insert(0, helper.mask_permutation(plainText[0:4], [3, 0, 1, 2])) expension.insert(1, helper.mask_permutation(plainText[0:4], [1, 2, 3, 0])) return expension