1617 Words

✉️ 3. How OpenPGP encrypts and signs

← 2. SSH · Overview · Next: 4. TLS, WireGuard, FIDO2 and Signal →

SSH has a live connection: both sides are online and can do a Diffie–Hellman exchange back and forth. Email and files do not. When you encrypt a file for Bob, Bob may be asleep, and he may read it in five years. There is no “back and forth”; you have one shot.

OpenPGP (the standard; GnuPG or gpg is the program most people use) solves this with Bob’s long-term public key. This page shows how, with real output of gpg --list-packets (GnuPG 2.4.8) for a test key “Alice Example”.


1. A key is a bundle

An OpenPGP “key” is not one key but a small bundle, signed together:

pub   ed25519/87A3C73BBE81F853 2026-11-29 [SC] [expires: 2027-11-29]
      AEAC1AA7E8CA08950B7BB8B787A3C73BBE81F853
uid                 [ultimate] Alice Example <alice@example.com>
sub   cv25519/3F9C0494C8F55AD6 2026-11-29 [E] [expires: 2027-11-29]
PartHereJob
Primary key (pub)Ed25519Certify (sign its own subkeys and user IDs, and other people’s keys) and Sign
User ID (uid)name + emailwho this key belongs to, bound to the key by a self-signature
Subkey (sub)Curve25519 (cv25519)Encrypt

Why separate keys? An Ed25519 key can only sign; encryption needs a Diffie–Hellman key (X25519). And it lets you keep the primary key offline (or on a YubiKey) and replace subkeys without losing your identity. A third kind of subkey, [A]uthenticate, is what lets gpg-agent act as your SSH key.

Inside the exported key you can see each part as a packet, and the signatures that tie them together:

:public key packet:
	version 4, algo 22, created 1795967857, expires 0
	pkey[0]: [80 bits] ed25519 (1.3.6.1.4.1.11591.15.1)
	pkey[1]: [263 bits]
:user ID packet: "Alice Example <alice@example.com>"
:signature packet: algo 22, keyid 87A3C73BBE81F853
	version 4, created 1795967857, md5len 0, sigclass 0x13
	hashed subpkt 27 len 1 (key flags: 03)
:public sub key packet:
	version 4, algo 18, created 1795967857, expires 0
	pkey[0]: [88 bits] cv25519 (1.3.6.1.4.1.3029.1.5.1)
	pkey[1]: [263 bits]
	pkey[2]: [32 bits]
:signature packet: algo 22, keyid 87A3C73BBE81F853
	version 4, created 1795967857, md5len 0, sigclass 0x18
	hashed subpkt 27 len 1 (key flags: 0C)
  • algo 22 is EdDSA, algo 18 is ECDH (the numbers come from the standard).
  • pkey[1] is the public point: 263 bits = one prefix byte + 32 bytes.
  • sigclass 0x13: “this user ID belongs to this key”. sigclass 0x18: “this subkey belongs to this key”. Both made by the primary key.
  • pkey[2] of the subkey: which hash and which cipher to use when wrapping a session key for it (section 2).

Fingerprint and key ID

The fingerprint is a hash of the primary public-key packet: for these (version 4) keys SHA-1, 160 bits. The long key ID is just its last 64 bits:

fingerprint  AEAC1AA7E8CA08950B7BB8B787A3C73BBE81F853
key ID                               87A3C73BBE81F853

The fingerprint is the identity of a key. Names and emails can be faked by anyone; the fingerprint cannot. Short 32-bit IDs (BE81F853) are easy to collide on purpose: never rely on them.


2. Encrypting: a random key, locked for each reader

Public-key maths is slow and limited in size, so OpenPGP does what every protocol does (part 1, §0): hybrid encryption.

  1. gpg makes a random session key, for example 256 bits for AES-256.
  2. The message (compressed, and usually signed first, see section 3) is encrypted with that session key: a fast symmetric cipher.
  3. The session key is locked separately for every recipient, with each recipient’s encryption subkey. That is a small packet per recipient (PKESK, public-key encrypted session key).

The result for one recipient:

:pubkey enc packet: version 3, algo 18, keyid 3F9C0494C8F55AD6      ← the session key, locked for Alice's subkey
	data: [263 bits]
	data: [392 bits]
:encrypted data packet:                                              ← the message, encrypted with the session key
	length: 208
	mdc_method: 2

Encrypt to five people and there are five pubkey enc packets in front of one encrypted message. gpg normally adds yourself as a recipient too (encrypt-to), otherwise you could not read your own sent mail.

How the session key is locked with Curve25519

Here the missing “back and forth” of Diffie–Hellman is solved with a trick: the sender plays both roles of the exchange at once, using a throwaway key.

Bob’s encryption subkey is a point $P_B = b \cdot G$; only Bob knows $b$.

Sender (Alice):

  1. Make a throwaway secret $v$ and its point $V = v \cdot G$.
  2. Calculate the shared point $S = v \cdot P_B$ (Diffie–Hellman with Bob’s long-term key).
  3. Turn $S$ into a key with a key derivation function (a hash over $S$ and the recipient’s key details): $k_{\mathrm{wrap}} = \mathrm{KDF}(S)$.
  4. Lock the session key with $k_{\mathrm{wrap}}$ (AES key wrap, RFC 3394).
  5. Send $V$ and the locked session key, then forget $v$.

Recipient (Bob):

$$S = b \cdot V = b \cdot v \cdot G = v \cdot (b \cdot G) = v \cdot P_B$$

Same point, same $k_{\mathrm{wrap}}$, so Bob unlocks the session key and then the message. In the packet above, data: [263 bits] is $V$ and data: [392 bits] is the locked session key.

It is exactly the ECDH of part 1, §5.4, except that Bob’s half was published long ago.

With an RSA subkey it is even simpler: the session key (with random padding) is encrypted directly, $c = m^e \bmod n$ (part 1, §3).

No forward secrecy

This has one big consequence. In SSH the key-exchange secrets are thrown away after the connection. In OpenPGP, Bob’s encryption subkey can decrypt every message ever sent to it. Whoever gets that subkey, today or in ten years, together with old copies of encrypted mail, reads all of it.

That is why it is wise to:

  • keep private keys on a smart card or YubiKey (the key cannot be copied off it),
  • give subkeys an expiry date and replace them now and then,
  • keep the primary key offline.

3. Signing

A signature proves that a message comes from the owner of a key and was not changed:

  1. Hash the message together with some metadata (time of signing, signature type, the issuer’s fingerprint).
  2. Sign that hash with the signing key: here Ed25519, a Schnorr-type signature (part 1, §6).

In our signed and encrypted message, after decrypting, you find:

:onepass_sig packet: keyid 87A3C73BBE81F853
	version 3, sigclass 0x00, digest 10, pubkey 22, last=1
:literal data packet:
	mode b (62), created 1795967857, name="msg.txt",
	raw data: 17 bytes
:signature packet: algo 22, keyid 87A3C73BBE81F853
	version 4, created 1795967857, md5len 0, sigclass 0x00
	digest algo 10, begin of digest 33 ff
	hashed subpkt 33 len 21 (issuer fpr v4 AEAC1AA7E8CA08950B7BB8B787A3C73BBE81F853)
	hashed subpkt 2 len 4 (sig created 2026-11-29)
	data: [255 bits]
	data: [250 bits]
  • onepass_sig says up front “a signature follows, with SHA-512 (digest 10) and EdDSA (pubkey 22)”, so the reader can hash the message while reading it, in one pass.
  • literal data is the actual file, msg.txt, 17 bytes.
  • The signature packet holds the two halves of the Ed25519 signature: $R$ and $s$.
  • begin of digest 33 ff: the first two bytes of the hash, a quick check before doing the real signature check.

The order is sign, then encrypt: the signature sits inside the encryption, so only the recipient sees who signed it.

The same signature format is used for Git commit signing, signed software packages (apt, dnf), and gpg --detach-sign for release files.


4. Trust: who owns this key?

The maths proves that the message was signed by key AEAC…F853. It does not prove that this key belongs to the real Alice. Every system has to solve that separately:

SystemHow you know whose key it is
SSHtrust on first use, then known_hosts (part 2, ⑤)
TLScertificate authorities (part 4)
OpenPGPyou decide: compare fingerprints in person, or trust people who signed the key (web of trust), or look it up via WKD / keys.openpgp.org
  • Fingerprint check: read the fingerprint out loud, over a channel you trust. The most reliable method.
  • Web of trust: you sign keys you have checked; others who trust you can rely on your signature. Powerful on paper, little used in practice.
  • keys.openpgp.org only publishes a user ID after the email address has been confirmed. WKD (Web Key Directory) lets a domain publish keys for its own addresses, at a fixed URL under https://domain/.well-known/openpgpkey/.

5. Versions: RFC 4880, RFC 9580 and LibrePGP

  • RFC 4880 (2007) is the version everybody supports: the version 4 keys shown above, SHA-1 fingerprints, and the “MDC” integrity check (mdc_method: 2).
  • RFC 9580 (2024) modernises it: version 6 keys with SHA-256 fingerprints, Ed25519 and X25519 as first-class algorithms, real authenticated encryption (AEAD, e.g. OCB), and Argon2 to protect secret keys with a passphrase. Implemented by Sequoia-PGP, OpenPGP.js, GopenPGP and others.
  • GnuPG did not adopt RFC 9580. It follows its own variant, LibrePGP, which has an incompatible version 5 key format.

For normal use with version 4 keys (as above) everything still works together. Just know that “OpenPGP” is currently two slightly different standards.


6. See it yourself

# a test key in a throwaway keyring, so your real keyring stays untouched
export GNUPGHOME="$(mktemp -d)"
gpg --quick-gen-key 'Alice Example <alice@example.com>' ed25519 sign,cert 1y
gpg --quick-add-key <FINGERPRINT> cv25519 encr 1y
gpg --list-keys --keyid-format long

echo "Meet me at noon." > msg.txt
gpg -r alice@example.com --sign --encrypt msg.txt     # → msg.txt.gpg
gpg --list-packets msg.txt.gpg                        # the packets shown above
gpg --list-packets <(gpg --export alice@example.com)  # the key bundle

Related notes: GnuPG · OpenPGP cheat sheet · YubiKey with OpenPGP · age, a simpler alternative

📬 Send me an encrypted message: see my contact page. The other standard for encrypted email, with certificates instead of a web of trust, is 5. S/MIME.


Sources