Skip to content

Secure Elements

Keep a device’s private key in dedicated hardware, where it can be used but never read out. Without one, a key in a microcontroller’s flash can be copied by anyone with physical access, unless flash encryption is on.

This guide uses the Embedded C SDK, with an ATECC608 as the secure element.

For token login, the SDK signs through a small interface, so the key can live anywhere. Your code signs a SHA-256 digest with whatever hardware you have, and the SDK does the rest.

static espoirnet_err_t signInChip(void *, const uint8_t hash[32], uint8_t sig[64]) {
return ECCX08.ecSign(0, hash, sig) ? ESPOIRNET_OK : ESPOIRNET_ERR_CRYPTO;
}
espoirnet_signer_t signer = { nullptr, signInChip };
device.beginWithSigner(cfg, signer);

To use other hardware, write your own version of that function:

  • Input and output: you receive the digest (32 bytes) and write the raw signature (64 bytes, r then s, big-endian, not DER). The key must be P-256.
  • Result: return ESPOIRNET_OK, or ESPOIRNET_ERR_CRYPTO on failure. A failure abandons that login attempt, and the SDK retries with a delay.
  • Timing: it’s called once per login, inside poll(), so keep it well under a second. An ATECC608A takes about 140 ms.
  • Registering the key: espoirnet_p256_public_key_pem() turns a raw public key (X then Y) into the text the console accepts.

The SDK’s two ATECC608 examples need the ArduinoECCX08 library (1.4.1), from the Arduino Library Manager or in PlatformIO as arduino-libraries/ArduinoECCX08@1.4.1. Your other sketches don’t need it.

Use a blank chip, such as the ATECC608A-SSHDA-B or ATECC608B-SSHDA-B, not one sold pre-provisioned (-TNGTLS, -TFLXTLS).

ATECC608 (SOIC-8) ESP32
Pin 8, VCC 3V3
Pin 4, GND GND
Pin 5, SDA GPIO 21, with a 4.7 kΩ pull-up to 3V3
Pin 6, SCL GPIO 22, with a 4.7 kΩ pull-up to 3V3

Pins 1, 2, 3 and 7 aren’t connected. Fit a 0.1 µF capacitor between VCC and GND, close to the chip.

Flash the SecureElementProvisioning example and open the serial monitor at 115200 baud, with newline line endings.

  1. s checks the chip: its serial number, and whether it’s locked.

  2. l writes the configuration and locks the chip. It checks the written configuration before locking.

  3. k generates the key inside the chip. You have to type yes, because this replaces any existing key.

  4. p prints the public key. Add it to the device in the console, as with any other key (see Device Provisioning).

Start from the SecureElement example, which is the Telemetry example with login signed inside the chip. Everything else about the SDK works as usual: readings, state reports, commands, and revoking the key from the console.

  • Token login only. Certificate login still reads its key from flash on ESP32, because the TLS layer in the Arduino core can’t sign through an external chip.
  • The link between the ESP32 and the chip isn’t paired or encrypted in this setup. The key can’t be read out, but someone with physical access to the board could ask the chip to sign. Protect your firmware with secure boot too.