GNU Privacy Guard (GPG or GnuPG)
Free-software replacement for Symantec’s PGP cryptographic software suite. The software is compliant with RFC 4880, the IETF standards-track specification of OpenPGP. Modern versions of PGP are interoperable with GnuPG and other OpenPGP-compliant systems.
GnuPG is part of the GNU Project and received major funding from the German government in 1999.
GnuPG is a hybrid-encryption software program because it uses a combination of conventional symmetric-key cryptography for speed, and public-key cryptography for ease of secure key exchange, typically by using the recipient’s public key to encrypt a session key which is used only once. This mode of operation is part of the OpenPGP standard and has been part of PGP from its first version.
The GnuPG 1.x series uses an integrated cryptographic library, while the GnuPG 1.x series replaces this with Libgcrypt.
— Wikipedia
Basic commands
You can generate GPG key with gpg --generate-key
command.
Or gpg --full-generate-key --expert
for more options.
If you use NixOS installer, you may need to temporarily install GnuPG and
pinentry-curses
:
nix-shell -p gnupg pinentry-curses
gpg --pinentry-mode loopback ...
To check GPG keys, installed on your system run gpg --list-key
.
To export public key with specific email,
run gpg --output <path/key_name.gpg> --export --armor <email>
.
To export private key with specific email,
run gpg --output <path/key_name.gpg> --export-secret-key --armor <email>
.
If required backup, recommended using this command:
gpg --output <path/key_name.gpg> --armor --export-secret-keys \ --export-options export-backup <email>
If required, you can trust your own key with this algorithm:
gpg --edit-key <email>
trust
5
(ultimate trust, in our case)y
(yes)quit
Subkeys Usage
Being able to store the primary key offline or a more secure device. If a machine with a subkey is harmed, you can easily revoke the subkey without all the hassles of revoking your primary key (sharing a new key, getting new signatures, …).
Usually master key can have only this access rights:
C
(Certify)S
(Sign)
Subkeys can have:
E
(Encrypt)A
(Authenticate)
Having different subkeys on different machines, for example a signing subkey on a build server. Again, revoking single keys is easy.
Using a larger primary key for long lifetime, and shorter, but faster subkeys for day-to-day usage.
Some algorithms do not support both encrypting and signing. For example, a DSA primary key requires another key for encryption, typically paired with ElGamal.
Adding subkey
GnuPG backup steps
First, before we begin, [email protected] represents your GPG key ID. This could be your email address or the key ID.
Indeed, your private key is critical, but other files are also important.
If you want save your user attributes, signatures (including local
signatures), and ownertrust values use --export-options backup
on export
and --import-options restore
on import.
- Export keys and ownertrust:
These commands are intended to export your keys and trust level. Public and private keys are used for encryption/decryption, and the trust level determines how much you trust other keys in your keyring.
Run the following commands in your command line (Command Prompt for Windows, Terminal for macOS and Linux). Replace “[email protected]” with your GPG key ID:
- Import keys and ownertrust:
If you need to restore your keys and trust level (e.g., after reinstalling the system or on a new computer), use the following commands:
WARNING: import keys without ssh-agent, for example not through ssh connection
(otherwise there possible issues if used NOT pinentry-curses) and set correct
~/.config/gnupg/
permissions (700
).
- Ultimately trust the imported key (optional if ownertrust is not imported):
This step is needed to set the ultimate trust level for your keys. Run the following command, then type trust, then 5 (which means “ultimate trust”) and hit Enter:
- Import revocation certificate (optional, if you need revoke key):
- Delete keys (optional!)
If you want to delete the Private Key file you created, you can use the Shred utility to delete the file permanently and make it harder to be recovered by overwriting it:
GnuPG to SSH
- Find fingerprint of your RSA key:
gpg --list-secret-keys --keyid-format short
orgpg -K
- Next, you can use openpgp2ssh tool distributed in with monkeyshpere project:
- You can now extract ssh public key using:
ssh-keygen -y -f id_rsa > id_rsa.pub
Uploading the key to keyserver
This section explains how to upload your public key to a keyserver so that anyone can download it. Once you have uploaded it to one keyserver, it will propagate to the other keyservers. Eventually most of the keyservers will have a copy of your key. You can accelerate the process by uploading your key to several keyservers.
To upload the key, enter:
Note that keyserver.ubuntu.com is only reachable via IPv4.
Test GnuPG
You can quickly test encrypt/decrypt commands with this command:
echo "test"|gpg -r [email protected] --encrypt|gpg --decrypt --no-comment -q --no-verbose
# output should be "test"
A useful gpg option to experiment is --homedir
:
This way your default keyring, trust database, etc. get ignored, and you have a fresh keyring with which you can test if the conversion/import was successful.
Fix permissions
To fix the gpg: WARNING: unsafe permissions on homedir '/home/path/to/user/.gnupg'
error
Key servers list:
- OpenPGP Keyserver. Ubuntu keyserver: federated, no verification, keys cannot be deleted.
- Mailvelope Key Server Mailvelope keyserver: central, verification of email IDs, keys can be deleted.
- keys.openpgp.org. Central, verification of email IDs, keys can be deleted, no third-party signatures (i.e. no Web of Trust support).
- Other: Key server (cryptographic) - Wikipedia.
How to import an SSH ed25519 key to GPG?
- First you need generate or use existing key.
- Setup gpg-agent with ssh support
- Add your SSH private key to GPG by
ssh-add ed25519-filename
. NOTE: that if you are asked for a new password, it is recommended that you use the same as your GPG master password. - Check keygrips
gpg -K --with-keygrip
Assume that you have already setup gpg-agent
.
- Setup
SSH_AUTH_SOCK
andGPG_TTY
environment variables byexport SSH_AUTH_SOCK=$(gpgconf --list-dirs agent-ssh-socket)
andexport GPG_TTY=$(tty)
. This optional, usually I have gpg-agent with ssh support. - Add your SSH private key to GPG by
ssh-add ed25519-filename
. NOTE: that if you are asked for a new password, it is recommended that you use the same as your GPG master password. - Identify the keygrip of the private key that you added by step 2.
- Check the keygrips of your existing GPG private keys by
gpg -K --with-keygrip
. - Check private key files by
ls -l $(gpgconf --list-dirs homedir)/private-keys-v1.d
. - The filename of step 3.2. that is not in the keygrips of step 3-1 is the keygrip that you added by step 2.
- Setup the key added by
ssh-add
to a subkey. - Start GPG by
gpg --expert --edit-key "Your-existing-GPG-Key-ID"
. - Enter
addkey
command. - Select “Existing key”. (Maybe “13”)
- Enter the keygrip identified by step 3.
- Set the capability as you wish and Finish.
- Then, answer the questions appropriately.
Note that you should answer
Y
toReally create?
andSave changes?
etc. - Verify that the import was successful by
gpg -K --with-keygrip
. You should be able to see the keygrip that you just added.
Offline master key strategy
Based on guide, how to manage primary GnuPG key offline 1.
-
Store your master key on some offline resources, such as USB stick (for example in encrypted container). To achieve this you can export your keys and validate them on another machine, which is not connected to internet.
-
Remove master key from keyring (guide for GnuPG 2.1)
BEFORE DELETE MASTER KEY, CHECK THAT YOU HAVE A BACKUP OF IT! WHICH GENERATED IN PREVIOUS STEP!
Ensure that the private master key has been removed, gpg2 --list-secret-keys
, you will see sec#
instead of sec
, or try to add
adduid
for validation.
- Use the offline master key Follow mount encrypted file container guide to open storage with master key with specific directory.
And work with master key in this directory.
References
- OpenPGP for application developers
- Keybase is secure messaging and file-sharing.
- An abridged guide to using ed25519 PGP keys with GnuPG and SSH
- GPG Keys - Create & Use
- GnuPG
- openssh - How to import an SSH ed25519 key to GPG?
- fa - backup your PGP key with pencil and paper
- Subkeys
- Using an offline GnuPG master key