Back to articles
Write-Ups

BreizhCTF 2025 | Write-up of my challenges

Write-up of my cryptography challenges for BreizhCTF 2025: AES-GCM, SIS/ISIS, LLL and Module-LWE.

BreizhCTF 2025 logo
Table of contents

Introduction

BreizhCTF is a French CTF held on-site at the Couvent des Jacobins in Rennes; there are no qualifications. This year it brought together more than 100 teams of 5 people each! The challenges are made every other year by students from ENSIBS (my school!) and ESNA (our friends from Rennes 👀). This year it was my school’s turn, and I had the opportunity, along with ThéoR, to design the cryptography challenges for the event.

Here is a summary table of our challenges:

Challenge Author Difficulty Solves Tags
Cube Median TheoR Easy 58 PRNG, QR code
AutHentification (ou pas…) skilo Easy 19 AES-GCM, Nonce reuse
Leaked TheoR Easy 6 Side Channel, Elliptic curve
Demi-tour skilo Medium 4 SIS
Locked TheoR Hard 0 PRNG, Time-lock encryption
AutHentification, le retour ! skilo Hard 1 AES-GCM, Implementation error
As-tu de bons yeux ? skilo Very hard 0 Module LWE, CRT

The blog article below details the solving steps for each of my challenges. Here is a TL;DR of the solution for each of them:

  • AutHentification (ou pas…) : No GCM tag verification and nonce reuse make it possible to forge a valid administrator ticket;
  • Demi-tour : This challenge implements a hash function built on the SIS problem (Short Integer Solution), i.e. given AA, find ss with a small infinity norm such that As=0(modq)As = 0 \pmod q). The SIS problem is poorly dimensioned, so it is possible to find a preimage using LLL or BKZ;
  • AutHentification, le retour ! : An implementation error in GCM mode makes it possible to forge valid tags (a Forbidden Fruit style attack);
  • As-tu de bons yeux ? : Requires breaking an instance of Module-LWE in its decisional version with the module Rq=Zq[X]/(X256−1)\mathcal{R}_q = \mathbb{Z}_q[X]/(X^{256} - 1); the quotient polynomial has a particular factorization allowing Module-LWE to be solved in several smaller-dimensional Euclidean lattices via the Chinese remainder theorem.

For all these challenges, the sources and Python/SageMath solving scripts are available at the following address: github.com/BreizhCTF/breizhctf-2025/crypto. In addition, my challenges are available for replay on the CryptoHack platform: cryptohack.org/challenges/ctf-archive/.

Challenge solutions

AutHentification 1

The goal of this challenge is to forge an admin cookie in order to access the administrator area. To do so, we must manage to encrypt JSON containing the payload: "role": "super_admin".

The encryption used is AES-GCM 128. In our case, we can view this encryption as AES-CTR; indeed, authenticity is not verified, which can be seen in the crypto.py code by diffing it against the same challenge at a higher difficulty.

We can also notice that the AES key is constant and the IV is fixed at 0, so the keystream will always be the same. By combining this with the fact that we can create an account by choosing the username (let’s call it account 1, and denote its cookie C1), we are able to forge an admin cookie (let’s call it account 2, and denote its cookie C2).

Indeed, the user cookie is: C1 = keystream XOR JSON_user; we know JSON_user, so we can recover the keystream because: keystream = C1 XOR JSON_user. We can now forge C2 by computing: C2 = keystream XOR JSON_admin, with JSON_admin being a JSON of our choice containing the super_admin role.

A complete implementation of this solution is available in Python in the solve.py file.

Demi-tour

Some notation

  • [β][\beta]: the set {−β,−β+1,…,0,…,β−1,β}\{ -\beta, -\beta+1, \ldots, 0, \ldots, \beta-1, \beta \}.
  • EkE^{k}: the set of vectors of size kk with coefficients in EE.
  • Ek×lE^{k \times l}: the set of matrices of size k×lk \times l with coefficients in EE.
  • Zn\mathbb{Z}_{n}: the ring of integers modulo nn, also denoted Z/nZ\mathbb{Z}/n\mathbb{Z}.
  • If x\mathbf{x} is a vector of size nn, then x1,x2,…,xnx_1, x_2, \ldots, x_n are its coordinates.

Parameters

Parameter Value
kk 48
ll 50
β\beta 2
nn 256

Goal

The goal of this challenge is to find a preimage for the hash() function.

def hash(msg: str):
    msg = msg.encode()
    msg = sanitize(msg, l)

    x = vector(Zn, msg)
    A = Matrix(Zn, k, l, get_a())

    return bytes(A*x)

Let us analyze this function, step by step.

sanitize

def sanitize(a: bytes, l: int):
    a = pad(a, BLOCK_LEN)
    out = b"\x00"*BLOCK_LEN

    for i in range(0, len(a), BLOCK_LEN):
        out = xor(out, a[i:i+BLOCK_LEN])

    return btq(out)[:l]

Here is how this function works:

  1. The message — after being padded with PKCS #7 — is grouped into 16-byte blocks.
  2. The blocks are all XORed together into out.
  3. The out variable is converted into a base-5 representation and then truncated to the ll-th element (i.e. it is converted into a vector of [β]l[\beta]^{l}); it is this truncated version that is returned.

The input of this function is a byte string of length 16 (see the assert in the chal.sage file), and the output is a vector of [β]l[\beta]^l.

>>> log(256**16, 2) # Size in bits of the input set
128.0
>>> log(5**50, 2)   # Size in bits of the output set
116.09640474436812

This function introduces a slight compression — 12 bits — so nothing serious; it will not be hard to reverse with brute force (indeed, we have a stopping condition: the flag format). Let us now analyze the rest of the hash() function.

A*x

We have A∈Z_nk×l\mathbf{A} \in \mathbb{Z}\_{n}^{k \times l}: the constant matrix (see the A.txt file), and x∈[β]l\mathbf{x} \in [\beta]^l: our message after going through the sanitize() function. The hash h\mathbf{h} is an element of Znk\mathbb{Z}_{n}^k, obtained by the product: Ax(modn)\mathbf{A} \mathbf{x} \pmod n.

The problem of recovering x\mathbf{x} in [β]l[\beta]^l (i.e. finding an x\mathbf{x} with only small coefficients; in [β][\beta]) from A\mathbf{A} and h\mathbf{h} is called ISIS (for Inhomogeneous Secret Integer Solution).

In our case, the parameters are weak, so we have to solve an ISIS problem of small dimension, which is very efficient with the LLL algorithm (or its BKZ variant).

Solving

A complete implementation of the solution is available in the solve.sage file

TL;DR

  • We will build the matrix whose kernel is the set of x∈Znkx \in \mathbb{Z}_{n}^k such that h=Ax(modn)h = Ax \pmod n.
  • Then we will run LLL() on the matrix generating this kernel in order to look for a small solution (yes, we are looking for a small x\mathbf{x}).
  • Magic (or not…): the small x\mathbf{x} we are looking for will be in the reduced matrix.
  • We brute-force to reverse sanitize(). Done.

If I build the matrix:

L=(A ∣−h ∣ nIk )L = \begin{pmatrix} \mathbf{A}~ \bigg| -\mathbf{h}~ \bigg| ~n\mathbf{I}_{k}~ \end{pmatrix}

We notice, by expanding the matrix product, that with y=(x1x2⋮xl1q1q2⋮ql)y = \begin{pmatrix} x_1 \\ x_2 \\ \vdots \\ x_l \\ 1 \\ q_1 \\ q_2 \\ \vdots \\ q_l \end{pmatrix}, the product Ay=0\mathbf{A} \mathbf{y} = 0 (iff the qiq_i are chosen correctly to remove or add the right number of times the modulus nn).

The vector y\mathbf{y} therefore lies in the kernel of L\mathbf{L}. It now suffices to compute this kernel. Since y\mathbf{y} has a small norm, applying LLL() or BKZ() to the matrix generating this kernel will allow us to recover y\mathbf{y} and therefore x\mathbf{x}.

Here is the implementation of this process in SageMath:

L = block_matrix([A, -h, n*identity_matrix(k)])
kerL = L.right_kernel().matrix()
llled = kerL.LLL()

for idx, vec in enumerate(llled.rows()):
    vec = vec[:l]

    if all([v in range(-beta, beta+1) for v in vec]):
        x = list(vec)
        break

It then suffices to brute-force the missing characters of the flag until the flag, as a bytestring, matches the flag format.

AutHentification 2

This challenge is the same as its first version, with one detail: the tag is now verified.


Let us recall the context:

  1. The cookies are encrypted and authenticated JSON containing two values: “username” and “role”.
  2. The goal is to forge an admin cookie, i.e. encrypt and authenticate the payload: { "username": "toto", "role": "super_admin"}.
  3. The integrated encryption is provided by a Python implementation of AES-GCM 128 with a constant, null IV.
  4. For each (key, IV) pair, we have an encryption oracle over a single JSON payload.

With this context, we quickly deduce that:

  • The encryption is very easy to bypass, see the AutHentification (ou pas...) challenge.
  • For authentication: it is impossible to use the Forbidden Attack (see point 4 above).

The statement and the header of the gcm.py file clearly say that the implementation was made by the intern; let us look for an implementation error. The code is quite long, so we will use a diffing approach. We will compare the functions one by one with the specifications cited in the comment at the top of the gcm.py file: NIST Special Publication 800-38D.


After comparing — algorithm by algorithm — the Python code with the specifications, we realize that in algorithm 5, at line 4, an increment is missing:

# Algorithm 5: GCM-AD_K
def decrypt(self, C, T, A=b""):
    assert len(T) == BLOCK_LEN
    J = self.iv + b"\x00"*(CTR_LEN-1) + b"\x01"
    P = self.gctr(J, C) # Ici, sur le J.

    TT = self.build_tag(C, A, J)
    
    if TT != T:
        return (P, False)

    return (P, True)

This may seem harmless at first glance, but looking more closely at the diagram below, we realize that the nonces of the two GCTR_K calls (the one ensuring confidentiality and the one ensuring authentication) are then identical.

Vulnerable GCM diagram

We deduce the following: (all the following operations take place in F2128/f\mathbb{F}_2^{128}/f, with f=1+α+α2+α7+α128f = 1 + \alpha + \alpha^2 + \alpha^7 + \alpha^{128})

T=Ks+GHASH(A || C || lenA || lenC)T = K_s + \text{GHASH}(A \text{ || } C \text{ || } \text{len}A \text{ || } \text{len}C)

With KsK_s the keystream (we know it; it is the same as the one ensuring confidentiality), AA and CC the AAD and the ciphertext (we will consider each of them to be one block long).

Moreover, according to the definition of GHASH:

T=Ks+AH4+CH3+lenAH2+lenCH1T = K_s + AH^4 + CH^3 + \text{len}AH^2 + \text{len}CH^1

We know KsK_s; it suffices to subtract it and then solve this equation for HH. Once HH is recovered, it becomes trivial to forge a valid tag. A complete solution is implemented in the solve.sage file.

As-tu de bons yeux ?

Some notation

  • [β][\beta]: the set {−β,−β+1,…,0,…,β−1,β}\{-\beta, -\beta+1, \ldots, 0, \ldots, \beta-1, \beta\}.
  • Zn\mathbb{Z}_{n}: the ring of integers modulo nn, also denoted Z/nZ\mathbb{Z}/n\mathbb{Z}.

It is obviously impossible — at least today — to break the (D-)MLWE problem when it uses the level 5 parameters of ML-DSA; it is therefore necessary to find an implementation error.

We can notice that, in the parameters.sage file, the polynomial by which the ring Zq[X]\mathbb{Z}_q[X] is quotiented is X256−1X^{256} - 1 instead of X256+1X^{256} + 1. The former is factorizable in Z\mathbb{Z}. Indeed:

X256−1=(X128+1)(X64+1)(X32+1)(X16+1)(X8+1)(X4+1)(X2+1)(X+1)(X−1)X^{256} - 1 = {\left(X^{128} + 1\right)} {\left(X^{64} + 1\right)} {\left(X^{32} + 1\right)} {\left(X^{16} + 1\right)} {\left(X^{8} + 1\right)} {\left(X^{4} + 1\right)} {\left(X^{2} + 1\right)} {\left(X + 1\right)} {\left(X - 1\right)}

Let f=X256−1f = X^{256} - 1 and g=X−1g = X - 1; we have a ring morphism from Z_q[X]/f\mathbb{Z}\_q[X]/f to Z_q[X]/g\mathbb{Z}\_{q}[X]/g that maps the elements a=∑_i=0256a_iXi∈Z_q[X]a = \sum\_{i = 0}^{256} a\_iX^i \in \mathbb{Z}\_q[X] to a′=∑_i=0256a_i∈Z_q[X]a' = \sum\_{i = 0}^{256} a\_i \in \mathbb{Z}\_q[X] (in short, it is an evaluation at X=1X = 1).

It therefore maps the elements to Z_q\mathbb{Z}\_q (indeed, Z_q[X]/g\mathbb{Z}\_q[X]/g is isomorphic to Z_q\mathbb{Z}\_q); what is nice about this morphism is that it “preserves” roughly the size of the coefficients, meaning that if an element of Z_q[X]/f\mathbb{Z}\_q[X]/f has coefficients in [β][\beta], then, at the output of the morphism, it becomes an element of [256β][256\beta]. Since 256≪q256 \ll q, the element remains “small”.

If we have an instance of D-MLWE: t=As+e\mathbf{t} = \mathbf{A}\mathbf{s} + \mathbf{e}, where t\mathbf{t}, s\mathbf{s} and e\mathbf{e} are vectors of Z_q[X]/f\mathbb{Z}\_q[X]/f and A\mathbf{A} is a matrix over the same set. We can apply this morphism to each of these elements and thus end up with a D-MLWE instance t′=A′s′+e′t' = A's' + e' with very small parameters and in Z_qZ\_q (which is solved very efficiently with LLL()/BKZ()).

Indeed, if we find an s′s' and an e′e' with coefficients in [256β][256\beta], then it is an MLWE instance; otherwise, it is not. Thanks to this strategy, we can solve the challenge! A complete implementation of this solution is available in the solve.sage file.

For more resources on this topic, section 4.5 of the paper Basic Lattice Cryptography - The concepts behind Kyber (ML-KEM) and Dilithium (ML-DSA) by Vadim Lyubashevsky explains this attack very well.

Press Esc to close