johanneskueber.com

Unlocking LUKS with a YubiKey 5 and a Nitrokey 3

My laptop is a Framework 13 running Fedora. Its disk is LUKS2, and at boot the TPM unlocks it: the volume key is sealed against PCR 7 and 14 (the Secure Boot state and shim’s list of Machine Owner Keys) and released only together with a PIN. A passphrase in slot 0 is the fallback. The full setup lives in my homelab docs.

How a FIDO2 key unlocks a LUKS volume

FIDO2 keys are best known for passkeys, but most of them also implement the hmac-secret extension: the key computes an HMAC over a value the host sends, using a secret that never leaves the device. systemd builds disk unlocking on top of that.

sequenceDiagram
    participant H as LUKS2 header
    participant S as systemd-cryptsetup
    participant K as Security key
    S->>H: read the systemd-fido2 token (credential ID, salt, PIN/touch flags)
    S->>K: assertion with hmac-secret(salt) and the PIN
    K->>K: wait for a touch
    K-->>S: HMAC(device secret, salt)
    S->>H: the HMAC result opens the key's own keyslot

At enrolment, systemd-cryptenroll creates a credential on the key and writes everything needed to use it into a systemd-fido2 token in the LUKS2 header. A few properties follow from that, and I checked them against the systemd source rather than trusting my memory:

  • The credential is non-resident. systemd explicitly sets rk to false. It takes none of the key’s passkey slots, and the key keeps no record of it. Without the LUKS header, the key unlocks nothing.
  • PIN and touch are the defaults. User verification (biometrics) is off, the credential is ES256.
  • No PCRs are involved. The key does not care which shim booted. That is the whole point.
  • A FIDO2 reset kills it. Resetting the key replaces its secret, and the keyslot is dead from then on.

YubiKey 5 and Nitrokey 3: two vendors, the same commands

To systemd both keys are plain FIDO2 authenticators. Every command in this post is identical for both, and so are the parts that matter here:

YubiKey 5Nitrokey 3C NFC
hmac-secretyesyes
FIDO2 PIN retries8, replug after 3 wrong8, replug after 3 wrong
Firmwareclosed, cannot be updatedopen source, updatable with nitropy
Vendor toolykmannitropy
Used for boot viaUSBUSB-C (NFC plays no part at boot)

I enrol both, so that one of them can stay away from the laptop as a backup. A side effect of using two vendors: a firmware bug or a recall in one product line does not lock me out.

fido2-tools (part of libfido2) talks to both. With one key plugged in, and no sudo, because udev hands FIDO2 devices to the logged-in user:

1
2
fido2-token -L                       # lists the key as /dev/hidrawN
fido2-token -I /dev/hidrawN | grep -E 'extension strings|options|pin retries'

hmac-secret must appear among the extensions and clientPin among the options. noclientPin means no PIN has been set yet, and fido2-token -S /dev/hidrawN sets one. pin retries should read 8.

Those 8 retries deserve respect. Only a correct PIN resets the counter; time does not. At 0 the only way out is a FIDO2 reset, and that takes every passkey and every resident SSH key on the device with it. Keep that in mind for the PIN prompt further down.

Enrolling the keys

First a backup of the LUKS header, which goes off the laptop. It contains every keyslot as it is today, so it needs the same care as the passphrase:

1
2
sudo cryptsetup luksHeaderBackup /dev/nvme0n1p3 \
  --header-backup-file ~/luks-header-$(date +%F).img

Then I fixed the TPM, since its PCR state was now explained. Enrolling a new TPM slot and wiping the old one fits into a single call. systemd-cryptenroll enrols first and wipes afterwards, and it never wipes the slot it has just created:

1
2
sudo systemd-cryptenroll /dev/nvme0n1p3 --wipe-slot=tpm2 \
  --tpm2-device=auto --tpm2-pcrs=7+14 --tpm2-with-pin=yes

Now the keys, one at a time, with exactly one plugged in:

1
2
3
4
5
6
sudo systemd-cryptenroll /dev/nvme0n1p3 \
  --unlock-tpm2-device=auto \
  --fido2-device=auto \
  --fido2-with-client-pin=yes \
  --fido2-with-user-presence=yes \
  --fido2-with-user-verification=no

--unlock-tpm2-device=auto authorises the new slot with the TPM PIN instead of the passphrase. The tool then asks for the key’s FIDO2 PIN and a touch. The three --fido2-with-* options are the defaults; I spell them out so that the command documents itself. Swap keys, run it again, and the header holds four ways in:

1
2
3
4
5
6
$ sudo systemd-cryptenroll /dev/nvme0n1p3
SLOT TYPE
   0 password
   1 tpm2
   2 fido2
   3 fido2

The exact slot numbers depend on the order of enrolment.

One PIN prompt, many tokens

At boot there is a single prompt for a token PIN. Which token gets that PIN first turned out to be the most important detail of the whole setup. libcryptsetup hands the PIN to the tokens one after another until one of them opens its keyslot:

flowchart TD
    pin["One PIN prompt at boot"] --> p1["Pass 1: tokens whose keyslot has priority 'prefer'"]
    p1 --> p2["Pass 2: tokens whose keyslot has priority 'normal'"]
    p1 -->|"a token opens its keyslot"| ok([Disk unlocked])
    p2 -->|"a token opens its keyslot"| ok
    p2 -->|"wrong PIN, PCR mismatch or key missing everywhere"| pw["Passphrase prompt"]

Within a pass, tokens are tried in the order they appear in the header’s JSON. That order follows the history of enrolments, not the token ID. Every token receives the same PIN, and a wrong PIN or a missing device simply moves on to the next one.

Now think through what that means once the TPM has been re-enrolled after the keys: the FIDO2 tokens come first. Say I type the TPM PIN while the YubiKey is plugged in. The YubiKey receives the TPM PIN, rejects it, and loses one of its 8 retries. The TPM unlocks, the laptop boots, and nothing looks wrong. After eight such boots the key is blocked, and its passkeys are lost with the reset.

The fix is one setting. Keyslots in LUKS2 have a priority, and libcryptsetup tries the prefer slots before all others:

1
sudo cryptsetup config /dev/nvme0n1p3 --key-slot 1 --priority prefer

Now the TPM always gets the PIN first. If I type the TPM PIN, the keys are never asked. If I type a key’s PIN, the TPM rejects it first, and that costs one TPM dictionary-attack failure instead of a key retry. Unlike the key’s counter, the TPM forgives those over time; my laptop’s TPM drops one failure every two hours (TPM2_PT_LOCKOUT_INTERVAL in tpm2_getcap properties-variable). Using the same PIN for the TPM and both keys would avoid even that, at the price of one PIN guarding every path.

The priority belongs to the keyslot, not to the token type. Every TPM re-enrolment creates a new slot at normal, so the prefer has to be set again each time.

Testing without rebooting

cryptsetup can check a token without activating anything. Restricting it to the FIDO2 tokens keeps the TPM out of the test:

1
sudo cryptsetup open --test-passphrase --token-only --token-type systemd-fido2 -v /dev/nvme0n1p3

PIN, touch, Command successful. Once per key. Then two real boots: one with the TPM PIN and a key plugged in, after which fido2-token -I must still report 8 retries, and one with the key’s PIN, which ends in the key asking for a touch.

At boot there is one rule to remember: the key has to be plugged in before the PIN is confirmed. A key that is missing at that moment is skipped, and the boot falls through to the passphrase.

What I did not have to change

Neither /etc/crypttab nor the initramfs. My crypttab line still only says tpm2-device=auto, and yet the FIDO2 tokens are used, because systemd-cryptsetup first offers the PIN to every token in the header through libcryptsetup’s token plugins. Only a handful of options switch that off: tpm2-measure-pcr=, tpm2-measure-keyslot-nvpcr=, fixate-volume-key=, or a manually configured fido2-cid=. On Fedora, dracut’s systemd-cryptsetup module also pulls in the FIDO2 token plugin by default:

1
2
3
4
$ sudo lsinitrd | grep -E 'token-systemd-fido2|libfido2'
-rwxr-xr-x   1 root     root        20200 Sep 30 02:00 usr/lib64/cryptsetup/libcryptsetup-token-systemd-fido2.so
-rwxr-xr-x   1 root     root       243624 Sep 30 02:00 usr/lib64/libfido2.so.1.17.0
lrwxrwxrwx   1 root     root           18 Sep 30 02:00 usr/lib64/libfido2.so.1 -> libfido2.so.1.17.0

The enrolment data lives in the LUKS header, which the initramfs reads at boot, so nothing has to be rebuilt.

The next shim update

It will come, and the TPM will refuse again. The difference is what happens next. I plug in the YubiKey, enter its PIN, touch it, and the laptop boots. Then the TPM gets re-enrolled, unlocked by the key instead of the passphrase:

1
2
3
4
5
sudo systemd-cryptenroll /dev/nvme0n1p3 \
  --unlock-fido2-device=auto \
  --wipe-slot=tpm2 \
  --tpm2-device=auto --tpm2-pcrs=7+14 --tpm2-with-pin=yes
sudo cryptsetup config /dev/nvme0n1p3 --key-slot <new tpm2 slot> --priority prefer

And because the failure stays silent, I now check the journal once after updates that touch shim, firmware or dbx:

1
journalctl -b -t systemd-cryptsetup

The passphrase stays in slot 0 as the last resort. The difference is that I no longer depend on it as the only alternative to the TPM.


stat /posts/2026-11-25-luks-fido2-yubikey-nitrokey/

2026-11-25: Initial publication of the article

apropos /posts/2026-11-25-luks-fido2-yubikey-nitrokey/