Post navigation

GNU/Linux Crypto: GnuPG Keys

Many tools that use cryptography on GNU/Linux and the internet revolve around the
Pretty Good Privacy software standard (OpenPGP). The GNU Privacy Guard
(GnuPG or GPG) is a popular free software implementation of this standard.

You can install GnuPG with its gpg(1) frontend on Debian like so:

# apt-get install gnupg

You can do a lot of very cool things with GPG, but it boils down to four
central ideas:

Generation of keypairs, randomly-generated and mathematically linked
pairs of files, one of which is kept permanently secret (the private
key) and one of which is published (the public key). This is the
basis of asymmetric key cryptography.

Managing keys, both your own public and private key, along with other
people’s public keys, so that you can verify others’ messages and files, or
encrypt them so that only those people can read them. This might include
publishing your public key to online keyservers, and getting people to sign
it to confirm that the key is really yours.

Signing files and messages with your private key to enable others to
verify that a file or message was authored or sighted by you, and not
edited in transmission over untrusted channels like the internet. The
message itself remains readable to everybody.

Encrypting files and messages with other people’s public keys, so that
only those people can decrypt and read them with their private keys. You
can also sign such messages with your own private key so that people can
verify that it was sent by you.

We’ll run through the fundamentals of each of these. We won’t concern ourselves
too much with the mathematics or algorithms behind these operations; the
Wikipedia article for asymmetric key cryptography explains this very
well for those curious for further details.

Generating a keypair

Let’s start by generating a 4096-bit RSA keypair, which should be more than
sufficient for almost everyone at the time of writing. We’ll observe a few of
the best practices recommended for the Debian developers.

Doing this on a private, up-to-date desktop machine is best, as it’s easier to
generate entropy this way. It’s still possible on an SSH-only headless server,
but you may have to resort to less cryptographically sound methods to generate
proper randomness.

Create or edit the file ~/.gnupg/gpg.conf on your system, and add the
following lines:

The expiry date is up to you. Good practice is to set an expiry date about a
year out, because as long as you have access to the private key material, you
can update the expiry date indefinitely, even if it’s already expired. For
this particular example, we’ll set an expiry date one year out:

Next, we’re prompted for some basic information to name the key. In almost all
circumstances you should use your real name, as without a real-world means to
actually verify your identity, public keys are much less useful long-term. For
the comment, you can include the key’s purpose, or your public aliases, or any
other information relevant to the key:

Key passphrase

Next, we’re prompted for a passphrase to encrypt the key, so that if it ever
falls into the wrong hands, nobody will be able to use it without knowing the
passphrase.

You need a Passphrase to protect your secret key.

Choose a sequence of random words, or possibly a unique sentence you can
easily memorise in any language, the longer the better. Don’t choose
anything that might be feasibly guessable, like proverbs or movie quotes. You
will also need to remember how you typed the passphrase exactly; I recommend
using all-lowercase and no punctuation. Wikipedia has a few guidelines
here.

You’ll need to type the passphrase twice to confirm it, and it won’t echo on
your terminal, much as if you were typing a password.

Entropy generation

Finally, the system will prompt us to generate some entropy:

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
Not enough random bytes available. Please do some other work to give
the OS a chance to collect more entropy! (Need 283 more bytes)

This step is necessary for the computer to generate sufficient random
information to ensure that the private key being generated could not feasibly
be reproduced. Moving the mouse around and using the keyboard on a desktop
system is ideal, but generating any kind of hardware activity (including
spinning disks up) should do the trick. Running expensive find(1) operations
over a filesystem (with contents that couldn’t be reasonably predicted or
guessed) helps too.

This step benefits from patience. You might find discussion online about
forcing the use of the non-blocking PRNG random device /dev/urandom instead,
using a tool like rngd(1). This definitely speeds up the process, but if
you’re going to be using your key for anything serious, I recommend actually
interacting with the computer and using hardware noise to seed the randomness
adequately, if you can.

When adequate entropy is read and the key generation is done, you’ll be
presented with some details for your master signing key pair and its encrypting
subkey pair, and the private and public keys for each are automatically added
to your keyring for use:

The directory ~/.gnupg contains the managed keys. It’s very, very important
to keep this directory private and to back it up securely, preferably to
removable media that you keep in some physically secure place. Don’t lose it!

In most contexts in GnuPG, you can refer to a key by the name of its owner, or
by its eight-digit hex ID. I prefer the latter method. Here, the short ID of my
main key is 040FE79B. While you shouldn’t use this for any actual
verification, it’s sufficiently unique that you can use it to identify
a specific key on your keyring with which you want to work.

For example, if we want to provide someone with a copy of our public key,
a friendly way to do so is to export it in ASCII format with --armor,
providing the appropriate key’s short ID:

$ gpg --armor --export 040FE79B > tom-ryder.public.asc

While you can export private keys the same way with --export-secret-key, you
should never, ever provide anyone with your private key, so this shouldn’t be
necessary.

Revocation certificate

After generating your keys, you should generate a revocation certificate:

$ gpg --output revoke.asc --gen-revoke 040FE79B
sec 4096R/040FE79B 2013-03-23 Tom Ryder (Test Key Only) <tom@sanctum.geek.nz>
Create a revocation certificate for this key? (y/N) y
Please select the reason for the revocation:
0 = No reason specified
1 = Key has been compromised
2 = Key is superseded
3 = Key is no longer used
Q = Cancel
(Probably you want to select 1 here)
Your decision? 1
Enter an optional description; end it with an empty line:
>
Reason for revocation: Key has been compromised
(No description given)
Is this okay? (y/N) y
You need a passphrase to unlock the secret key for
user: "Tom Ryder (Test Key Only) <tom@sanctum.geek.nz>"
4096-bit RSA key, ID 040FE79B, created 2013-03-23
ASCII armored output forced.
Revocation certificate created.
Please move it to a medium which you can hide away; if Mallory gets
access to this certificate he can use it to make your key unusable.
It is smart to print this certificate and store it away, just in case
your media become unreadable. But have some caution: The print system of
your machine might store the data and make it available to others!

You should store the resulting revoke.asc file somewhere safe. You can use
this certificate to revoke your key later on if the private key is ever
compromised, so that people know the key should no longer be used or trusted.
You may even like to print it out and keep a hard copy, as the output of gpg
suggests.

With the above setup done, we can proceed with some basic usage of GnuPG, as
discussed in the next article.

Subkeys

In the output of both commands, you’ll note we actually have two private and
two public keys. The sub line refers to the encryption subkey
automatically generated for you. The master key is used for cryptographic
signing, and the subkey for encryption; this is how GnuPG does things by
default with RSA keypairs.

For extra security, it might be appropriate to physically remove the master
private key from your computer, and instead use a second generated subkey for
signing files as well. This is desirable because it allows you to keep the
master key secure on some removable media (preferably with a backup), and not
loaded on your main computer in case you get compromised.

This means you can sign and encrypt files as normal with your signing subkey
and encryption subkey. If those keys ever get compromised, you can simply
revoke them and generate new ones with your uncompromised master key; everyone
who has signed your public master key or otherwise indicated they trust it will
not have to do that all over again.