GPG/PGP: signing, encryption, web of trust

Updated November 1, 2026

GPG is an open-source implementation of OpenPGP. A key pair (RSA/ECDSA), public for verify/encrypt, private for sign/decrypt. The keyring lives locally, web-of-trust runs between people. You use it for git-commit signing, package-signing (apt/rpm), email.

What OpenPGP is

OpenPGP (RFC 4880) is a standard for asymmetric cryptography over data at rest: signatures and encryption. GPG (GnuPG) is the main FOSS implementation on Linux/macOS. Alternatives: Sequoia-PGP (Rust, new), RNP (for Thunderbird).

Historical purpose:

  • Encrypted email between people without a CA
  • Signing release artifacts (tar.gz, deb, rpm)
  • Web of trust, trust through signed public keys

Today:

  • Git-commit signing (git commit -S)
  • Package signing: APT repo, RPM repo, npm-publish
  • secrets-tool (pass) for storing passwords
  • Sign tarball in release notes (historically, for verification)

Email encryption is mostly dead, replaced by Signal/Matrix.

Key pair

A GPG key is a bundle: a master key (signs the sub-keys) plus N sub-keys (encryption, signing, authentication).

Algorithms:

  • RSA 4096, the classic, compatible with everything
  • EdDSA Ed25519 for signing, modern, faster, smaller
  • Curve25519 (ECDH) for encryption, modern

The default in recent GnuPG is Ed25519 + Curve25519.

Create:

gpg --full-generate-key
# interactive: choose type, size, expiry, name+email

Or non-interactive:

gpg --batch --gen-key <<EOF
Key-Type: EDDSA
Key-Curve: ed25519
Subkey-Type: ECDH
Subkey-Curve: cv25519
Name-Real: Alice Example
Name-Email: alice@example.com
Expire-Date: 2y
Passphrase: ...
EOF

Inspect:

gpg --list-keys                     # public
gpg --list-secret-keys              # with private
gpg --list-keys --keyid-format LONG # 16-hex fingerprint

Fingerprint and keyID

Each key has:

  • Full fingerprint, 40 hex characters (SHA1, 20 bytes; or SHA256 in recent versions): EB42 3B25 14F1 6F5C ...
  • Long key ID, last 16 hex characters: EB423B2514F16F5C
  • Short key ID, last 8: 14F16F5C (dangerous, easy to collide in spoofing, do NOT use)

On import or verification, compare by full fingerprint, not by name/email/short-id.

Signing (sign)

# Detached signature (separate .sig file)
gpg --detach-sign release-1.2.tar.gz
# -> release-1.2.tar.gz.sig

# Embedded
gpg --sign message.txt              # binary + signature inside
gpg --clearsign message.txt          # ASCII-armor (readable)

Verify:

gpg --verify release-1.2.tar.gz.sig release-1.2.tar.gz
# gpg: Good signature from "Alice <alice@example.com>"
# gpg: WARNING: This key is not certified with a trusted signature!

The warning "not certified" appears because the key is not in your web-of-trust. This is expected on first import, and verifying the key identity is on you.

Encryption (encrypt)

The key point: encrypt with the recipient's public key:

# Import Bob's public key
gpg --import bob-public.asc

# Encrypt for him
gpg --encrypt --recipient bob@example.com message.txt
# -> message.txt.gpg

# Bob decrypts with his private key
gpg --decrypt message.txt.gpg

You can encrypt for several recipients at once:

gpg --encrypt -r alice@... -r bob@... -r carol@... file

Internally, a random session key (AES) plus N copies of that session key, each encrypted with a recipient's public key.

Keyring

Local storage in ~/.gnupg/:

~/.gnupg/
  ├── pubring.kbx        # public keys
  ├── private-keys-v1.d/ # private (new format)
  ├── trustdb.gpg        # web-of-trust info
  └── gpg-agent.conf     # agent settings

Operations:

gpg --import alice.asc                # import a key
gpg --export alice@example.com > alice.asc      # export public
gpg --export-secret-keys alice > alice-private.asc # backup private
gpg --delete-key alice@example.com    # delete public
gpg --delete-secret-key ...           # delete private

ASCII-armor (-a or --armor) is a base64 wrapper for plaintext compatibility (email, pastebin):

-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----

Web of trust

The idea replaces a CA: you trust whoever is trusted by someone you trust.

When Alice meets Bob in person, checks his fingerprint, and signs his key with her own signature:

gpg --sign-key bob@example.com
# then publish Bob's updated key back
gpg --send-keys --keyserver hkps://keys.openpgp.org BOB_KEY_ID

Bob now imports it and sees Alice's signature. A third person who trusts Alice will now trust Bob through her signature.

Trust levels in gpg:

  • unknown, no data
  • never, explicitly do not trust (trust revoked)
  • marginal, N such signatures are required to trust in aggregate
  • full, one signature is enough
  • ultimate, your own key

Downsides:

  • Noticeably hard for users
  • Key-signing parties, a dead practice
  • Most people today just use TOFU (trust on first use)
  • Replaced for most cases by CA-style models (sigstore/cosign, keyless through OIDC)

Keyservers

In the past: upload a public key to a keyserver (hkps://pool.sks-keyservers.net), and anyone could download it. Now:

  • keys.openpgp.org, modern, valid email confirmation
  • keyserver.ubuntu.com, for Ubuntu PPA
  • GitHub, gh keys add, available at https://github.com/<user>.gpg
gpg --keyserver hkps://keys.openpgp.org --recv-keys EB423B2514F16F5C
gpg --keyserver hkps://keys.openpgp.org --send-keys EB423B2514F16F5C

curl https://github.com/alice.gpg | gpg --import

Git commit signing

The most widespread modern use:

# Configure once
git config --global user.signingkey EB423B2514F16F5C
git config --global commit.gpgsign true

# Sign a commit
git commit -S -m "fix bug"

# Sign a tag
git tag -s v1.2.3 -m "Release 1.2.3"

# Verify
git log --show-signature
git verify-tag v1.2.3

GitHub/GitLab verify the signature and show a "Verified" badge on the commit. This protects against identity spoofing (anyone can commit as Linus Torvalds <torvalds@...>).

Package signing

APT (Debian/Ubuntu):

  • Each repo signs its Release file with its own GPG key
  • APT stores trusted keys in /etc/apt/trusted.gpg.d/
  • On apt update it verifies the Release signature
curl -fsSL https://download.docker.com/linux/debian/gpg | \
  sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpg

RPM (RHEL/Fedora) works the same way, but through rpm --import:

rpm --import https://packages.example.com/RPM-GPG-KEY-myrepo

Without a signature, apt/dnf will complain with a warning or error.

Subkeys and offline master

Best practice:

  • Keep the master key offline (on USB, in a safe, on a YubiKey)
  • Use subkeys for daily work (Encrypt, Sign, Auth) on the laptop
  • If a subkey is compromised, revoke it, the master stays intact

Create a subkey:

gpg --edit-key alice@example.com
> addkey
> save

Export only the subkeys (without the master):

gpg --export-secret-subkeys alice@example.com > subkeys-only.asc
# on the laptop import only this

Store on YubiKey/HSM:

gpg --edit-key alice@example.com
> key 1
> keytocard           # move the encryption subkey to the YubiKey

Revocation

If a private key is lost or compromised, you need to publish a revocation certificate. Better to generate it right at creation:

gpg --output revoke.asc --gen-revoke alice@example.com

And store it somewhere safe. On compromise:

gpg --import revoke.asc
gpg --keyserver hkps://keys.openpgp.org --send-keys EB423B...
# everyone who refreshes the key will see the revocation

GPG vs cosign vs signed commits

GPGcosigngit-signed-commits
Identityself-published key + WoTOIDC-keylessgit config + GPG
Signing supportuniversalonly OCI artifactsonly git objects
When to chooseemail, packages, file-sigscontainer images, supply chaingit history integrity

When something goes wrong

  • gpg: decryption failed: No secret key, you have no private key for this ciphertext. Check gpg --list-secret-keys and match the key-id with gpg --list-packets file.gpg.
  • gpg: signature ... not made by ... key, the signature is valid but made by a different key. Check the sender's fingerprint.
  • Inappropriate ioctl for device on --gen-key in a script: pinentry wants a TTY. Set GPG_TTY=$(tty) or use --pinentry-mode loopback --passphrase ....
  • gpg-agent does not find the passphrase, run gpg-connect-agent reloadagent /bye or kill the old agent.
  • apt update complains with NO_PUBKEY, the repo key is not imported, download it from a keyserver: apt-key adv --recv-keys (deprecated) or through signed-by in sources.list:
deb [signed-by=/etc/apt/trusted.gpg.d/foo.gpg] http://repo ...
  • YubiKey not visible, gpg --card-status should show it. If not, pcscd is not running or USB permissions are wrong.
  • WoT trust score "marginal", you need to sign the key first (--sign-key) or raise trust manually (--edit-key -> trust).
  • The private key backup is lost, all data encrypted to it is irreversibly lost. Only revoke. There is no "password reset" in asymmetric crypto.

Commands

  • gpg --full-generate-key: Create a new key pair with interactive choice of algorithm and expiry
  • gpg --list-keys --keyid-format LONG: All public keys in the keyring with long-id (16 hex, safer than short)
  • gpg --detach-sign release.tar.gz: Create a .sig file next to the release, the standard pattern for tarballs
  • gpg --verify release.tar.gz.sig release.tar.gz: Check the signature: Good signature plus eyeballing the fingerprint
  • gpg --export --armor alice@example.com > alice.asc: Export a public key as ASCII, to send or upload to a keyserver
  • git config --global commit.gpgsign true && git config --global user.signingkey <KEYID>: Sign every git commit with the chosen key, GitHub shows Verified
  • curl -fsSL https://example.com/gpg-key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/example.gpg: The modern way to add an apt repo with an explicit trust key (instead of apt-key)

See also