Home
Writeups Misc About
DancingQueen

Dancing Queen

The challenge gives us the description that there is some ChaCha20 implementation, and searching Google for the name of the challenge shows some hints about this implementation being done "incorrectly".

Hence we should take a look at the way that ChaCha20 is supposed to be implemented. One good resource is RFC 8439, clicking on the link should take you to the page that contains the high-level pseudocode of how ChaCha20 works. Then we just do some reading on the RFC and check if the implementation is correct.

I do not want to go into details on how ChaCha20 works, as the RFC 8439 is very clear and should be self-explanatory. The difference between the given implementation and the RFC specification is that the state must be added with the pseudorandom permutation state += initial_state. However, in the Python implementation, no such addition is performed, the state is only permuted using Add Rotate Xor (ARX). Hence, information is retained, and we can reverse the operation to derive the key from a given keystream. Combine this with the fact that the initial state is just the IV and the key appended to a counter, along with a known plaintext for the cipher, and key recovery becomes trivial.

Why adding the original state to the permuted state should be explained in this question on Crypto StackExchange.

We can recover the keystream by just simply xor the given sample message with the corresponding plaintext (only the first 64-byte block is needed), retrieve the key, then from the key and the given IV, do the decryption operation to obtain the flag.