#!/usr/bin/env python
from itertools
import
product
target = [92, 218, 119, 47, 163, 198, 62, 57, 182, 240, 243, 237, 81, 90, 153, 134]
matrix = [0x6bcdc67a,0x6b2b7c9d,0x8da459b1,0xab9d0680,
0x34a20b18,0x5f897785,0xd22d2e34,0x79b028b4,
0xd19686ae,0x8e1ff12b,0x5c32df1f,0x2582f7ab,
0xc6fee491,0x48e115ba,0x14d3caa5,0x31513d0e,
0x1fd94f56,0x57385aec,0x43eb9049,0x72baad47,
0xfb4cef16,0xac74b5fa,0xef9f25b3,0x9d2588f4,
0xe4885048,0x48fce5b2,0xa763c001,0x3a4648f5,
0xfedab6c8,0xb626537a,0x1145937b,0x2b03db8e,
0x563af39,0xb345fc43,0xa2006f38,0x8903b4b6,
0x65eee19e,0xd6ab1ddd,0x74ab72e5,0xfda8c653,
0x915a0cca,0x47f11117,0x335a63f2,0xcef2a5a1]
round = 10
b = [0x5cda772f,0xa3c63e39,0xb6f0f3ed,0x515a9986]
a = [matrix[round*4+i]
for
i
in
range(4)]
c = [0]*4
for
x
in
range(len(a)):
c[x] = a[x] ^ b[x]
d =
''
.
join
(
format
(x,
'08x'
)
for
x
in
c)
print d
e = [ord(x)
for
x
in
d.decode(
'hex'
)]
print map(hex, e)
f = [
0x63, 0xc6, 0x7c, 0xf8,
0x77, 0xee, 0x7b, 0xf6, 0xf2, 0xff, 0x6b, 0xd6, 0x6f, 0xde, 0xc5, 0x91,
0x30, 0x60, 0x01, 0x02, 0x67, 0xce, 0x2b, 0x56, 0xfe, 0xe7, 0xd7, 0xb5,
0xab, 0x4d, 0x76, 0xec, 0xca, 0x8f, 0x82, 0x1f, 0xc9, 0x89, 0x7d, 0xfa,
0xfa, 0xef, 0x59, 0xb2, 0x47, 0x8e, 0xf0, 0xfb, 0xad, 0x41, 0xd4, 0xb3,
0xa2, 0x5f, 0xaf, 0x45, 0x9c, 0x23, 0xa4, 0x53, 0x72, 0xe4, 0xc0, 0x9b,
0xb7, 0x75, 0xfd, 0xe1, 0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ae, 0xb9, 0x69, 0x86, 0x17, 0xc1, 0x99, 0x1d, 0x3a, 0x9e, 0x27,
0xe1, 0xd9, 0xf8, 0xeb, 0x98, 0x2b, 0x11, 0x22, 0x69, 0xd2, 0xd9, 0xa9,
0x8e, 0x07, 0x94, 0x33, 0x9b, 0x2d, 0x1e, 0x3c, 0x87, 0x15, 0xe9, 0xc9,
0xce, 0x87, 0x55, 0xaa, 0x28, 0x50, 0xdf, 0xa5, 0x8c, 0x03, 0xa1, 0x59,
0x89, 0x09, 0x0d, 0x1a, 0xbf, 0x65, 0xe6, 0xd7, 0x42, 0x84, 0x68, 0xd0,
0x41, 0x82, 0x99, 0x29, 0x2d, 0x5a, 0x0f, 0x1e, 0xb0, 0x7b, 0x54, 0xa8,
0xbb, 0x6d, 0x16, 0x2c ];
def getTrans(off1,off2):
low = f[off1*2]
high = f[off1*2+1]
tmp = [high,low,low,(low^high)]
tmp2 = tmp[-off2:] + tmp[:-off2]
return
tmp2[0]<<24|tmp2[1]<<16|tmp2[2]<<8|tmp2[3]
tr
= [[getTrans(i,0)
for
i
in
range(256)],
[getTrans(i,1)
for
i
in
range(256)],
[getTrans(i,2)
for
i
in
range(256)],
[getTrans(i,3)
for
i
in
range(256)]]
def findpos(x):
for
i
in
range(len(f)
/2
):
if
f[i*2] == x:
return
i
g = [findpos(x)
for
x
in
e]
def trans(x):
a = [[x[0],x[13],x[10],x[7]],
[x[4],x[1],x[14],x[11]],
[x[8],x[5],x[2],x[15]],
[x[12],x[9],x[6],x[3]]]
b = [(int(
'0x'
+
''
.
join
(
format
(c,
'02x'
)
for
c
in
a[i]),16))
for
i
in
range(4)]
return
b
b2 = trans(g)
def trans2(x):
a = [[x[0],x[1],x[2],x[3]],
[x[4],x[5],x[6],x[7]],
[x[8],x[9],x[10],x[11]],
[x[12],x[13],x[14],x[15]]]
b = [(int(
'0x'
+
''
.
join
(
format
(c,
'02x'
)
for
c
in
a[i]),16))
for
i
in
range(4)]
return
b
round -= 1
b2 = [0x570b0a03, 0xa33211fc, 0x5e69eda9, 0x7f423800]
while
(round > 0) :
round -= 1
a2 = [matrix[round*4+i]
for
i
in
range(4)]
print map(hex, b2)
print map(hex, a2)
c2 = [ a2[x]^b2[x]
for
x
in
range(len(a2)) ]
print map(hex, c2)
d = [0] * 16;
for
x,y,z
in
product(range(256),range(256),range(256)):
data1 =
tr
[0][x]^
tr
[1][y]^
tr
[2][z]^c2[0]
data2 =
tr
[0][x]^
tr
[1][y]^
tr
[2][z]^c2[1]
data3 =
tr
[0][x]^
tr
[1][y]^
tr
[2][z]^c2[2]
data4 =
tr
[0][x]^
tr
[1][y]^
tr
[2][z]^c2[3]
if
data1
in
tr
[3]:
print hex(c2[0]),hex(x),hex(y),hex(z),hex(
tr
[3].index(data1))
d[0],d[5],d[10],d[15] = x,y,z,
tr
[3].index(data1)
if
data2
in
tr
[3]:
print hex(c2[1]),hex(x),hex(y),hex(z),hex(
tr
[3].index(data2))
d[4],d[9],d[14],d[3] = x,y,z,
tr
[3].index(data2)
if
data3
in
tr
[3]:
print hex(c2[2]),hex(x),hex(y),hex(z),hex(
tr
[3].index(data3))
d[8],d[13],d[2],d[7] = x,y,z,
tr
[3].index(data3)
if
data4
in
tr
[3]:
print hex(c2[3]),hex(x),hex(y),hex(z),hex(
tr
[3].index(data4))
d[12],d[1],d[6],d[11] = x,y,z,
tr
[3].index(data4)
b2 = trans2(d)
print map(hex,b2)