content/blog/2022-07-14-gnupg.org

e8577d98faff1bed72ba6f2d204b8b44c6827c23
cleberg.net/content/blog/2022-07-14-gnupg.org rendered · source · history · blame · raw

309 lines · 11832 bytes

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