griin@crypto:~$
All the Writeups

CH - Linear Cryptanalysis - Beatboxer

CryptoHack - Linear Cryptanalysis 20261 ottobre 2026

Challenge

The server runs a home-made AES with a custom S-box and gives us two options:

  • encrypt_message: encrypt one 16-byte message of our choice, only once.
  • encrypt_flag: encrypt the flag (as many times as we want).

The key is random, so we cannot recover it directly. The source code of the service is provided.

Solution

The custom S-box is affine over F2\mathbb{F}_2. This can be checked in Sage with has_linear_structure() and polynomials(): every output bit is just an XOR of input bits plus a constant.

S(x)=L(x)⊕cS(x) = L(x) \oplus c

Now look at every step of AES:

  • SubBytes: affine (because of the bug)
  • ShiftRows: linear (it only moves bytes)
  • MixColumns: linear (XORs and multiplications in F28\mathbb{F}_{2^8})
  • AddRoundKey: XOR with a constant

Everything is affine, so the whole cipher collapses into

E(p)=A⋅p⊕KE(p) = A \cdot p \oplus K

where AA is a 128×128128 \times 128 matrix over F2\mathbb{F}_2 that does not depend on the key, and KK is a 128-bit constant that depends on the key but is the same for every message.

If we XOR two ciphertexts, KK disappears:

E(p1)⊕E(p2)=A⋅(p1⊕p2)E(p_1) \oplus E(p_2) = A \cdot (p_1 \oplus p_2)

With p2=0p_2 = 0 we get

E(p)⊕E(0)=A⋅p⟹p=A−1 (E(p)⊕E(0))E(p) \oplus E(0) = A \cdot p \quad\Longrightarrow\quad p = A^{-1}\,\big(E(p) \oplus E(0)\big)

So the plan is:

  1. Build AA locally, without the key. I added linear_encrypt and linear_encrypt_block to the AES class: they run the cipher without AddRoundKey. This keyless function F(p)=A⋅p⊕c0F(p) = A \cdot p \oplus c_0 has the same matrix AA, so F(p)⊕F(0)F(p) \oplus F(0) gives A⋅pA \cdot p.
  2. Encrypt the 128 basis vectors (one bit set) and XOR each result with F(0)F(0). Each result is one column of AA. Then invert the matrix.
  3. Use our single encrypt_message call to get E(0)E(0).
  4. Get the encrypted flag, and for each block compute A−1 (E(fi)⊕E(0))=fiA^{-1}\,(E(f_i) \oplus E(0)) = f_i.
  5. Remove the padding.

Methods added to the source:

def linear_encrypt_block(self):
    for i in range(1, 10):
        self._sub_bytes()
        self._shift_rows()
        self._mix_columns()
    self._sub_bytes()
    self._shift_rows()

def linear_encrypt(self, plaintext):
    ciphertext = b''
    self._state = self._transpose([c for c in plaintext])
    self.linear_encrypt_block()
    ciphertext += bytes(bytearray(self._transpose(self._state)))
    return ciphertext

Exploit (Sage, ^^ is XOR):

from sage.crypto.sbox import SBox
import pwn, json

sboax = SBox(xboxed)
print(sboax.has_linear_structure())
print(sboax.polynomials(groebner=True))

io = pwn.remote("socket.cryptohack.org", 13406)

cipher = AES(b"\x00" * 16)  # the key does not matter: we never use AddRoundKey
rows = []

def zero_pad(m):
    return (b"\x00" * (16 - len(m))) + m

# F(0)
c0 = cipher.linear_encrypt(b"\x00" * 16)
c0_bits = [int(a) for a in bin(bytes_to_long(c0))[2:].zfill(128)]

# columns of A: F(e_i) xor F(0)
for i in range(128):
    poly = zero_pad(long_to_bytes(1 << (127 - i)))
    tot = cipher.linear_encrypt(poly)
    tot_bits = [int(a) for a in bin(bytes_to_long(tot))[2:].zfill(128)]
    rows.append([a ^^ b for a, b in zip(tot_bits, c0_bits)])

M = matrix(GF(2), rows).transpose()
inv_M = M.inverse()

io.recvline()

# E(0) from the server (only one call allowed)
io.sendline(json.dumps({"option": "encrypt_message", "message": (b"\x00" * 16).hex()}).encode())
ct1 = bytes.fromhex(json.loads(io.recvline().strip().decode())["encrypted_message"])

# encrypted flag
io.sendline(json.dumps({"option": "encrypt_flag"}).encode())
ct_flag = bytes.fromhex(json.loads(io.recvline().strip().decode())["encrypted_flag"])
flag_blocks = [ct_flag[i * 16:(i + 1) * 16] for i in range(len(ct_flag) // 16)]

def _xor(a, b):
    return [x ^^ y for x, y in zip(a, b)]

flag_left = b""
for bl in flag_blocks:
    # E(f_i) xor E(0) = A * f_i
    ct_xored = b"".join(bytes([a]) for a in _xor(bl, ct1))
    ready = vector(GF(2), [int(a) for a in bin(bytes_to_long(ct_xored))[2:].zfill(128)])
    # f_i = A^-1 * (A * f_i)
    flag_left += long_to_bytes(int("".join(str(a) for a in (inv_M * ready)), 2))

print(unpad(flag_left, 16).decode())

Takeaway: AES is secure only because its S-box is non-linear. With an affine S-box the cipher is just a matrix multiplication plus a constant, and one known plaintext/ciphertext pair is enough to cancel the key.

Flag

crypto{5b0x_l1n34r17y_15_d35457r0u5}