SamFeistel-4: four-round cipher
SamCard terminals seal data with the 'lightweight' block cipher SamFeistel-4. The design documentation is lost, but the source code remains and its full specification is below. You have a samfeistel_capture.json dump of the device: sbox (256 numbers, a permutation of 0..255), known_pairs (6 known plaintext / ciphertext pairs, hex), sealed_flag_ecb (flag, ECB, PKCS#7), and archive_ecb_old_key (an archive copy encrypted with an old key, cannot be decrypted with the current key).
Specification. Block size is 8 bytes: L0 = BE32(blok[0:4]), R0 = BE32(blok[4:8]). For a 32-bit word x, xj = (x >> 8j) & 0xFF (j = 0 is the least significant byte). SB(y) applies sbox to each byte of y. F(x, k) = ROTL32(SB(x ^ k), 8). Four rounds, each with an independent 32-bit key k0..k3 (128 bits total): (L, R) → (R, L ^ F(R, ki)). After the final round, the halves are not swapped: the ciphertext is BE32(L4) || BE32(R4).
Task: recover the keys and decrypt sealed_flag_ecb. Flag format: BAYROQ{FEISTEL_QISQA_xxxxxxxxxxxxxxxx} (last 16 characters lowercase hex). A 128-bit key cannot be brute-forced directly; the computational cost of a smart attack is around 220.