CH - Linear Cryptanalysis - Beatboxer
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 . 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.
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 )AddRoundKey: XOR with a constant
Everything is affine, so the whole cipher collapses into
where is a matrix over that does not depend on the key, and is a 128-bit constant that depends on the key but is the same for every message.
If we XOR two ciphertexts, disappears:
With we get
So the plan is:
- Build locally, without the key. I added
linear_encryptandlinear_encrypt_blockto theAESclass: they run the cipher withoutAddRoundKey. This keyless function has the same matrix , so gives . - Encrypt the 128 basis vectors (one bit set) and XOR each result with . Each result is one column of . Then invert the matrix.
- Use our single
encrypt_messagecall to get . - Get the encrypted flag, and for each block compute .
- 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}