Using Token2 Security Keys to encrypt restic backups

Last updated 2026-11-27

restic is a fast, modern, open-source backup program and one of the most trusted tools of its kind. It encrypts everything on the client side with AES-256 by default, deduplicates data so backups stay compact, verifies integrity with content hashes, runs on Linux, Windows and macOS, and writes to many storage backends (local, SFTP, REST server, S3, Backblaze B2, Azure, Google Cloud and more).
restic protects a repository with a password. The important question is where to keep that password: if it is stored on disk, a stolen machine gives it away. With a Token2 FIDO2 security key the password is not stored at all — the key computes it on demand, using the FIDO2 hmac-secret extension, and only after the key's PIN and a touch.
In this guide we configure Token2 security keys to protect a restic repository on Linux.

How it works

The FIDO2 hmac-secret extension turns a Token2 key into a deterministic secret generator: for a fixed credential and salt, the key always returns the same 32-byte secret — but only after PIN and touch, and only from that physical key. That secret is used as the restic repository password, so it exists nowhere on disk. Without the key, the backups are unreadable ciphertext.

Requirements:

• A Linux host (Debian/Ubuntu) with restic installed.
• A Token2 FIDO2 security key.
• The libfido2 command-line tools.

Installing the tools

sudo apt install fido2-tools restic openssl
fido2-token -L        # list keys, note your device, e.g. /dev/hidraw0
Below, the device path is referred to as $DEV. If the key has no PIN yet, set one with fido2-token -S /dev/hidraw0.

Step 1 - Enroll the key (once)

Create a FIDO2 credential with hmac-secret enabled, requiring user verification (PIN), and store its credential id and a fixed salt. Together with the key they reproduce the password; without the key they are useless.
DEV=$(fido2-token -L | head -1 | cut -d: -f1)
sudo install -d -m 700 /etc/restic-fido2

printf '%s\n%s\n%s\n%s\n' \
  "$(head -c32 /dev/urandom | base64)" "restic-backup" "restic" "$(head -c16 /dev/urandom | base64)" \
  | fido2-cred -M -h -v "$DEV" | fido2-cred -V -h -o /tmp/cred.out

sudo sed -n 1p /tmp/cred.out | sudo tee /etc/restic-fido2/credential >/dev/null
head -c32 /dev/urandom | base64 | sudo tee /etc/restic-fido2/salt      >/dev/null
sudo rm -f /tmp/cred.out; sudo chmod 600 /etc/restic-fido2/*
When prompted, enter the key's PIN and touch it.
Back up /etc/restic-fido2 and enroll a second Token2 key as well (repeat the step) - FIDO2 credentials cannot be copied between keys, so losing the only key means losing access.

Step 2 - Create the password helper

A small script asks the key (PIN and touch) and prints the derived secret. It is fed to tools through stdin, so the secret never appears on a command line.
sudo tee /usr/local/bin/restic-fido2-pass.sh >/dev/null <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
DIR=/etc/restic-fido2
DEV=$(fido2-token -L | head -1 | cut -d: -f1)
printf '%s\n%s\n%s\n%s\n' \
  "$(head -c32 /dev/urandom | base64)" "restic-backup" \
  "$(cat "$DIR/credential")" "$(cat "$DIR/salt")" \
  | fido2-assert -G -h -v "$DEV" | tail -n1
EOF
sudo chmod 700 /usr/local/bin/restic-fido2-pass.sh
Run it twice: with PIN and touch each time it prints the same string, which proves the key yields a stable secret.
sudo /usr/local/bin/restic-fido2-pass.sh
sudo /usr/local/bin/restic-fido2-pass.sh

Step 3 - Verify by encrypting and decrypting a file

Before trusting the key with backups, prove it really protects data. Encrypt a file with the key-derived secret, then decrypt it - each operation needs PIN and touch. A wrong password cannot decrypt it, which shows the key is truly required.
echo "Token2 secret $(date)" | sudo tee /tmp/plain.txt

sudo /usr/local/bin/restic-fido2-pass.sh \
  | sudo openssl enc -aes-256-cbc -pbkdf2 -salt -pass stdin -in /tmp/plain.txt -out /tmp/cipher.bin
sudo od -A x -t x1z /tmp/cipher.bin | head -3

sudo /usr/local/bin/restic-fido2-pass.sh \
  | sudo openssl enc -d -aes-256-cbc -pbkdf2 -pass stdin -in /tmp/cipher.bin -out /tmp/decrypted.txt
sudo cat /tmp/decrypted.txt

echo wrong-password \
  | sudo openssl enc -d -aes-256-cbc -pbkdf2 -pass stdin -in /tmp/cipher.bin -out /dev/null
The ciphertext is unreadable, the correct key brings the text back, and a wrong password fails with "bad decrypt".
restic uses the same helper with restic --password-command /usr/local/bin/restic-fido2-pass.sh, so every init, backup and restore is protected by the Token2 key.

Notes

• Set a PIN on the key with fido2-token -S $DEV; user verification depends on it.
• Always use the -v option. The hmac-secret differs between a PIN-verified and a non-verified assertion, which would change the derived password.
• Enroll a backup key and store it safely; credentials cannot be copied between keys.
• The device path (/dev/hidrawN) can change; the helper re-detects it with fido2-token -L.