-
Notifications
You must be signed in to change notification settings - Fork 0
/
keccak.py
176 lines (155 loc) · 4.97 KB
/
keccak.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
# https://keccak.team/keccak_specs_summary.html
rho = [
0, 1, 62, 28, 27,
36, 44, 6, 55, 20,
3, 10, 43, 25, 39,
41, 45, 15, 21, 8,
18, 2, 61, 56, 14
]
iota = [
0x0000000000000001,
0x0000000000008082,
0x800000000000808a,
0x8000000080008000,
0x000000000000808b,
0x0000000080000001,
0x8000000080008081,
0x8000000000008009,
0x000000000000008a,
0x0000000000000088,
0x0000000080008009,
0x000000008000000a,
0x000000008000808b,
0x800000000000008b,
0x8000000000008089,
0x8000000000008003,
0x8000000000008002,
0x8000000000000080,
0x000000000000800a,
0x800000008000000a,
0x8000000080008081,
0x8000000000008080,
0x0000000080000001,
0x8000000080008008
]
ROTL32 = lambda x, n: ((x << n) ^ (x >> (32 - n))) % 2**32
ROTL64 = lambda x, n: ((x << n) ^ (x >> (64 - n))) % 2**64
load32 = lambda x: sum((c << (8*i)) for i, c in zip(range(4), x))
store32 = lambda x: bytes((x >> (8*i)) & 0xff for i in range(4))
load64 = lambda x: sum((c << (8*i)) for i, c in zip(range(8), x))
store64 = lambda x: bytes((x >> (8*i)) & 0xff for i in range(8))
def keccak_800(msg, outlen=32):
def round(A, RC):
# theta
B, C, D = {}, {}, {}
for x in range(5):
C[x] = A[(x, 0)] ^ A[(x, 1)] ^ A[(x, 2)] ^ A[(x, 3)] ^ A[(x, 4)]
for x in range(5):
D[x] = C[4 if x == 0 else (x - 1)] ^ ROTL32(C[(x + 1) % 5], 1)
for y in range(5):
for x in range(5):
A[(x, y)] ^= D[x]
# rho and pi
for y in range(5):
for x in range(5):
B[(y, (2*x + 3*y) % 5)] = ROTL32(A[(x, y)], rho[x + 5*y] % 32)
# chi
for y in range(5):
for x in range(5):
A[(x, y)] = B[(x, y)] ^ ((~B[((x + 1) % 5, y)]) & B[((x + 2) % 5, y)])
# iota
A[(0, 0)] ^= RC
return A
def f_perm(A):
for i in range(22):
A = round(A, iota[i] & 0xffffffff)
return A
A = {}
for y in range(5):
for x in range(5):
A[(x, y)] = 0
pos = 0
while pos <= len(msg):
if pos + 36 <= len(msg):
for y in range(2):
for x in range(5):
i = x + 5*y
if i < 9:
A[(x, y)] ^= load32(msg[pos + 4*i:pos + 4*i + 4])
else:
buffer = msg[pos:]
buffer += b'\x81' if len(buffer) == 35 else (b'\x01' + ((34 - len(buffer)) * b'\x00') + b'\x80')
for y in range(2):
for x in range(5):
i = x + 5*y
if i < 9:
A[(x, y)] ^= load32(buffer[4*i:4*i + 4])
A = f_perm(A)
pos += 36
output = b''
while len(output) < outlen:
for y in range(2):
for x in range(5):
if x + 5*y <= 9:
output += store32(A[(x, y)])
if len(output) < outlen:
A = f_perm(A)
return output[:outlen] if outlen < len(output) else output
def keccak_1600(msg, outlen=32):
def round(A, RC):
# theta
B, C, D = {}, {}, {}
for x in range(5):
C[x] = A[(x, 0)] ^ A[(x, 1)] ^ A[(x, 2)] ^ A[(x, 3)] ^ A[(x, 4)]
for x in range(5):
D[x] = C[4 if x == 0 else (x - 1)] ^ ROTL64(C[(x + 1) % 5], 1)
for y in range(5):
for x in range(5):
A[(x, y)] ^= D[x]
# rho and pi
for y in range(5):
for x in range(5):
B[(y, (2*x + 3*y) % 5)] = ROTL64(A[(x, y)], rho[x + 5*y])
# chi
for y in range(5):
for x in range(5):
A[(x, y)] = B[(x, y)] ^ ((~B[((x + 1) % 5, y)]) & B[((x + 2) % 5, y)])
# iota
A[(0, 0)] ^= RC
return A
def f_perm(A):
for i in range(24):
A = round(A, iota[i])
return A
A = {}
for y in range(5):
for x in range(5):
A[(x, y)] = 0
pos = 0
while pos <= len(msg):
if pos + 136 <= len(msg):
for y in range(4):
for x in range(5):
i = x + 5*y
if i < 17:
A[(x, y)] ^= load64(msg[pos + 8*i:pos + 8*i + 8])
else:
buffer = msg[pos:]
buffer += b'\x81' if len(buffer) == 135 else (b'\x01' + ((134 - len(buffer)) * b'\x00') + b'\x80')
for y in range(4):
for x in range(5):
i = x + 5*y
if i < 17:
A[(x, y)] ^= load64(buffer[8*i:8*i + 8])
A = f_perm(A)
pos += 136
output = b''
while len(output) < outlen:
for y in range(4):
for x in range(5):
if x + 5*y <= 17:
output += store64(A[(x, y)])
if len(output) < outlen:
A = f_perm(A)
return output[:outlen] if outlen < len(output) else output