Cryptography / GPG

  • resources:

  • Usage

    • gpg --list-secret-keys --with-keygrip --fingerprint
    • gpg --export -a SavoryBillion | save encryption_key.pub
    • gpg --export-secret-subkeys -a "78F8 26FF 13FC C70A 5DC3 47DA 9A48 A782 EF82 61F3" | save -f encryption_key
    • gpg --recipient <KeyID> --encrypt <data file>
    • gpg --decrypt <encrypted file> pub key is self-contained
    • gpg --clearsign outputs text, easier for reading
    • gpg --verify <signature file> again which pub key should be used to verify is self-contained
    • Backup and Recover primary/sub keys

      • gpg --export-secret-keys <KeyID>
        • of both primary and all sub keys
        • equivalently, mv/rm the private-keys.d/keygrip.key file directly
      • gpg --export-secret-subkeys <KeyID>
        • of only sub keys
      • gpg --delete-secret-keys <KeyID>
        • gpg --edit-key, select then delkey will delete the pub/secret pair
    • Config and apply

      • ~/.gnupg/gpg.conf
      • restart agent to apply: gpg-connect-agent /bye
    • Expiration and --edit-key functionality

      • gpg --edit-key <KeyID>
        • use key <index> to select primary/sub key, starting from 0
        • use expire to set expiration again
        • use delkey to delete pub and private pair of a specified (sub)key only
    • Add Subkey

      • gpg --edit-key <KeyID> then addkey
    • Set capabilities

      • gpg --edit-key <KeyID> then key <index> then change-usage
  • 原理相关 - 和其他常见 Public-Key Cryptography 加密方式比较

    • comparison to CryptoCurrencies: btc and eth use a different curve, secp256k1, for no specific advantage, likely historical reason
    • comparison to ssh
      • Key Formats: GPG and SSH keys are stored in different formats. GPG uses its own format, which is compatible with the OpenPGP standard, while SSH keys are stored in a format that is specific to SSH.
        Purpose and Functionality: GPG provides a suite of encryption technologies for emails and files, including key management systems and access to public key directories. SSH keys are specifically designed for secure access to remote servers and don't natively support document signing or encryption in the way GPG does.
    • comparison to https TLS
      • also a chain of trust: rootCA -> intermediateCA -> server certificates
      • also uses Public-Key Cryptography: signatures and verifications
    • comparison
  • InfoSec best practices

    • cryptographic algorithm: use ED25519. No <3k RSA, no NIST
    • revocation cert
    • digest: no SHA1, prefer SHA512
    • primary + sub keys, with primary offline
      • understand one-primary+multi-sub v.s. multi-primary keys

        • > Examples for using multiple primary keys:
          You don't want to mix up your private and professional keys
          You need some key not connected with your "real life" identity, eg. when prosecuted by the authorities
        • > Examples for using subkeys:
          You want to use multiple keys for multiple devices (so you won't have to revoke your computer's key if you lose your mobile)
          You want to switch keys regularly (eg., every some years) without losing your reputation in the Web of Trust
        • multiple signing subkeys or encrypting subkeys?
          • only the latest encrypting subkey is used?
    • different keys for sign/encryption
    • disable AEAD
    • generate with care
      • $ gpg --full-generate-key --expert
  • Fungible and Non-Fungible key capabilities

    • Sign and Authenticate (e.g. ssh ).
      • 默认用最后一个add的pub sign key来sign,没法指定别的。
      • 但无论是哪一个sign/auth都代表同一个primaryKey所代表的identity 所以无所谓,可以每个设备一个,丢了revoke
    • Encrypt and Certify: non-fungible.
      • encrypt: 哪一个subkey加密,谁解密,丢了sub, primary也没法帮忙揭秘
        • 所以最好多设备share一个不然电脑加密手机没法解密
      • certify: 只有primary certify subs没有grand-children
      • 强行默认用最后一个add的pub encrypt key来encrypt, 没法用别的
    • When create a primary key, only "Certify" capability is a must.
  • Expired v.s. Revoked

    • expire means "needs update" (from keyserver or somewhere)
      • owner can extend expiration even AFTER expiried
    • revoked means "no longer used" (for can-be-specified reason/comments)
  • Keyserver and How to find a (pub) key on the Internet

    • gpg --import sss/sb_240922.pgp
    • preferred-keyserver-url can be any URL to download your key file. Its a public key attribute.
    • honor-keyserver-url will try the above attribute first
    • therefore, one can visit the URL as long as they have your pub key with that attribute set
    • You can choose to include info on how to find your pub key in signatures:
    • the entire pub key (too long)
    • the keyserver-url (best)
    • keyID, user, email, etc. (avoid)
      -
  • GPG public and private keys files/formats explained

    • keyID vs fingerprint
      • fingerprint is the only unique id. keyID (0xAAAAA...) is the last 4 4-hex groups of the fingerprint
    • Fingerprint vs keygrip
      • fingerprint is a OpenPGP standard while keygrip is GnuPG internal structure. it's used as private key file names
      • they are both hashes (a.k.a. digests) of public key information. Fingerprint contains algo while keygrip does not
    • gpg --list-keys <KeyID> public key info output explained:
      • ```
        pub ed25519 2024-02-18 [SC] [expires: 2027-02-17]
        E9438CCFA834E1F65390F82FDF89B1399C3332A1
        uid test1 <test1@test.com>
        sub cv25519 2024-02-18 [E] [expires: 2027-02-17]
        ```
      • pub ed25519 2024-02-18 [SC] [expires: 2027-02-17]: This line indicates you have an Ed25519 public key, generated on February 18, 2024. The [SC] flags mean this key is used for Signing (S) and Certification (C) of other keys. The key has an expiration date set to February 17, 2027.
      • E9438CCFA834E1F65390F82FDF89B1399C3332A1: This is the fingerprint of your Ed25519 public key. The fingerprint is a shorter way to uniquely identify the key.
      • uid test1 <test1@test.com>: This is the user ID associated with the key, including a name and email address. It's used to identify the owner of the key.
      • sub cv25519 2024-02-18 [E] [expires: 2027-02-17]: This line indicates a subkey used for Encryption (E). It's based on the Curve25519 (cv25519) elliptic curve, generated on the same date as the primary key and also expiring on February 17, 2027.
    • gpg private keys are stored under ~/.gnupg/private-keys-v1.d, QQ and dd in plain text format storing
      • There'll be two private keys by default, one for master key, one for encryption sub key
      • if it's password protected, then the dd part will be further encrypted
    • gpg pulic keys can be exported by gpg --armor --export <KeyID> > mypublickey.asc it's base64 encoded ASCII containing meta data and public points of all keys and subkeys
      -
      -
  • (ref)

A digital garden, perpetually growing.