Home Blog

PGP and SSH keys on a YubiKey

I use a YubiKey for both PGP keys and FIDO2-backed SSH keys. They use different features of the token, so I keep the setup notes separate.

Generating and moving a PGP key

gpg --expert --full-gen-key

Back up the secret key before running keytocard. The command moves it to the card and replaces the local private key with a stub. Without a backup, losing the YubiKey means losing access to that key.

gpg --export-secret-key --armor $KEYID > privkey.armor
gpg --export-secret-key $KEYID | paperkey > privkey.paperkey
gpg --export $KEYID > pubkey.armor

paperkey strips out everything reconstructible from the public key and leaves only the secret bits, which is what makes a printed copy a reasonable size. Store the armored copy somewhere offline.

Then move it:

gpg --edit-key $KEYID

and keytocard. Select the key first by entering key 1, otherwise the command operates on the primary key rather than the subkey you meant.

Restoring on another machine

The card holds the private key, but a fresh machine still has no idea the public key exists. Fetch it from a keyserver:

gpg --keyserver hkps://keys.openpgp.org --search-keys "[email protected]"

Then use the card’s fetch command to retrieve the public key information:

gpg --edit-card

and fetch.

Finally, trust it:

gpg --edit-key $KEYID

and trust. Note that this is --edit-key, not --edit-card. Getting these two confused is easy and the error messages do not point at the mistake.

git credentials through pass

pass-git-helper lets git pull credentials out of a pass store, which is itself encrypted to the PGP key on the card:

yay -S pass-git-helper
git config --global credential.helper /usr/bin/pass-git-helper
git config credential.useHttpPath true

Create ~/.config/pass-git-helper/git-pass-mapping.ini:

[github.com]
target=github.com/<username>

credential.useHttpPath true matters if you keep per-repository credentials, since without it every host collapses to a single entry.

FIDO2-backed SSH keys

OpenSSH 8.2 added the ed25519-sk and ecdsa-sk key types for hardware-backed authentication. These require libfido2, and the local key file cannot be used without the token.

Both ends need to understand the key type, so an old server will reject these outright.

ssh-keygen -t ed25519-sk

You will be asked for the PIN and a touch to confirm generation, and normally a touch on every connection afterwards.

Point your config at the key explicitly:

Host SERVER1
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_ed25519_sk

IdentitiesOnly yes limits SSH to the configured identity. Without it, SSH may try other available keys and prompt for token touches I did not expect.

Skipping the touch requirement

ssh-keygen -O no-touch-required -t ed25519-sk

There are a few requirements and limitations:

An ecdsa-sk keypair works the same way, with the usual reasons to prefer ed25519 over ECDSA still applying.

Reference: the Arch wiki's SSH keys page and Yubico's importing keys guide.