A secret compiled into the firmware is the same on every device. One lost device exposes the whole fleet, and the only fix is a new secret on every device. It is still a common way to connect the first ten ESP32s: an API key, a database password or one client certificate, compiled in and flashed ten times. This article explains why that choice cannot be undone cheaply. It is part of the series “Production details”, after why IoT devices can take a website down and certificate expiry.
Why do so many prototypes put one key on every device?
Many prototypes put one key on every device because it is the quickest path that works. The device needs to authenticate, the database or API offers a key, and the key goes into a secrets.h that is compiled into the firmware. A careful coding agent with web search often builds per-device keys when asked for a production setup, but a quick prototype rarely asks. The result works for one device and for ten, and every copy of the firmware carries the same secret.
What happens when one device is lost?
When one device is lost, the whole fleet is exposed. Without flash encryption, the flash of an ESP32 can be read over the serial port with standard tools, and the key is in it. Whoever has it can send data as any device and read what the key allows, and nothing distinguishes them from a real device. Weak, guessable and hardcoded credentials are the first entry of the OWASP IoT Top 10 (OWASP). The consumer IoT standard ETSI EN 303 645 requires passwords to be unique per device (ETSI EN 303 645). The repair is a new key on every device, which needs an update channel, and the old key keeps working on every device that has not taken the update.
What does “own identity” mean for an ESP32?
Own identity means every device has its own key pair and a certificate for it, signed by a certificate authority that your servers trust. The device proves who it is with mutual TLS: it shows its certificate in the handshake and signs with its private key. The server checks that your CA signed the certificate and that it is not revoked. One device can then be blocked alone, and a stolen device exposes only itself. The certificate needs a renewal path from the start, as described in the article on expiry.
Where should the private key live?
The private key should be generated on the chip and never leave it. Only a certificate signing request (CSR) with the public key travels to the CA. With Mbed TLS in ESP-IDF:
#include "mbedtls/pk.h"#include "mbedtls/ecp.h"#include "mbedtls/x509_csr.h"#include "mbedtls/ctr_drbg.h"/* Creates a P-256 key pair on the device and writes a CSR (PEM) for it. */int make_csr(mbedtls_pk_context *key, mbedtls_ctr_drbg_context *rng, const char *device_id, unsigned char *out, size_t out_len){ char subject[64]; snprintf(subject, sizeof subject, "CN=%s", device_id); mbedtls_pk_init(key); int rc = mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); if (rc == 0) rc = mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(*key), mbedtls_ctr_drbg_random, rng); if (rc != 0) return rc; mbedtls_x509write_csr csr; mbedtls_x509write_csr_init(&csr); mbedtls_x509write_csr_set_md_alg(&csr, MBEDTLS_MD_SHA256); mbedtls_x509write_csr_set_subject_name(&csr, subject); mbedtls_x509write_csr_set_key(&csr, key); rc = mbedtls_x509write_csr_pem(&csr, out, out_len, mbedtls_ctr_drbg_random, rng); mbedtls_x509write_csr_free(&csr); return rc;}A software key must be stored encrypted (NVS encryption, flash encryption). On ESP32-S2, S3, C3, C6 and H2, the Digital Signature (DS) peripheral goes further. It holds an RSA key encrypted with a key in eFuse and signs inside the hardware, so the firmware never sees the key (ESP-IDF Digital Signature peripheral). Espressif’s esp_secure_cert_mgr component stores the device certificate and the DS parameters in a dedicated partition (esp_secure_cert_mgr).
How does device number 1,000 get its identity?
Device number 1,000 gets its identity the same way as device number 1, which is why the method is chosen before device 1 ships. By hand works for ten devices on a desk. For a pilot of several hundred units or a production line there are three common ways:
| Method | How it works | Fits |
|---|---|---|
| CSR at first boot | The device generates its key and requests a certificate the first time it has network, authenticated by a one-time claim code | Small series, devices set up by your team |
| Factory provisioning | A station generates the key in the device during production and gets the certificate signed before the device leaves the line | Larger series with your own production step |
| Pre-provisioned modules | Modules arrive with a key and a certificate from the module manufacturer, and your backend trusts their issuing CA or registers each certificate | Series without any key handling in production |
All three keep the private key on the device.
How do I configure mutual TLS in ESP-IDF?
Configure ESP-MQTT with the device certificate and, on chips with the DS peripheral, the DS context instead of a private key:
#include "mqtt_client.h"#include "esp_secure_cert_read.h"extern const char backend_root_pem[]; /* the root that signed the server certificate */esp_mqtt_client_handle_t start_mqtt_with_identity(void){ char *cert = NULL; uint32_t cert_len = 0; esp_secure_cert_get_device_cert(&cert, &cert_len); /* from the secure cert partition */ esp_ds_data_ctx_t *ds = esp_secure_cert_get_ds_ctx(); /* key stays in the DS peripheral */ esp_mqtt_client_config_t cfg = { .broker.address.uri = "mqtts://devices.example.com:8883", .broker.verification.certificate = backend_root_pem, .credentials.authentication.certificate = cert, .credentials.authentication.ds_data = ds, }; esp_mqtt_client_handle_t client = esp_mqtt_client_init(&cfg); esp_mqtt_client_start(client); return client;}On a chip without the DS peripheral, set .credentials.authentication.key to the device’s own key from encrypted storage instead of ds_data, and never to a key that another device also has.
Sources
- OWASP Internet of Things Project, IoT Top 10: https://owasp.org/www-project-internet-of-things/
- ETSI EN 303 645, cyber security for consumer IoT: https://www.etsi.org/deliver/etsi_en/303600_303699/303645/03.01.03_60/en_303645v030103p.pdf
- Espressif, Digital Signature peripheral (ESP32-S3): https://docs.espressif.com/projects/esp-idf/en/stable/esp32s3/api-reference/peripherals/ds.html
- Espressif, esp_secure_cert_mgr: https://github.com/espressif/esp_secure_cert_mgr
- Espressif, ESP-MQTT client: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/protocols/mqtt.html
- Espressif, flash encryption: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/security/flash-encryption.html
- Reed Smith, RED cybersecurity obligations (unique credentials) from August 2025: https://www.reedsmith.com/our-insights/blogs/viewpoints/102ktki/red-eu-cybersecurity-obligations-for-iot-products-from-august-2025/
Frequently asked questions
Is a per-device API key enough?
It is better than one key for all, because a single device can be blocked. But the key is still a secret stored in flash that was created outside the device and travelled to it. A key pair generated on the device never leaves it, and the certificate proves which device holds it.
Can I rotate a shared secret without reflashing?
Only through an update channel that the first firmware already has, and only on devices that take the update. The old secret keeps working on every device that has not updated, so the exposure does not end until the whole fleet has moved.
What is the Digital Signature peripheral on ESP32?
A hardware block in the ESP32-S2, S3, C3, C6 and H2 that signs with an RSA key stored encrypted with a key in eFuse. Firmware can ask it to sign a TLS handshake but cannot read the private key.
Can the flash of an ESP32 be read?
Yes, unless flash encryption is enabled. Without it the flash contents can be dumped over the serial port with standard tools. Flash encryption makes extraction much harder, but it does not change that a shared key is shared.
What does provisioning cost per device?
With a CSR at first boot, mostly the time of the first connection and the operation of a certificate authority. Factory provisioning adds a station and a few seconds per device on the line. Pre-provisioned modules move the cost into the module price.