Securing a device: TLS, secrets and secure boot
About this lesson
A device on a network can be attacked in three ways: someone reads or fakes its traffic, someone holding the board copies its flash, or someone loads their own firmware. Follow an ESP32 sensor node as it opens a TLS connection to its MQTT broker on port 8883: it checks the broker's certificate against the one CA it trusts, agrees a key without ever sending it, and seals every message. Then protect the chip itself: flash encryption (AES-256, key in an eFuse) hides the Wi-Fi password from a flash dump, and secure boot (3072-bit RSA signatures) refuses code you did not sign. The Networking lessons “IoT: MQTT and constrained devices” and “How HTTPS works” and the Security lessons “Certificates and PKI” and “Digital signatures” explain the protocols and the maths; here you use them on an ESP32. Secure boot also checks the images from “Over-the-air updates and rollback”.
Three ways in
A device on a network can be attacked three ways. Someone reads or fakes its traffic. Someone holding the board copies its memory, passwords and all. Someone loads their own program onto it. Each needs its own defence.
threat defence read or fake the traffic -> TLS copy the flash memory -> flash encryption run their own firmware -> secure boot
Read the lesson step by step
Three ways in
A device on a network can be attacked three ways. Someone reads or fakes its traffic. Someone holding the board copies its memory, passwords and all. Someone loads their own program onto it. Each needs its own defence.
threat defence read or fake the traffic -> TLS copy the flash memory -> flash encryption run their own firmware -> secure bootPlain MQTT is readable
Plain MQTT on port 1883 sends bytes exactly as written. Anyone on the path can read the reading, 21.5, and the password inside the CONNECT packet. They can also pretend to be the broker. MQTT leaves security to you.
port 1883 MQTT without TLS PUBLISH room/temp "21.5" readable by anyone on the path CONNECT user + password sent as they are and nothing proves the server is the real brokerCarry one trusted certificate
TLS fixes both problems. First the device needs something to trust. setCACert gives it a root certificate: the public key of the authority, or CA, that signed the broker's certificate. It ships inside the firmware.
line 1 WiFiClientSecure net; a TCP connection wrapped in TLS line 2 net.setCACert(ROOT_CA); the CA certificate the device trusts certificate a name + a public key, signed by a CA never call net.setInsecure(): it skips every checkThe broker shows its certificate
connect opens a connection to port 8883 and starts the TLS handshake. The device says hello, and the broker answers with its certificate: its name, its public key and a CA's signature.
line 4 mqtt.setServer(HOST, 8883); line 5 mqtt.connect(ID, USER, PASS); device -> ClientHello broker -> ServerHello, Certificate, CertificateVerify (CertificateVerify: signed with the broker's private key)The device checks it
The device checks the CA's signature with the root certificate it carries, and that the name matches the host it asked for. The broker also signs the handshake with its private key. An impostor fails here and the connection stops.
check 1 CA signature on the certificate valid check 2 name in it = broker.example.com yes check 3 dates in range check 4 broker signed this handshake so it holds the private key any check fails -> connect() returns falseAgree a key, never send it
Now both sides make a shared secret key. Each keeps a secret number and sends a public one: 4 one way, 10 the other. Each mixes its secret with what it got, and both arrive at 18. The key itself never crossed.
toy Diffie-Hellman: p = 23, g = 5 device secret a = 4 sends g^a mod p = 4 broker secret b = 3 sends g^b mod p = 10 device: 10^4 mod 23 = 18 broker: 4^3 mod 23 = 18 the same key real TLS: elliptic curves and far larger numbersPublish, sealed
Everything after the handshake, the password and the 21.5 included, is encrypted with that key and carries a check value. The listener copies only scrambled bytes, and any change they make is detected.
line 6 mqtt.publish(TOPIC, "21.5"); on the air scrambled bytes + a check value confidentiality only the two ends can read it integrity a changed byte is detectedSecrets sit in flash
The network is safe, but the board is not. The Wi-Fi password, "kite42", is compiled into the app in flash. Anyone with the board and a USB cable can copy the whole flash with esptool and read it.
const char* WIFI_PASS = "kite42"; lands in the app image esptool -p PORT -b 460800 read-flash 0 ALL flash_contents.bin -> the dump contains "kite42" as plain bytesFlash encryption
Flash encryption scrambles the app with AES-256, using a 256-bit key in an eFuse: a one-time bit inside the chip. Software cannot read that key back. The chip decrypts as it runs, but a copied dump is noise.
cipher AES-256, key in eFuse BLK1 (flash_encryption) key read- and write-protected: not readable by software covers bootloader, partition table, otadata, every app dump "kite42" now reads as 9f3ac1...Secure boot checks the code
Secure boot stops new code. You sign each image with your private key. The chip keeps only a SHA-256 digest of your public key in eFuse. At power-on it checks the signatures and refuses to run an attacker's unsigned app.
signature RSA-PSS, 3072-bit RSA key, SHA-256 eFuse BLK2 SHA-256 digest of your public key ROM checks the bootloader; the bootloader checks the app no valid signature -> the boot stopsShip it locked
Together: TLS guards the traffic, flash encryption the secrets, secure boot the code. Over-the-air updates are checked against the same signature. In release mode JTAG and UART download are off, and eFuses can never be unburned.
TLS traffic: private, unaltered, real broker flash encryption secrets in flash secure boot only signed code runs, OTA images too release mode JTAG off, UART download off eFuse bits 0 -> 1 only: test on a spare board first
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Your ESP32 publishes over plain MQTT on port 1883. What can someone on the same network path do?
Plain MQTT sends its bytes as written. The MQTT 3.1.1 standard leaves security to the implementer and warns that clear-text credentials allow man-in-the-middle and replay attacks. TLS is registered on port 8883.
02What does net.setCACert(ROOT_CA) let the device do?
When it connects, the client uses the CA certificate to verify the certificate the server presents. setInsecure() would skip that check, and then an impostor could pose as the broker.
03In the toy key exchange (p = 23, g = 5) the device sends 4 and the broker sends 10. The device's secret is 4. What key does it compute?
10^4 mod 23 = 18. The broker computes 4^3 mod 23 = 18, the same number, and 18 itself never crossed the network.
04An attacker copies the whole flash of an ESP32 with flash encryption enabled. What do they get?
The app, bootloader and partition table are encrypted with AES-256. The key lives in eFuse BLK1, read- and write-protected, so a raw dump does not reveal the contents. Note that the plain NVS partition is not covered: it needs NVS encryption.
05With Secure Boot v2 enabled, what happens if someone writes an unsigned app into flash?
The ROM checks the bootloader and the bootloader checks the app against the SHA-256 digest of your public key in eFuse BLK2. With no valid 3072-bit RSA signature, that image is refused. The same check applies to every over-the-air update.
Glossary
- TLS
- Transport Layer Security: a layer under a protocol such as MQTT or HTTP that proves who the server is, keeps the data private and detects any change.
- Certificate
- A signed statement that binds a name, such as a broker's host name, to a public key. A certificate authority (CA) does the signing.
- Root CA certificate
- The certificate of the authority a device trusts. The device checks other certificates against it.
- Diffie-Hellman
- A way for two sides to reach the same secret key by exchanging only public numbers. TLS uses an elliptic-curve version.
- eFuse
- A one-time bit inside the chip. Once burned from 0 to 1 it can never go back. The ESP32 stores its security keys in eFuses.
- Flash encryption
- Scrambling the contents of flash memory with a key kept inside the chip, so a copy of the flash cannot be read.
- Secure boot
- Checking a digital signature on each piece of firmware before running it, so only code you signed can run.
- Release mode
- The production setting of the ESP32's security features: plaintext flashing, JTAG and UART download are turned off.
Notes and sources
- The card shows the six lines that open the connection. The whole sketch also joins Wi-Fi and defines HOST, ID, USER, PASS, TOPIC and ROOT_CA (paste your CA's PEM text there). In the current arduino-esp32 core WiFiClientSecure is another name for NetworkClientSecure.
- The handshake is simplified. In TLS 1.3 the key shares travel in the first two hello messages, so the shared key is ready before the certificate is sent, and the certificate itself is already encrypted. The checks are the same.
- The key exchange uses textbook Diffie-Hellman with tiny numbers (p = 23, g = 5) so you can check it by hand. Real TLS uses an elliptic-curve version with numbers far too large to guess.
- Flash encryption covers the bootloader, partition table, otadata and every app partition by default. The NVS key-value partition is not covered: secrets saved there need NVS encryption, which keeps its own keys in an encrypted partition.
- The ESP32 has 4 eFuse blocks of 256 bits: BLK1 holds the flash encryption key and BLK2 the secure boot key digest. Secure Boot v2 needs chip revision v3.0 or later. In development mode flash encryption can be turned off only 3 times per chip. Both features are set up through an ESP-IDF project configuration and Espressif's tools, and some steps cannot be undone.
- OASIS MQTT Version 3.1.1 (ports §4.2, security §5)
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
- Espressif ESP-IDF (ESP32): Flash Encryption
- Espressif ESP-IDF (ESP32): Secure Boot v2
- Espressif ESP-IDF (ESP32): eFuse Manager
- Espressif ESP-IDF (ESP32): Security overview
- Espressif esptool: basic commands (read-flash)
- arduino-esp32: NetworkClientSecure README
- arduino-esp32: NetworkClientSecure.h (setCACert, setInsecure)
- arduino-esp32: WiFiClientSecure.h
- PubSubClient.h (N. O'Leary)
Quiz Unlocks after the last step.