-
Notifications
You must be signed in to change notification settings - Fork 2
/
m30.py
executable file
·203 lines (159 loc) · 6.38 KB
/
m30.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""Break an MD4 keyed MAC using length extension"""
import struct
from copy import copy
from typing import Union
from collections.abc import Iterator
from Crypto.Random import get_random_bytes
from Crypto.Random.random import randrange
from m28 import HashBase, merkle_pad
Register = Union[tuple[int, ...], tuple[int, int, int, int]]
class MD4(HashBase):
name = "md4"
block_size = 64
digest_size = 16
register = (0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476)
def __init__(self, data: bytes = b"") -> None:
self._register: Register = MD4.register
self.data = b""
self._vandercorput = [self._binaryreverse(x) for x in range(16)]
self.update(data)
def copy(self) -> "MD4":
return copy(self)
def update(self, data: bytes) -> None:
self._register = MD4.register
self.data += data
for chunk in self._chunks():
self._compress(chunk)
@staticmethod
def pad_message(data: bytes) -> bytes:
data = merkle_pad(data, MD4.block_size, "little")
assert len(data) % MD4.block_size == 0
return data
def _chunks(self) -> Iterator[bytes]:
data = self.pad_message(self.data)
for i in range(0, len(data), self.block_size):
yield data[i:i + self.block_size]
@staticmethod
def _lrot(x: int, n: int = 1) -> int:
return x << n | x >> 32 - n
@staticmethod
def _binaryreverse(x: int, n: int = 4) -> int:
r = 0
for _ in range(n):
r = (r << 1) + (x & 1)
x >>= 1
return r
@staticmethod
def _F(x: int, y: int, z: int) -> int:
return (x & y) | (~x & z)
@staticmethod
def _G(x: int, y: int, z: int) -> int:
return (x & y) | (x & z) | (y & z)
@staticmethod
def _H(x: int, y: int, z: int) -> int:
return x ^ y ^ z
def round_one_op(self, x: list[int], a: int, b: int, c: int, d: int,
k: int, s: int) -> int:
return self._lrot(a + MD4._F(b, c, d) + x[k] & 0xffffffff, s)
def round_two_op(self, x: list[int], a: int, b: int, c: int, d: int,
k: int, s: int) -> int:
return self._lrot(a + MD4._G(b, c, d) + x[k]
+ 0x5a827999 & 0xffffffff, s)
def round_three_op(self, x: list[int], a: int, b: int, c: int, d: int,
k: int, s: int) -> int:
return self._lrot(a + MD4._H(b, c, d) + x[k]
+ 0x6ed9eba1 & 0xffffffff, s)
def round_one(self, x: list[int], a: int, b: int, c: int, d: int) -> Register:
for k in range(16):
if k % 4 == 0:
a = self.round_one_op(x, a, b, c, d, k, 3)
elif k % 4 == 1:
d = self.round_one_op(x, d, a, b, c, k, 7)
elif k % 4 == 2:
c = self.round_one_op(x, c, d, a, b, k, 11)
else:
b = self.round_one_op(x, b, c, d, a, k, 19)
return a, b, c, d
def round_two(self, x: list[int], a: int, b: int, c: int, d: int) -> Register:
for i in range(16):
k = (i // 4 + 4 * i) % 16
if i % 4 == 0:
a = self.round_two_op(x, a, b, c, d, k, 3)
elif i % 4 == 1:
d = self.round_two_op(x, d, a, b, c, k, 5)
elif i % 4 == 2:
c = self.round_two_op(x, c, d, a, b, k, 9)
else:
b = self.round_two_op(x, b, c, d, a, k, 13)
return a, b, c, d
def round_three(self, x: list[int], a: int, b: int, c: int, d: int) -> Register:
for i in range(16):
k = self._vandercorput[i]
if i % 4 == 0:
a = self.round_three_op(x, a, b, c, d, k, 3)
elif i % 4 == 1:
d = self.round_three_op(x, d, a, b, c, k, 9)
elif i % 4 == 2:
c = self.round_three_op(x, c, d, a, b, k, 11)
else:
b = self.round_three_op(x, b, c, d, a, k, 15)
return a, b, c, d
def _compress(self, chunk: bytes) -> None:
X = list(struct.unpack("<16I", chunk))
A, B, C, D = self._register
A, B, C, D = self.round_one(X, A, B, C, D)
A, B, C, D = self.round_two(X, A, B, C, D)
A, B, C, D = self.round_three(X, A, B, C, D)
A = (self._register[0] + A) & 0xffffffff
B = (self._register[1] + B) & 0xffffffff
C = (self._register[2] + C) & 0xffffffff
D = (self._register[3] + D) & 0xffffffff
self._register = (A, B, C, D)
def hexdigest(self) -> str:
h = list(map(lambda x: x.to_bytes(4, "little"), self._register))
return "".join(x.hex() for x in h)
def digest(self) -> bytes:
h = list(map(lambda x: x.to_bytes(4, "little"), self._register))
return b"".join(h)
def md4_mac(message: bytes, key: bytes) -> MD4:
return MD4(key + message)
def md4_state_from_hex(d: str) -> Register:
return struct.unpack("<4I", bytes.fromhex(d))
def md4_state_from_binary(d: bytes) -> Register:
h = [d[4 * i:4 * (i + 1)] for i in range(4)]
return tuple(map(lambda x: int.from_bytes(x, "little"), h))
def md4_state_from_object(d: MD4) -> Register:
return d._register # pylint: disable=protected-access
def verify_md4_mac(d: MD4, message: bytes, key: bytes) -> bool:
return d.digest() == md4_mac(message, key).digest()
def md_padding(data: bytes) -> bytes:
return MD4.pad_message(data)[len(data):]
def extend_md4(d: MD4, z: bytes) -> Iterator[MD4]:
register = md4_state_from_hex(d.hexdigest())
padding = z + md_padding(z)
for n in range(1, 100):
data_int = 8 * (MD4.block_size * n + len(z))
data_bits = data_int.to_bytes(8, "little")
padding = padding[:-8] + data_bits
q = MD4()
# pylint: disable=protected-access
q._register = register
q._compress(padding)
yield q
def main() -> None:
message = b"comment1=cooking%20MCs;userdata=foo;" \
b"comment2=%20like%20a%20pound%20of%20bacon"
key = get_random_bytes(randrange(50))
z = b";admin=true"
# server-side
mac = md4_mac(message, key)
m_prime = message + bytearray(md_padding(key + message)) + z
# client-side
for q in extend_md4(mac, z):
if verify_md4_mac(q, m_prime, key):
print(q.hexdigest())
return
raise RuntimeError("Failed to break MD4-MAC")
if __name__ == "__main__":
main()