GnuPG

GnuPG (GNU Privacy Guard), also known as GPG, is a free implementation of the OpenPGP standard (RFC4880). GPG supports both symmetric and public-key cryptography, and typically operates using both.

GPG can be used for digital signing, authentication, and encryption of data. It is often used to encrypt and sign email messages, but can also be used with files or plaintext.

GPG supports hardware security devices with an OpenPGP module, such as the YubiKey.

includes which can be used as a ssh agent.

Additional software
GnuPG can be used alone, but integrates with a wide variety of software.

Mail clients

 * PinePGP.
 * Mutt - A small but very powerful text-based mail client.
 * Evolution - A GNOME Microsoft Outlook work alike.
 * KMail - KDE's mail client.
 * Installing KGPG might be of interest when using the KDE desktop environment. This small program allows for the generation of key pairs, importing keys from ASCII files, signing imported keys, exporting keys, among a few other nifty features.
 * Thunderbird - since version 78 has built-in encryption and no longer uses GnuPG.

Other

 * Tor - Tor can be used to contact keyservers anonymously. See Tor for configuration.

Configuration
GPG requires very little or no configuration to actually be used, most configuration tends to be centered around how the  and  behave. It is entirely optional, but recommended to adjust the configuration at  to increase security.

GPG Security
The following options are part of GLEP 63, some are already defaults:


 * utf8-strings - By default, GPG assumes arguments are ended in the same character set as display-charset, setting this option makes GPG interpret arguments at utf8
 * fixed-list-mode - Default since GnuPG2.0.10 - Do not merge the user ID and primary key when using --with-colon output mode
 * keyid-format - Shows the key ID in the specified format, options with 0x in front add that to the key ID to notate that it is hexadecimal.
 * personal-digest-preferences - Adjusts digest algorithm selection order, can be utilized to prefer stronger hash types when signing messages.
 * default-preference-list - Sets the preference list, used by setpref in the edit menu, can be utilized to prefer stronger methods.
 * verify-options - show-uid-validity is enabled by default, this option is used to adjust which types of validation are shown when keys are verified.
 * list-option - show-uid-validity is enabled by default, used to adjust which types of validation are shown when keys or signatures are listed.
 * cert-digest-algo - The digest algorithm that will be used when signing a key.
 * s2k-cipher-algo - The cipher algorithm which will be used by default for symmetric encryption.
 * s2k-digest-algo - The digest algorithm which will be used to mangle passphrases used for symmetric encryption.

Pinentry
is a helper application that uses to request the passphrase in a graphical window. It comes in many flavors, including: gtk3, qt5, tty, and curses.

If was installed with more than one popup window type, it is possible to choose between the windows with the  command:

GPG Agent
is packaged with which can be used to cache passphrases and manage unlocked key access.

GPG must be configured to use, this can be accomplished with:

Configuration changes can be reloaded with:

Using gpg-agent for SSH
To use 's SSH socket as for SSH, the SSH_AUTH_SOCK environment variable must be changed to point to the socket created by, this can be accomplished with:

will not use auth keys unless the keygrips are present in

Keygrips can be obtained by running:

Where the [A] key is the Authorization key, which is used for ssh login.

Changing pinentry for SSH logins
If is used over SSH, a graphical  password prompt will not come up in the login shell. This causes all operations that require a password to fail. The following snippet can be added to, this tells to use a curses prompt in the current shell. The snippet does not affect the settings when using local shells.

Generic
A generic method to autostart the GPG agent is to add  to a shell's rc file.

KDE
KDE manages the GPG agent using the following files:


 * 1) - System agent startup configuration
 * 2) - System agent shutdown configuration
 * 3) - User startup configuration
 * 4) - User shutdown configuration

The system configuration should contain a template for the GPG agent, it can be activated by uncommenting the following. To enable it for a single user, the configuration must be added to the user configuration.

Generating keys
can be used to generate a basic keypair, but using offers many useful options, such as the creation of elliptic curve keys.

Primary key
The GPG key creation process can be started with:

(11) ECC (set your own capabilities) is selected, so this first generated key will have sign and certify functions. Additional subkeys may be created for encryption and/or authentication capabilities.

By default, the first key should have the sign and certify options set:

After selecting q, (1) Curve 25519, or whatever key type is preferred can be selected.

Once the key type is selected, a expiration date should be set, setting it to 0 makes the key not expire, but setting an expiry can help with security, because if a key or passphrase is leaked, at least it eventually expires. This is not a substitute for key revocation.

Now that the key parameters have been defined, identification information must be provided.

Finally, a passphrase must be added to the key. This passphrase is important, because it is used to protect the private key.

Add an encryption key
To view and edit the created key, can be used. should also work, as long as Larry exists as a user in the detected keyrings.

Like with the creation of the sign and certify key, an exipriation date must be set, and GPG will prompt for the key password before adding the key.

Add an Authentication key
An Authentication key must be created to use GPG keys for SSH.

The authentication key can be created with:

Listing keys
Loaded public keys can be displayed with :

Private keys can be listed by using instead of :

Public keys
Public keys can be exported using --export, to export keys in armored form to stdout:

Or to a file:

Private Keys
Private keys can be exported with: and  can be used, where {key} is the uid or fingerprint associated with the key.

Importing keys
A key can be imported from a file using:, or from stdin using where the key can be pasted, and loaded by sending an EOF using +.

Gentoo release keys
Gentoo's GPG keys can be imported by running:

or

Loading a key from stdin
If a private key is being loaded, may prompt for the key passwords:

When importing key backups, it may make sense to mark the keys as ultimately trusted:

Signing keys
An imported key can be signed (certified) once it's verified.

Certifying a key implies it is trusted by the signer. The signed key can be sent back to the owner, and and the key could be redistributed, so anyone who trusts the key that was used to sign that key can implicitly trust the signed key.

Here, the repomirror key is being signed by Larry:

If Larry sent this signed key to someone, and someone else sent it to someone who knew and trusted Larry, they would have reason to trust the key, even if it was distributed using an insecure method.

When a key is marked as not trusted, signatures can also be used to designate that a certain user does not trust the key.

Sending keys to key servers
Now that a key has been generated, it is probably a good idea to send it to a world key server. There are a lot of key servers in the world and most of them exchange keys. In this next example Larry the cow's key will be sent to the pool.sks-keyservers.net server. Sending keys uses HTTP, so if a proxy is used for HTTP traffic do not forget to set it accordingly. The command for sending the key is:

Where 75447B14 is the ID of the key you would like to send. If a HTTP proxy is not needed then the  option can be removed.

Sending other people's keys that Larry has signed to the key server is also a good idea. We could send Luis Pinto's key to the key server. This way someone who trusts Larry's key can use the signature that he has placed there to trust Luis's key.

Getting keys from key servers
Now we are going to search for Gustavo Felisberto's key and add it to the keyring of Larry the cow.

From the server response it is possible to see few keys have been submitted to the key server, however only  is used. Now Larry the cow can get the key and sign it if he trusts it.

Refreshing existing keys
Existing PGP keys should be refreshed on a regular interval (twice a month is common). To refresh keys, define a key server with which to connect:

This command can be added to a cron job or systemd timer.

General usage
Some parameters that apply to several GPG commands are:


 * --armor - Adds PGP headers and footers to the data encoded as base64, useful when outputting to stdout
 * --local-user (-u) - Specifies the key to use for signing
 * --output (-o) - The output file, stdout is used otherwise

Signing
Signing verifies that someone with the private key associated with a known public key signed the data. If you trust that the key holder securely stores their key, you can be reasonably certain they sent a message signed with it. It can be used alongside encryption to ensure that a message can only be read by the intended recipients, and can be verified as coming from the correct source.

Data can be signed with --sign (-s):

Clear signing
Plaintext data can be signed, to verify the sender or publisher of the data. Unlike --sign, --clear--sign will keep the message in plaintext, putting the signature below it:

Detached signature
GPG can be used to sign data, keeping the signature separate from the data itself. The signature file can be distributed with the data. This is accomplished by using --detach-sig. The following command will output a file named sign-me.sig which contains a signature for the file sign-me:

Symmetric Encryption
GPG's Symmetric encryption can be used to cipher data to be accessible by anyone who possesses the cipher key.

Available cipher algorithms can be viewed with under Cipher::

A cipher type can be selected with --cipher-algo, and symmetric encryption can be used with --symmetric (-c):

Signed
Anyone who decrypts symmetrically encrypted data will not be able to verify the authenticity of it without a signature or external (non-GPG) methods being used. To sign this symmetrically encrypted data, use:

Asymmetric (Public Key) Encryption
GPG is generally used to perform asymmetric cryptography.

To send a message to a recipient, first view available public keys with :

To encrypt a message to Larry, use --encrypt (-e) with Larry as the recipient:

Signed
This message can also be signed, so the recipient can verify the sender:

Signature verification
Verifying the signature of signed data is very important, if this is not done, the sender of the data is not validated. Verification is generally done using the --verify flag:

Detached signature
Using the detached signing example, the file sign-me can be verified by running:

Decryption
The method for decrypting asymmetric or symmetric data is the same in GPG, the --decrypt (-d) flag is used:

Using the examples above:

Advanced features
There are some nice advanced features in GnuPG. To find them, open the file.

Uncomment the two lines above. With this modification, any time GnuPG needs to check a signature and does not find the public key on the local keyring it will contact the key server at hkps://keys.gentoo.org in an attempt to fetch the public key from the server.

Another nice command is. This will contact the key server defined in the configuration file and refresh the public keys in the local keyring from there. It is capable of searching for revoked keys, new IDs, and new signatures on keys. It is a wise idea to run this command once or twice a month; if a user revokes their key this can provide a notification the key can no longer be trusted.

About email signatures
GnuPG is used with email 95% of the time, either for signing/encrypting outgoing messages or for reading signed/encrypted messages.

There are two ways to sign/encrypt an email with GnuPG, the old way and the new way. In the old way messages would appear in plain text, with no possible formatting and attached files would be unsigned/unencrypted. Here is an example of a message signed the old way:

Messages this way are not good in today's world, where there are nice GUIs and email readers that understand HTML.

To solve this an addition to the MIME (Multipurpose Internet Mail Extensions) was created. This adds a field to the email that tells the mail reader that the full content of the message is signed and/or encrypted. The problem with this is that not all mail readers support such features. Some even mess up the content (Microsoft's Outlook is famous for not working with this).

Kgpg
Kgpg is a wonderful GUI for GnuPG. The main screen provides an area to paste text to sign or encrypt. The reverse is also true: ASCII armored text to be decrypted can also be entered.

Seahorse
Seahorse aims to be a GnuPG GUI interface for the GNOME desktop. The software has been evolving fast, but it still lacks many important features that can be found in Kgpg or the command line version.

KMail
If the  USE flag is set, KMail will be compiled with gpg support, and will be able to encrypt and decrypt inline PGP mails automatically as well as encrypting OpenPGP/MIME mails. To decrypt OpenPGP/MIME mails (which most users want) a GPG agent must be running.

To verify if KMail is properly configured navigate to. A GpgME-based backend should be listed and the OpenPGP checkbox should be checked. If it is listed but grayed out, click on. If the GpgME-based backend remains grayed out, KMail is not working properly.

When unable to get KMail to behave, see the official KMail PGP page for more information.

Claws-Mail
Claws Mail is very fast with big mailboxes and works well with GPG. The only problem is that it does not work with the old PGP signatures.

To use a GPG key with Claws-Mail navigate to. Once there choose which key to use, most users should go with the default key.

Thunderbird
Thunderbird - since version 78 has built-in encryption, but it can be made to use system GnuPG.

External resources

 * https://www.void.gr/kargig/blog/2013/12/02/creating-a-new-gpg-key-with-subkeys/ - An article explaining the creation of subkeys.
 * https://keys.openpgp.org/ - OpenPGP.org's PGP infrastructure. Key server is running the Hagrid keyserver software. Use hkps://keys.openpgp.org for accessing from.
 * https://keys.gentoo.org/ - Gentoo Infrastructure's official PGP key server.
 * https://sks-keyservers.net/ - Key servers ran by a former Gentoo developer.
 * https://www.gnupg.org/gph/en/manual.html - John Michael Ashley's "The GNU Privacy Handbook". Very good book for beginners.