def import_masks(masks_list): z = '' for i in range(len(masks_list)): z += '#include "Masks/masks_' + gen_name( masks_list[i]['kind'], masks_list[i]['width']) + '.h"\n' return z
def gen_function_sig(kind,width,num): union = gen_union(kind,width) name = gen_name(kind, width) name += '_' + str(num) z = '\n' z += 'uint64_t ' + name + '(state *saved_states, union Register *saved_ciphers);\n' z += 'uint32_t ' + name + '_weight();\n' return z;
def defines(kind, width, test): name = gen_name(kind, width) z = '' z += '#define STATE ' + name + '_print_state\n' z += '#define PRINT ' + name + '_print\n' z += '#define RAND ' + name + '_rand_init\n' z += '#define ENCR ' + name + '_encrypt\n' z += '#define COPY ' + name + '_copy_state\n' z += '#define MASK ' + name + '_' + str(test) + '\n' z += '#define WEIGHT ' + name + '_' + str(test) + '_weight()\n' return z
def gen_function(kind, width, num, states, ciphers, weight): union = gen_union(kind, width) name = gen_name(kind, width) name += '_' + str(num) z = '\n' z += 'uint64_t ' + name + '(state *saved_states, union Register *saved_ciphers) {\n' z += '\tuint' + str(width) + '_t res = 0;\n' for K, C in ciphers.items(): if kind == 1: z += '\tres ^= saved_ciphers' + K + '.' + union + ' & ' + hex( gen_mask(C)) + ';\n' else: z += '\tres ^= saved_ciphers' + K + '.' + union + '[0] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_ciphers' + K + '.' + union + '[1] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_ciphers' + K + '.' + union + '[2] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_ciphers' + K + '.' + union + '[3] & ' + hex( gen_mask(C)) + ';\n' for K, C in states.items(): if kind == 1: z += '\tres ^= saved_states' + K + '.' + union + ' & ' + hex( gen_mask(C)) + ';\n' else: z += '\tres ^= saved_states' + K + '.' + union + '[0] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_states' + K + '.' + union + '[1] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_states' + K + '.' + union + '[2] & ' + hex( gen_mask(C)) + ';\n' z += '\tres ^= saved_states' + K + '.' + union + '[3] & ' + hex( gen_mask(C)) + ';\n' z += '\treturn 1 & HW' + str(width) + '(res);\n' z += '}\n' z += 'uint32_t ' + name + '_weight() {\n' z += '\treturn ' + str(weight) + ';\n' z += '}\n' z += '\n' return z
def gen_function(kind,width,num,states,ciphers, weight): union = gen_union(kind,width) name = gen_name(kind, width) name += '_' + str(num) z = '\n' z += 'uint64_t ' + name + '(state *saved_states, union Register *saved_ciphers) {\n' z += '\tuint' + str(width) + '_t res = 0;\n' if kind != 1: z += '\tuint' + str(width) + '_t t = 0;\n' for K, C in ciphers.items(): if kind == 1: z += '\tres ^= saved_ciphers' + K + '.' + union + ' & ' + hex(gen_mask(C)) + ';\n' else: if width == 32: z += '\tt = (uint32_t) _mm_extract_epi32(saved_ciphers' + K + '.' + union + ', 0);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_ciphers' + K + '.' + union + ', 1);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_ciphers' + K + '.' + union + ', 2);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_ciphers' + K + '.' + union + ', 3);\n' z += '\tres ^= t &' + hex(gen_mask(C)) + ';\n' else: z += '#ifdef __AVX2__\n' z += '\tt = (uint64_t) _mm256_extract_epi64(saved_ciphers' + K + '.' + union + ', 0);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_ciphers' + K + '.' + union + ', 1);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_ciphers' + K + '.' + union + ', 2);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_ciphers' + K + '.' + union + ', 3);\n' z += '\tres ^= t &' + hex(gen_mask(C)) + ';\n' z += '#endif\n' for K, C in states.items(): if kind == 1: z += '\tres ^= saved_states' + K + '.' + union + ' & ' + hex(gen_mask(C)) + ';\n' else: if width == 32: z += '\tt = (uint32_t) _mm_extract_epi32(saved_states' + K + '.' + union + ', 0);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_states' + K + '.' + union + ', 1);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_states' + K + '.' + union + ', 2);\n' z += '\tt ^= (uint32_t) _mm_extract_epi32(saved_states' + K + '.' + union + ', 3);\n' z += '\tres ^= t &' + hex(gen_mask(C)) + ';\n' else: z += '#ifdef __AVX2__\n' z += '\tt = (uint64_t) _mm256_extract_epi64(saved_states' + K + '.' + union + ', 0);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_states' + K + '.' + union + ', 1);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_states' + K + '.' + union + ', 2);\n' z += '\tt ^= (uint64_t) _mm256_extract_epi64(saved_states' + K + '.' + union + ', 3);\n' z += '\tres ^= t &' + hex(gen_mask(C)) + ';\n' z += '#endif\n' z += '\treturn 1 & HW' + str(width) + '(res);\n' z += '}\n'; z += 'uint32_t ' + name + '_weight() {\n' z += '\treturn ' + str(weight) + ';\n'; z += '}\n'; z += '\n'; return z
def gen_function_sig(kind,width,num): union = gen_union(kind,width) name = gen_name(kind, width) name += '_' + str(num) z = '\n' z += 'uint64_t ' + name + '(state *saved_states, union Register *saved_ciphers);\n' z += 'uint32_t ' + name + '_weight();\n' return z; if __name__ == "__main__": for i in range(len(masks_list)): file_name = 'masks_' + gen_name(masks_list[i]['kind'],masks_list[i]['width']) H = '' H += header_head() Z = '' Z += main_head(file_name) for j in range(len(masks_list[i]['weight'])): Z += gen_function(masks_list[i]['kind'], masks_list[i]['width'], (j+1), masks_list[i]['states'][j], masks_list[i]['ciphers'][j], masks_list[i]['weight'][j]) H += gen_function_sig(masks_list[i]['kind'],masks_list[i]['width'],(j+1)) print(bcolors.YELLOW + "Generate " + file_name + ".[h|c] "+ bcolors.ENDC)