Secure Firmware Updates and Signing
Every finding in the previous lessons ends with "ship a fix", and a fix is only as good as the update mechanism that delivers it. The update channel is also the most powerful attack path into a fleet: whoever can make devices install their image owns all of them at once. A secure update system must therefore answer five questions for every update, on the device, before it writes a byte to flash: who made it, is it for me, is it current, is it newer, and is it intact? This lesson builds an update client that asks them, then looks at the frameworks and key handling that make the answers trustworthy.
What can go wrong
| Attack | What the attacker does | What stops it |
|---|---|---|
| malicious image | serves their own firmware (compromised server, DNS hijack, a machine in the middle, a USB stick) | a signature over the update, checked on the device with a key the device already trusts |
| tampered image | changes a genuine image in transit or on a mirror | the signed manifest carries the image's hash and size |
| rollback | serves an old, genuinely signed image with a known vulnerability | a version check against the installed version and an anti-rollback counter in tamper-resistant storage |
| wrong target | serves a genuine image for another model, bricking or weakening the device | the manifest names the device class |
| freeze / replay of metadata | keeps serving yesterday's "no update available", hiding a security fix | expiry dates on signed metadata |
| key compromise | steals the signing key | keys in HSMs, separate roles and thresholds, key rotation built into the design |
| bricking | power loss or a bad image mid-update | A/B slots: write the inactive slot, switch only after a successful boot |
TLS on the download protects the transport and is good defence in depth, but it is not the root of trust: it proves you reached a server, not that the server's file is genuine (TLS limits).
An update client in Python
The vendor signs a small JSON manifest with Ed25519 (Ed25519): device class, version, image size, image SHA-256 and an expiry date. The device knows the vendor's public key (in firmware, itself protected by secure boot) and an anti-rollback floor. The key comes from a fixed label so the output is stable; a real signing key is generated in and never leaves an HSM.
import hashlib, json
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.exceptions import InvalidSignature
UPDATE_KEY = Ed25519PrivateKey.from_private_bytes(hashlib.sha256(b"labhome update key").digest()) # lab key; real: HSM
DEVICE = {"class": "labhome-hub-v2", "installed": (1, 4, 2), "rollback_floor": (1, 4, 0), # floor kept in OTP/RPMB
"trusted_key": UPDATE_KEY.public_key(), "now": "2026-03-01"}
def publish(version, image, device_class="labhome-hub-v2", expires="2026-06-30", key=UPDATE_KEY):
manifest = {"class": device_class, "version": version, "size": len(image),
"sha256": hashlib.sha256(image).hexdigest(), "expires": expires}
blob = json.dumps(manifest, sort_keys=True).encode()
return blob, key.sign(blob), image
def install(blob, sig, image, dev=DEVICE):
try:
dev["trusted_key"].verify(sig, blob) # 1. who made this manifest?
except InvalidSignature:
return "REJECT: manifest signature invalid"
m = json.loads(blob)
v = tuple(m["version"])
if m["class"] != dev["class"]: # 2. is it for this kind of device?
return f"REJECT: built for {m['class']}"
if m["expires"] < dev["now"]: # 3. is it fresh? (stops freeze/replay of old metadata)
return "REJECT: manifest expired"
if v <= dev["installed"] or v < dev["rollback_floor"]: # 4. is it newer? (stops rollback)
return f"REJECT: version {v} is not newer than {dev['installed']}"
if len(image) != m["size"] or hashlib.sha256(image).hexdigest() != m["sha256"]: # 5. is it the right image?
return "REJECT: image doesn't match the signed hash"
return f"INSTALL {v} into the inactive slot, reboot, keep the old slot until it boots"
new = b"LHFW firmware 1.4.3 (telnet removed)"
cases = {
"genuine 1.4.3": publish([1, 4, 3], new),
"1.4.3, image swapped in transit": publish([1, 4, 3], new)[:2] + (new + b" + backdoor",),
"genuine but old 1.3.9": publish([1, 3, 9], b"LHFW firmware 1.3.9 (has the telnet bug)"),
"genuine 1.5.0 for another model": publish([1, 5, 0], new, device_class="labhome-plug-v1"),
"genuine 1.4.3, stale manifest": publish([1, 4, 3], new, expires="2026-01-31"),
"1.9.9 signed by another key": publish([1, 9, 9], new, key=Ed25519PrivateKey.from_private_bytes(bytes(32))),
}
for name, (blob, sig, image) in cases.items():
print(f"{name:33} -> {install(blob, sig, image)}")genuine 1.4.3 -> INSTALL (1, 4, 3) into the inactive slot, reboot, keep the old slot until it boots
1.4.3, image swapped in transit -> REJECT: image doesn't match the signed hash
genuine but old 1.3.9 -> REJECT: version (1, 3, 9) is not newer than (1, 4, 2)
genuine 1.5.0 for another model -> REJECT: built for labhome-plug-v1
genuine 1.4.3, stale manifest -> REJECT: manifest expired
1.9.9 signed by another key -> REJECT: manifest signature invalid
Each rejection matches one row of the table. Note the order: the signature is checked first, before any field of the manifest is trusted. A parser that reads the version before checking the signature is an attack surface of its own, which is why many update bugs live in manifest parsing.
The anti-rollback floor is stored outside the image (fuses, a replay-protected memory block or a secure element) and raised when a version fixes a serious vulnerability, so that even a valid signature on 1.3.9 can never get it installed again.
Frameworks instead of home-made designs
- The Update Framework (TUF): separate signed roles for the root of trust, the targets (what to install), snapshots and timestamps (freshness), threshold signatures and built-in key rotation; designed so that compromising one key or the repository doesn't compromise clients.
- Uptane: TUF adapted to vehicles, with a director repository that tells each car which images to install.
- IETF SUIT (RFC 9019 architecture, RFC 9124 information model): a compact signed manifest format for constrained devices, carrying exactly the fields used above plus instructions for installation.
- Platform updaters such as Mender, SWUpdate, RAUC and Android's A/B updates implement signed images and A/B slots for embedded Linux.
Using one of them is almost always better than designing your own manifest, but the five questions are a good checklist for reviewing whichever one a product uses.
Signing keys
The update key is the key to the fleet. Practice from code signing applies (code signing, supply-chain security):
- generated and used inside an HSM or a cloud KMS; signing through an audited pipeline, never on a developer laptop;
- separate keys for development, test and production, and per product line, so a leaked test key can't sign production images;
- threshold or role separation (TUF) and a rotation plan that devices understand before you need it;
- signatures over the exact bytes that devices install, produced by a reproducible build when possible.
How update weaknesses are detected
- Lab tests on your own devices: serve an unsigned image, a modified image, an older signed image, an image for another model, and stale metadata from a test server; every one must be rejected.
- Review of the update client code: verification before parsing, constant comparison of hashes, no "skip verification" debug flags left on, no fallback to an unsigned path when the signed one fails.
- Key management audit: where the private key lives, who can sign, whether test and production keys differ.
- Fleet telemetry: version distribution (devices stuck on old versions are exposed), update failures and rollbacks.
How it is fixed
- Sign manifests and images, verify on the device before installing; check class, freshness, version and hash.
- Anti-rollback counters in tamper-resistant storage; raise the floor after security fixes.
- A/B slots with automatic fallback; health checks after boot before committing.
- Automatic updates by default, with a published support period (the law).
- HSM-held keys, separated by environment, with rotation designed in.
Pitfalls
- Verifying the signature of the manifest but not that the downloaded image matches its hash.
- "Downgrade for support reasons" left open in production.
- Signing with a key stored in the CI system's plain environment variables.
Practice
1. Add a case where the attacker reuses the genuine 1.4.3 manifest and signature but changes expires to 2027 in the JSON. What happens and why?
REJECT: manifest signature invalid. The signature covers the exact manifest bytes, so changing any field, including the expiry, invalidates it.
2. Why must the rollback floor live outside the firmware image?
If it were inside the image, installing an old image would also install the old, lower floor. It has to be stored in fuses, replay-protected memory or a secure element that the old image can't lower.
3. The vendor's update server uses HTTPS. A manager asks whether signing images is still necessary. Answer in two sentences.
Yes: HTTPS proves the device reached the vendor's server, not that the file on it is genuine, and it doesn't protect against a compromised server, mirror or build system. A signature checked by the device proves the image came from the vendor's signing key, wherever it was downloaded.
Test yourself
What stops a genuinely signed but old, vulnerable image from being installed?
- TLS
- A version check and an anti-rollback counter
- A CRC
- The device class
Why does the client verify the signature before reading any manifest field?
- Speed
- Unverified fields can't be trusted, and parsing attacker data before verification is an attack surface
- JSON requires it
- To save memory
What does an expiry date on signed metadata prevent?
- Bricking
- Freeze attacks that keep serving old 'no update' metadata
- Key theft
- Wrong device class
What do A/B update slots protect against?
- Malicious images
- Bricking from power loss or a bad image
- Rollback
- Key compromise