DEV Community

Cover image for Rebuilding MuPDF's old PDF key generator in Python: nine bits of randomness behind AES-256
Jose Pollman
Jose Pollman

Posted on Fully Autonomous

Rebuilding MuPDF's old PDF key generator in Python: nine bits of randomness behind AES-256

MuPDF versions before 1.28.5 wrote AES-256 encrypted PDFs whose 256-bit file key depended on nine bits of randomness, so it was cut from one of only 512 possible byte streams. The cipher was fine. The key came from a 48-bit linear congruential generator seeded with the clock, and only nine bits of that seed ever reached the key. Rick de Jager's write-up at V12 (27 October) explains it and covers a few other PDF tools as well. I only checked MuPDF.

I read MuPDF's source at the last affected tag, rebuilt the generator in about twenty lines of Python, and checked it against the example values V12 published. This post is that rebuild, the bit arithmetic behind the number, and a small scanner for the same mistake in Python code.

What MuPDF 1.28.4 actually did

Three files cover it. In source/fitz/context.c at 1.28.4, every new context seeds the generator once:

fz_srand48(ctx, (uint32_t)time(NULL));
Enter fullscreen mode Exit fullscreen mode

source/fitz/random.c is a port of musl's drand48 family, and the function that hands out "random" bytes keeps one byte per call (shown without its CLUSTER test-build branch):

void fz_memrnd(fz_context *ctx, unsigned char *data, int len)
{
    while (len-- > 0)
        *data++ = (unsigned char)fz_lrand48(ctx);
}
Enter fullscreen mode Exit fullscreen mode

And in source/pdf/pdf-crypt.c, revision 6 encryption (the AES-256 one) fills the file key from that function, then draws the password salts right after it:

/* 7.6.4.4.1 states that the file encryption key are 256 random bits. */
fz_memrnd(ctx, crypt->key, nelem(crypt->key));
Enter fullscreen mode Exit fullscreen mode

In pdf-write.c, when the document has no /ID yet, new_identity() draws 32 bytes for it before pdf_new_encrypt() runs. So if nothing else has drawn from the generator in that context, the order is: 32 bytes of /ID, which is stored in the file in plaintext, then 32 bytes of key.

The generator itself is the one POSIX specifies for drand48: X(n+1) = (a * X(n) + c) mod 2^48 with a = 0x5DEECE66D and c = 0xB. srand48() "sets the high-order 32 bits" of the state from the seed, and the low 16 bits "are set to the arbitrary value 330E". lrand48() returns the top 31 bits, which in MuPDF's code is state >> 17.

Rebuilding it in Python

import time

A = 0x5DEECE66D
C = 0xB
MASK48 = (1 << 48) - 1

def srand48(seed):
    # POSIX: high 32 bits of the state come from the seed, low 16 bits are 0x330E
    return ((seed & 0xFFFFFFFF) << 16) | 0x330E

def memrnd(state, n):
    # MuPDF before 1.28.5: one lrand48() per byte, keep the low 8 bits
    out = bytearray()
    for _ in range(n):
        state = (A * state + C) & MASK48
        out.append((state >> 17) & 0xFF)
    return state, bytes(out)
Enter fullscreen mode Exit fullscreen mode

V12's post lists the values from one test document: time(NULL) mod 512 = 80, both halves of /ID, and the recovered key. If the rebuild is right, seeding it with 80 and drawing 64 bytes should reproduce all three:

_, stream = memrnd(srand48(80), 64)
print("ID[0] ", stream[0:16].hex())
print("ID[1] ", stream[16:32].hex())
print("key   ", stream[32:64].hex())
Enter fullscreen mode Exit fullscreen mode
ID[0]  7628523958da31d6c21c90e39344b28e
ID[1]  8be910bddaa1f1a542e5a27ceb115346
key    c3888ecc8ff554ac93acd689da2ecd6223cb760168622e8a0c44a533a037c212
Enter fullscreen mode Exit fullscreen mode

All three match V12's published values byte for byte. That also confirms the draw order I read in pdf-write.c: /ID first, key second.

Why 512 and not 2^32

Run the generator on the current time and on the time 512 seconds later:

now = int(time.time())
_, a = memrnd(srand48(now), 64)
_, b = memrnd(srand48(now + 512), 64)
_, c = memrnd(srand48(now + 1), 64)
print("now            ", now)
print("now vs now+512 ", "identical" if a == b else "different")
print("now vs now+1   ", "identical" if a == c else "different")

day = {memrnd(srand48(now - s), 64)[1] for s in range(86400)}
print("distinct 64-byte streams in the last 24 hours:", len(day))
Enter fullscreen mode Exit fullscreen mode
now             1795077722
now vs now+512  identical
now vs now+1    different
distinct 64-byte streams in the last 24 hours: 512
Enter fullscreen mode Exit fullscreen mode

86,400 seeds produced 512 different streams, and the reason is arithmetic. Multiplication and addition modulo 2^48 carry information from low bits to high bits and never the other way. Bit 24 of the next state depends only on bits 0 to 24 of the current state. Each output byte is bits 17 to 24 of the state, so the top 23 bits of the state can never influence any byte MuPDF writes, no matter how many steps you take.

That leaves bits 0 to 24. Bits 0 to 15 are always 0x330E. Bits 16 to 24 are the lowest nine bits of the timestamp. Nine bits is 512 values, and the timestamp repeats them every 512 seconds, a little over eight and a half minutes.

You can check the claim directly by running the same generator with a 25-bit state:

MASK25 = (1 << 25) - 1

def memrnd_25(state, n):
    # the same generator, but throwing away state bits 25 to 47 at every step
    state &= MASK25
    out = bytearray()
    for _ in range(n):
        state = (A * state + C) & MASK25
        out.append((state >> 17) & 0xFF)
    return bytes(out)

seed = now
full = memrnd(srand48(seed), 100_000)[1]
small = memrnd_25(srand48(seed), 100_000)
print("100,000 bytes, 48-bit state vs 25-bit state:", "identical" if full == small else "different")
Enter fullscreen mode Exit fullscreen mode
100,000 bytes, 48-bit state vs 25-bit state: identical
Enter fullscreen mode Exit fullscreen mode

So the low bits of a power-of-two LCG behave like a smaller generator of their own. POSIX has the output functions copy their bits from the high-order end of the state, and lrand48() does that, returning bits 17 to 47. Then the (unsigned char) cast in fz_memrnd() threw the high bits away again.

The salts make it worse. Reading pdf_compute_user_password_r6(), the validation salt and key salt are drawn straight after the key and copied into bytes 32 to 47 of the /U entry, which sits in the file unencrypted. So even without /ID, the file carries plaintext output from the same stream next to the key. Public values and secret values from one non-cryptographic stream is the second mistake here. It would matter even with a better seed, because an LCG's later output follows from its earlier output.

The fix, and which versions had it

The generator arrived in commit 2d5b4683e912 on 3 January 2018 ("Add portable pseudo-random number generator based on the lrand48 family"), and that same commit added the time(NULL) seed to context.c. The generator code was not changed again until commit b4482e76cd9d on 31 August 2026 replaced it. I did not trace when the AES-256 writing path first called fz_memrnd(), so I can't say which release first produced weak AES-256 files. The release history lists it under 1.28.5 (25 September 2026) as "Bug 709668: Use ChaCha20 cryptographic PRNG for randomness." The new fz_memrnd() seeds ChaCha20 from getentropy() (or CryptGenRandom on Windows) and drops the time(NULL) seed from context.c. 1.28.6 is the current release.

Upgrading fixes new files only. V12 makes the same point: "Previously published copies cannot be retroactively protected." A password-protected PDF that an older MuPDF wrote and that has already been sent somewhere keeps its weak key; re-encrypting your own copy does nothing for the one that left.

Checking your own Python code for the same mistake

The pattern is not specific to PDFs: a fast, seedable generator built for simulations, seeded from the clock, producing something that is supposed to be secret. Python's own random docs carry the warning: "The pseudo-random generators of this module should not be used for security purposes." The secrets docs describe random as "designed for modelling and simulation, not security or cryptography".

Here is a small AST scanner that flags two things: random.seed() with the clock in its arguments, and any random module call inside a function whose name suggests a secret.

import ast
import sys

SECRETY = ("key", "token", "salt", "nonce", "iv", "password", "secret", "otp")
RANDOM_FUNCS = {"random", "randint", "randrange", "getrandbits", "randbytes",
                "choice", "choices", "sample", "shuffle", "seed"}

def scan(path):
    tree = ast.parse(open(path, encoding="utf-8").read(), path)
    for fn in ast.walk(tree):
        if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        risky_name = any(w in fn.name.lower() for w in SECRETY)
        for node in ast.walk(fn):
            if not (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)):
                continue
            mod = node.func.value
            if not (isinstance(mod, ast.Name) and mod.id == "random"):
                continue
            name = node.func.attr
            if name == "seed" and "time" in ast.unparse(node):
                print(f"{path}:{node.lineno}: random.seed() from the clock in {fn.name}()")
            elif name in RANDOM_FUNCS and risky_name:
                print(f"{path}:{node.lineno}: random.{name}() inside {fn.name}(), use secrets")

for p in sys.argv[1:]:
    scan(p)
Enter fullscreen mode Exit fullscreen mode

On a test file with a reset token, an API key, a playlist shuffle and a salt made with secrets.token_bytes(), it flags the first two and leaves the shuffle and the salt alone:

sample_app.py:6: random.seed() from the clock in make_reset_token()
sample_app.py:7: random.getrandbits() inside make_reset_token(), use secrets
sample_app.py:10: random.choice() inside make_api_key(), use secrets
Enter fullscreen mode Exit fullscreen mode

It is a heuristic. It goes by function names and misses from random import choice and secrets made outside a function. Treat a clean run as "nothing obvious", not as proof. The fix for anything it finds is one line: secrets.token_bytes(32), secrets.token_hex(16) or secrets.choice(alphabet). The secrets module uses "the most secure source of randomness that your operating system provides", and there is no seed to get wrong.

What I did not check

I did not build MuPDF or produce an encrypted PDF with it; the match against V12's example values is the only end-to-end confirmation. I read only MuPDF's source. V12's post also describes the key generation in other PDF tools, and I have not checked whether or how those have changed. I am also not publishing anything that reads or decrypts a PDF; the code above only rebuilds the generator.

Written with AI assistance from my own notes and test results. I checked every fact and command before publishing.

Top comments (1)

Collapse
 
alexcodebytes profile image
Oleksandr •

The most interesting part is how the 48-bit state effectively collapses to just nine bits of useful entropy because of the output truncation. The 100,000-byte comparison between the full and 25-bit generators makes that explanation especially convincing. I also like that the post distinguishes fixing the generator from fixing PDFs that were already distributed — upgrading MuPDF can't undo exposure of existing files. The AST scanner is a useful starting point too, as long as its heuristic limitations are kept in mind. A good reminder that AES-256 is only as strong as the key generation behind it.