Try Embedded

Signed firmware and the chain of trust

Hashes, HMACs and signatures, a root of trust, key management, encryption and anti-rollback versions.

What a signature proves

An update path is an open door into the device: whoever can send it an image can run code on it. Integrity checks (a CRC, a hash) prove an image was not damaged in transit; they prove nothing about who made it, because an attacker computes a new CRC as easily as you do. Authenticity needs a secret only the manufacturer has, and a check the device can make without that secret: a digital signature.

  1. At release, the build server computes the image's hash (SHA-256: 32 bytes that change completely if one bit of the image does).
  2. It signs the hash with the manufacturer's private key (ECDSA P-256, Ed25519 or RSA), producing a 64- to 256-byte signature, attached to the image.
  3. The device holds only the public key. It recomputes the hash of what it received and verifies the signature against it. Only someone with the private key can produce a signature that verifies.

The private key never leaves the signing server (ideally a hardware security module); the public key in the device can be read by anyone, because it can only check, not sign. That asymmetry is the point. A shared secret (an HMAC key) also authenticates, but every device then holds the key that makes valid images, and extracting it from one device opens them all.

#include <stdio.h>
#include <stdint.h>
#include <string.h>

/* Why a CRC is not a signature: anyone who changes the image computes the new CRC. */
static uint32_t crc32(const uint8_t *p, unsigned n) {
  uint32_t crc = 0xFFFFFFFFu;
  while (n--) { crc ^= *p++; for (int i = 0; i < 8; i++) crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u))); }
  return ~crc;
}

int main(void) {
  uint8_t image[16];
  memcpy(image, "unlock_if_pin_ok", 16);
  uint32_t shipped = crc32(image, 16);
  memcpy(image, "unlock_always!!!", 16);           /* the attacker's change */
  uint32_t forged = crc32(image, 16);               /* and the attacker's new CRC */
  printf("shipped crc %08X, forged image's crc %08X: a device checking only the CRC accepts it\n", (unsigned)shipped, (unsigned)forged);
  return 0;
}

The chain of trust

A signature check is only as trustworthy as the code that performs it. If an attacker can replace the bootloader, they replace the check. So secure boot is a chain: each stage verifies the next before running it, back to a first stage that cannot be changed.

boot ROM (in silicon, cannot be written)  ─ verifies ─▶  bootloader  ─ verifies ─▶  application
   holds the hash of the public key               holds the public key
   (in one-time-programmable fuses)               (or its hash)
  • The root of trust is immutable: a ROM in the chip (NXP's HAB, ESP32's secure boot V2, STM32's RSS or the SBSFU's locked bootloader), or a bootloader in flash made unwritable by the chip's write protection and readout protection.
  • The key's identity is in fuses or option bytes the attacker cannot change: usually a hash of the public key, so the image can carry the key itself and the ROM checks that it is the right one.
  • Each stage verifies before it jumps, and on failure does not run the image: it falls back to the other slot, or to a recovery mode that only accepts a signed image.

Devices without a ROM bootloader that verifies (most small MCUs) build the chain from the flash bootloader, and its protection becomes the critical part: readout protection (RDP level 1 or 2 on an STM32) so the flash cannot be read or changed through the debug port, write protection of the bootloader's sectors, and on chips that have them, secure areas that hide the bootloader's code once it has run.

Keys

The cryptography is rarely what fails; the keys are.

  • The private key lives in a hardware security module or at least an offline signing machine; builds request signatures, nobody copies the key. A leaked signing key cannot be revoked from devices that trust only it, so plan for several keys (the ROM of many chips accepts a list) and a way to revoke one.
  • Development and production keys are different, and a production device never accepts a development-signed image. Ship the wrong key and every device in the field trusts the developers' laptops.
  • Secrets in the device (a device's own identity key, a key for decrypting images) belong in the chip's protected key storage, a secure element, or at least an area readout protection covers. A key in plain flash is a key in every firmware dump; the firmware-analysis lesson shows how quickly one is found.

Encryption is a separate question

A signature stops anyone from changing the firmware; it does not stop them reading it. Encrypting the image (AES with a key in the device's protected storage) hides the code in transit and in the update file, which protects the manufacturer's IP and makes finding vulnerabilities harder. But the device must decrypt it, so the key is in every device: encryption raises the cost of analysis, while signing is what keeps control of the device. Do both in that order of importance: sign always, encrypt when the code is worth hiding.

Versions: the rollback attack

A valid signature says the manufacturer made the image, not that it is the current one. An attacker who keeps an old, signed image with a known vulnerability can install it and then exploit it. Anti-rollback stops that: each image carries a security version, the device keeps the lowest version it still accepts in storage the attacker cannot lower (a monotonic counter, OTP fuses blown one per version, or a protected flash record), and the bootloader refuses an image below it. The counter goes up only after the new image has proved itself, which is the next lesson's subject.

Going deeper

Interview question: What is the difference between a CRC, a hash, an HMAC and a signature for firmware images?

Answer: A CRC detects accidental corruption and is trivial to recompute, so it gives no security. A cryptographic hash such as SHA-256 detects any change, but anyone can compute the hash of their own image, so on its own it also proves only integrity. An HMAC uses a secret key, so only key holders can produce a valid tag, but the verifying device must hold the same key, and extracting it from one device lets an attacker sign images for all. A digital signature uses a private key to sign and a public key to verify, so the device holds only the public key, which cannot create signatures; that is what secure boot uses.

Interview question: Explain a chain of trust for secure boot on a microcontroller.

Answer: Each boot stage verifies the next before transferring control, starting from an immutable root of trust. The first stage is code that cannot be modified, a boot ROM in silicon or a bootloader in flash locked by write protection and readout protection, and it holds the public key or a hash of it in one-time-programmable memory or protected option bytes. It verifies the signature of the next stage, the bootloader or the application, over a hash of the image, and runs it only if the check passes; that stage may in turn verify further images. On failure the device does not run the image but falls back to a known-good one or a recovery mode that accepts only signed images. Debug access must be locked too, or the chain can be bypassed by writing flash directly.

Interview question: What is a rollback attack, and how is it prevented?

Answer: An attacker installs an older firmware image that is genuinely signed but has a known vulnerability, then exploits that vulnerability. Because the signature is valid, signing alone does not stop it. Prevention is a security version in each image and an anti-rollback counter in the device, kept in storage the attacker cannot decrease, such as OTP fuses or a monotonic counter in protected memory. The bootloader refuses images whose version is below the counter, and the counter is raised only after a new image has been confirmed to work, so a failed update can still fall back.

Explain it: In your own words, explain why a device can safely contain the public key that checks its firmware, but not the key that signs it.

Answer: The public key can only verify: given an image and a signature it says whether the signature was made with the matching private key, but nothing about it helps make a new signature. Anyone reading it from a firmware dump learns nothing that lets them sign their own image. The private key makes signatures, so a device holding it, or holding a shared secret as with an HMAC, gives anyone who extracts it from one device the power to create firmware that every device accepts. Keeping the signing key off the device, on a protected signing server, is what lets the verification key be public.