← Back to section · Tutorials & Labs

Error-Handling Discipline for liboqs: Status Codes, Exceptions, and the Failures That Stay Silent

A systematic tour of OQS_STATUS return values in the C API, the two mechanism exceptions in the Python bindings, and the most dangerous failure class of all — decapsulation and verification paths that return wrong results without any error.

Published 22 November 2026 · 6 min read · PQ Frontier

Error-Handling Discipline for liboqs: Status Codes, Exceptions, and the Failures That Stay Silent
AI-generated site artwork
Video explainer · PQC programming: calling real libraries

Error handling in cryptographic code differs fundamentally from ordinary business code. Ordinary failures tend to crash, throw, or at least leave a trace in the logs. The most dangerous failures in cryptography are quiet: the function reports success, the program carries on — it has simply computed the wrong secret. The liboqs C API and its Python bindings each have their own error-reporting conventions; this tutorial organizes them systematically, with emphasis on which failures make noise, which do not, and how to defend against each.

The C API: everything rides on OQS_STATUS

C functions in liboqs cannot use exceptions (C has none), so all results travel through return values. Algorithm functions return OQS_STATUS: OQS_SUCCESS for success, OQS_ERROR for failure. There is one rule, and it must be followed without exception: check the return value of every single call, including OQS_KEM_keypair, which “obviously can’t fail” — it can, when the random source misbehaves or memory runs out, and marching on with an uninitialized public key is far worse than exiting on the spot.

Two other return types are not OQS_STATUS; do not confuse them. Constructors such as OQS_KEM_new and OQS_SIG_new return pointers and signal failure with NULL — test for null, not for a status code. Query functions such as OQS_KEM_alg_is_enabled return an integer, 1 for enabled and 0 for not enabled. Writing these three checking styles into your team’s coding conventions eliminates a large fraction of beginner mistakes.

Cleanup on error paths has a fixed shape too: funnel everything through a single cleanup exit (the classic goto cleanup pattern), so that no matter which step failed, every allocated buffer and the algorithm object are released, with secret keys and shared secrets wiped before release. The worst pattern is duplicating release code in every failure branch — with enough branches, one path always gets forgotten.

The Python bindings: two mechanism exceptions, two different problems

Constructing objects in the oqs module can raise two exceptions, and telling them apart matters for debugging. oqs.MechanismNotSupportedError means the algorithm name does not exist in liboqs at all — usually a typo such as MLKEM-768 instead of ML-KEM-768, or an algorithm this version has not yet adopted. oqs.MechanismNotEnabledError means the algorithm exists but was not compiled into your particular liboqs build — it is supported but not enabled, and you need to rebuild liboqs or switch to a distribution that includes it.

The corresponding defence is to move the check to program start-up: use oqs.get_enabled_kem_mechanisms() and oqs.get_supported_kem_mechanisms() to distinguish “supported but not enabled” from “not supported at all” and report it clearly, instead of tripping over the exception deep inside a business path. Handle the two exceptions differently when you catch them, too: a bad name is a programming bug that should surface during testing; a disabled mechanism is a deployment problem, and the message should say that liboqs must be rebuilt or replaced.

The silent failures: implicit rejection and verify returning False

Now for the most important class. In both of the following situations the library “returns normally” — and the result is wrong:

First, decapsulation of a tampered ciphertext. ML-KEM’s implicit-rejection design means decap_secret (and OQS_KEM_decaps on the C side) never errors on a bad ciphertext; it returns a “fake” shared secret derived from the secret key and the ciphertext hash. Attackers cannot use your behavior to tell valid ciphertexts from invalid ones — but neither can you tell from the return value that the exchange has failed. The defence lives at the protocol layer: after deriving keys from the shared secret, perform key confirmation (for example, exchange MACs computed under the session key), and drop the whole session if confirmation fails.

Second, signature verification failure. oqs.Signature.verify returns False for an invalid signature instead of raising. That is a reasonable API design, but it means any code that calls verify without checking the return value has effectively not verified anything. The defence is, again, discipline: make “the return value of verify must feed a conditional” a mandatory item on your code-review checklist.

Seen together, the conclusion is the same: in a cryptographic API, “no exception” proves only that the flow ran to completion, not that the result is correct. Confirming correctness is always the caller’s job.

Logging discipline: what to record and what never to record

Half of error handling lives in the logs. Record what diagnosis requires: the algorithm name and parameter set, which step failed, the returned status code or exception type, and a context identifier for the call (such as a request ID). Never record any key material itself — not one byte of a secret key, shared secret, seed or internal random state. Logs have a far wider readership, retention period and backup footprint than key storage; writing a secret into a log disperses it across an entire system you do not control. By the same token, ciphertexts and signatures may be logged as lengths and hash fingerprints for reconciliation, but do not dump full ciphertexts with their context for convenience. One more detail: do not echo detailed errors back to an untrusted peer. Internal logs may be verbose, but externally returned errors should stay vague — “which step failed and why” handed to an attacker becomes a debugging interface for them, and many error oracles in cryptographic protocols grew out of exactly such helpful error messages.

Common mistakes checklist

  1. Checking only the last step’s return value. Uninitialized buffers left by an earlier failed step get consumed as legitimate input by later steps, burying the error until it explodes somewhere deeper.
  2. Lumping the two mechanism exceptions together. A typo’d name and a disabled build are entirely different problems; treating them as one sends debugging in the wrong direction from the start.
  3. Forgetting to wipe sensitive buffers on failure branches. Error paths are the least audited code in most projects; secret keys lingering in freed memory are a real leakage surface.
  4. Taking “no exception” as “negotiation succeeded”. Under implicit rejection, the key-confirmation step is not optional.
  5. Retrying a failed library call unchanged. Failures like bad parameters or disabled algorithms will never succeed on retry; classify first, then fix the code, fix the deployment, or abort.

Summary

Error handling for liboqs comes in three layers: at entry, check availability and classify exception types; during execution, check every return value step by step; at the protocol layer, explicitly confirm against the silent failures. Only with all three layers in place is cryptographic code genuinely robust. As a follow-up exercise, retrofit the C program from the first tutorial with a complete goto cleanup structure, then feed it a tampered ciphertext and watch how — without key confirmation — it “successfully” computes the wrong secret. That experiment tends to make implicit rejection unforgettable.

Tags: liboqsError HandlingProgrammingRobustness