Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Choosing and enforcing algorithms

Every JWT 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 token choose how it would be checked.

Deciding in advance means writing the decision down per issuer and per kind of token. Lab Photos signs its access tokens with ES256, so the photo API'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 authentication 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 pair: the issuer signs with its private key, and anyone with the public key 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 hash the algorithm uses.

The JOSE algorithms in common use
NameKindKeyNotes
HS256MAC (HMAC)One shared secretFits two systems that share a secret. Wrong for an issuer whose tokens many receivers check, because every receiver could then make tokens too.
RS256Signature (RSA)RSA key pair, 2048 bits or moreThe long-standing default, using RSA's original signing scheme: the same claims always give the same signature.
PS256Signature (RSA)RSA key pair, 2048 bits or moreRSA 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.
ES256Signature (ECDSA)Elliptic-curve key pair on the P-256 curveMuch smaller keys and signatures than RSA for the same strength. ES384 and ES512 use larger curves.
EdDSASignature (Ed25519)Edwards-curve key pairModern 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 set 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.

The photo API compares each header with its own rule before it looks for a key.

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 token, 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.

A service publishes an RSA public key for RS256. An attacker sends a token with alg HS256, using that public key's text as the HMAC secret. What stops the forgery? 1 of 3

Choose one answer.

A verifier that is not built to expect unsigned tokens receives one with alg set to none. What should it do? 2 of 3

Choose one answer.

Lab Photos is replacing RS256 with ES256 for its access tokens, and the photo API currently allows only RS256. Which order avoids refused tokens? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity