-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc53.py
63 lines (56 loc) · 1.44 KB
/
c53.py
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
from matasano import *
size = 2 #Size between 1 and 8
k = 7
M = randbytes(2**k*size)
def my_hash(m,iv=None):
if not iv:
h = b"\x00"*size
else:
h = iv
key = b"\x00"*16
for i in range(0,len(m),size):
h = aes_ecb_enc(h+m[i:i+size]+b"\x00"*(16-2*size),key)[:size]
return h
def get_collision(a,b,iv=None):
dummy1 = b"\x00"*((a-1)*size)
dummy2 = b"\x00"*((b-1)*size)
a_iv = my_hash(dummy1,iv=iv)
b_iv = my_hash(dummy2,iv=iv)
a_found = {}
b_found = {}
while True:
x = randbytes(size)
h = my_hash(x,iv=a_iv)
if h in b_found:
return dummy1+x,dummy2+b_found[h]
a_found[h] = x
h = my_hash(x,iv=b_iv)
if h in a_found:
return dummy1+a_found[h],dummy2+x
def make_expandable(k):
iv = None
exp = []
for i in range(1,k+1):
exp.append(get_collision(1,2**(k-i)+1,iv=iv))
iv = my_hash(exp[-1][0],iv=iv)
return exp
intermediate = {}
last = b"\x00"*size
for i in range(0,len(M),size):
last = my_hash(M[i:i+size],iv=last)
intermediate[last] = i+size
exp = make_expandable(k)
iv = my_hash(b"".join([x[0] for x in exp]))
glue = randbytes(size)
h = my_hash(glue,iv=iv)
while h not in intermediate:
glue = randbytes(size)
h = my_hash(glue,iv=iv)
suff = glue + M[intermediate[h]:]
target = (len(M) - len(suff))//size - k
msg = suff
for i in range(k):
msg = exp[k-i-1][(target>>i)&1] + msg
assert(len(M) == len(msg))
assert(my_hash(M) == my_hash(msg))
print("Constructed preimage!")