Skip to content

Constant time ​

Beyond "the published code does what the source says", a wallet's cryptography has to be constant time: how long it runs must not depend on its secrets (keys, nonces, PINs, seeds), or timing can leak them. A compiler is free to turn branch-free source into a branch, since C says nothing about time, and it does: see Breaking Bad: How Compilers Break Constant-Time Implementations (ASIA CCS 2025). So this has to be checked on the machine code, and on the published machine code in particular.

cverif does this (its README, "Constant-time code"). For a function marked constant time, with the parameters or objects that hold secrets, it proves on the published bytes that no branch, no memory address and no variable-time instruction operand (division, on cores where it's variable) depends on the secrets. Every helper the function passes secrets to is checked the same way, at any depth. A violation comes with the instruction, the source line, and two values of the secrets that make the code behave differently.

Which functions: the specs in ct/ ​

The vendors' sources don't carry cverif's annotations, so the marks live here, in one spec per source codebase. Trezor One, Trezor core, OneKey and KeepKey all build trezor-crypto, so one curated list serves them all:

speccodebaseused by
ct/trezor-crypto.cttrezor-crypto: ECDSA, Ed25519, HMAC, PBKDF2, AES, ChaCha20-Poly1305Trezor, OneKey, KeepKey
ct/secp256k1-zkp.ctlibsecp256k1 with the zkp modulesTrezor, OneKey
ct/trezor-storage.ctPIN-protected storageTrezor, OneKey

A target lists the specs that apply in CT_SPECS (targets/trezor/common.sh).

# <function>  ct=<secret parameters>   # <names>: <why>
ecdsa_sign_digest   ct=*1      # priv_key: the signing key
consteq             ct=*0,*1   # s1, s2: the constant-time comparison itself
storage_key         secret     # an object whose contents are secret

Parameters are given by position from 0, and *n means the memory parameter n points to. Positions are used rather than names because the names differ between headers, definitions and forks. What isn't marked is public: lengths, curves, public keys, digests to sign, output buffers. Only entry points need a mark: their helpers (bignum arithmetic, field operations, hash compression) are checked for the secrets they receive.

What goes in a spec:

  • functions whose inputs include long-term secrets (private keys, seeds, the PIN and keys derived from it, symmetric keys) or per-operation secrets (nonces);
  • the comparison functions written to be constant time (consteq, secequal, MAC checks);
  • not functions whose inputs are all public (signature verification, address encoding), and not functions that reveal their secret-dependent result by contract (the boolean a PIN check returns is fine; what leads to it isn't).

How marks reach cverif ​

At the verify step, lib/hww/ct.py reads the specs and finds each name in the image (the transplanted ELF's symbol table). It works out from the link map which translation unit the function comes from, and so which flag group verifies it. It then writes verify/group<n>.ct.nm, an nm-format table carrying the marks at the image's addresses, which cverif reads with --symbols next to the ELF (cverif README, "Marks in the symbol table"). Names an image doesn't have (inlined, not linked, another library version) are skipped and listed. cverif rejects a mark it can't apply, such as a parameter the function doesn't have; that mark is dropped, the group re-run, and the drop reported.

Reading the results ​

For each marked function, and each helper given secrets:

claimmeaning
provedequivalent to the source, and constant time on every path, loops by induction
boundedconstant time on the paths explored; not a proof
violationthe published code's timing can depend on the secrets: see the witness
inconclusive, unsupportednot decided

Every violation is triaged in the result's findings.json with a ct-triage finding:

verdictmeaning
timing-leakreal: the published code's timing depends on a secret
declassified-by-designit reveals only what the protocol reveals anyway (whether a PIN was right, whether a nonce must be retried)
spec-too-broadthe mark calls something secret that isn't secret there: fix the spec
cverif-false-alarmthe code is constant time; cverif can't show it yet (goes to cverif-issues.md)

Limits ​

These are cverif's limits; its README has the full list. The timing model is the core's instruction timing. Caches, flash wait states and accelerators (an STM32's ART cache can make table lookups in flash variable), bus contention, power and electromagnetic side channels are out of scope. For this reason a secret-dependent memory address is reported as a violation whatever the hardware does. Pointers that aren't marked are taken not to point into secret memory. memcpy, memmove and memset are trusted by name. Assembly routines are compared instruction by instruction, not analysed for timing.

Results are produced by scripts anyone can re-run. MIT licensed.