Choose encryption algorithms

Pick the alg that delivers the content key to your recipient and the enc that locks the content, and know what each needs and what is refused.

Every JWE header names two algorithms. alg (key management) is how the content key reaches the recipient: wrapped with a key you share, locked with their public key, derived from a password, or no wrapping at all. enc (content encryption) is how the payload itself is locked with that content key. The first depends on who opens the token; the second you can almost always leave at the default, A256GCM.

#Pick alg by who opens the token

Your situationPickKey
Your own server encrypts and decrypts (cookies, state, caches)A256KW, or dirgenerateJWK("A256KW"), or generateJWK("A256GCM") for dir
Many senders, one recipient who publishes a public keyECDH-ES+A256KWgenerateJWK("ECDH-ES+A256KW")
The recipient's system only speaks RSARSA-OAEP-256generateJWK("RSA-OAEP-256")
A person types the secretPBES2-HS256+A128KW (or a stronger PBES2)the password string
One ciphertext for several recipientsECDH-ES+A*KW, RSA-OAEP* or A*KW per recipientsee Encrypt for several recipients
  • dir is the shortest token: your key is the content key, so there's no encrypted key segment. It suits a key that never leaves your servers.
  • ECDH-ES+A256KW derives a fresh wrapping key per token from an ephemeral key pair; keys are small and X25519 works too (generateJWK("ECDH-ES+A256KW", { namedCurve: "X25519" })). Bare ECDH-ES uses the derived key directly, for one recipient only.
  • RSA-OAEP (SHA-1 inside OAEP) exists for systems that only speak it. Pick RSA-OAEP-256 for new keys.
  • PBES2 is only as strong as the password. Prefer a generated key whenever no person has to type it.

#Key management algorithms

algKeyEncrypted key segment
dirthe content key itself, an oct key of the enc sizeempty
A128KW, A192KW, A256KWAES key of 16, 24 or 32 bytescontent key plus 8 bytes
A128GCMKW, A192GCMKW, A256GCMKWAES key of 16, 24 or 32 bytes; adds iv and tagcontent key length
RSA-OAEP, RSA-OAEP-256, RSA-OAEP-384, RSA-OAEP-512RSA key pair, modulus of 2048 bits or moremodulus length
ECDH-ESEC (P-256, P-384, P-521) or X25519 key pair; adds epkempty
ECDH-ES+A128KW, ECDH-ES+A192KW, ECDH-ES+A256KWsame as ECDH-EScontent key plus 8 bytes
PBES2-HS256+A128KW, PBES2-HS384+A192KW, PBES2-HS512+A256KWa password string; adds p2s and p2ccontent key plus 8 bytes

PBES2 counts and salts:

Parameterencrypt defaultencrypt acceptsdecrypt accepts by default
p2c (count)600,0001000 to 600,0001000 to 600,000 (minIterations, maxIterations)
p2s (salt)16 random bytes8 bytes or more8 bytes or more

#Content encryption algorithms

encContent keyConstruction
A128GCM16 bytesAES-GCM
A192GCM24 bytesAES-GCM
A256GCM32 bytesAES-GCM, the default
A128CBC-HS25632 bytesAES-128-CBC with HMAC-SHA-256
A192CBC-HS38448 bytesAES-192-CBC with HMAC-SHA-384
A256CBC-HS51264 bytesAES-256-CBC with HMAC-SHA-512

Keep A256GCM unless a recipient requires another. The A*CBC-HS* family exists for systems that predate GCM. The 192-bit variants need a runtime whose Web Crypto supports 192-bit AES keys, which Chromium-based browsers don't. A dir key's size follows the enc it serves: generateJWK("A128CBC-HS256") makes 32 bytes.

#What is refused

  • none as alg: ERR_JWE_ALG_NOT_ALLOWED, before any key is read.
  • RSA1_5 (RSA PKCS#1 v1.5 encryption), open to padding-oracle attacks: ERR_JWE_ALG_NOT_ALLOWED ("Key management algorithm not supported").
  • Any other enc, such as A256CTR: ERR_JWE_INVALID.
  • zip compression, on both sides.
  • X448 keys, and RSA keys under 2048 bits, at generation and use.
  • dir and bare ECDH-ES in a multi-recipient object.
  • Generating a dir or PBES2 key: generateJWK throws ERR_JWK_UNSUPPORTED. Generate a content key (generateJWK("A256GCM")) for dir, and pass a password string for PBES2.

#Going further