ENع
←Device security
Lesson 52 · Connectivity and the capstone node

Securing a device: TLS, secrets and secure boot

14 min11 steps5 questionsAdvanced
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”.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 52 · Interactive
01 / 11

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Plain 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 broker
  3. Carry 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 check
  4. The 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)
  5. 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 false
  6. Agree 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 numbers
  7. Publish, 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 detected
  8. Secrets 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 bytes
  9. Flash 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...
  10. 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 stops
  11. Ship 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

Quiz Unlocks after the last step.