Skip to content
Back to CurriculumLesson 18 of 30
jwt-tokensAdvanced

Asymmetric Digital Signatures (RS256/ES256) vs Shared Secrets (HS256) & Algorithm Confusion

HS256 uses a symmetric shared secret where every party that verifies a token can also forge one. RS256/ES256 use asymmetric cryptography where only the authorization server holds the private key to sign, while resource servers use the public key to verify. Defenses must prevent Algorithm Confusion (substituting public keys as HMAC secrets) and 'alg: none' attacks.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explainerjwt-tokens
Asymmetric Digital Signatures (RS256/ES256) vs Shared Secrets (HS256) & Algorithm Confusion
Steps:
Private Key Signing (IdP)
IdP signs token using RSA 2048-bit Private Key stored in HSM/KMS. Private key never leaves the identity server.
Public Key Verification (Microservices)
100+ microservices verify the signature using the cached Public Key from JWKS with 0 database roundtrips.
Simulate Payload Tampering (role: user → admin):
Step 1 of 6
Auto-stepping (1x)

Step 1: Auth server generates an RSA-2048 or ECDSA P-256 key pair.

Security Guarantee: Always use asymmetric algorithms (RS256, ES256) for multi-service or third-party token verification.
Why This Architectural Concept Exists

In a microservice ecosystem of 30 services, distributing a shared symmetric secret (HS256) means any compromised service can forge tokens for the entire company. Asymmetric keys decouple signing from verification. However, naive validation code can be tricked into verifying RS256 tokens as HS256 using the public key as the secret.

Intuitive Mental Model

The Royal Signet Ring vs The Shared Passphrase: With HS256, all 100 border guards know the royal secret passphrase; any rogue guard can whisper it to forge entrance passes. With RS256, only the King holds the golden signet ring (private key), while every guard carries an open plaster imprint (public key) to check seals.

Architectural System TopologyProtocol Diagram
[ Attacker Payload: alg: "HS256" ]
              │
              ▼ (Attacker signs token with IdP's PUBLIC RSA KEY as HMAC secret!)
[ Vulnerable Resource Server ]
       │
       ├── Reads header: alg = "HS256"
       ├── Naive code: secret = getPublicKey()
       ├── Verifies HMAC(secret, Header.Payload) ──► FORGERY ACCEPTED! (CRITICAL BREACH)
       │
[ Hardened CosmosApp Resource Server ]
       │
       ├── Enforces STRICT whitelist: algorithms = ['RS256']
       ├── Header says "HS256" ──► REJECT IMMEDIATELY (Algorithm Mismatch!)
CosmosApp Production Milestone

CosmosApp migrates its microservices to RS256. To defend against the famous 'alg: none' and Algorithm Confusion attacks, CosmosApp hardcodes an algorithm whitelist on every API gateway. If an attacker submits a token signed with HS256 using CosmosApp's public certificate as an HMAC key, the gateway rejects the token before executing any verification math.

Step-by-Step Execution Sequence
01

Step 1: Auth server generates an RSA-2048 or ECDSA P-256 key pair.

02

Step 2: Private key is stored strictly inside AWS KMS or HashiCorp Vault; public key is published on JWKS.

03

Step 3: Auth server signs JWTs using the private key, setting header alg='RS256' and kid='key_1'.

04

Step 4: Resource server receives token and checks that alg exactly matches the expected algorithm in its whitelist.

05

Step 5: Resource server fetches the public key corresponding to kid and verifies the signature.

06

Step 6: If alg does not match the whitelist or signature verification fails, returns HTTP 401.

Production Architecture Blueprint
import jwt from 'jsonwebtoken';

// CosmosApp RS256 Token Verification (Immunized against Algorithm Confusion):
export function verifyRs256Token(token: string, rsaPublicKey: string) {
  return jwt.verify(token, rsaPublicKey, {
    // CRITICAL: Force RS256! Never allow dynamic alg selection from header!
    algorithms: ['RS256'],
    issuer: 'https://auth.cosmos.app',
    audience: 'https://api.cosmos.app',
  });
}
Common Architectural Misconceptions
  • ✕Trusting the 'alg' header in the token to decide which verification algorithm to use (The token's header is controlled by the untrusted caller; the server must dictate allowed algorithms).
  • ✕HS256 is obsolete for all use cases (HS256 is appropriate for single monolith architectures where only one server ever creates and verifies tokens).

Key Architectural Takeaways

  • ✓Always use asymmetric algorithms (RS256, ES256) for multi-service or third-party token verification.
  • ✓Never allow the token's header 'alg' parameter to dictate the cryptographic algorithm used by the server.
  • ✓Explicitly reject tokens specifying 'alg: none' or unexpected symmetric algorithms.
Common Architectural Pitfall

Accepting whatever algorithm is specified in the JWT header without an explicit whitelist, enabling Algorithm Confusion attacks.

Production Best Practice Fix

Always pass an explicit algorithms: ['RS256'] option to the verification function.

Interactive Knowledge Check

How does an Algorithm Confusion attack exploit an asymmetric JWT implementation?