1. Today's topic

A protected boot and OTA chain:

text
release build
→ firmware signing
→ provisioning
→ ROM bootloader
→ bootloader verification
→ app verification
→ OTA
→ self-test
→ confirm or rollback
→ optional anti-rollback floor

Mechanisms:

MechanismPurpose
Secure BootRuns only authorised code
Flash EncryptionConceals the contents of external Flash
OTA rollbackReturns to a working version after unsuccessful OTA
Anti-rollbackRejects an old, signed but vulnerable version

The main idea: these are different mechanisms, and they must be enabled in the correct order.

2. Why this matters

The device downloads OTA updates through EC25. TLS helps, but risks remain:

text
a compromised server;
a substituted file;
an old, signed but vulnerable version;
physical replacement of SPI Flash;
UART/JTAG access.

Secure Boot authenticates booted code; encryption conceals selected Flash regions; anti-rollback rejects images below a retained security-version threshold. None of these makes application code vulnerability-free.

3. Theory

Secure Boot

The ESP32 chain:

text
ROM -> signed second-stage bootloader -> signed app

The private signing key is not stored on the device. The device stores verification material/a digest. On the classic ESP32, Secure Boot v2 is available only on revision 3.0+. Before provisioning:

text
esptool -p COM7 chip-id
espefuse -p COM7 summary --format json --file efuses-before.json

Flash Encryption

This encrypts external Flash. Production mode must use a unique flash-encryption key for each device. Do not confuse:

text
Secure Boot -> authenticity
Flash Encryption -> confidentiality

Enabling order

With external provisioning, the usual sequence is:

text
Flash Encryption first
Secure Boot second

On the classic ESP32 with Secure Boot v2, read-protect the Flash-encryption key, but keep the Secure Boot public-key digest readable and write-protected. Enabling Secure Boot normally prevents later read-protection changes. Follow exact target instructions for external provisioning; the bootloader’s combined setup has its own ordering. eFuses are irreversible.

OTA rollback

With rollback enabled, a new OTA image is marked ESP_OTA_IMG_NEW; on its first boot, the bootloader changes it to ESP_OTA_IMG_PENDING_VERIFY. After successful validation, confirm it and handle the return status:

c
esp_ota_mark_app_valid_cancel_rollback();

On validation failure, request rollback and handle the result: recovery can fail if no other eligible image exists:

c
esp_ota_mark_app_invalid_rollback_and_reboot();

Anti-rollback

The ESP-IDF API field is secure_version: a security version distinct from the product’s semantic version. Documentation also calls it the security version. The rule:

text
Product version changes frequently.
security_version is increased only to revoke vulnerable releases.

Do not increase the security version in every nightly build.

Self-test

Confirm OTA only after a local self-test:

text
config valid;
critical tasks started;
input service alive;
watchdog supervisor alive;
power stable;
heap budget OK;
hardware revision compatible.

Do not make an unrelated MQTT-broker outage an unconditional reason to roll back locally healthy firmware. Confirmation criteria depend on the update, however: a CA or OTA-channel change may require an authenticated update-server connection before confirmation, or the device can lose future update access. Define a bounded test and recovery policy.

4. Common mistakes

  • Treating Flash Encryption as a replacement for Secure Boot.
  • Treating Secure Boot as a replacement for TLS.
  • Enabling security on the only development board.
  • Not checking the ESP32 revision.
  • Storing the private signing key in Git/CI.
  • Using one Flash Encryption key for the entire fleet.
  • Enabling Secure Boot before writing read-protected keys.
  • Increasing security_version in every release.
  • Increasing the anti-rollback floor before the self-test.
  • Making MQTT a mandatory OTA self-test.
  • Leaving an old, forbidden image in the inactive OTA slot.
  • Repeating eFuse commands without understanding that they are irreversible.

5. A practical task for 30–60 minutes

Without changing eFuses or enabling production security, create SECURITY_LIFECYCLE.md:

text
# Firmware security lifecycle
## Development
- Hardware Secure Boot disabled.
- Flash Encryption disabled or development-only on sacrificial boards.
- Development signing keys only.
- JTAG and UART available.
## Pilot
- Signed OTA images required.
- OTA rollback enabled.
- Hardware security tested only on dedicated boards.
- Production key is not present on developer workstations.
## Production
- Secure Boot enabled.
- Flash Encryption release mode.
- Unique flash-encryption key per device.
- UART/JTAG policy explicitly configured.
- Anti-rollback enabled only after staged fleet validation.
- Provisioning and eFuse reports archived.

Create a test signing key:

text
espsecure generate-signing-key --version 2 --scheme rsa3072 keys/dev_secure_boot.pem

Enable signed applications without hardware Secure Boot:

text
CONFIG_SECURE_SIGNED_APPS_NO_SECURE_BOOT=y
CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT=y
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK=n

Check:

text
idf.py secure-verify-signature --keyfile keys/dev_secure_boot.pem build/app.bin

Change one byte in the binary and confirm that verification fails.

6. What to read or try next

  • ESP-IDF Secure Boot v2, Flash Encryption, OTA Rollback and Anti-rollback.
  • espefuse summary and production provisioning.
  • STM32Trust, X-CUBE-SBSFU and MCUboot/TrustZone for the specific STM32 series.
Which mechanism rejects an old but correctly signed vulnerable firmware version?

Criteria: Match the threat to the mechanism; do not change eFuses in this exercise.

Exercise

Compare OTA rollback with anti-rollback. What should be checked before confirmation, and how does an unrelated MQTT outage differ from losing the authenticated OTA channel?

Self-check criteria: Keep recovery and revocation separate, preserve the local self-test requirement and avoid irreversible provisioning actions.

Show the supplied answer

OTA rollback selects another eligible image after a new-image failure; anti-rollback excludes images below the security threshold. Confirm only after the defined checks. An unrelated broker outage should not automatically invalidate local health, but an OTA-channel change needs verification that authenticated future updates remain possible.