// Engineering Log
Secrets and zero trust: Part 4 — FIDO2 and YubiKey: hmac-secret, libfido2 and access permissions
Published on 2026-11-23 · Author: Mikhail Gorbachev
// Fast route
This article belongs to the topic Servers and infrastructure.
The FIDO2 hardware key is usually known as a second-factor for site login: you enter a password, touch the key, and the site lets you in. But the same key has an extension hmac-secret that turns it into a secret source: for the same input string the key always returns the same 32 bytes, and only after a touch. Those bytes can be used as an encryption key: for a disk, for a file, for a storage master password. This article explains how FIDO2 works, how hmac-secret works, how to prepare Linux to work with the key, and how to obtain the secret manually with libfido2 utilities.
What is FIDO2
FIDO2 is two standards together:
- WebAuthn (W3C) — the browser API through which a site asks to create an account or confirm a login;
- CTAP2 (FIDO Alliance) — the protocol by which the computer (in the standard’s terms — the platform) talks to the key over USB, NFC, or Bluetooth.
For server or terminal work WebAuthn is not needed: libfido2 utilities talk to the key directly over CTAP. Current CTAP versions are listed in the FIDO Alliance specifications: 2.1 (2021-06-15), 2.2 (2025-07-14) and 2.3 (2026-02-26). Which versions your key supports is shown by fido2-token -I (more on that below).
A FIDO2 credential is a key pair created inside the device for a specific relying party (RP). The relying party is identified by the RP ID string: in the browser it’s the site’s domain, and for local use it’s any agreed-upon string. The private key inside the device is not extractable.
Resident and non-resident credentials
Credentials are of two kinds.
Resident (called discoverable in CTAP 2.1) is stored in the key’s memory. To use it you only need to know the RP ID: the key finds the credential itself and returns its identifier. That’s how passwordless login (without username) works.
Non-resident is not stored in the key. When creating the credential the key returns a credential ID — a data blob without which the credential cannot be found. The relying party stores it: a site in its database or you in a file. On each use the credential ID is passed back to the key.
The difference matters for two reasons:
- space is limited. YubiKey 5 holds up to 100 resident credentials on firmware 5.7.4 and newer and 25 on older ones; for firmwares 5.7.0–5.7.3 Yubico documentation gives inconsistent numbers. You can create an unlimited number of non-resident credentials;
- key theft. A resident credential can be used knowing only the RP ID. For a non-resident one you additionally need the credential ID file.
For local encryption non-resident is more convenient: the credential ID sits next to the encrypted data and doesn’t consume key memory.
The hmac-secret extension
hmac-secret is described in CTAP 2.2 (section 12.7). It works like this.
- When creating a credential with this extension the key generates two random 32-byte values: CredRandomWithUV and CredRandomWithoutUV. Both remain inside the key and are not revealed.
- When requesting an assertion the platform provides one or two 32-byte salts. The salts are sent encrypted under a shared secret that the platform and key agree on before the request.
- The key computes HMAC-SHA-256(CredRandom, salt) and returns 32 bytes for each salt.
This implies several properties that everything below relies on.
Deterministic result. The same credential and the same salt always produce the same secret. A different salt produces a different secret. A different credential on the same key also yields a different secret.
Requires touch. The extension requires user presence: without a touch the key will not return the response. Touch does not show which program requested the secret or why, so it does not protect against a malicious program that requests the secret at the moment you touch the key for another reason.
PIN changes the result. User verification (UV) for hmac-secret is optional, but it determines which of the two internal values is used. If the request is made with a PIN, the key uses CredRandomWithUV; if without a PIN — CredRandomWithoutUV. The secret obtained with a PIN and the secret obtained without a PIN are different. If data are encrypted with the secret obtained without a PIN and later you enable a PIN, you won’t be able to decrypt them: you must decrypt them the old way and re-encrypt.
Works with both credential types. The extension is supported for both resident and non-resident credentials.
credProtect
The credProtect extension sets the protection level of the credential at creation:
0x01— userVerificationOptional: credential is available without a PIN (default);0x02— the credential is available without a PIN only if the platform provided its credential ID in the list. For a non-resident credential this changes nothing: its credential ID is always provided;0x03— userVerificationRequired: the key does not use the credential at all without a PIN.
Level 0x03 together with hmac-secret means the secret can only be obtained with a PIN. Such a key stolen along with the credential ID file is useless without the PIN.
Support in YubiKey
In the YubiKey 5 application table the extensions hmac-secret and credProtect are marked as supported on firmwares 5.2.x–5.8.x. The variant hmac-secret-mc, which returns the secret already at credential creation, is listed by Yubico for firmware 5.8.x. YubiKey firmware cannot be updated, so check its version before buying a second key.
Where hmac-secret is used
- systemd-cryptenroll with the
--fido2-deviceparameter (since systemd 248) adds a LUKS slot that is unlocked with the secret from the key. By default it requests both PIN and touch. - age-plugin-fido2-hmac — a plugin for the age encryption tool, uses non-resident credentials. Prior to version 1.0.0 the author considers it experimental. If the credential was created with a PIN, a forgotten PIN means data loss.
It’s important to understand the difference from signature-based login. The pam-u2f module and site logins use not hmac-secret but signatures: the server sends a random challenge, the key signs it with the private key, and the server verifies the signature with the public key. That scheme answers “is this key present”, but it does not give a secret you can encrypt with: the signature is different every time. hmac-secret gives exactly a secret, constant for each invocation, so it’s suitable for encrypting data at rest.
Installing libfido2 on Debian and Ubuntu
The libfido2 library from Yubico is the basis of most Linux programs working with FIDO2. The command-line utilities are in a separate package:
sudo apt update
sudo apt install fido2-toolsThe package installs fido2-token, fido2-cred, fido2-assert and the library libfido2-1 as a dependency. In Debian 13 (trixie) this is version 1.15.0; the current version at Yubico is 1.17.0. For the commands described here 1.15 is sufficient.
Permissions for /dev/hidraw
The FIDO2 key on Linux appears as a HID device /dev/hidrawN. By default such devices belong to root, and fido2-token -L run by a normal user will return an empty list. There are two ways to grant access.
uaccess for a local session. In systemd the rule 60-fido-id.rules recognizes FIDO keys, and 70-uaccess.rules grants permissions to them for the active local session user — the one sitting at the screen. No group is needed. You can check like this:
getfacl /dev/hidraw2The output should include a line user:<your name>:rw-.
Group for SSH and services. uaccess grants rights only to the active local session. If you logged in by SSH or a service uses the key, that ACL record doesn’t include you and the device is inaccessible. In that case you need a udev rule with a group. Example from the libfido2 README for Yubico keys:
sudo tee /etc/udev/rules.d/70-fido-plugdev.rules >/dev/null <<'EOF'
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", MODE="0660", GROUP="plugdev", ATTRS{idVendor}=="1050"
EOF
sudo udevadm control --reload-rules
sudo udevadm trigger
sudo usermod -aG plugdev "$USER"1050 is Yubico’s vendor ID; for keys from other vendors substitute your own (it is shown by fido2-token -L in the vendor field). The libfido2 repository contains a ready file udev/70-u2f.rules with many vendor IDs. The plugdev group exists by default in Debian and Ubuntu. New group membership takes effect after re-login: exit the SSH session and log in again, then check id and ls -l /dev/hidraw*.
A rule with a group opens the key to all members of plugdev, not only you; other users without that group have no rights (the libfido2 README uses 0664, here the mode is intentionally stricter). On a multi-user machine keep only those who actually need the key in that group.
Getting to know the key
List keys:
fido2-token -L/dev/hidraw2: vendor=0x1050, product=0x0402 (Yubico YubiKey FIDO)The device name is only the first field up to the colon. In scripts it’s extracted like this:
DEV=$(fido2-token -L | head -n1 | cut -d: -f1)
echo "$DEV"If you pass the whole line to utilities or mistype the name, instead of a clear message you will get FIDO_ERR_INTERNAL. The same code appears when there are insufficient permissions on /dev/hidraw*.
Information about the key:
fido2-token -I "$DEV"In the output pay attention to three lines:
version strings:— supported protocol versions (FIDO_2_0,FIDO_2_1and others);extension strings:— extensions; for what’s next you needhmac-secretto be listed, and for credential protection —credProtect;options:— capabilities of the key.clientPinmeans a PIN is set,noclientPinmeans the key supports a PIN but it is not configured.
List relying parties with resident credentials — fido2-token -L -r "$DEV" (it will ask for a PIN). Delete a resident credential — fido2-token -D -i <credential ID> "$DEV". fido2-token -R resets the FIDO application on the key completely: all FIDO2 credentials and the PIN are erased, and their hmac-secret values are lost forever. The utility does not ask for confirmation; Yubico keys accept reset only in the first seconds after connection, after that you need to touch the key.
Creating a credential with hmac-secret
fido2-cred -M reads four lines from standard input (or from a file with -i):
- client data hash in base64 — for local use 32 random bytes are enough;
- RP ID — the string that denotes the credential’s purpose;
- username;
- user ID in base64.
Prepare a directory and the input file:
mkdir -p ~/.config/fido-demo
chmod 700 ~/.config/fido-demo
cd ~/.config/fido-demo
RP=unlock.example.ru
{
openssl rand -base64 32 # client data hash
echo "$RP" # RP ID
echo "$USER" # username
openssl rand -base64 32 # user ID
} > cred_paramRP ID here is just a label: the key does not verify whether the domain belongs to you, the browser does that. You need to remember it: when obtaining the secret the same RP ID is supplied, otherwise the key won’t find the credential.
Create a non-resident credential with hmac-secret:
fido2-cred -M -h -i cred_param "$DEV" > cred_outTouch the key immediately after starting the command: it will blink and wait for touch. If a PIN is set on the key the utility may ask for it: this depends on the key and settings. On the YubiKey 5 we tested with a PIN set the credential was created without requesting the PIN, because the key allows credential creation without user verification. If created with -v or -c 3, a PIN will be required.
Useful flags for fido2-cred -M:
-h— enablehmac-secret;-r— create a resident credential (not needed for our task);-v— request user verification (PIN);-c 3— credProtect level userVerificationRequired.
The output is several lines: client data hash, RP ID, attestation format, authenticator data, credential ID, signature and attestation certificate. We need the fifth line — the credential ID:
sed -n 5p cred_out > cred
chmod 600 credThe cred file by itself is not a secret: without the physical key nothing can be obtained from it. But together with a stolen key and with credProtect level 1 it yields the secret with one touch, so 600 permissions are not excessive. The rest of cred_out and cred_param can be removed.
Obtaining the secret
fido2-assert -G reads lines:
- client data hash in base64;
- RP ID;
- credential ID (not needed for a resident credential created with
-r); - salt in base64, exactly 32 bytes (only with
-h).
Generate the salt once and store it next to the credential ID:
openssl rand -base64 32 > salt
chmod 600 saltRequest the secret:
secret=$({
openssl rand -base64 32 # client data hash, new each time
echo "$RP"
cat cred
cat salt
} | fido2-assert -G -h -p "$DEV" | tail -n1)
echo "${#secret}" # length 44; the secret itself is not printed-h enables hmac-secret, -p explicitly requires user presence. Touch the key as soon as the command starts. The last line of output is the secret in base64, 44 characters (32 bytes). The client data hash does not affect the secret: it only affects the signature, while the secret is determined by the credential, the salt and whether a PIN was used.
If the credential was created with -c 3, -v is mandatory. If it was created with -v or the key has the alwaysUv parameter enabled, the PIN will be asked for even without explicit -v, and the secret will be the “with PIN” one, i.e., based on CredRandomWithUV. If you add -v, the utility will ask for the PIN and the key will use that value.
Checking stability
Before encrypting anything with this secret make sure the key returns the same value. It’s convenient to wrap the request in a function:
fido_secret() {
{ openssl rand -base64 32; echo "$RP"; cat cred; cat "$1"; } \
| fido2-assert -G -h -p "$DEV" | tail -n1
}
a=$(fido_secret salt) # touch
b=$(fido_secret salt) # touch
[ "${#a}" -eq 44 ] && [ "${#b}" -eq 44 ] || { echo "secret not obtained"; exit 1; }
[ "$a" = "$b" ] && echo "identical" || echo "DIFFERENT"
openssl rand -base64 32 > salt2
c=$(fido_secret salt2) # touch
[ "$a" = "$c" ] && echo "matched — shouldn't happen" || echo "different salt — different secret"
echo "${#a}" # 44Expected result: two requests with the same salt give the same string, a request with another salt gives a different one, length is 44 characters. If the key has a PIN and you include -v in the second call, the result will also be different: this checks that PIN changes the secret. On a key without a PIN -v will fail.
Do not print the secret to the terminal or save it in files: here it is only put into a variable. After this it should be passed to the encryption program via standard input or an environment variable, not as a command-line argument which is visible in the process list.
Common errors
- FIDO_ERR_RX or FIDO_ERR_OPERATION_DENIED (0x27). The key did not receive the touch in time. In our experience the key responds to a touch made after the command starts; if it times out one of these errors appears (the documentation does not describe this behavior). Run the command and touch the key as soon as it blinks.
- FIDO_ERR_INTERNAL. Most often the device name is incorrect: the whole line from
fido2-token -Lwas passed or a device with a different number. The hidraw number can change after reconnecting the key or rebooting, so scripts determine the name on each run. The same error also occurs with insufficient permissions on/dev/hidraw*. - Empty
fido2-token -L. No permissions on/dev/hidraw*. Over SSH this is expected without a udev rule with a group; after adding to the group you need to re-login. - The key cannot find the credential. A different RP ID was supplied, the
credfile is corrupted (extra space, newline in the middle) or the credential was deleted by resetting the key. - The secret “suddenly” became different. The salt changed, the request was made with a PIN instead of without PIN (or vice versa), or a different key was used.
Backup key and lost key
You cannot clone a YubiKey: private keys and CredRandom values are not extractable. That means everything encrypted with a secret from one key is lost with it. Hence the rules:
- always have a second access method. For site logins a spare key is registered with each service in advance. For encryption create a second credential with
hmac-secreton a second key and encrypt the same data with its secret as well. Store the second key separately from the first; - the original secret outside the key. If the key unlocks a master password, the master password itself should be written down and kept safely offline. The key speeds up unlocking, it does not replace the password;
- in case of loss log in with the backup key or recovery codes and remove the lost key from all services. For local encryption remove the
credfiles and data encrypted with its secret, and re-encrypt with a new key. Yubico cannot restore access to third-party accounts: the company has no access to them; - resetting the key (
fido2-token -R) destroys secrets too: decrypt everything that depends on it before reset.
About the threat model. Touch without a PIN protects against a remote attack and against theft of disk files: without the physical key the secret cannot be obtained. It does not protect against someone who has both your computer and your key at the same time. If that scenario is realistic for you, use a PIN or credProtect level 3 — how to decide if you need it is discussed in “Threat models and zero trust principles”.
A key with hmac-secret, the credential ID file and the salt are all you need to keep a storage master password encrypted on disk and decrypt it by touching. How Bitwarden and Vaultwarden themselves are structured is described in the article “Password managers: Part 3 — Bitwarden and Vaultwarden”, and working with its CLI client is in “Part 3 — Bitwarden CLI on Linux: bw, session and secure operation”.
// Similar task
If you are dealing with something similar
This article belongs to one of the main working topics. You can keep reading on the topic, go to the homepage to understand what I do, or open the service pages directly.
Article topic
Servers and infrastructure
VPS, Linux, web stack, migrations, hosting, databases, and core operations.
Typical tasks behind this topic
- Move a site or service to a new server
- Set up Linux, Nginx, databases, and backups
- Figure out why the system behaves unstably
// Next step
If you need help with this topic, not just another article, it is better to go straight to the service page. The homepage and topic collection stay available as secondary routes.
Open services// Contact
Need help?
Get in touch with me and I'll help solve the problem
I reply within one business day (03:00-13:00 GMT)
Or leave a request here:
// Related