cmc/cleberg.net
My personal web garden & blog.
clone: git clone https://gitbay.org/cmc/cleberg.net.git
main: content/blog/2022-07-14-gnupg.org · raw
1#+date: [2022-07-14 Thu 00:00:00]
2#+title: GnuPG: Encryption, Signing, Web of Trust
3#+description: How GnuPG works and how I use it for encryption and signing.
4#+slug: gnupg
5#+filetags: :linux:security:
6
7* The History of GPG
8
9[[https://gnupg.org/][GNU Privacy Guard]], also known as GnuPG and GPG, is a free ("free" as in both
10speech and beer) software that fully implements the OpenPGP Message Format
11documented in [[https://www.rfc-editor.org/rfc/rfc4880][RFC 4880]].
12
13I won't go in-depth on the full history of the software in this post, but it is
14important to understand that GPG is not the same as PGP (Pretty Good Privacy),
15which is a different implementation of RFC 4880. However, GPG was designed to
16interoperate with PGP.
17
18GPG was originally developed in the late 1990s by [[https://en.wikipedia.org/wiki/Werner_Koch][Werner Koch]] and has
19historically been funded generously by the German government.
20
21Now that we have all the high-level info out of the way, let's dive into the
22different aspects of GPG and its uses.
23
24* Encryption Algorithms
25
26GPG supports a wide range of different encryption algorithms, including
27public-key, cipher, hash, and compression algorithms. The support for these
28algorithms has grown since the adoption of the Libgcrypt library in the 2.x
29versions of GPG.
30
31As you will be able to see below in an example of a full key generation with the
32GPG command line tool, GPG recommends the following algorithms to new users:
33
34#+begin_src sh
35Please select what kind of key you want:
36 (1) RSA and RSA
37 (2) DSA and Elgamal
38 (3) DSA (sign only)
39 (4) RSA (sign only)
40 (9) ECC (sign and encrypt) *default*
41 (10) ECC (sign only)
42#+end_src
43
44I am not doing an in-depth explanation here in order to keep the focus on GPG
45and not encryption algorithms. If you want a deep dive into cryptography or
46encryption algorithms, please read my other posts:
47
48- [[https://cleberg.net/blog/aes-encryption.html][How AES Encryption Works]] (2018)
49- [[https://cleberg.net/blog/cryptography.html][A Practical Guide to Encryption, Keys, and Secure Communication]] (2020)
50
51** Vulnerabilities
52
53As of 2022-07-14, there are a few different vulnerabilities associated with GPG
54or the libraries it uses:
55
56- GPG versions 1.0.2--1.2.3 contains a bug where "as soon as one (GPG-generated)
57 ElGamal signature of an arbitrary message is released, one can recover the
58 signer's private key in less than a second on a PC." ([[https://www.di.ens.fr/~pnguyen/pub_Ng04.htm][Source]])
59- GPG versions prior to 1.4.2.1 contain a false positive signature verification
60 bug. ([[https://lists.gnupg.org/pipermail/gnupg-announce/2006q1/000211.html][Source]])
61- GPG versions prior to 1.4.2.2 cannot detect injection of unsigned data. (
62 [[https://lists.gnupg.org/pipermail/gnupg-announce/2006q1/000218.html][Source]])
63- Libgcrypt, a library used by GPG, contained a bug which enabled full key
64 recovery for RSA-1024 and some RSA-2048 keys. This was resolved in a GPG
65 update in 2017. ([[https://lwn.net/Articles/727179/][Source]])
66- The [[https://en.wikipedia.org/wiki/ROCA_vulnerability][ROCA Vulnerability]] affects RSA keys generated by YubiKey 4 tokens.
67 ([[https://crocs.fi.muni.cz/_media/public/papers/nemec_roca_ccs17_preprint.pdf][Source]])
68- The [[https://en.wikipedia.org/wiki/SigSpoof][SigSpoof Attack]] allows an attacker to spoof digital signatures. ([[https://arstechnica.com/information-technology/2018/06/decades-old-pgp-bug-allowed-hackers-to-spoof-just-about-anyones-signature/][Source]])
69- Libgcrypt 1.9.0 contains a severe flaw related to a heap buffer overflow,
70 fixed in Libgcrypt 1.9.1 ([[https://web.archive.org/web/20210221012505/https://www.theregister.com/2021/01/29/severe_libgcrypt_bug/][Source]])
71
72*** Platforms
73
74Originally developed as a command-line program for *nix systems, GPG now has a
75wealth of front-end applications and libraries available for end-users. However,
76the most recommended programs remain the same:
77
78- [[https://gnupg.org][GnuPG]] for Linux (depending on distro)
79- [[https://gpg4win.org][Gpg4win]] for Windows
80- [[https://gpgtools.org][GPGTools]] for macOS
81
82* Creating a Key Pair
83
84In order to create a GPG key pair, a user would first need to install GPG on
85their system. If we're assuming that the user is on Fedora Linux, they would
86execute the following:
87
88#+begin_src sh
89sudo dnf install gpg
90#+end_src
91
92Once installed, a user can create a new key pair with the following command(s):
93
94#+begin_src sh
95gpg --full-generate-key
96#+end_src
97
98GPG will walk the user through an interactive setup that asks for an algorithm
99preference, expiration date, name, and email to associate with this key.
100
101See the following example key set-up for a default key generation using the
102GnuPG command-line interface:
103
104#+begin_src sh
105gpg (GnuPG) 2.3.6; Copyright (C) 2021 Free Software Foundation, Inc.
106This is free software: you are free to change and redistribute it.
107There is NO WARRANTY, to the extent permitted by law.
108
109Please select what kind of key you want:
110 (1) RSA and RSA
111 (2) DSA and Elgamal
112 (3) DSA (sign only)
113 (4) RSA (sign only)
114 (9) ECC (sign and encrypt) *default*
115 (10) ECC (sign only)
116 (14) Existing key from card
117Your selection? 9
118Please select which elliptic curve you want:
119 (1) Curve 25519 *default*
120 (4) NIST P-384
121Your selection? 1
122Please specify how long the key should be valid.
123 0 = key does not expire
124 <n> = key expires in n days
125 <n>w = key expires in n weeks
126 <n>m = key expires in n months
127 <n>y = key expires in n years
128Key is valid for? (0) 0
129Key does not expire at all
130Is this correct? (y/N) y
131
132GnuPG needs to construct a user ID to identify your key.
133
134Real name: John Doe
135Email address: johndoe@example.com
136Comment: test key
137You selected this USER-ID:
138 "John Doe (test key) <johndoe@example.com>"
139
140Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O
141We need to generate a lot of random bytes. It is a good idea to perform
142some other action (type on the keyboard, move the mouse, utilize the
143disks) during the prime generation; this gives the random number
144generator a better chance to gain enough entropy.
145We need to generate a lot of random bytes. It is a good idea to perform
146some other action (type on the keyboard, move the mouse, utilize the
147disks) during the prime generation; this gives the random number
148generator a better chance to gain enough entropy.
149gpg: revocation certificate stored as 'example.rev'
150public and secret key created and signed.
151
152pub ed25519 2022-07-14 [SC]
153 E955B7700FFC11EF51C2BA1FE096AACDD4C32E9C
154uid John Doe (test key) <johndoe@example.com>
155sub cv25519 2022-07-14 [E]
156#+end_src
157
158Please note that graphical apps may differ slightly from the GPG command-line
159interface.
160
161* Common Usage
162
163As noted in RFC (Request for Comments) 4880, the general functions of OpenPGP
164are as follows:
165
166- digital signatures
167- encryption
168- compression
169- Radix-64 conversion
170- key management and certificate services
171
172From this, you can probably gather that the main use of GPG is for encrypting
173data and/or signing the data with a key. The purpose of encrypting data with GPG
174is to ensure that no one except the intended recipient(s) can access the data.
175
176Let's explore some specific GPG use-cases.
177
178** Email
179
180One of the more popular uses of GPG is to sign and/or encrypt emails. With the
181use of a GPG keypair, you can encrypt a message, its subject, and even the
182attachments within.
183
184The first process, regarding the signing of a message without any encryption, is
185generally used to provide assurance that an email is truly coming from the
186sender that the message claims. When I send an email, and it's signed with my
187public key, the recipient(s) of the message can verify that the message was
188signed with my personal key.
189
190The second process, regarding the actual encryption of the message and its
191contents, works by using a combination of the sender's keys and the recipient's
192keys. This process may vary slightly by implementation, but it most commonly
193uses asymmetric cryptography, also known as public-key cryptography. In this
194version of encryption, the sender's private key to sign the message and a
195combination of the sender's keys and the recipient's public key to encrypt the
196message.
197
198If two people each have their own private keys and exchange their public keys,
199they can send encrypted messages back and forth with GPG. This is also possible
200with symmetric cryptography, but the process differs since there are no key
201pairs.
202
203Implementation of email encryption varies greatly between email clients, so you
204will need to reference your email client's documentation to ensure you are
205setting it up correctly for that specific client.
206
207** File Encryption
208
209As noted in the section above regarding emails, GPG enables users to be able to
210send a message to each other if they are both set-up with GPG keys. In this
211example, I am going to show how a user could send a file called
212=example_file.txt= to another user via the recipient's email.
213
214The sender would find the file they want to send and execute the following
215command:
216
217#+begin_src sh
218gpg --encrypt --output example_file.txt.gpg --recipient \
219recipient@example.com example_file.txt
220#+end_src
221
222Once received, the recipient can decrypt the file with the following command:
223
224#+begin_src sh
225gpg --decrypt --output example_file.txt example_file.txt.gpg
226#+end_src
227
228** Ownership Signatures
229
230One important aspect of GPG, especially for developers, is the ability to sign
231data without encrypting it. For example, developers often sign code changes when
232they commit the changes back to a central repository, in order to display
233ownership of who made the changes. This allows other users to look at a code
234change and determine that the change was valid.
235
236In order to do this using [[https://git-scm.com][Git]], the developer simply needs to alter the =git
237commit= command to include the =-S= flag. Here's an example:
238
239#+begin_src sh
240git commit -S -m "my commit message"
241#+end_src
242
243As an expansion of the example above, Git users can configure their environment
244with a default key to use by adding their GPG signature:
245
246#+begin_src sh
247git config --global user.signingkey XXXXXXXXXXXXXXXX
248#+end_src
249
250If you're not sure what your signature is, you can find it titled =sig= in the
251output of this command:
252
253#+begin_src sh
254gpg --list-signatures
255#+end_src
256
257** File Integrity
258
259When a person generates a signature for data, they are allowing users the
260ability to verify the signature on that data in the future to ensure the data
261has not been corrupted. This is most common with software applications hosted on
262the internet - developers provide signatures so that users can verify a website
263was not hijacked and download links replaced with dangerous software.
264
265In order to verify signed data, a user needs to have:
266
2671. The signed data
2682. A signature file
2693. The public GPG key of the signer
270
271Once the signer's public key is imported on the user's system, and they have the
272data and signature, they can verify the data with the following commands:
273
274#+begin_src sh
275# If the signature is attached to the data
276gpg --verify [signature-file]
277
278# If the signature is detached as a separate file from the data
279gpg --verify [signature-file] [original-file]
280#+end_src
281
282*** Finding Public Keys
283
284In order to use GPG with others, a user needs to know the other user(s) keys.
285This is easy to do if the user knows the other user(s) in person, but may be
286hard if the relationship is strictly digital. Luckily, there are a few options.
287The first option is to look at a user's web page or social pages if they have
288them.
289
290Otherwise, the best option is to use a keyserver, such as:
291
292- [[https://pgp.mit.edu][pgp.mit.edu]]
293- [[https://keys.openpgp.org][keys.openpgp.org]]