> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nerves-hub.org/llms.txt
> Use this file to discover all available pages before exploring further.

# TPM

> Authenticate a device with a certificate and private key held by a Trusted Platform Module, using the TPM configurator in nerves_hub_link.

If your hardware has a Trusted Platform Module, NervesHubLink can read the device certificate from it and use its private key for the TLS handshake. NervesHub sees an ordinary device certificate. Everything in [Device certificates](https://docs.nerves-hub.org/auth/device-certificates.md) about registering the CA and adding devices applies.

## Requirements

* `nerves_hub_link` 2.9.0 or later. Restoring the key file from the TPM needs 2.10.1 or later.
* The [`tpm`](https://hex.pm/packages/tpm) library, `~> 0.2.0`, in your firmware.
* A TPM that already holds the device's private key and certificate. NervesHubLink reads them. It does not provision the TPM.
* A Nerves system with the kernel module for your TPM. The configurator loads it with `modprobe`.
* The CA that signed the device certificates, registered with your organization.

## Enable it

**1. Add the dependency**

```elixir theme={null}
# mix.exs
def deps do
  [
    {:nerves_hub_link, "~> 2.14"},
    {:tpm, "~> 0.2.0"}
  ]
end
```

**2. Configure the host**

```elixir theme={null}
# config/target.exs
config :nerves_hub_link,
  host: "devices.nervescloud.com"
```

When `tpm` is in the project, `nerves_key` is not, and you have not set `configurator`, NervesHubLink uses `NervesHubLink.Configurator.TPM`. If your project has both libraries, select it explicitly:

```elixir theme={null}
config :nerves_hub_link,
  configurator: NervesHubLink.Configurator.TPM
```

**3. Set the TPM options, if the defaults do not fit**

```elixir theme={null}
config :nerves_hub_link, :tpm,
  probe_name: "tpm_tis_spi",
  key_path: "/data/.ssh",
  key_name: "nerves_hub_link_key",
  key_address: "0x1000000",
  certificate_address: "0x1000001"
```

These are the defaults. See [Options](#options).

**4. Register the CA and make the devices known**

Register the CA that signed the device certificates. See [Register a certificate authority](https://docs.nerves-hub.org/auth/device-certificates.md#register-a-certificate-authority).

Then add each device before it connects, or give the CA a [just-in-time provisioning](https://docs.nerves-hub.org/auth/just-in-time-provisioning.md) profile. The device identifier is the common name in the certificate.

## What the configurator does

At startup the configurator:

1. Runs `modprobe` with `probe_name`. It raises if `modprobe` fails.
2. Checks that the TPM responds. It tries up to six times, 300 ms apart, then raises `TPM Unavailable`.
3. Sets the `ssl` `key` option to the TPM private key for the file at `key_path` joined with `key_name`.
4. If that file does not exist, reads the NV index at `key_address` and writes its contents to the file.
5. Sets the `ssl` `cert` option to the certificate read from the NV index at `certificate_address`. The certificate must be stored there as PEM.
6. Sets `cacerts` to the CA certificates NervesHubLink trusts, unless you set `cacerts` or `cacertfile` yourself. See [Connecting devices](https://docs.nerves-hub.org/setup/connecting.md).

A `key` or `cert` you set in `ssl` yourself is kept, and the matching step is skipped.

A failure in any of these steps raises when NervesHubLink starts.

## Options

Set these under the `:tpm` key.

**probe\_name** `string` (default: tpm\_tis\_spi)

The kernel module to load, for example `tpm_tis_i2c`.

**key\_path** `string` (default: /data/.ssh)

The directory that holds the key file.

**key\_name** `string` (default: nerves\_hub\_link\_key)

The key file's name inside `key_path`.

**key\_address** `string` (default: 0x1000000)

The NV index the key file is restored from when it is missing.

**certificate\_address** `string` (default: 0x1000001)

The NV index that holds the device certificate.

## Check that it works

Boot the device and open its **Settings** tab in NervesHub. The **Certificates** card lists the certificate read from the TPM and the CA that signed it.

If the key file was missing, the device logs:

```text theme={null}
[NervesHubLink:TPM] `/data/.ssh/nerves_hub_link_key` not found in file system. Restoring from TPM.
```

## Related pages

* [X.509 device certificates](https://docs.nerves-hub.org/auth/device-certificates.md)
* [Just-in-time provisioning](https://docs.nerves-hub.org/auth/just-in-time-provisioning.md)
* [NervesKey](https://docs.nerves-hub.org/auth/nerveskey.md)
* [Security: PKI and the chain of trust](https://docs.nerves-hub.org/concepts/pki.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.