Skip to content

Methodology ​

The question ​

A hardware wallet runs a firmware image signed by its vendor. The vendor publishes source code and says the image was built from it. The question this project answers, for each published image, is:

Does the machine code in the image do what the published source says, and how much of the image is that statement true for?

The usual answer is a reproducible build: anyone can build the tagged source in the vendor's build environment and compare hashes. That is necessary, and it's the first thing we check, but it isn't sufficient:

  • it trusts the compiler and the toolchain in the build environment (a compromised or buggy compiler reproduces perfectly),
  • it trusts binary inputs in the tree (precompiled libraries, generated tables, blobs) as much as source,
  • it says nothing when the build does not reproduce, which is common.

cverif answers the question directly. Given the C source and the machine code, it proves for each function that the code computes exactly what the source computes, for every input: same return values, same memory writes, same calls with the same arguments, same peripheral accesses in the same order. It does not run or trust a compiler. What it can't prove is reported as such (BOUNDED, UNSUPPORTED), never assumed; a difference is reported (MISMATCH) with a concrete input that shows it.

So one proof answers two questions: is this image the published code? and did the compiler keep the code's meaning? A miscompiled function, a function a compromised toolchain patched, and one changed in the image after the build all fail it alike. For cryptographic code, a third question matters as much: did the compiler keep it constant time? C says nothing about timing, so an optimiser may turn branch-free code into a branch on a key. For the functions listed in ct/, cverif proves on the published bytes that timing doesn't depend on the secrets (constant-time.md).

The chain of evidence ​

For one release, each step produces a fact, and the report states each one:

#StepFact establishedRelies on
1fetchthe image is the one the vendor distributes (URL, sha256), and its header's hashes cover its codethe vendor's official channel (HTTPS)
2sourcethe source checked out is the commit the vendor names for the releasegit, the vendor's metadata
3buildthe vendor's reproducible build of that commit gives the published image, signatures asidethe vendor's build script, Docker
4extractthe code bytes verified are exactly the published image minus its headerthis repository's image parser
5verifyeach PROVED function in the published code computes what its source computescverif, the symbol table as a hypothesis (below)
6reportwhich bytes of the image and which source lines are covered by proofscverif's coverage accounting

Step 5 is run on the published bytes, not on the local build. The local build contributes only two things: the symbol table (where each function starts, from the link map) and the source bundle (the exact files and flags the compiler saw). Neither is trusted for the result:

  • A symbol table that names the wrong function at an address makes that function's proof fail. It can't make wrong code pass. A wrong size shows up as unproved bytes or a failed proof.
  • A source bundle that is not what the vendor compiled (wrong flags, wrong generated header) makes proofs fail. It can't make code pass that doesn't compute what the bundle says, and the bundle is published with its sha256 manifest so anyone can read exactly what was proved against.

So a PROVED function is a statement about the published image and the published source (plus generated files, which are listed), and nothing else.

Source bundles ​

The rebuild runs in the vendor's own build container with two wrappers first on PATH (arm-none-eabi-gcc, arm-none-eabi-ar; see lib/container/ccwrap.sh). They log each call (working directory, arguments) and run the real tool with the same arguments. The rebuild must come out byte-identical to the official build, or the step fails.

lib/container/export_bundle.py then:

  1. finds the logged link that produced the firmware ELF and runs it again with -Wl,-Map into a scratch file, which must be identical to the ELF;
  2. reads from the map every object and archive member that was linked in, and matches each to the compile call that produced it (through the logged ar calls for archive members). Members of the toolchain's own libraries (newlib, libgcc) have no source in the tree and are listed as such;
  3. reruns the preprocessor's dependency listing (-M) for each linked translation unit with its own flags, and copies every file read (sources, headers, generated headers, and the toolchain's headers) to root/<original absolute path>;
  4. preprocesses .S files with their own flags (cverif reads assembly as written).

The bundle's manifest.tsv (sha256 per file) goes into the results.

Flag groups ​

cverif takes one set of -I/-D options for all the sources of a run, but firmware builds give some translation units flags of their own (a vendored library with its own configuration, for example). lib/hww/verify.py groups the units by the options that change how a source reads (-I, -D, -U, -include, -fshort-enums, -fno-strict-aliasing, plus the toolchain's default include directories), runs cverif once per group on the same image and symbol table, and merges:

  • functions: each binary function is verified in the group that holds its source; if two groups give different results for one function, the report shows both;
  • binary coverage: union of the byte ranges proved in any group, over the union of the ranges counted in any group.

Options that only change code generation (-O, -ftrivial-auto-var-init, ...) are not part of the grouping: cverif reads the source as C and checks whatever code the compiler produced. It derives the target's predefined macros from the image itself.

What the results say, and don't say ​

  • PROVED: for every input, the function's machine code in the published image does what its source does. Proofs are modular: a call to another function of the image is checked as a call (same callee, same arguments, same memory), and the callee is proved separately. The report says when a proof rests on a callee that isn't proved. Loops are proved by induction. Termination is not proved.
  • BOUNDED: no difference on any path explored within the budget. Not a proof.
  • UNSUPPORTED / INCONCLUSIVE: cverif can't decide (a construct it doesn't model). The bytes count as unverified.
  • MISMATCH: the code provably differs from the source, with a counterexample. Each one has to be looked at: it may be a miscompilation, a source/binary discrepancy, a false alarm (a cverif limit, which is a bug in cverif to fix), or a construct where C leaves the behaviour open.
  • binary coverage: the share of the image's code bytes accounted for by proofs (instructions on proved paths, literal pools and tables they read, constants equal to the source's). The rest is unverified: functions not proved, code without source (toolchain libraries), and anything nothing reaches.
  • initialised data: objects the source defines are compared with the image.

Out of scope (each is noted in the per-vendor documents where it applies):

  • code that isn't C compiled for Arm Thumb: Rust, MicroPython bytecode, code for other processors (secure elements, radio chips), encrypted blobs;
  • the bootloader and boardloader, unless the vendor publishes their images (they are separate targets when they are);
  • signatures: we check that the vendor's header commits to the code (chunk hashes) and that the image is the one distributed. We don't yet check the signature cryptographically against the vendor's keys;
  • side channels (cverif can prove constant-time properties of annotated code, which is future work here), hardware, and anything about the device beyond its firmware image.

Reproducing a result ​

Every result directory records the inputs (image URL and sha256, source repository, tag and commit, cverif release, flags of every group). bin/hww run <target> <version> redoes it. cverif's results are deterministic for a given release and work limit: --work-limit counts work, not time, so they don't depend on the machine.

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