Post-Quantum JWTs: Measure HTTP Header Budgets Before Changing Your Signing Algorithm
A signed token can be perfectly valid and still fail to reach an application. A reverse proxy might refuse a large request header, a browser might reject a cookie, or an authentication service might have a shorter database field than the new token requires. These problems become more visible when an application moves from conventional digital signatures to post-quantum algorithms.
Our recent introduction to post-quantum signature sizes explains the basic arithmetic. In this guide, we will go one step further: calculate the size of a representative signed JWT, build repeatable transport tests, and identify which component of a real deployment needs attention. No private keys or live credentials are needed.
1. Understand what actually travels over HTTP
A JWT represented as a compact JSON Web Signature (JWS) contains three dot-separated Base64url segments:
protected-header.payload.signature
The header describes the signing algorithm and can identify the signing key. The payload contains the claims. The third segment contains a digital signature, not a copy of the private key. A signed JWT provides integrity and authentication when verified correctly; it does not hide the claims from someone who can read the token.
NIST FIPS 204 specifies ML-DSA, and RFC 9964 defines its representation in JOSE and COSE. The existence of a specification does not guarantee that your particular issuer, verifier or identity provider supports it.
The signature has a fixed length for each parameter set. The header and payload vary with the application’s actual serialized data.
| Algorithm | Raw signature bytes | Unpadded Base64url characters |
|---|---|---|
| ML-DSA-44 | 2,420 | 3,227 |
| ML-DSA-65 | 3,309 | 4,412 |
| ML-DSA-87 | 4,627 | 6,170 |
Base64url encodes bytes into printable characters. Unlike padded Base64, the compact JWS representation omits trailing = padding. For a byte count n, its encoded length is ceil(n * 4 / 3).
A 4,096-character allowance for the whole token is therefore already too small for an ML-DSA-65 signature, even before adding the other two segments. But this is just a hypothetical field allowance, not a universal HTTP or cookie limit.
2. Build a reusable sizing function
Use Node.js 22 or later for the following examples. Create token-budget.mjs and add the signature table and two helpers first.
import { Buffer } from "node:buffer";
// RFC 9964 / FIPS 204: detached signature sizes in raw bytes.
export const signatureSizes = Object.freeze({
"ML-DSA-44": 2420,
"ML-DSA-65": 3309,
"ML-DSA-87": 4627
});
// Base64url without padding: three input bytes become four characters.
export function encodedLength(bytes) {
if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > 1e9) {
throw new RangeError("Expected a byte count from 0 to 1 billion");
}
return Math.ceil(bytes * 4 / 3);
}
// Serialize exactly once, then count the UTF-8 bytes that will travel.
function jsonSegment(value) {
return Buffer.from(JSON.stringify(value), "utf8").toString("base64url");
}
The JSON helper measures UTF-8 rather than JavaScript’s UTF-16 code units. For example, an accented letter can use more than one UTF-8 byte; a string’s .length is not a reliable byte count before encoding. Our UTF-8 troubleshooting guide explains why.
Next, append the function that assembles the budget. It does not create or sign a token. It only measures the expected serialization.
export function measureToken(alg, claims, limit = 4096) {
if (!Object.hasOwn(signatureSizes, alg)) {
throw new Error("Unsupported sizing parameter set");
}
if (!Number.isSafeInteger(limit) || limit < 0) {
throw new RangeError("Invalid field capacity");
}
// Model a protected header. A production library may add more fields.
const header = jsonSegment({ alg, typ: "JWT", kid: "demo-key-01" });
const payload = jsonSegment(claims);
const signature = encodedLength(signatureSizes[alg]);
// Two ASCII dots separate the three Base64url segments.
const total = header.length + payload.length + signature + 2;
return {
alg, header: header.length, payload: payload.length, signature,
total, remaining: limit - total, fits: total <= limit
};
}
The result distinguishes the signature from the other components. A negative remaining value indicates that the modeled field is too small. Notice that the algorithm name and key identifier are included in the header calculation: they take up space too.
3. Compare a realistic set of claims
Create a second file, compare-tokens.mjs, next to the first one. Use the same claims for all three parameter sets so that the comparison remains meaningful.
import { signatureSizes, measureToken } from "./token-budget.mjs";
// Fixed synthetic claims make comparisons reproducible.
const claims = {
sub: "user-123", iss: "https://example.test",
aud: "demo-api", exp: 1800000000, name: "Jos\u00e9",
roles: ["reader"]
};
console.table(Object.keys(signatureSizes).map(alg => measureToken(alg, claims)));
Run node compare-tokens.mjs. You will get a table showing each segment, the combined size and the remaining room. The name in this example is written as a Unicode escape in the source; JavaScript turns it into the actual character before JSON serialization.
For the sample above, the results are:
| Algorithm | Header | Payload | Signature | Total | Remaining from 4,096 |
|---|---|---|---|---|---|
| ML-DSA-44 | 68 | 154 | 3,227 | 3,451 | 645 |
| ML-DSA-65 | 68 | 154 | 4,412 | 4,636 | -540 |
| ML-DSA-87 | 68 | 154 | 6,170 | 6,394 | -2,298 |
Replace these synthetic claims with a representative non-sensitive claim set from your own application. Compare a minimal user account and an account with many permissions. Add any header fields your identity service actually uses. If your library serializes a different representation, measure its final output instead of assuming this model is exact.
Use the JSON Formatter and Validator to inspect a sanitized example of the header and payload. Pretty-printing is useful for inspection, but do not assume the pretty-printed size is the size of the compact representation.
4. Separate the token budget from the request budget
An Authorization header contains a scheme as well as a token. In HTTP/1.1, a line such as Authorization: Bearer ... also has a field name, punctuation and a line terminator. HTTP/2 and HTTP/3 represent headers differently and may compress them, yet endpoints and intermediaries can still apply limits to the decoded header section.
A cookie has other costs: its name, attributes on the response that sets it, and other cookies on subsequent requests. Browsers and servers may impose different limits. A token that fits an individual application field may still fail when sent with analytics, language and session cookies.
Treat every limit as a separate question:
- What is the maximum stored value in the issuer or session database?
- What is the client or browser willing to store and transmit?
- What are the individual field and aggregate header limits at the CDN, reverse proxy and origin?
- Does an authentication library impose an independent token size limit?
The smallest effective limit on the actual request path determines what works. Avoid solving a storage problem by indiscriminately raising server limits, which can increase resource consumption and abuse risk.
5. Exercise the transport path without generating credentials
Use a diagnostic route on your own staging environment that returns the byte count of a custom test header without logging its value. Never run these tests against infrastructure you do not control. The following POSIX shell commands use synthetic ASCII data only.
# Make a 5,000-character value without a trailing newline.
probe=$(node -e 'process.stdout.write("A".repeat(5000))')
# Replace the URL with your own staging diagnostic endpoint.
curl --silent --show-error \
--write-out '\nHTTP %{http_code}\n' \
--header "X-Size-Probe: $probe" \
https://staging.example.test/header-check
A successful response should report 5,000 bytes. Repeat with 2,000, 4,000 and 6,500 characters, provided those values are within the approved test range. Use the cURL command line builder to assemble equivalent requests when changing methods or headers.
A 431 response can identify a request-header capacity problem. Some products instead return a different error or terminate the connection. Correlate the response with staging logs, and compare the public gateway path against an authorized direct-origin test.
The custom header probe measures transport capacity only. It does not test browser cookie acceptance, Authorization-specific policies, or cryptographic verification. For those, use short-lived credentials issued by a compatible library in a dedicated test environment. Verify both successful authentication and rejection of altered tokens. Never make an authentication endpoint accept fake signatures for the sake of a test.
6. Decide whether the token belongs in the browser at all
For a traditional server-rendered website, an opaque session identifier can keep browser-side credentials small while the server stores session state. The browser storage and cookies guide explains this architecture and its trade-offs. A distributed API may need signed tokens for independent verification, which makes the decision different.
If a protocol supports binary signed objects, COSE can avoid the signature’s Base64url expansion. It is not a drop-in replacement for JWT: both ends must support the new format, including its key representation and validation rules. Detached signatures can also be appropriate for documents or software packages, not necessarily for bearer authentication.
For larger deployments, estimate the aggregate cost as well. If each of 100,000 requests carries an extra 4,000 bytes of uncompressed token data, that is 400 MB before protocol overhead or compression. The File Transfer Time Calculator can help translate a data volume into a rough transfer time. This is not a latency benchmark; connection reuse, header compression and server verification time affect the result.
Do not select a lower security parameter set solely because it fits. The required security level, interoperability profile, key rotation, verification performance and deployment policy all matter. Record these constraints before changing the algorithm.
Start with a measurable budget
An effective migration plan has separate checks for serialized size, transport compatibility and signature verification. It also tests worst-case claims and the oldest supported client. A small sizing script can make the first check automatic in continuous integration, while integration tests cover the rest.
Before issuing a new token, try the Post-quantum size calculator with the algorithm and capacity your protocol actually specifies. It is a useful way to spot an impossible field budget before changing your authentication stack.