Skip to content
← All posts
· Delta1 Labs SecurityKeyright

Don't trust the algorithm a license declares

A signed license token that carries its own algorithm field invites the classic algorithm-confusion attack: flip it to "none", or to HMAC keyed on your own public key, and a verifier that dispatches on that field waves the forgery through. This deep-dive shows the exact forge against an Ed25519 token, the two lines of C# that make it work, and why a correct verifier pins the algorithm at deploy time and never reads the one the token claims.

A license token is often shaped like a JWT: a header, a payload, a signature, each base64url-encoded and joined with dots. The header names the algorithm — {"alg":"Ed25519"} — so the verifier knows what to run. It reads clean, it is self-describing, and it carries the single most dangerous idea in token security. The algorithm field travels with the token, which means it is controlled by whoever holds the token, which means an attacker gets to choose which cryptographic check your client runs. This is the algorithm-confusion family of attacks, and it has quietly broken real licensing code the same way it broke early JWT libraries.

Key takeaway: The algorithm used to verify a signature is a property of your deployment, not of the token in front of you. A correct verifier pins the algorithm and the public key at build or deploy time and never dispatches on the alg the token claims. The attack works precisely because the naive verifier does dispatch on it — flip the field to none and the signature check is skipped, or flip it to HMAC and the attacker signs with your own public key, which is not a secret. The fix is to delete the branch that reads the field.

The real question: who picks the algorithm?

Consider a verifier that supports three signature algorithms because the product has shipped a few over the years. It reads the header, switches on the name, and calls the matching check:

// VULNERABLE: the token chooses which check runs.
public static bool VerifyNaive(LicenseToken token, VerificationKeys keys)
{
    string signingInput = token.HeaderB64 + "." + token.PayloadB64;
    return token.Header.Alg switch
    {
        "none"    => true,                                        // skips verification entirely
        "HS256"   => HmacMatches(signingInput, token.Signature,   // symmetric: key is the "secret"
                                 keys.Ed25519PublicKey),
        "ES256"   => EcdsaVerify(signingInput, token.Signature, keys.EcdsaPublicKey),
        "Ed25519" => Ed25519Verify(signingInput, token.Signature, keys.Ed25519PublicKey),
        _         => false,
    };
}

Every line here looks defensive. There is a whitelist of algorithms, an explicit default => false, real signature verification for the asymmetric cases. And it is forgeable two different ways by anyone who has ever received one valid token and can read the public key — which, in an offline-first client, is everyone, because the public key is embedded in the binary so the app can verify without a server.

Forge one: alg: none

The none branch exists because somewhere, sometime, an unsigned token was convenient for a test fixture. The attacker takes a genuine token, base64url-decodes the header, rewrites it to {"alg":"none"}, re-encodes, and leaves the payload set to whatever they want — "tier":"enterprise", an expiry a decade out. The signature segment can be empty. VerifyNaive sees none, returns true, and the forged payload is trusted. No cryptography was defeated because none was run.

Forge two: key confusion

The second forge is the subtle one, and it is the reason “just remove the none branch” is not enough. The attacker declares a symmetric algorithm — HS256 — and exploits the asymmetry the verifier forgot about. An Ed25519 public key is public: it is in the client, so a legitimate offline check can run. HMAC-SHA256 takes a shared secret. The naive verifier, having no HS256 key of its own, passes the only key it has — the Ed25519 public key — into HmacMatches. So the attacker computes the same HMAC over their chosen signing input using those exact public-key bytes as the secret, and the verifier recomputes an identical tag and accepts.

Same forged token — two verifiersforged token{"alg":"HS256"}tier=enterpriseVULNERABLE: dispatch on algswitch (token.Header.Alg)"HS256" → HMAC keyed on Ed25519 PUBLIC keypublic key is not secret → attacker knows itrecomputed tag matchesACCEPTED — forgery trustedPINNED: deployment chooses algEd25519.Verify(pinnedPubKey, input, sig)token.Header.Alg is never read for dispatchHMAC tag is not a valid Ed25519 signatureREJECTED — forgery stoppedThe only difference is whether attacker-controlled data selects the code path.
One forged token, two verifiers. The vulnerable path lets the token's own alg field choose the check; the pinned path fixes the algorithm at deploy time and ignores what the token claims.

The forge is a few lines of the attacker’s own code:

// Attacker side — needs only the public key shipped in the client.
string header  = Base64Url(Encoding.UTF8.GetBytes("{\"alg\":\"HS256\"}"));
string payload = Base64Url(Encoding.UTF8.GetBytes("{\"tier\":\"enterprise\",\"exp\":\"2036-01-01\"}"));
string signingInput = header + "." + payload;

using var h = new HMACSHA256(ed25519PublicKeyBytes);           // public key AS the HMAC secret
byte[] tag = h.ComputeHash(Encoding.ASCII.GetBytes(signingInput));
string forged = signingInput + "." + Base64Url(tag);           // verifies against VerifyNaive

Nothing exotic happened. The attacker never saw the private key. They leaned on the fact that the verifier let the token nominate a symmetric scheme and then handed it the one key material lying around.

The fix: the verifier owns the algorithm

The correct verifier does not have a switch. It knows, as a fact of this deployment, that licenses are Ed25519-signed with one pinned public key, and it runs exactly that check:

// SAFE: the deployment pins the algorithm; the token has no say.
public sealed class LicenseVerifier
{
    private readonly PublicKey _pinned;   // Ed25519, loaded once at startup

    public LicenseVerifier(ReadOnlySpan<byte> ed25519PublicKey) =>
        _pinned = PublicKey.Import(SignatureAlgorithm.Ed25519,
                                   ed25519PublicKey, KeyBlobFormat.RawPublicKey);

    public bool Verify(LicenseToken token)
    {
        byte[] input = Encoding.ASCII.GetBytes(token.HeaderB64 + "." + token.PayloadB64);
        // token.Header.Alg is NEVER consulted to choose a path.
        return SignatureAlgorithm.Ed25519.Verify(_pinned, input, token.Signature);
    }
}

(The API above is NSec.Cryptography, which Keyright uses for Ed25519 on frameworks where System.Security.Cryptography does not yet expose it.) A forged alg: none token has no valid Ed25519 signature, so Verify returns false. The HMAC forge produces 32 bytes that are not a 64-byte Ed25519 signature over the input, so it fails too. Neither forge survives, because neither one can make the pinned check pass without the private key.

If you want to keep the alg field for diagnostics or a migration, read it only after a pinned verifier has already validated the signature, and use it solely to reject a mismatch:

if (!verifier.Verify(token)) return LicenseResult.Invalid;
if (token.Header.Alg != "Ed25519") return LicenseResult.Invalid;   // defence in depth, post-verify

That ordering is the whole discipline: cryptography first, with an algorithm you chose; the token’s self-description is never trusted to steer the check.

How this applies in Keyright

Keyright’s SDK verifier is pinned by construction. You hand it the public key you got when you created the product, and it only ever runs the algorithm that key is. There is no configuration switch that reads an algorithm out of the token, so neither forge above has a surface to land on. For teams that carried a homegrown scheme first, this is the single most common vulnerability we see in migration audits — a JWT library configured to accept “any supported algorithm,” left on by default.

If you are running more than one algorithm during a key rotation, you pin both and accept a token that either pinned verifier validates. That is still the deployment choosing its trusted set, not the token:

bool ok = _current.Verify(token) || _previousDuringOverlap.Verify(token);

The rotation overlap itself — minting under the new key while still accepting the old — is covered in the signing-key rotation post; the point here is only that it never means reading the algorithm off the wire.

Caveats and the honest limits

This is not a replacement for the rest of verification. Pinning the algorithm stops a forged signature. It does nothing about replay of a genuine token, a revoked key still being accepted, or an expired license whose exp you forgot to check. Signature pinning is one gate; revocation and expiry are others.

Constant-time comparison still matters for the symmetric paths you keep. If you legitimately verify an HMAC somewhere — an inbound billing webhook, say — compare tags with CryptographicOperations.FixedTimeEquals, not == or SequenceEqual, so a byte-by-byte timing leak does not hand an attacker the tag. That is a separate bug from algorithm confusion but it lives in the same code.

A pinned verifier cannot protect a client that has been patched. If an attacker rewrites your Verify method to return true, no algorithm discipline helps — that is a tampering problem, not a signature problem, and the answer is to protect the check itself. Pairing Keyright with Nebula.NET obfuscation and anti-tamper is how you stop the verifier from being the patch point.

Takeaway

A self-describing token is a convenience that quietly delegates a security decision to whoever holds the token. The algorithm used to verify a signature belongs to your deployment, not to the bytes on the wire: pin it, run it, and never let a switch read the alg the token claims — that one branch is what turns a public key into a forgery tool and an alg: none into a skeleton pass. Keyright ships a verifier that is pinned by construction, so the branch does not exist to be exploited; the Community Edition is free and self-hosted, which means you can mint a key, inspect the token format, and try to forge against your own client end to end before you trust it in production. The full SDK surface is in the Keyright docs.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.