コード例 #1
0
import sys
import bitpy as bp

wl = int(sys.argv[1])
system = bp.get_string('./structures/sha_1.txt')

system = bp.simplify_across(system)

bp.store_string('./structures/sha_1_no_assignments.txt', system)

H = bp.get_words('H.txt')
for i in range(len(H)):
    for j in range(wl):
        system = system.replace(bp.bit('H' + bp.fi(0) + bp.fi(i), j),\
                str(bool(H[i] >> j & 1)).lower())

system = bp.simplify_across(system)

bp.store_string('./structures/sha_1_first_block.txt', system)

W = bp.words_from_string(wl, "Chiara")

system = bp.xor_in_standard_syntax(system)
system = bp.not_in_python_syntax(system)

for i in range(len(W)):
    for j in range(wl):
        system = system.replace(bp.bit('W' + bp.fi(0) + bp.fi(i), j),\
                str(bool(W[i] >> j & 1)))

exec(system)
コード例 #2
0
import re
import bitpy as bp

wl = int(sys.argv[1])
round_to_attack = int(sys.argv[2])
file_id = sys.argv[3]
preimage_length = int(sys.argv[4])
cnf = sys.argv[5]

system = bp.get_string(cnf)
clauses = system.split('\n')
last_carry = 'g' + bp.fi(0) + bp.fi(round_to_attack - 1) + bp.fi(wl - 2)
last_clause = list(filter(lambda x: last_carry in x, clauses))[-1]
system = '\n'.join(clauses[0:clauses.index(last_clause) + 1])

states = bp.get_words('./maps/states_' + file_id + '.txt')
for i in range(5):
    states[i] = states[round_to_attack - 4 + i] >> (wl * 4)

for i in range(5):
    for j in range(wl):
        system = system.replace(\
                bp.bit('T' + bp.fi(0) + bp.fi(round_to_attack - 5 + i), j),\
                str(bool(states[i] >> j & 1)).lower())

fixed, system = bp.fix_bits(wl, system, preimage_length)
system = bp.cnf_simplify_across(system)
system = system.replace('~', '-')
system = system.replace('|', ' ')
system = system.replace('Xor(', 'x')
system = system.replace(')', '')
コード例 #3
0
'''

import sys
import re
import bitpy as bp

wl = int(sys.argv[1])
mode = sys.argv[2]
string = sys.argv[3]
file_id = sys.argv[4]

if mode == '0':
    W = bp.words_from_string(wl, string)
else:
    if mode == '1':
        W = bp.get_words(string)

system = bp.get_string('./structures/sha_1.txt')
system = bp.xor_in_standard_syntax(system)
system = bp.not_in_python_syntax(system)

#K = bp.get_words('K.txt')
H = bp.get_words('H.txt')

#for i in range(len(K)):
#    for j in range(wl):
#        system = system.replace(bp.bit('K' + bp.fi(i), j),\
#                str(bool(K[i] >> j & 1)))
for i in range(len(H)):
    for j in range(wl):
        system = system.replace(bp.bit('H' + bp.fi(0) + bp.fi(i), j),\
コード例 #4
0
    carries[i] = ''.join(('(',\
            C[i - 1], '&', X[i], ')|(',\
            C[i - 1], '&', Y[i], ')|(',\
            X[i], '&', Y[i], ')'))
carries = bp.wequal(C, carries)
results = [''] * wl
results[0] = ''.join(('Xor(', X[0], ',', Y[0], ')'))
for i in range(1, wl):
    results[i] = ''.join(('Xor(', X[i], ',', Y[i], ',', C[i - 1], ')'))
results = bp.wequal(Z, results)
bp.store_string('./models/equations/addition.txt',\
        '\n'.join(carries + results))

# Addition modulo 2 ^ wl with the first part of the K constant

K = bp.get_words('K.txt')
K_additions = [''] * len(K)
for i in range(len(K)):
    new_addition = '\n'.join(carries + results)
    for j in range(wl):
        new_addition = new_addition.replace(bp.bit('y', j),\
                str(bool(K[i] >> j & 1)).lower())
    new_addition = new_addition.split('\n')
    for j in range(wl - 1):
        new_addition[j] = ''.join((new_addition[j][0: 4],\
                str(sympy.to_cnf(new_addition[j][4:], True))))
    for j in range(wl - 1, 2 * wl - 1):
        new_addition[j] = ''.join((new_addition[j][0: 4],\
                bp.reduce_xor(new_addition[j][4:], r'~?[xyc][0-9]{2}')))
    K_additions[i] = '\n'.join(new_addition).replace(' ', '')
for i in range(len(K)):