Choosing and enforcing algorithms
Every JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. header names an algorithm, but the receiver decides which algorithms it accepts. This lesson looks at the algorithm families a header can name, the two ways a receiver that follows the header gets fooled, and how a service changes algorithms without refusing its own tokens.
The algorithm is the receiver's choice
Digital signatures set the rule: alg is a hint, and the receiver decides for itself which algorithms it accepts. That rule is worth following through, because a surprising number of JWT libraries once did the opposite. They read alg from the header and verified with whatever it named, which let the sender of the tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. choose how it would be checked.
Deciding in advance means writing the decision down per issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts. and per kind of token. Lab Photos signs its access tokens with ES256, so the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests.'s configuration says: tokens from https://auth.photos.example are checked with ES256 and nothing else. When a token arrives, its alg is compared with that setting before any key is chosen. A token that names a different algorithm is refused at that point, even if the API's library could verify it.
An algorithm name is not just another way of saying "signed". It names one specific operation on one specific kind of key. ES256 and RS256 are different mathematics on different keys, and HS256, as the next section shows, isn't a public-key signature at all.
The families
JOSE algorithms fall into two groups. A MAC, a message authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do. code, uses one shared secret: the sender and receiver hold the same key, and either can both make and check a tag. A signature uses a key pairA private key and a public key generated together and mathematically linked. The private key creates signatures and the public key checks them, but the private key cannot practically be worked out from the public one.: the issuer signs with its private keyThe half of a key pair that its owner keeps to themselves and uses to create signatures or decrypt data. Anyone who obtains it can act as its owner., and anyone with the public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt. can check a signature but can't make one. The number in each name, such as the 256 in ES256, is the size of the SHA-2 hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a hash. the algorithm uses.
| Name | Kind | Key | Notes |
|---|---|---|---|
HS256 | MAC (HMAC) | One shared secret | Fits two systems that share a secret. Wrong for an issuer whose tokens many receivers check, because every receiver could then make tokens too. |
RS256 | Signature (RSA) | RSA key pair, 2048 bits or more | The long-standing default, using RSA's original signing scheme: the same claims always give the same signature. |
PS256 | Signature (RSA) | RSA key pair, 2048 bits or more | RSA with its newer signing scheme, which mixes a fresh random value into every signature and has a stronger security proof. Preferred over RS256 for new RSA systems. |
ES256 | Signature (ECDSA) | Elliptic-curve key pair on the P-256 curve | Much smaller keys and signatures than RSA for the same strength. ES384 and ES512 use larger curves. |
EdDSA | Signature (Ed25519) | Edwards-curve key pair | Modern and fast, with one fixed curve. It does its own hashing inside the algorithm. |
A public key published for RS256 or ES256 can be copied by anyone, because it is meant to be public. That is safe for as long as receivers use it only to check signatures with the algorithm it was made for.
The dangerous ones: none and confusion
JOSE defines one more value, alg: none, for a token with no signature at all. It exists for the rare design where an outer layer already protects the token. A receiver that follows the header and accepts none believes any claims anyone sends it, because there is nothing to check. Unless a receiver is built specifically for unsigned tokens, it refuses none outright.
The second trap is called algorithm confusion. Lab Photos publishes its public key so that receivers can check signatures. Now imagine a receiver whose library verifies with "the issuer's key" using whatever alg the header names. A token that says HS256 makes that library treat the published public key as an HMAC shared secret. Since the public key is public, whoever made the token could compute a matching HMAC tag over claims of their choosing, and the receiver would accept them. A receiver that has pinned ES256 for this issuer never gets that far: it refuses HS256 at the algorithm check.
The same check settles the other cases a receiver meets. A token signed correctly with RS256 is refused by a receiver that allows only ES256, even though nothing is wrong with the signature. A token that names an allowed algorithm but a kid the issuer's key setA JSON document, published at a fixed HTTPS address, that lists an issuer's current public keys so others can verify its signatures. It never contains private keys. doesn't hold is refused at the key check, because the receiver uses only keys it already trusts for that issuer.
What the token asks for
- ES256, trusted kid
- Allowed algorithm and a known key
- RS256
- A correct signature, wrong algorithm
- HS256
- Public key used as a shared secret
- ES256, unknown kid
- Allowed algorithm, key not in the set
Allow only ES256 for this issuer
- Accepted
- Refused at the algorithm check
- Refused at the algorithm check: stops confusion
- Refused at the key check
The receiver checks its own rule before it looks for a key, so the token never chooses how it is checked.
RFC 8725, the JWT best-practices document, names both traps and gives one defense for both: never let the token choose the algorithm.
One key, one algorithm
That defense is an allowlist. For each issuer and each kind of token, the receiver lists the exact algorithms it accepts and refuses everything else, none included, before it looks at a key or a claim. An API that expects ES256 lists ES256 and nothing more. One allowed algorithm is better than several, because it leaves no room for confusion.
RFC 8725 adds a companion rule: use each key with exactly one algorithm. A receiver that records "this key is an ES256 signing key" can't be talked into using it any other way. Keys in a published key set can carry an alg member saying what they are for, and a careful receiver honours it.
Different kinds of token need separate rules too. An ID tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs., an access token and a signed request may each come with a different expected issuer, audience, typ and key. Each is validated under its own policy, and a token that passes the wrong policy is still the wrong token, however genuine its signature.
Changing algorithms safely
Algorithms are occasionally replaced, when a key type is upgraded or an older scheme is retired. A receiver pinned to the old algorithm refuses new tokens the moment the issuer switches, so receivers are prepared first. The issuer publishes the new key. Each receiver adds the new algorithm to its allowlist beside the old one. Once new tokens verify everywhere, the issuer starts signing with the new key, and after the last old token has expired, the old algorithm is removed from every allowlist.
What a receiver must never do is widen its allowlist to rescue one late consumer, or check a refused token again under looser rules. Either one hands the choice of algorithm back to the incoming token, the opening that none and algorithm confusion exploit.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Match an allowed algorithm to the trusted keyCompare a real ES256 token with its key, let the case receiver refuse a validly signed RS256 sample and a wrong-purpose one, then check four prepared bad samples against your own verifier's allowlist.