Using Your YubiKey with OpenPGP

Applicable Products


These instructions will show you how to set up your YubiKey with OpenPGP. Before you begin, decide if you want to generate the private key on the YubiKey device, or if you want to generate the private key off of the YubiKey and then move the subkeys to the YubiKey. To allow for your PGP keys to be backed up, we recommend you generate them on your computer, not directly on the YubiKey. We also recommend that you personalize YubiKey by changing the PIN, setting the admin PIN, and so on, before you move the subkeys to the device.

Requirements

  • A compatible YubiKey.
  • A current version of the GnuPG software installed.
    • Windows: GPG4Win
    • macOS: GPG Tools
    • Linux: Pre-installed on all common distributions.

Instructions

Generating the Key on Your Local System (Recommended)

  1. Insert the YubiKey into the USB port.
  2. Enter the GPG command: gpg --expert --full-gen-key
  3. When prompted to specify the key type, enter 1 (for "RSA and RSA (Default)") and press Enter.
  4. Specify the size of key you want to generate. Do one of the following:
    • For a YubiKey NEO, enter 2048 and press Enter.
    • For a YubiKey 4 device, enter 4096 and press Enter.
  5. Specify the expiration date of the key, and press Enter. Verify the expiration date when prompted.
  6. Now you will enter your user information. Enter your Real Name and press Enter. Be sure to enter both your first and last name.
  7. Enter your Email Address and press Enter.
  8. If desired, enter a Comment about this key, and press Enter. (To leave the comment blank, just press Enter.)
  9. Review the information you entered, make any changes if necessary. If all information is correct, enter O (for Okay) and press Enter.
  10. A dialog box is displayed so you can enter the passphrase for your key. While the key is being generated, move your mouse around or type on the keyboard to gain enough entrophy.When the key has been generated, you will see several messages displayed. Make a note of the key ID, that is displayed in the message such as "gpg: key 1234ABC marked as ultimately trusted". The key ID in this case is 1234ABC and you will need this key ID to perform other operations.

To add an authentication key:

  1. Insert the YubiKey into the USB port.
  2. Enter the GPG command: gpg --expert --edit-key 1234ABC (where 1234ABC is the key ID of your key)
  3. Enter the command: addkey
  4. Enter the passphrase for the key. Note that this is the passphrase, and not the PIN or admin PIN.
  5. You are prompted to specify the type of key. Enter 8 for RSA.
  6. To add an authentication key, enter A, and then Q if you are finished.
  7. Specify the key size.
  8. Specify the expiration of the authentication key (this should be the same expiration as the key).
  9. When prompted to save your changes, enter y (yes).

To add a signing key:

  • Repeat the previous steps, except in step 6 enter an S for a signing key.

To create a backup of your key:

  1. Insert the YubiKey into the USB port.
  2. Enter the GPG command: gpg --export-secret-key --armor 1234ABC (where 1234ABC is the key ID of your key)
  3. Store the output from this in a safe place.

To import the key on your YubiKey:

  1. Insert the YubiKey into the USB port.
  2. Enter the GPG command: gpg --edit-key 1234ABC (where 1234ABC is the key ID of your key)
  3. Enter the command: toggle
  4. Enter the command: keytocard
  5. When prompted if you really want to move your primary key, enter y (yes).
  6. When prompted where to store the key, select 1. This will move the signature subkey to the PGP signature slot of the YubiKey.
  7. Enter the command: key 1
  8. Enter the command: keytocard
  9. When prompted where to store the key, select 2. This will move the encryption subkey to the YubiKey.
  10. Enter the command: key 1
  11. Enter the command: key 2
  12. Enter the command: keytocard
  13. When prompted where to store the key, select 3. This will move the authentication subkey to the YubiKey.
  14. Enter the command: quit
  15. When prompted to save your changes, enter y (yes). You have now saved your keyring to your YubiKey.

Generating Your PGP Key Directly on Your YubiKey

Warning: Generating the PGP on the YubiKey ensures that malware can never steal your PGP private key, but it means that the key can not be backed up so if your YubiKey is lost or damaged the PGP key is irrecoverable. 

  1. Insert the YubiKey into the USB port.
  2. Open Command Prompt (Windows) or Terminal (macOS / Linux).
  3. Enter the GPG command: gpg --card-edit
  4. At the gpg/card> prompt, enter the command: admin
  5. If you want to use keys larger than 2048 bits, run: key-attr
  6. Enter the command: generate
  7. When prompted, specify if you want to make an off-card backup of your encryption key. 
    • Note: This is a shim backup of the private key, not a full backup, and cannot be used to restore to a new YubiKey.
  8. Specify how long the key should be valid for (specify the number in days, weeks, months, or years).
  9. Confirm the expiration day.
  10. When prompted, enter your name.
  11. Enter your email address.
  12. If needed, enter a comment.
  13. Review the name and email, and accept or make changes.
  14. Enter the default admin PIN again. The green light on the YubiKey will flash while the keys are being written.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.