Home
Writeups Misc About
Trust Games

Trust Games

The challenge uses a LCG to generate plaintext, key and IV. To receive the flag we must present the AES-CBC encrypted plaintext given the key and IV, only we don't know the key. The LCG resets a new state every 16 states (from the refresh function). Observing the code, we can learn that:

We must recover the state from the last 8 byte of the plaintext and the first 8 byte from the IV. From that, we can recover the first 8 bytes of the key (by generating the following 8 states after the last 8 states of the plaintext), and the last 8 bytes of the key (by generating the preceding 8 states before the first 8 states of the IV).

This is the attack detailed in this paper Reconstructing Truncated Integer Variables Satisfying Linear Congruences, which goes into detail the math involves. There is some implementation online for this attack, I use this implementation, which is nice to use as the author left comments on the arguments used.

Another good reference about lattice constructions is this link

With these figured out, generating the key is trivial. Generating the following 8 states using the plaintext is simply just take the last state of the PRNG generating the plaintext, then output the next 8 bytes. The following 8 bytes of the key is a bit trickier, but still very straightforward. We know the first state used for generating the IV, denoted by si. The preceding states follow the relation of:

asi1+b=simodm

which is equivalent to:

si1=(sib)a1modm

Then the task is to generate the preceding 8 states and we should obtain the last 8 bytes of the flag.

Sage Implementation: