-
Notifications
You must be signed in to change notification settings - Fork 2
/
m50.py
executable file
·33 lines (24 loc) · 870 Bytes
/
m50.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
#!/usr/bin/env python3
"""Hashing with CBC-MAC"""
from m09 import pkcs7
from m10 import decrypt_aes_cbc
from m49 import cbc_mac
BLOCKSIZE = 16
def forge_hash(m: bytes, m_prime: bytes, key: bytes, iv: bytes) -> bytes:
t = cbc_mac(key, iv, pkcs7(m))
t_prime = cbc_mac(key, iv, m_prime)
# We need to find m'' such that E_k(m'' xor t') = t
# by solving D_k(t) xor t' = m''.
m_prime_suffix = decrypt_aes_cbc(key, t_prime, t)
return m_prime + m_prime_suffix
def main() -> None:
m = b"alert('MZA who was that?');\n"
key = b"YELLOW SUBMARINE"
iv = bytes(16)
# No padding because it aligns with the blocksize.
m_prime = b"alert('Ayo, the Wu is back!');//"
forgery = forge_hash(m, m_prime, key, iv)
assert cbc_mac(key, iv, pkcs7(m)) == cbc_mac(key, iv, forgery)
print(forgery)
if __name__ == "__main__":
main()