Skip to main content
With a shared secret, a device authenticates with a key and a secret instead of an X.509 certificate. The device sends the key and signs its identifier with the secret. The secret itself is never sent. It suits development and early R&D. There are two kinds of key:

Requirements

  • Shared secrets must be turned on for the NervesHub instance with DEVICE_SHARED_SECRETS_ENABLED=true. It is off by default, and only certificate authentication works without it. See Feature prerequisites.
  • Product keys need NervesHub 2.0.0 or later and nerves_hub_link 2.1.0 or later. Shared secret is the default configurator from nerves_hub_link 2.4.0.
  • Device keys need NervesHub 2.5.0 or later.
  • Your firmware must not include the nerves_key or tpm libraries, or you must set the configurator explicitly. See Choose the configurator.
When shared secrets are off, the Shared secrets card in Product settings says: “Shared secret authentication hasn’t been turned on for this platform.”

Use a product key

1

Create the secret

Open Product settings in the product sidebar. In the Shared secrets card, click Create secret. This needs the Manage role or higher.
2

Copy the key and secret

The card lists each key. Copy the key, which starts with nhp_. Use the copy button next to the key to copy its secret.
3

Configure the firmware

System.fetch_env!/1 runs when you build the firmware, so the values are compiled into it. Set both variables in the shell or CI job that runs mix firmware.
4

Boot the device

The device connects with its serial number as its identifier. If no device with that identifier exists in the product, NervesHub creates one.

Use a device key

A device key is issued to a device that already exists. A provisioning station typically creates the device and its key together and writes the key and secret to the device.
1

Issue the key

Open Devices, click Add device, and under How it connects choose A shared secret of its own. After you click Add device, the dialog shows the Key and Secret.
The secret is shown once and cannot be retrieved again. If you lose it, create another key and deactivate the old one.
2

Put the key and secret on the device

NervesHubLink reads a device key from the key and secret options:
Compiling a device key into firmware ties that firmware to one device. To keep one firmware image for the fleet, store the values in the Nerves key-value store instead. NervesHubLink reads nh_shared_key and nh_shared_secret from it, and they take priority over the config.
3

Boot the device

The device must connect with the identifier of the device the key was issued to.
NervesHubLink uses the first key it finds, in this order:
  1. nh_shared_key in the Nerves key-value store
  2. key in the shared_secret config
  3. nh_shared_product_key in the Nerves key-value store
  4. product_key in the shared_secret config
If the key starts with nhp_, the secret comes from nh_shared_product_secret in the key-value store, then product_secret in the config. For any other key, it comes from nh_shared_secret in the key-value store, then secret in the config.

Choose the configurator

NervesHubLink.Configurator.SharedSecret is the default only when neither nerves_key nor tpm is in your project. If one of them is, select shared secrets explicitly:

Check that it works

Open Devices in the product. The device shows as Online once it connects. For a device key, the Shared secrets card on the device’s Settings tab lists each key with who created it. If the device does not connect:
  • Check the device clock. NervesHub accepts a signature for 90 seconds after it was made. From nerves_hub_link 2.13.0, a device whose connection is refused reads the time from the server’s response and signs its next attempt with it, so a device that boots with a wrong clock connects on a retry.
  • Check that the key is active. A deactivated key shows as Deactivated in the card.
  • Check that the device has not been deleted. A deleted device’s keys stop authenticating, and a product key cannot create a new device under a deleted device’s identifier.

Options

Set these inside shared_secret:
string
The identifier the device connects as. Defaults to Nerves.Runtime.serial_number().
atom
default:":sha256"
The digest used to derive the signing key.
integer
default:"1000"
The number of PBKDF2 iterations. From NervesHub 2.5.3, a device asking for more than 10,000 is refused.
integer
default:"32"
The derived key length in bytes. From NervesHub 2.5.3, a device asking for more than 64 is refused.
If you write your own configurator for shared secrets, use nerves_hub_link 2.13.1 or later. Earlier versions reused a custom configurator’s first signature on reconnect, and NervesHub refused it once it expired.

Deactivate a key

Keys are deactivated, not deleted. A deactivated key stays in the list. Issuing and deactivating device keys are recorded in the device’s audit log. To replace a key, create a new one, move your devices to it, then deactivate the old one.

Limits

  • A device key stays with the device when it moves to another product or organization. Whoever holds the secret can still connect as the device. When a device moves to an organization that should not share credentials with the one it left, issue a new key there and deactivate the old ones.
  • A product key lets its holder connect as any identifier in the product, including identifiers that do not exist yet. Use device keys or certificates for production fleets.