Help me build a secure firmware update mechanism where the image is signed with Ed25519…
- User
Help me build a secure firmware update mechanism where the image is signed with Ed25519 and the bootloader verifies against a public key in OTP memory. I need A/B partitioning with a rollback counter to prevent anti-replay attacks from downgrading to vulnerable versions.
- Assistant
Outlining secure firmware update design
I'm detailing a secure update mechanism using Ed25519-signed firmware images, with the bootloader verifying signatures against a public key stored in OTP. The design includes A/B partitions for fallback and a monotonic rollback counter stored securely to prevent downgrades, illustrating key steps and pseudocode for verification and update logic. A fairly standard way to get what you want is to treat the boot ROM/first‑stage bootloader and a few words of immutable storage (OTP fuses) as your root of trust, and then build everything else around signed metadata.
At a high level:
- Generate a long‑term Ed25519 keypair offline and burn the public key into your device's OTP at manufacture time. Lock the OTP so it can't be modified or read out from untrusted code.
- Define an update “manifest” structure that contains at minimum a magic number, the target slot (A/B), a version/rollback counter, the length and hash of the payload image, and an Ed25519 signature over all of that. An example structure:
struct firmware_manifest { uint32_t magic; uint8_t slot; // 0 for A, 1 for B uint32_t version; // monotonically increasing uint32_t image_size; uint8_t image_hash[32]; // SHA‑256 hash of image uint8_t signature[64]; // Ed25519 signature over the preceding fields };- On the build server, compute the hash of the new firmware image, fill in a manifest with the new version number, and sign it with the offline Ed25519 private key. Package
manifest || imagefor distribution. - Partition your flash into two equal‑sized slots (A and B) and a small metadata area. Only ever write updates to the inactive slot.
The first‑stage bootloader then does:
- Read the trusted public key and the current anti‑rollback counter from OTP/secure storage.
- Read the manifests from both slots and verify the signature using the OTP public key. Reject any slot whose manifest fails verification or whose
versionis less than the stored anti‑rollback counter. - Prefer the slot with the highest
versionthat passes signature verification. If the preferred slot fails a hash check or boot health check, fall back to the other valid slot. - Before handing off, update a copy of the anti‑rollback counter in secure storage to the
versionyou are about to boot, and optionally mark the slot as active. Monotonic counters implemented in fuses or a secure element are ideal; if you have to emulate one in flash, include it in the signed manifest to prevent replay.
A very simple C‑like pseudocode for the verification logic using libsodium’s Ed25519 routines might look like:
#include <sodium.h> bool verify_slot(uint8_t slot_index, uint32_t min_version, uint8_t pubkey[32]) { struct firmware_manifest m; read_manifest(slot_index, &m); if (m.magic != EXPECTED_MAGIC) return false; if (m.version < min_version) return false; // verify signature if (crypto_sign_verify_detached(m.signature, (uint8_t*)&m, sizeof(m) - sizeof(m.signature), pubkey) != 0) { return false; } // verify image hash matches uint8_t calc_hash[32]; sha256(slot_image_start(slot_index), m.image_size, calc_hash); if (memcmp(calc_hash, m.image_hash, 32) != 0) return false; return true; } void boot(void) { uint8_t pubkey[32]; uint32_t rollback_ctr; otp_read_pubkey(pubkey); otp_read_rollback_counter(&rollback_ctr); bool slotA_ok = verify_slot(0, rollback_ctr, pubkey); bool slotB_ok = verify_slot(1, rollback_ctr, pubkey); int slot_to_boot = select_best_slot(slotA_ok, slotB_ok); if (slot_to_boot >= 0) { uint32_t new_version = get_manifest(slot_to_boot)->version; if (new_version > rollback_ctr) { otp_update_rollback_counter(new_version); } jump_to_slot(slot_to_boot); } else { fail_safe(); } }On the update side, the application writes the new
manifest || imageinto the inactive slot, then sets a “pending” flag. The bootloader sees the pending flag, verifies the new slot as above, and if boot of the new slot succeeds, clears the flag and updates the rollback counter to the new version. If boot fails, it can revert to the previous slot.A few additional hardening points:
- Make the anti‑rollback counter monotonic and tamper‑resistant. If your hardware supports fuse‑backed counters or a secure element, use that. Otherwise, store the highest‑seen version in a small flash sector protected by its own signature/HMAC.
- Include the rollback version in the signed manifest. That way, an attacker cannot present an old manifest with a higher image version but a vulnerable payload; the version is part of what is authenticated.
- Consider protecting against partial writes/power loss by using slot status flags with CRCs or duplicating manifest headers.
- Keep the Ed25519 private key offline; only the bootloader ever needs the public half.
With this structure you get authenticated updates, A/B redundancy, and a simple anti‑replay mechanism that prevents going back to older, vulnerable images even if they were legitimately signed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.