Skip to main content
  1. Writeups/

Oh no(nce) - Autorské řešení úlohy

·170 words·1 min
hackrrr
Challenge author
Hackrrr
hackrrr
Writeup author
Hackrrr
Table of Contents

Solution #

Máme zašifrované data pomocí AESu v EAX módu. Známe ciphertext, tag a klíč, neznáme ale nonce/IV.

Naštěstí tag obsahuje informaci o nonce. Naneštěstí v tagu jsou zamíchané (XORem) i ostatní “informace” - OMAC ze cipertexu + OMAC z AD. Naštěstí OMAC dokážeme (se znalostí klíče) také zjistit a z tagu vyXORováním lze získat nonce, kterou lez použít pro dešifrování dat.

Exploit script #

from Crypto.Cipher import AES
from Crypto.Hash import CMAC


def xor(a: bytes, b: bytes) -> bytes:
    return bytes([x ^ y for x, y in zip(a, b)])


with open("output.txt") as f:
    KEY = bytes.fromhex(f.readline().split(": ", 1)[1])
    DATA = bytes.fromhex(f.readline().split(": ", 1)[1])
    TAG = bytes.fromhex(f.readline().split(": ", 1)[1])


# OMAC1 (for authenticated data; empty)
omac1 = CMAC.new(KEY, b"\x00" * 15 + b"\x01", AES)

# OMAC2 (for encrypted data)
omac2 = CMAC.new(KEY, b"\x00" * 15 + b"\x02", AES)
omac2.update(DATA)

# Recover OMAC0 digest/result
omac0 = xor(xor(omac1.digest(), omac2.digest()), TAG)

# Construct AES CTR with recovered OMAC0 digest as initial counter state
cipher = AES.new(
    KEY, AES.MODE_CTR, nonce=b"", initial_value=int.from_bytes(omac0)
)
plaintext = cipher.decrypt(DATA)

print(plaintext)