RFC 4880

OpenPGP Message Format

๐Ÿ“… published November 2007, from ietf

Part 1 of 19 ๐ŸŽ€

Okay bestie, welcome to RFC 4880: OpenPGP Message Format ๐Ÿ’– It's a Standards Track doc from November 2007, so this is an official protocol spec ๐Ÿ‘‘

The crew who wrote it: J. Callas (PGP Corporation), L. Donnerhacke (IKS GmbH), H. Finney (PGP Corporation), D. Shaw, and R. Thayer. It obsoletes RFC 1991 and RFC 2440 ๐Ÿฆ‹, so those are retired. The title block doesn't list anything it updates.


Status of This Memo โœจ

Okay so this section is the "what kind of doc is this" bit. This document specifies an Internet standards track protocol for the Internet community, and it's asking for discussion and suggestions for improvements ๐Ÿ’ฌ. For the current standardization state and status, check the "Internet Official Protocol Standards" (STD 1) ๐Ÿ”. Distribution of this memo is unlimited, so share away ๐ŸŒธ.


Abstract ๐Ÿงš

The whole point of this doc is to publish every piece of info you need to build interoperable apps on the OpenPGP format ๐Ÿซถ It is not a step-by-step cookbook for writing an application, though ๐Ÿ™ˆ It describes only the format and methods needed to read, check, generate, and write conforming packets crossing any network. It doesn't deal with storage and implementation questions. It does, however, discuss implementation issues necessary to avoid security flaws ๐Ÿšจ so pay attention to that part, periodt.

OpenPGP software uses a combination of strong public-key and symmetric cryptography to provide security services for electronic communications and data storage ๐Ÿ” Those services include confidentiality, key management, authentication, and digital signatures ๐Ÿ’… This document specifies the message formats used in OpenPGP โœจ

Table of Contents โœจ

   1. Introduction ....................................................5
      1.1. Terms ......................................................5
   2. General functions ...............................................6
      2.1. Confidentiality via Encryption .............................6
      2.2. Authentication via Digital Signature .......................7
      2.3. Compression ................................................7
      2.4. Conversion to Radix-64 .....................................8
      2.5. Signature-Only Applications ................................8
   3. Data Element Formats ............................................8
      3.1. Scalar Numbers .............................................8
      3.2. Multiprecision Integers ....................................9
      3.3. Key IDs ....................................................9
      3.4. Text .......................................................9
      3.5. Time Fields ...............................................10
      3.6. Keyrings ..................................................10
      3.7. String-to-Key (S2K) Specifiers ............................10
           3.7.1. String-to-Key (S2K) Specifier Types ................10
                  3.7.1.1. Simple S2K ................................10
                  3.7.1.2. Salted S2K ................................11
                  3.7.1.3. Iterated and Salted S2K ...................11
           3.7.2. String-to-Key Usage ................................12
                  3.7.2.1. Secret-Key Encryption .....................12
                  3.7.2.2. Symmetric-Key Message Encryption ..........13
   4. Packet Syntax ..................................................13
      4.1. Overview ..................................................13
      4.2. Packet Headers ............................................13
           4.2.1. Old Format Packet Lengths ..........................14
           4.2.2. New Format Packet Lengths ..........................15
                  4.2.2.1. One-Octet Lengths .........................15
                  4.2.2.2. Two-Octet Lengths .........................15
                  4.2.2.3. Five-Octet Lengths ........................15
                  4.2.2.4. Partial Body Lengths ......................16
           4.2.3. Packet Length Examples .............................16
      4.3. Packet Tags ...............................................17
   5. Packet Types ...................................................17
      5.1. Public-Key Encrypted Session Key Packets (Tag 1) ..........17
      5.2. Signature Packet (Tag 2) ..................................19
           5.2.1. Signature Types ....................................19
           5.2.2. Version 3 Signature Packet Format ..................21
           5.2.3. Version 4 Signature Packet Format ..................24
                  5.2.3.1. Signature Subpacket Specification .........25
                  5.2.3.2. Signature Subpacket Types .................27
                  5.2.3.3. Notes on Self-Signatures ..................27
                  5.2.3.4. Signature Creation Time ...................28
                  5.2.3.5. Issuer ....................................28
                  5.2.3.6. Key Expiration Time .......................28
                  5.2.3.7. Preferred Symmetric Algorithms ............28
                  5.2.3.8. Preferred Hash Algorithms .................29
                  5.2.3.9. Preferred Compression Algorithms ..........29
                  5.2.3.10. Signature Expiration Time ................29
                  5.2.3.11. Exportable Certification .................29
                  5.2.3.12. Revocable ................................30
                  5.2.3.13. Trust Signature ..........................30
                  5.2.3.14. Regular Expression .......................31
                  5.2.3.15. Revocation Key ...........................31
                  5.2.3.16. Notation Data ............................31
                  5.2.3.17. Key Server Preferences ...................32
                  5.2.3.18. Preferred Key Server .....................33
                  5.2.3.19. Primary User ID ..........................33
                  5.2.3.20. Policy URI ...............................33
                  5.2.3.21. Key Flags ................................33
                  5.2.3.22. Signer's User ID .........................34
                  5.2.3.23. Reason for Revocation ....................35
                  5.2.3.24. Features .................................36
                  5.2.3.25. Signature Target .........................36
                  5.2.3.26. Embedded Signature .......................37
           5.2.4. Computing Signatures ...............................37
                  5.2.4.1. Subpacket Hints ...........................38
      5.3. Symmetric-Key Encrypted Session Key Packets (Tag 3) .......38
      5.4. One-Pass Signature Packets (Tag 4) ........................39
      5.5. Key Material Packet .......................................40
           5.5.1. Key Packet Variants ................................40
                  5.5.1.1. Public-Key Packet (Tag 6) .................40
                  5.5.1.2. Public-Subkey Packet (Tag 14) .............40
                  5.5.1.3. Secret-Key Packet (Tag 5) .................41
                  5.5.1.4. Secret-Subkey Packet (Tag 7) ..............41
           5.5.2. Public-Key Packet Formats ..........................41
           5.5.3. Secret-Key Packet Formats ..........................43
      5.6. Compressed Data Packet (Tag 8) ............................45
      5.7. Symmetrically Encrypted Data Packet (Tag 9) ...............45
      5.8. Marker Packet (Obsolete Literal Packet) (Tag 10) ..........46
      5.9. Literal Data Packet (Tag 11) ..............................46
      5.10. Trust Packet (Tag 12) ....................................47
      5.11. User ID Packet (Tag 13) ..................................48
      5.12. User Attribute Packet (Tag 17) ...........................48
           5.12.1. The Image Attribute Subpacket .....................48
      5.13. Sym. Encrypted Integrity Protected Data Packet (Tag 18) ..49
      5.14. Modification Detection Code Packet (Tag 19) ..............52
   6. Radix-64 Conversions ...........................................53
      6.1. An Implementation of the CRC-24 in "C" ....................54
      6.2. Forming ASCII Armor .......................................54
      6.3. Encoding Binary in Radix-64 ...............................57
      6.4. Decoding Radix-64 .........................................58
      6.5. Examples of Radix-64 ......................................59
      6.6. Example of an ASCII Armored Message .......................59
   7. Cleartext Signature Framework ..................................59
      7.1. Dash-Escaped Text .........................................60
   8. Regular Expressions ............................................61
   9. Constants ......................................................61
      9.1. Public-Key Algorithms .....................................62
      9.2. Symmetric-Key Algorithms ..................................62
      9.3. Compression Algorithms ....................................63
      9.4. Hash Algorithms ...........................................63
   10. IANA Considerations ...........................................63
      10.1. New String-to-Key Specifier Types ........................64
      10.2. New Packets ..............................................64
           10.2.1. User Attribute Types ..............................64
                  10.2.1.1. Image Format Subpacket Types .............64
           10.2.2. New Signature Subpackets ..........................64
                  10.2.2.1. Signature Notation Data Subpackets .......65
                  10.2.2.2. Key Server Preference Extensions .........65
                  10.2.2.3. Key Flags Extensions .....................65
                  10.2.2.4. Reason For Revocation Extensions .........65
                  10.2.2.5. Implementation Features ..................66
           10.2.3. New Packet Versions ...............................66
      10.3. New Algorithms ...........................................66
           10.3.1. Public-Key Algorithms .............................66
           10.3.2. Symmetric-Key Algorithms ..........................67
           10.3.3. Hash Algorithms ...................................67
           10.3.4. Compression Algorithms ............................67
   11. Packet Composition ............................................67
      11.1. Transferable Public Keys .................................67
      11.2. Transferable Secret Keys .................................69
      11.3. OpenPGP Messages .........................................69
      11.4. Detached Signatures ......................................70
   12. Enhanced Key Formats ..........................................70
      12.1. Key Structures ...........................................70
      12.2. Key IDs and Fingerprints .................................71
   13. Notes on Algorithms ...........................................72
      13.1. PKCS#1 Encoding in OpenPGP ...............................72
           13.1.1. EME-PKCS1-v1_5-ENCODE .............................73
           13.1.2. EME-PKCS1-v1_5-DECODE .............................73
           13.1.3. EMSA-PKCS1-v1_5 ...................................74
      13.2. Symmetric Algorithm Preferences ..........................75
      13.3. Other Algorithm Preferences ..............................76
           13.3.1. Compression Preferences ...........................76
           13.3.2. Hash Algorithm Preferences ........................76
      13.4. Plaintext ................................................77
      13.5. RSA ......................................................77
      13.6. DSA ......................................................77
      13.7. Elgamal ..................................................78
      13.8. Reserved Algorithm Numbers ...............................78
      13.9. OpenPGP CFB Mode .........................................78
      13.10. Private or Experimental Parameters ......................79
      13.11. Extension of the MDC System .............................80
      13.12. Meta-Considerations for Expansion .......................80
   14. Security Considerations .......................................81
   15. Implementation Nits ...........................................84
   16. References ....................................................86
      16.1. Normative References .....................................86
      16.2. Informative References ...................................88

1. Introduction ๐ŸŽ€

okay so this doc is all about the message-exchange packet formats that OpenPGP uses to do encryption, decryption, signing, and key management ๐Ÿ” it's literally the rulebook for how all those pieces fit together, and I'm obsessed.

it's a revision of RFC 2440, "OpenPGP Message Format", which itself replaces RFC 1991, "PGP Message Exchange Formats" [RFC1991] [RFC2440]. so this is the newest glow-up of the whole family tree ๐Ÿฆ‹โœจ

1.1. Terms โœจ

okay so first, the vocab ๐Ÿ“– bestie, let's get the names straight ๐Ÿ’…

  • OpenPGP - this is a term for security software that uses PGP 5.x as a basis, formalized in RFC 2440 and this document. ๐Ÿ”
  • PGP - Pretty Good Privacy. PGP is a family of software systems developed by Philip R. Zimmermann, and OpenPGP is based on it. ๐ŸŽ€
  • PGP 2.6.x - this version has tons of variants, which is why it's called PGP 2.6.x. It only used RSA, MD5, and IDEA for its cryptographic transforms. An informational RFC, RFC 1991, was written describing this version of PGP. ๐Ÿฆ‹
  • PGP 5.x - this version used to be called "PGP 3" in the community, and also in the predecessor of this document, RFC 1991. It has new formats and fixes a bunch of problems in the PGP 2.6.x design. It's referred to here as PGP 5.x because that software was the first release of the "PGP 3" code base. ๐Ÿ’–
  • GnuPG - GNU Privacy Guard, also called GPG. GnuPG is an OpenPGP implementation that avoids all encumbered algorithms. Consequently, early versions of GnuPG did not include RSA public keys. GnuPG may or may not have (depending on version) support for IDEA or other encumbered algorithms. ๐ŸŒธ

"PGP", "Pretty Good", and "Pretty Good Privacy" are trademarks of PGP Corporation and are used with permission. The term "OpenPGP" refers to the protocol described in this and related documents. ๐Ÿ’œ

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC2119]. ๐Ÿงš

The key words "PRIVATE USE", "HIERARCHICAL ALLOCATION", "FIRST COME FIRST SERVED", "EXPERT REVIEW", "SPECIFICATION REQUIRED", "IESG APPROVAL", "IETF CONSENSUS", and "STANDARDS ACTION" that appear in this document when used to describe namespace allocation are to be interpreted as described in [RFC2434]. ๐Ÿ—‚๏ธ


2. General functions ๐ŸŒˆ

OpenPGP provides data integrity services for messages and data files by using these core technologies: ๐Ÿ”ฅ

  • digital signatures
  • encryption
  • compression
  • Radix-64 conversion

In addition, OpenPGP provides key management and certificate services, but many of these are beyond the scope of this document. ๐Ÿซถ

2.1. Confidentiality via Encryption ๐Ÿ”’

OpenPGP combines symmetric-key encryption and public-key encryption to provide confidentiality. When made confidential, first the object is encrypted using a symmetric encryption algorithm. Each symmetric key is used only once, for a single object. A new "session key" is generated as a random number for each object (sometimes referred to as a session). Since it is used only once, the session key is bound to the message and transmitted with it. To protect the key, it is encrypted with the receiver's public key. The sequence is as follows: ๐Ÿ’…

  1. The sender creates a message.
  2. The sending OpenPGP generates a random number to be used as a session key for this message only.
  3. The session key is encrypted using each recipient's public key. These "encrypted session keys" start the message.
  4. The sending OpenPGP encrypts the message using the session key, which forms the remainder of the message. Note that the message is also usually compressed.
  5. The receiving OpenPGP decrypts the session key using the recipient's private key.
  6. The receiving OpenPGP decrypts the message using the session key. If the message was compressed, it will be decompressed.

With symmetric-key encryption, an object may be encrypted with a symmetric key derived from a passphrase (or other shared secret), or a two-stage mechanism similar to the public-key method described above in which a session key is itself encrypted with a symmetric algorithm keyed from a shared secret. ๐Ÿ“

Both digital signature and confidentiality services may be applied to the same message. First, a signature is generated for the message and attached to the message. Then the message plus signature is encrypted using a symmetric session key. Finally, the session key is encrypted using public-key encryption and prefixed to the encrypted block. โœจ

2.2. Authentication via Digital Signature โœ๏ธ

The digital signature uses a hash code or message digest algorithm, and a public-key signature algorithm. The sequence is as follows: ๐Ÿฆ‹

  1. The sender creates a message.
  2. The sending software generates a hash code of the message.
  3. The sending software generates a signature from the hash code using the sender's private key.
  4. The binary signature is attached to the message.
  5. The receiving software keeps a copy of the message signature.
  6. The receiving software generates a new hash code for the received message and verifies it using the message's signature. If the verification is successful, the message is accepted as authentic.

2.3. Compression ๐Ÿ—œ๏ธ

OpenPGP implementations SHOULD compress the message after applying the signature but before encryption. ๐Ÿ’–

If an implementation does not implement compression, its authors should be aware that most OpenPGP messages in the world are compressed. Thus, it may even be wise for a space-constrained implementation to implement decompression, but not compression. ๐Ÿง 

Furthermore, compression has the added side effect that some types of attacks can be thwarted by the fact that slightly altered, compressed data rarely uncompresses without severe errors. This is hardly rigorous, but it is operationally useful. These attacks can be rigorously prevented by implementing and using Modification Detection Codes as described in sections following. ๐Ÿ›ก๏ธ

2.4. Conversion to Radix-64 ๐Ÿ”ค

OpenPGP's underlying native representation for encrypted messages, signature certificates, and keys is a stream of arbitrary octets. Some systems only permit the use of blocks consisting of seven-bit, printable text. For transporting OpenPGP's native raw binary octets through channels that are not safe to raw binary data, a printable encoding of these binary octets is needed. OpenPGP provides the service of converting the raw 8-bit binary octet stream to a stream of printable ASCII characters, called Radix-64 encoding or ASCII Armor. ๐ŸŽ€

Implementations SHOULD provide Radix-64 conversions. ๐ŸŒธ

2.5. Signature-Only Applications โœ๏ธ

OpenPGP is designed for applications that use both encryption and signatures, but there are a number of problems that are solved by a signature-only implementation. Although this specification requires both encryption and signatures, it is reasonable for there to be subset implementations that are non-conformant only in that they omit encryption. ๐Ÿ‘‘


3. Data Element Formats ๐Ÿง

This section describes the data elements used by OpenPGP. ๐Ÿ’œ

3.1. Scalar Numbers ๐Ÿ”ข

Scalar numbers are unsigned and are always stored in big-endian format. Using n[k] to refer to the kth octet being interpreted, the value of a two-octet scalar is ((n[0] << 8) + n[1]). The value of a four-octet scalar is ((n[0] << 24) + (n[1] << 16) + (n[2] << 8) + n[3]). ๐Ÿงฎ

3.2. Multiprecision Integers (MPIs) ๐ŸŒŸ

Multiprecision integers (also called MPIs) are unsigned integers used to hold large integers such as the ones used in cryptographic calculations. ๐Ÿ”

An MPI consists of two pieces: a two-octet scalar that is the length of the MPI in bits followed by a string of octets that contain the actual integer. ๐Ÿฆ‹

These octets form a big-endian number; a big-endian number can be made into an MPI by prefixing it with the appropriate length. ๐ŸŽ€

Examples:

(all numbers are in hexadecimal)

The string of octets [00 01 01] forms an MPI with the value 1. The string [00 09 01 FF] forms an MPI with the value of 511. โœจ

Additional rules:

The size of an MPI is ((MPI.length + 7) / 8) + 2 octets. ๐Ÿ“

The length field of an MPI describes the length starting from its most significant non-zero bit. Thus, the MPI [00 02 01] is not formed correctly. It should be [00 01 01]. ๐Ÿ™ˆ

Unused bits of an MPI MUST be zero. ๐Ÿ’…

Also note that when an MPI is encrypted, the length refers to the plaintext MPI. It may be ill-formed in its ciphertext. ๐Ÿ˜ญ

3.3. Key IDs ๐Ÿชช

A Key ID is an eight-octet scalar that identifies a key. Implementations SHOULD NOT assume that Key IDs are unique. The section "Enhanced Key Formats" below describes how Key IDs are formed. ๐Ÿ”‘

3.4. Text ๐Ÿ“

Unless otherwise specified, the character set for text is the UTF-8 [RFC3629] encoding of Unicode [ISO10646]. ๐ŸŒˆ

3.5. Time Fields โฐ

A time field is an unsigned four-octet number containing the number of seconds elapsed since midnight, 1 January 1970 UTC. ๐Ÿ•›

3.6. Keyrings ๐Ÿ—๏ธ

A keyring is a collection of one or more keys in a file or database. Traditionally, a keyring is simply a sequential list of keys, but may be any suitable database. It is beyond the scope of this standard to discuss the details of keyrings or other databases. ๐Ÿซง

3.7. String-to-Key (S2K) Specifiers ๐Ÿง‚

String-to-key (S2K) specifiers are used to convert passphrase strings into symmetric-key encryption/decryption keys. They are used in two places, currently: to encrypt the secret part of private keys in the private keyring, and to convert passphrases to encryption keys for symmetrically encrypted messages. ๐Ÿ”

3.7.1. String-to-Key (S2K) Specifier Types ๐Ÿฐ

There are three types of S2K specifiers currently supported, and some reserved values: ๐Ÿ’–

       ID          S2K Type
       --          --------
       0           Simple S2K
       1           Salted S2K
       2           Reserved value
       3           Iterated and Salted S2K
       100 to 110  Private/Experimental S2K

These are described in Sections 3.7.1.1 - 3.7.1.3. ๐Ÿ‘€

3.7.1.1. Simple S2K ๐ŸŒธ

This directly hashes the string to produce the key data. See below for how this hashing is done. ๐Ÿซถ

       Octet 0:        0x00
       Octet 1:        hash algorithm

Simple S2K hashes the passphrase to produce the session key. The manner in which this is done depends on the size of the session key (which will depend on the cipher used) and the size of the hash algorithm's output. If the hash size is greater than the session key size, the high-order (leftmost) octets of the hash are used as the key. ๐Ÿ”‘

If the hash size is less than the key size, multiple instances of the hash context are created -- enough to produce the required key data. These instances are preloaded with 0, 1, 2, ... octets of zeros (that is to say, the first instance has no preloading, the second gets preloaded with 1 octet of zero, the third is preloaded with two octets of zeros, and so forth). ๐Ÿงฉ

As the data is hashed, it is given independently to each hash context. Since the contexts have been initialized differently, they will each produce different hash output. Once the passphrase is hashed, the output data from the multiple hashes is concatenated, first hash leftmost, to produce the key data, with any excess octets on the right discarded. ๐ŸŒŠ

3.7.1.2. Salted S2K ๐Ÿง‚โœจ

This includes a "salt" value in the S2K specifier -- some arbitrary data -- that gets hashed along with the passphrase string, to help prevent dictionary attacks. ๐Ÿ›ก๏ธ

       Octet 0:        0x01
       Octet 1:        hash algorithm
       Octets 2-9:     8-octet salt value

Salted S2K is exactly like Simple S2K, except that the input to the hash function(s) consists of the 8 octets of salt from the S2K specifier, followed by the passphrase. ๐Ÿง‚๐Ÿ’œ

3.7.1.3. Iterated and Salted S2K ๐Ÿง‚

okay so this one is the glow-up version ๐Ÿ’… it includes BOTH a salt AND an octet count. The salt gets mixed with the passphrase, and then the whole thing gets hashed over and over. That makes it so much more work for an attacker trying dictionary attacks ๐Ÿ™…โ€โ™€๏ธโœจ

   Octet  0:        0x03
   Octet  1:        hash algorithm
   Octets 2-9:      8-octet salt value
   Octet  10:       count, a one-octet, coded value

the count is squished into one octet using this formula ๐Ÿงฎ

    #define EXPBIAS 6
        count = ((Int32)16 + (c & 15)) << ((c >> 4) + EXPBIAS);

that's C, btw, where "Int32" is a 32-bit integer type, and "c" is the coded count (that's Octet 10) ๐Ÿค“

ngl the important bit: iterated-salted S2K keeps hashing the passphrase and salt, and the count tells you the total number of octets to hash, not an iteration count ๐Ÿซถ so don't mix those two up bestie!!

at the start, one or more hash contexts get set up like with the other S2K algorithms, depending on how much key data you need ๐Ÿ”‘ then the salt, followed by the passphrase, gets hashed again and again until you've hit the octet count. the one exception: if the octet count is smaller than salt plus passphrase, the full salt plus passphrase still gets hashed, even though that's bigger than the count ๐Ÿ˜ญ (main character energy, no notes)

when the hashing is done, the data gets unloaded from the hash context(s), same as the other S2K algorithms ๐Ÿฆ‹


3.7.2. String-to-Key Usage ๐Ÿ”

okay the important vibe check here: Implementations SHOULD use salted or iterated-and-salted S2K specifiers, since simple S2K specifiers are way more vulnerable to dictionary attacks ๐Ÿšจ get the salt in there, periodt ๐Ÿ’–

3.7.2.1. Secret-Key Encryption ๐Ÿ—๏ธ

so an S2K specifier can live in the secret keyring and tell you how to turn a passphrase into the key that unlocks the secret data โœจ older versions of PGP were kinda basic about it. they just stored a cipher algorithm octet in front of the secret data, or a zero if the data wasn't encrypted at all ๐Ÿฅฑ and they always used MD5 to turn the passphrase into a key for that cipher.

for compatibility, when an S2K specifier is used, the special value 254 or 255 goes in the spot where the hash algorithm octet would've been in the old structure ๐ŸŽ€ then comes a one-octet algorithm identifier, and then the S2K specifier, encoded like we said above ๐Ÿซง

so the thing sitting in front of the secret data can be one of these:

    0:           secret data is unencrypted (no passphrase)
    255 or 254:  followed by algorithm octet and S2K specifier
    Cipher alg:  use Simple S2K algorithm using MD5 hash

ok the last one is the messy throwback: a cipher algorithm number with an implicit MD5 and IDEA. it's there purely for backward compatibility ๐Ÿ•ฐ๏ธ it MAY be understood, but SHOULD NOT be generated, and it's deprecated ๐Ÿ’€ don't make new stuff like this!!

after all that, you get an Initial Vector the same length as the cipher's block size (for decrypting the secret values, if they're encrypted), and then the secret-key values themselves ๐Ÿงš

3.7.2.2. Symmetric-Key Message Encryption ๐Ÿ’Œ

OpenPGP can make a Symmetric-key Encrypted Session Key (ESK) packet at the front of a message ๐ŸŽ this lets you use S2K specifiers for the passphrase conversion, or mix symmetric-key ESKs with public-key ESKs in the same message. the result: the message can be decrypted with either a passphrase OR a public-key pair. iconic flexibility ๐Ÿ‘‘

PGP 2.X always used IDEA with Simple string-to-key conversion when it encrypted a message with a symmetric algorithm ๐Ÿ˜ฌ that's deprecated, but it MAY be used for backward-compatibility ๐Ÿซถ


4. Packet Syntax ๐Ÿ“ฆโœจ

this section is all about the packets OpenPGP uses ๐Ÿ’œ

4.1. Overview ๐ŸŒธ

an OpenPGP message is built from a bunch of records that everyone calls packets ๐Ÿ“ฎ a packet is a chunk of data with a tag that says what it means. a message, keyring, certificate, and so on are all made of packets. some packets can even hold other OpenPGP packets inside them (like a compressed data packet, which, once uncompressed, has OpenPGP packets in it) ๐Ÿช†

every packet is a packet header followed by the packet body ๐ŸŽ€ and the header is variable length, so it's not always the same size bestie.

4.2. Packet Headers ๐ŸŽ€

the first octet of the packet header is the "Packet Tag" ๐Ÿท๏ธ it decides the header's format and tells you what the contents are. the rest of the header is the packet's length.

quick note: the most significant bit is the leftmost one, called bit 7. its mask is 0x80 in hex ๐Ÿ”ข

           +---------------+
      PTag |7 6 5 4 3 2 1 0|
           +---------------+
      Bit 7 -- Always one
      Bit 6 -- New packet format if set

PGP 2.6.x only uses old format packets ๐Ÿ™ˆ so if you want to work with those versions, you've gotta be old format too, no exceptions. if interoperability isn't a concern, the new packet format is RECOMMENDED ๐Ÿ’– also, old format packets only have four bits for tags and new ones have six, so some features just can't be used and still stay backward-compatible. it's giving trade-off ๐Ÿฅฒ

ALSO packets with a tag greater than or equal to 16 MUST use new format packets!! old format can only express tags up to 15, so that's the rule ๐Ÿšซ

old format packets hold:

      Bits 5-2 -- packet tag
      Bits 1-0 -- length-type

new format packets hold:

      Bits 5-0 -- packet tag

4.2.1. Old Format Packet Lengths ๐Ÿ“

the length-type in old format packets means:

0: the packet has a one-octet length. header is 2 octets long ๐ŸŒท

1: the packet has a two-octet length. header is 3 octets long ๐ŸŒท

2: the packet has a four-octet length. header is 5 octets long ๐ŸŒท

3: the packet has an indeterminate length. header is 1 octet long, and the implementation has to figure out how long the packet is ๐Ÿ˜ตโ€๐Ÿ’ซ if the packet's in a file, that means it runs until the end of the file. in general, an implementation SHOULD NOT use indeterminate-length packets, except where the end of the data is obvious from context. and even then, a definite length or a new format header is better ๐Ÿซ  (the new format headers have a way to precisely encode data of indeterminate length, so you're covered ๐Ÿฆ‹)

4.2.2. New Format Packet Lengths ๐Ÿงฎ

new format packets have four ways to encode length, bestie ๐ŸŒˆ

  1. a one-octet Body Length header encodes packet lengths of up to 191 octets.
  2. a two-octet Body Length header encodes packet lengths of 192 to 8383 octets.
  3. a five-octet Body Length header encodes packet lengths of up to 4,294,967,295 (0xFFFFFFFF) octets. (this really encodes a four-octet scalar number, fr fr ๐Ÿค“)
  4. when the issuer doesn't know the length of the packet body in advance, Partial Body Length headers encode a packet of indeterminate length, basically making it a stream ๐ŸŒŠ

4.2.2.1. One-Octet Lengths ๐Ÿ“

a one-octet Body Length header encodes a length from 0 to 191 octets. you can spot it because the octet value is less than 192 โœจ the body length is:

    bodyLen = 1st_octet;

4.2.2.2. Two-Octet Lengths ๐Ÿ’œ

a two-octet Body Length header encodes a length from 192 to 8383 octets. you can recognize it because its first octet is in the range 192 to 223 ๐ŸŽ€ the body length is:

    bodyLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192

4.2.2.3. Five-Octet Lengths ๐Ÿฆ‹

a five-octet Body Length header is one octet holding the value 255, followed by a four-octet scalar ๐Ÿ’… the body length is:

    bodyLen = (2nd_octet << 24) | (3rd_octet << 16) |
              (4th_octet << 8)  | 5th_octet

this basic set of one, two, and five-octet lengths also gets reused internally by some packets ๐Ÿซถ

4.2.2.4. Partial Body Lengths ๐Ÿงฉ

a Partial Body Length header is one octet long and only encodes the length of part of the data packet ๐Ÿฐ the length is a power of 2, from 1 to 1,073,741,824 (that's 2 to the 30th power) ๐Ÿ”ฅ you can tell it's partial because its octet value is greater than or equal to 224, and less than 255. the Partial Body Length is:

    partialBodyLen = 1 << (1st_octet & 0x1F);

each Partial Body Length header is followed by a chunk of packet body data, and the header says how long that chunk is โœจ then another length header (one-octet, two-octet, five-octet, or partial) comes right after that chunk. the LAST length header in the packet MUST NOT be a Partial Body Length header ๐Ÿ˜ญ they're only allowed for the non-final parts of the packet, periodt.

also, the last Body Length header can be a zero-length header ๐Ÿ™ˆ

an implementation MAY use Partial Body Lengths for data packets, whether they're literal, compressed, or encrypted ๐ŸŽ the first partial length MUST be at least 512 octets long ๐Ÿง  and Partial Body Lengths MUST NOT be used for any other packet types ๐Ÿšซ

4.2.3. Packet Length Examples ๐Ÿญ

these examples show ways new format packets might encode their lengths ๐ŸŒธ

a packet with length 100 might have its length encoded in one octet: 0x64. that's followed by 100 octets of data ๐Ÿ“

a packet with length 1723 might have its length encoded in two octets: 0xC5, 0xFB. the header is followed by the 1723 octets of data ๐Ÿ’–

a packet with length 100000 might have its length encoded in five octets: 0xFF, 0x00, 0x01, 0x86, 0xA0 ๐Ÿฆ‹

it could also be encoded in this octet stream: 0xEF, first 32768 octets of data; 0xE1, next two octets of data; 0xE0, next one octet of data; 0xF0, next 65536 octets of data; 0xC5, 0xDD, last 1693 octets of data ๐ŸŒŠ that's just ONE possible encoding. there are lots of variations on the sizes of the Partial Body Length headers, as long as a regular Body Length header encodes the last portion of the data ๐Ÿงš

last thing: in all of these explanations, the total packet length is the length of the header(s) plus the length of the body ๐Ÿซถ

4.3. Packet Tags ๐Ÿท๏ธ

the packet tag says what type of packet the body holds ๐Ÿ’… old format headers can only have tags under 16, while new format headers can go up to 63. here are the defined tags (in decimal):

    0        -- Reserved - a packet tag MUST NOT have this value
    1        -- Public-Key Encrypted Session Key Packet
    2        -- Signature Packet
    3        -- Symmetric-Key Encrypted Session Key Packet
    4        -- One-Pass Signature Packet
    5        -- Secret-Key Packet
    6        -- Public-Key Packet
    7        -- Secret-Subkey Packet
    8        -- Compressed Data Packet
    9        -- Symmetrically Encrypted Data Packet
    10       -- Marker Packet
    11       -- Literal Data Packet
    12       -- Trust Packet
    13       -- User ID Packet
    14       -- Public-Subkey Packet
    17       -- User Attribute Packet
    18       -- Sym. Encrypted and Integrity Protected Data Packet
    19       -- Modification Detection Code Packet
    60 to 63 -- Private or Experimental Values

5. Packet Types ๐Ÿง

5.1. Public-Key Encrypted Session Key Packets (Tag 1) ๐Ÿ”๐Ÿ’–

okay so a Public-Key Encrypted Session Key packet is the little envelope that carries the session key โœจ Zero or more of these, and/or Symmetric-Key Encrypted Session Key packets, can sit in front of a Symmetrically Encrypted Data Packet, which is where the actual encrypted message lives ๐Ÿ’Œ The message gets encrypted with the session key, and then the session key gets encrypted too and tucked into the Encrypted Session Key packet(s). The Symmetrically Encrypted Data Packet is preceded by one Public-Key Encrypted Session Key packet for each OpenPGP key the message is encrypted to ๐ŸŽ€ So the recipient finds the session key that's been encrypted to their public key, decrypts it, and then uses it to unlock the message ๐Ÿ”“ main character energy, literally.

The body of this packet is made up of:

  • A one-octet number giving the version number of the packet type. The currently defined value for packet version is 3. ๐Ÿงš
  • An eight-octet number that gives the Key ID of the public key the session key is encrypted to. If the session key is encrypted to a subkey, then the Key ID of this subkey is used here instead of the Key ID of the primary key. ๐Ÿ—๏ธ
  • A one-octet number giving the public-key algorithm used. ๐ŸŒธ
  • A string of octets that is the encrypted session key. This string takes up the remainder of the packet, and its contents depend on the public-key algorithm used. ๐Ÿ’…

Algorithm Specific Fields for RSA encryption:

  • multiprecision integer (MPI) of RSA encrypted value m**e mod n. ๐Ÿ”ฅ

Algorithm Specific Fields for Elgamal encryption:

  • MPI of Elgamal (Diffie-Hellman) value g**k mod p. โœจ
  • MPI of Elgamal (Diffie-Hellman) value m * y**k mod p. ๐Ÿฆ‹

Now the m in those formulas comes from the session key, and the process is giving math but make it cute ๐Ÿงฎ First, the session key gets prefixed with a one-octet algorithm identifier that names the symmetric encryption algorithm used on the following Symmetrically Encrypted Data Packet. Then a two-octet checksum gets appended. That checksum is equal to the sum of the preceding session key octets (not counting the algorithm identifier), modulo 65536. Last step: the result gets encoded as described in PKCS#1 block encoding EME-PKCS1-v1_5 in Section 7.2.1 of [RFC3447] to form the "m" value used in the formulas above. See Section 13.1 of this document for notes on OpenPGP's use of PKCS#1 ๐Ÿ“š

Heads up, bestie: when an implementation forms several PKESKs with one session key, so that one message can be decrypted by several keys, the implementation MUST make a new PKCS#1 encoding for each key. No copy-pasting the encoding ๐Ÿ™…โ€โ™€๏ธ

An implementation MAY accept or use a Key ID of zero as a "wild card" or "speculative" Key ID ๐Ÿƒ In that case, the receiving implementation would try all available private keys and check for a valid decrypted session key. This format helps reduce traffic analysis of messages ๐Ÿ•ต๏ธโ€โ™€๏ธ iykyk.

5.2. Signature Packet (Tag 2) โœ๏ธ๐ŸŽ€

A Signature packet describes a binding between some public key and some data. The most common ones are a signature over a file or a block of text, and a signature that certifies a User ID ๐Ÿ’–

Two versions of Signature packets are defined. Version 3 gives you basic signature information, while version 4 gives you an expandable format with subpackets that can specify more info about the signature ๐Ÿงฉ PGP 2.6.x only accepts version 3 signatures, so it's a little stuck in its ways ๐Ÿ™ˆ

Implementations SHOULD accept V3 signatures. Implementations SHOULD generate V4 signatures.

Note that if an implementation is creating an encrypted and signed message that is encrypted to a V3 key, it is reasonable to create a V3 signature ๐Ÿซถ

5.2.1. Signature Types ๐ŸŒˆ

okay so a signature can mean a bunch of different things, and the meaning is stored in a signature type octet in each signature ๐ŸŽญ Please don't read the vagueness here as a flaw, because it's actually a feature of the system โœจ OpenPGP puts the final say on validity in the hands of the receiver. So one signer's casual little act might end up being more rigorous than some other authority's official move ๐Ÿ’… See Section 5.2.4, "Computing Signatures", for detailed info on how to compute and verify signatures of each type.

The meanings go like this:

  • 0x00: Signature of a binary document. ๐Ÿ’พ
    • This means the signer owns it, created it, or certifies that it has not been modified.
  • 0x01: Signature of a canonical text document. ๐Ÿ“
    • This means the signer owns it, created it, or certifies that it has not been modified. The signature is calculated over the text data with its line endings converted to <CR><LF>.
  • 0x02: Standalone signature. ๐Ÿงโ€โ™€๏ธ
    • This signature is a signature of only its own subpacket contents. It is calculated identically to a signature over a zero-length binary document. Note that it doesn't make sense to have a V3 standalone signature.
  • 0x10: Generic certification of a User ID and Public-Key packet. ๐Ÿชช
    • The issuer of this certification does not make any particular assertion as to how well the certifier has checked that the owner of the key is in fact the person described by the User ID.
  • 0x11: Persona certification of a User ID and Public-Key packet. ๐ŸŽญ
    • The issuer of this certification has not done any verification of the claim that the owner of this key is the User ID specified.
  • 0x12: Casual certification of a User ID and Public-Key packet. โ˜•
    • The issuer of this certification has done some casual verification of the claim of identity.
  • 0x13: Positive certification of a User ID and Public-Key packet. ๐Ÿ‘‘
    • The issuer of this certification has done substantial verification of the claim of identity.
    • Most OpenPGP implementations make their "key signatures" as 0x10 certifications. Some implementations can issue 0x11-0x13 certifications, but few differentiate between the types. ๐Ÿ™ƒ
  • 0x18: Subkey Binding Signature ๐Ÿ”—
    • This signature is a statement by the top-level signing key that indicates that it owns the subkey. This signature is calculated directly on the primary key and subkey, and not on any User ID or other packets. A signature that binds a signing subkey MUST have an Embedded Signature subpacket in this binding signature that contains a 0x19 signature made by the signing subkey on the primary key and subkey. ๐Ÿ’
  • 0x19: Primary Key Binding Signature ๐Ÿค
    • This signature is a statement by a signing subkey, indicating that it is owned by the primary key and subkey. This signature is calculated the same way as a 0x18 signature: directly on the primary key and subkey, and not on any User ID or other packets.
  • 0x1F: Signature directly on a key ๐Ÿ”‘
    • This signature is calculated directly on a key. It binds the information in the Signature subpackets to the key, and is appropriate to be used for subpackets that provide information about the key, such as the Revocation Key subpacket. It is also appropriate for statements that non-self certifiers want to make about the key itself, rather than the binding between a key and a name.
  • 0x20: Key revocation signature ๐Ÿšซ
    • The signature is calculated directly on the key being revoked. A revoked key is not to be used. Only revocation signatures by the key being revoked, or by an authorized revocation key, should be considered valid revocation signatures.
  • 0x28: Subkey revocation signature ๐Ÿฅ€
    • The signature is calculated directly on the subkey being revoked. A revoked subkey is not to be used. Only revocation signatures by the top-level signature key that is bound to this subkey, or by an authorized revocation key, should be considered valid revocation signatures.
  • 0x30: Certification revocation signature ๐Ÿซฅ
    • This signature revokes an earlier User ID certification signature (signature class 0x10 through 0x13) or direct-key signature (0x1F). It should be issued by the same key that issued the revoked signature or an authorized revocation key. The signature is computed over the same data as the certificate that it revokes, and should have a later creation date than that certificate.
  • 0x40: Timestamp signature. โฐ
    • This signature is only meaningful for the timestamp contained in it.
  • 0x50: Third-Party Confirmation signature. ๐Ÿงพ
    • This signature is a signature over some other OpenPGP Signature packet(s). It is analogous to a notary seal on the signed data. A third-party signature SHOULD include Signature Target subpacket(s) to give easy identification. Note that we really do mean SHOULD. There are plausible uses for this (such as a blind party that only sees the signature, not the key or source document) that cannot include a target subpacket. ๐Ÿซฃ

5.2.2. Version 3 Signature Packet Format ๐ŸŽ€

okay so the body of a version 3 Signature Packet is basically a checklist ๐Ÿ“ and it's got:

  • One-octet version number (3).
  • One-octet length of following hashed material. MUST be 5.
    • One-octet signature type.
    • Four-octet creation time.
  • Eight-octet Key ID of signer.
  • One-octet public-key algorithm.
  • One-octet hash algorithm.
  • Two-octet field holding left 16 bits of signed hash value.
  • One or more multiprecision integers comprising the signature. This part is algorithm specific, as described below โœจ

so the data being signed, plus the signature type and creation time from the Signature packet (that's 5 extra octets, bestie ๐Ÿซถ), all gets hashed together. That hash value is what goes into the signature algorithm. Then the high 16 bits (first two octets) of the hash get stuck into the Signature packet as a quick vibe check to reject some invalid signatures ๐Ÿ—‘๏ธ

Algorithm-Specific Fields for RSA signatures:

  • multiprecision integer (MPI) of RSA signature value m**d mod n.

Algorithm-Specific Fields for DSA signatures:

  • MPI of DSA value r.
  • MPI of DSA value s.

the signature calculation is based on a hash of the signed data, as we just covered ๐Ÿ’… but the details are different for DSA than for RSA, so pay attention ๐Ÿ‘€

For RSA signatures, the hash value gets encoded using PKCS#1 encoding type EMSA-PKCS1-v1_5, as described in Section 9.2 of RFC 3447. This means inserting the hash value as an octet string into an ASN.1 structure, and that structure also includes the object identifier for whichever hash type is being used. Here are the hexadecimal representations for the currently defined hash algorithms ๐Ÿ“

MD5:        0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05
RIPEMD-160: 0x2B, 0x24, 0x03, 0x02, 0x01
SHA-1:      0x2B, 0x0E, 0x03, 0x02, 0x1A
SHA224:     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04
SHA256:     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01
SHA384:     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02
SHA512:     0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03

And the ASN.1 Object Identifiers (OIDs) are as follows ๐Ÿ”‘

MD5:        1.2.840.113549.2.5
RIPEMD-160: 1.3.36.3.2.1
SHA-1:      1.3.14.3.2.26
SHA224:     2.16.840.1.101.3.4.2.4
SHA256:     2.16.840.1.101.3.4.2.1
SHA384:     2.16.840.1.101.3.4.2.2
SHA512:     2.16.840.1.101.3.4.2.3

The full hash prefixes for these look like this ๐ŸŒˆ

MD5:        0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86,
            0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00,
            0x04, 0x10

RIPEMD-160: 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24,
            0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14

SHA-1:      0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0E,
            0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14

SHA224:     0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
            0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05,
            0x00, 0x04, 0x1C

SHA256:     0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
            0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
            0x00, 0x04, 0x20

SHA384:     0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
            0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
            0x00, 0x04, 0x30

SHA512:     0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
            0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
            0x00, 0x04, 0x40

okay the big DSA rule ๐Ÿšจ DSA signatures MUST use hashes that are equal in size to the number of bits of q, the group generated by the DSA key's generator value.

and if the output size of your chosen hash is bigger than the number of bits of q, you chop it down to fit by keeping the leftmost bits, exactly as many as q has. Then that (possibly trimmed) hash result gets treated as a number and used directly in the DSA signature algorithm ๐Ÿ”ฅ


5.2.3. Version 4 Signature Packet Format ๐Ÿฆ‹

the body of a version 4 Signature packet is giving full detail ๐Ÿงš and contains:

  • One-octet version number (4).
  • One-octet signature type.
  • One-octet public-key algorithm.
  • One-octet hash algorithm.
  • Two-octet scalar octet count for following hashed subpacket data. Note that this is the length in octets of all of the hashed subpackets. A pointer incremented by this number will skip over the hashed subpackets.
  • Hashed subpacket data set (zero or more subpackets).
  • Two-octet scalar octet count for the following unhashed subpacket data. Note that this is the length in octets of all of the unhashed subpackets. A pointer incremented by this number will skip over the unhashed subpackets.
  • Unhashed subpacket data set (zero or more subpackets).
  • Two-octet field holding the left 16 bits of the signed hash value.
  • One or more multiprecision integers comprising the signature. This part is algorithm specific, as described above.

so the concatenation of the data being signed and the signature data, from the version number through the hashed subpacket data (inclusive), gets hashed. That resulting hash value is what gets signed. The left 16 bits of the hash get included in the Signature packet as a quick test to reject some invalid signatures ๐Ÿ’–

There are two fields made of Signature subpackets. The first one is hashed along with the rest of the signature data, while the second one is unhashed. That second set isn't cryptographically protected by the signature, so it should include only advisory information ๐Ÿ™ˆ

The algorithms for turning the hash function result into a signature are described in a section below ๐Ÿ’…

5.2.3.1. Signature Subpacket Specification ๐ŸŒธ

a subpacket data set is zero or more Signature subpackets. In Signature packets, the subpacket data set starts with a two-octet scalar count of the length in octets of all the subpackets. A pointer incremented by this number will skip over the whole subpacket data set ๐Ÿฆ‹

each subpacket has a header and a body. The header is made of:

  • the subpacket length (1, 2, or 5 octets),
  • the subpacket type (1 octet),

and then the subpacket-specific data comes after it โœจ

The length includes the type octet but not the length field itself. Its format is similar to the "new" format packet header lengths, but it cannot have Partial Body Lengths. So basically:

if the 1st octet <  192, then
    lengthOfLength = 1
    subpacketLen = 1st_octet

if the 1st octet >= 192 and < 255, then
    lengthOfLength = 2
    subpacketLen = ((1st_octet - 192) << 8) + (2nd_octet) + 192

if the 1st octet = 255, then
    lengthOfLength = 5
    subpacket length = [four-octet scalar starting at 2nd_octet]

The subpacket type octet can be any of these values ๐ŸŽ€

 0 = Reserved
 1 = Reserved
 2 = Signature Creation Time
 3 = Signature Expiration Time
 4 = Exportable Certification
 5 = Trust Signature
 6 = Regular Expression
 7 = Revocable
 8 = Reserved
 9 = Key Expiration Time
10 = Placeholder for backward compatibility
11 = Preferred Symmetric Algorithms
12 = Revocation Key
13 = Reserved
14 = Reserved
15 = Reserved
16 = Issuer
17 = Reserved
18 = Reserved
19 = Reserved
20 = Notation Data
21 = Preferred Hash Algorithms
22 = Preferred Compression Algorithms
23 = Key Server Preferences
24 = Preferred Key Server
25 = Primary User ID
26 = Policy URI
27 = Key Flags
28 = Signer's User ID
29 = Reason for Revocation
30 = Features
31 = Signature Target
32 = Embedded Signature
100 To 110 = Private or experimental

An implementation SHOULD ignore any subpacket of a type that it does not recognize ๐Ÿ™…โ€โ™€๏ธ

Bit 7 of the subpacket type is the "critical" bit. When it's set, it means the subpacket is one that's critical for the evaluator of the signature to recognize. If the evaluator runs into a subpacket that's marked critical but is unknown to its software, the evaluator SHOULD consider the signature to be in error ๐Ÿšจ

An evaluator may "recognize" a subpacket without actually implementing it. The whole point of the critical bit is to let the signer tell an evaluator, "hey, I'd rather you throw an error than ignore this new feature I'm using" ๐Ÿ’œ

Implementations SHOULD implement the three preferred algorithm subpackets (11, 21, and 22), as well as the "Reason for Revocation" subpacket. Note, however, that if an implementation chooses not to implement some of the preferences, it is required to behave in a polite manner to respect the wishes of those users who do implement these preferences ๐Ÿซถ

5.2.3.2. Signature Subpacket Types ๐Ÿ’œ

a bunch of subpackets are defined right now ๐Ÿ“ Some apply to the signature itself and some are attributes of the key. Subpackets found on a self-signature get placed on a certification made by the key itself. And a key can have more than one User ID, so it can have more than one self-signature with different subpackets ๐Ÿ‘‘

a subpacket can show up in either the hashed or the unhashed subpacket sections of a signature. If a subpacket is not hashed, then the information in it can't be considered definitive, because it isn't part of the signature proper ๐Ÿ˜ญ

5.2.3.3. Notes on Self-Signatures โœจ

Okay so a self-signature is a binding signature made by the key that the signature is about ๐Ÿ’…. There are three flavors: the certification signatures (types 0x10-0x13), the direct-key signature (type 0x1F), and the subkey binding signature (type 0x18) ๐ŸŽ€. For certification self-signatures, each User ID can have its own self-signature, so different subpackets can show up in each one. For subkey binding signatures, every subkey has its own self-signature too ๐Ÿ”‘. Subpackets in a certification self-signature apply to the user name, subpackets in the subkey self-signature apply to that subkey, and subpackets on the direct-key signature apply to the entire key ๐Ÿ‘‘.

Implementing software should interpret a self-signature's preference subpackets as narrowly as possible ๐Ÿฆ‹. Say a key has two user names, Alice and Bob. Alice prefers the symmetric algorithm CAST5, and Bob prefers IDEA or TripleDES. If the software finds this key through Alice's name, the preferred algorithm is CAST5. If it finds the key through Bob's name, the preferred algorithm is IDEA ๐ŸŒธ. If the key is located by Key ID, the algorithm of the primary User ID of the key gives the preferred symmetric algorithm ๐Ÿงš.

Revoking a self-signature, or letting it expire, means something different depending on the signature type ๐Ÿ™ˆ. Revoking the self-signature on a User ID basically retires that user name. The self-signature is a statement, "My name X is tied to my signing key K," and other users' certifications back it up ๐Ÿ’œ. If another user revokes their certification, they're saying they don't believe that name and that key go together anymore. Same deal if the users revoke their own self-signature: they no longer go by that name, no longer have that email address, etc. ๐Ÿ’€ Revoking a binding signature retires that subkey, and revoking a direct-key signature cancels that signature. For more tea, check out the "Reason for Revocation" subpacket (Section 5.2.3.23) ๐Ÿซถ.

Since a self-signature holds important info about how the key gets used, an implementation SHOULD let the user rewrite the self-signature and the important stuff in it, like preferences and key expiration ๐Ÿ”ฅ.

It's good practice to check that a self-signature imported into an implementation doesn't advertise features the implementation doesn't support, and to rewrite the signature as needed ๐Ÿงผ.

An implementation that runs into multiple self-signatures on the same object can resolve the ambiguity however it sees fit ๐ŸŒˆ, but it is RECOMMENDED that priority go to the most recent self-signature โญ.


5.2.3.4. Signature Creation Time โฐ

(4-octet time field)

This is the time the signature was made.

The Signature Creation Time MUST be present in the hashed area ๐Ÿ’–.

5.2.3.5. Issuer ๐Ÿชช

(8-octet Key ID)

This is the OpenPGP Key ID of the key that issued the signature ๐ŸŽŸ๏ธ.

5.2.3.6. Key Expiration Time โŒ›

(4-octet time field)

This is the key's validity period: the number of seconds after the key creation time when the key expires. If this isn't present or has a value of zero, the key never expires ๐ŸŒŸ. This only shows up on a self-signature ๐Ÿ™ˆ.

5.2.3.7. Preferred Symmetric Algorithms ๐Ÿ”

(array of one-octet values)

These are the symmetric algorithm numbers the key holder prefers to use ๐Ÿ“. The subpacket body is an ordered list of octets, with the most preferred listed first. It's assumed that only the algorithms listed are supported by the recipient's software. Algorithm numbers live in Section 9. This is only found on a self-signature ๐Ÿ’….

5.2.3.8. Preferred Hash Algorithms ๐Ÿง

(array of one-octet values)

These are the message digest algorithm numbers that indicate which algorithms the key holder prefers to receive ๐Ÿ“ฌ. Like the preferred symmetric algorithms, the list is ordered. Algorithm numbers are in Section 9. This is only found on a self-signature ๐Ÿซง.

5.2.3.9. Preferred Compression Algorithms ๐Ÿ—œ๏ธ

(array of one-octet values)

These are the compression algorithm numbers the key holder prefers to use ๐ŸŽ€. Like the preferred symmetric algorithms, the list is ordered. Algorithm numbers are in Section 9. If this subpacket isn't included, ZIP is preferred. A zero means uncompressed data is preferred, and the key holder's software might not have any compression software in that implementation ๐Ÿ™ƒ. This is only found on a self-signature.

5.2.3.10. Signature Expiration Time โณ

(4-octet time field)

This is the signature's validity period: the number of seconds after the signature creation time when the signature expires. If this isn't present or has a value of zero, it never expires ๐Ÿฆ‹.

5.2.3.11. Exportable Certification ๐Ÿ“ค

(1 octet of exportability, 0 for not, 1 for exportable)

This subpacket says whether a certification signature is "exportable," meaning other users besides the signature's issuer can use it ๐Ÿ’Œ. The packet body holds a Boolean flag for whether the signature is exportable. If this packet isn't present, the certification is exportable, which is the same as a flag containing a 1 โœจ.

Non-exportable, or "local," certifications are signatures a user makes to mark a key as valid within that user's own implementation only ๐Ÿ”’.

So when an implementation gets a user's copy of a key ready to send to another user (this is the "exporting" process), any local certification signatures get deleted from the key ๐Ÿ—‘๏ธ.

The receiver of a transported key "imports" it, and likewise trims any local certifications. In normal operation there won't be any, as long as the import happens on an exported key. But it can reasonably happen in some cases. For example, if an implementation lets keys be imported from a key database as well as from an exported key, this situation can come up ๐Ÿง.

Some implementations don't represent the interest of a single user (a key server is one example). Those implementations always trim local certifications from any key they handle ๐Ÿ–ฅ๏ธ.

5.2.3.12. Revocable ๐Ÿšซ

(1 octet of revocability, 0 for not, 1 for revocable)

This is the signature's revocability status. The packet body holds a Boolean flag for whether the signature is revocable. Signatures that aren't revocable have any later revocation signatures ignored ๐Ÿ™…โ€โ™€๏ธ. They're a promise from the signer that they can't revoke their signature for the life of their key ๐Ÿซถ. If this packet isn't present, the signature is revocable.

5.2.3.13. Trust Signature ๐Ÿค

(1 octet "level" (depth), 1 octet of trust amount)

The signer says the key is not only valid but also trustworthy at the specified level ๐Ÿ‘‘. Level 0 means the same thing as an ordinary validity signature. Level 1 means the signed key is asserted to be a valid trusted introducer, and the 2nd octet of the body specifies the degree of trust ๐ŸŒท. Level 2 means the signed key is asserted to be trusted to issue level 1 trust signatures, meaning it's a "meta introducer" ๐Ÿง . Generally, a level n trust signature asserts that a key is trusted to issue level n-1 trust signatures ๐Ÿ”. The trust amount is in a range from 0-255, read like this: values less than 120 mean partial trust, and values of 120 or greater mean complete trust ๐ŸŒˆ. Implementations SHOULD emit values of 60 for partial trust and 120 for complete trust ๐Ÿ’–.

5.2.3.14. Regular Expression ๐Ÿ”Ž

(null-terminated regular expression)

This is used together with trust Signature packets (of level > 0) to limit the scope of the trust being extended ๐ŸŽฏ. Only signatures by the target key on User IDs that match the regular expression in this packet's body get trust extended by the trust Signature subpacket. The regular expression uses the same syntax as Henry Spencer's "almost public domain" regular expression [REGEX] package. You can find a description of the syntax in Section 8 below ๐Ÿ“–.

5.2.3.15. Revocation Key ๐Ÿ—๏ธ

(1 octet of class, 1 octet of public-key algorithm ID, 20 octets of fingerprint)

This authorizes the specified key to issue revocation signatures for this key ๐Ÿ›ก๏ธ. The class octet MUST...

Correction for accuracy: the class octet must have bit 0x80 set. If bit 0x40 is set, the revocation information is sensitive. Other bits are for future expansion to other kinds of authorizations. This is found on a self-signature ๐ŸŽ€.

If the "sensitive" flag is set, the keyholder feels this subpacket holds private trust information that describes a real-world sensitive relationship ๐Ÿคซ. When this flag is set, implementations SHOULD NOT export this signature to other users, except where the data has to be available: when the signature is being sent to the designated revoker, or when it comes with a revocation signature from that revoker ๐Ÿšจ. Heads up: it may be a good idea to isolate this subpacket in a separate signature so it isn't combined with other subpackets that need to be exported ๐Ÿ’….

5.2.3.16. Notation Data ๐Ÿ“

    (4 octets of flags, 2 octets of name length (M),
                        2 octets of value length (N),
                        M octets of name data,
                        N octets of value data)

This subpacket describes a "notation" the issuer wants to put on the signature ๐Ÿช„. The notation has a name and a value, and each one is a string of octets. A signature can have more than one notation ๐Ÿญ. Notations can be used for any extension the issuer of the signature cares to make ๐ŸŒธ. The "flags" field holds four octets of flags.

All undefined flags MUST be zero ๐Ÿšซ. The defined flags are as follows:

    First octet: 0x80 = human-readable.  This note value is text.
    Other octets: none.

Notation names are arbitrary strings encoded in UTF-8 ๐ŸŒ. They live in two namespaces: the IETF namespace and the user namespace.

The IETF namespace is registered with IANA ๐Ÿ›๏ธ. These names MUST NOT contain the "@" character (0x40). That character is a tag for the user namespace, so it's off limits here ๐Ÿ™…โ€โ™€๏ธ.

Names in the user namespace consist of a UTF-8 string tag, followed by "@", followed by a DNS domain name. Note that the tag MUST NOT contain an "@" character. For example, the "sample" tag used by Example Corporation could be sample@example.com ๐Ÿ’Œ.

Names in a user space are owned and controlled by the owners of that domain. Obviously, it's bad form to create a new name in a DNS space you don't own ๐Ÿ˜ค.

Since the user namespace is in the form of an email address, implementers MAY wish to arrange for that address to reach a person who can be consulted about how the named tag is used ๐Ÿ“ง. Note that because of UTF-8 encoding, not every valid user space name tag is a valid email address ๐Ÿค“.

If there's a critical notation, the criticality applies to that specific notation, not to notations in general โš ๏ธ.

5.2.3.17. Key Server Preferences ๐Ÿ–ฅ๏ธ

(N octets of flags)

This is a list of one-bit flags that show the key holder's preferences for how the key is handled on a key server ๐Ÿซง. All undefined flags MUST be zero.

First octet: 0x80 = No-modify: the key holder requests that this key only be modified or updated by the key holder or an administrator of the key server ๐Ÿ”.

This is found only on a self-signature ๐Ÿ’œ.

5.2.3.18. Preferred Key Server ๐ŸŒธ

(String)

okay so this is a URI of a key server that the key holder prefers be used for updates ๐Ÿ”‘โœจ bestie, keys with multiple User IDs can have a preferred key server for each User ID, which is so main character energy ๐Ÿ’…. and ngl this is a URI, so the key server can literally be a copy of the key fetched by ftp, http, finger, etc. ๐Ÿฆ‹

5.2.3.19. Primary User ID ๐Ÿ‘‘

(1 octet, Boolean)

this is a flag in a User ID's self-signature that says whether this User ID is the main User ID for this key ๐Ÿ’–. it's reasonable for an implementation to resolve ambiguities in preferences, etc. by looking at the primary User ID ๐Ÿงš. if this flag is absent, its value is zero. if more than one User ID in a key is marked as primary, the implementation may resolve the ambiguity in any way it sees fit, but it is RECOMMENDED that priority be given to the User ID with the most recent self-signature ๐Ÿซถ.

when this shows up on a self-signature on a User ID packet, the subpacket only applies to User ID packets. when it shows up on a self-signature on a User Attribute packet, it only applies to User Attribute packets. so yeah, there are two different and independent "primaries" ๐Ÿ’œ: one for User IDs, and one for User Attributes. iconic ๐ŸŽ€.

5.2.3.20. Policy URI ๐Ÿ“œ

(String)

this subpacket contains a URI of a document that describes the policy under which the signature was issued ๐ŸŒˆ.

5.2.3.21. Key Flags ๐Ÿšฉ

(N octets of flags)

this subpacket holds a list of binary flags that tell you stuff about a key ๐Ÿ“. it's a string of octets, and an implementation MUST NOT assume a fixed size ๐Ÿ’…, because it can grow over time โœจ. if a list is shorter than an implementation expects, the unstated flags are considered to be zero. the defined flags are:

First octet:

0x01 - This key may be used to certify other keys.

0x02 - This key may be used to sign data.

0x04 - This key may be used to encrypt communications.

0x08 - This key may be used to encrypt storage.

0x10 - The private component of this key may have been split by a secret-sharing mechanism.

0x20 - This key may be used for authentication.

0x80 - The private component of this key may be in the possession of more than one person.

Usage notes ๐Ÿ™ˆ:

the flags can show up in self-signatures or in certification signatures, and they mean different things depending on who's making the statement ๐Ÿ’€. like, a certification signature with the "sign data" flag is saying the certification is for that use. the "communications encryption" flag in a self-signature is stating a preference that a given key be used for communications. but ngl, it's a thorny issue to decide what counts as "communications" and what counts as "storage" ๐Ÿ˜ญ. that decision is left wholly up to the implementation; the authors of this document do not claim any special wisdom on the issue and realize that accepted opinion may change ๐ŸŒธ.

The "split key" (0x10) and "group key" (0x80) flags are placed on a self-signature only; they are meaningless on a certification signature. They SHOULD be placed only on a direct-key signature (type 0x1F) or a subkey signature (type 0x18), one that refers to the key the flag applies to ๐Ÿ”ฅ.

5.2.3.22. Signer's User ID ๐ŸŽ€

(String)

this subpacket lets a keyholder say which User ID is responsible for the signing ๐Ÿ’–. lots of keyholders use a single key for different purposes, like business communications and personal communications ๐Ÿงš. this subpacket lets such a keyholder state which of their roles is making a signature โœจ.

this subpacket is not appropriate to use to refer to a User Attribute packet ๐Ÿ™…โ€โ™€๏ธ.

5.2.3.23. Reason for Revocation ๐Ÿ’€

(1 octet of revocation code, N octets of reason string)

this subpacket is only used in key revocation and certification revocation signatures ๐Ÿฅ€. it describes why the key or certificate was revoked.

the first octet has a machine-readable code that denotes the reason for the revocation:

0 - No reason specified (key revocations or cert revocations) 1 - Key is superseded (key revocations) 2 - Key material has been compromised (key revocations) 3 - Key is retired and no longer used (key revocations) 32 - User ID information is no longer valid (cert revocations) 100-110 - Private Use

after the revocation code comes a string of octets that gives info about the Reason for Revocation in human-readable form (UTF-8) ๐Ÿ“. the string may be null, that is, of zero length. the length of the subpacket is the length of the reason string plus one. an implementation SHOULD implement this subpacket, include it in all revocation signatures, and interpret revocations appropriately ๐Ÿซถ. there are important semantic differences between the reasons, and so there are important reasons for revoking signatures ๐Ÿ˜ญ.

if a key has been revoked because of a compromise, all signatures created by that key are suspect ๐Ÿšจ. but if it was merely superseded or retired, old signatures are still valid ๐ŸŒˆ. if the revoked signature is the self-signature for certifying a User ID, a revocation denotes that the user name is no longer in use. such a revocation SHOULD include a 0x20 code ๐Ÿ’….

note that any signature may be revoked, including a certification on some other person's key ๐Ÿ‘€. there are lots of good reasons for revoking a certification signature, like when a keyholder leaves the employ of a business with an email address ๐Ÿ’ผ. a revoked certification is no longer part of validity calculations ๐Ÿฆ‹.

5.2.3.24. Features ๐Ÿงš

(N octets of flags)

the Features subpacket shows which advanced OpenPGP features a user's implementation supports ๐ŸŒŸ. this is so that as features are added to OpenPGP that cannot be backwards-compatible, a user can say they can use that feature ๐Ÿ’–. the flags are single bits that indicate a given feature is supported.

this subpacket is similar to a preferences subpacket, and only shows up in a self-signature ๐Ÿ’œ.

an implementation SHOULD NOT use a feature listed when sending to a user who does not state that they can use it ๐Ÿ™ˆ.

Defined features are:

First octet:

0x01 - Modification Detection (packets 18 and 19)

if an implementation implements any of the defined features, it SHOULD implement the Features subpacket, too ๐ŸŽ€.

an implementation may freely infer features from other suitable implementation-dependent mechanisms โœจ.

5.2.3.25. Signature Target ๐ŸŽฏ

(1 octet public-key algorithm, 1 octet hash algorithm, N octets hash)

this subpacket identifies a specific target signature that a signature refers to ๐Ÿ’…. for revocation signatures, this subpacket explicitly designates which signature is being revoked ๐Ÿ™Œ. for a third-party or timestamp signature, this designates what signature is signed ๐ŸŒธ. all arguments are an identifier of that target signature.

the N octets of hash data MUST be the size of the hash of the signature ๐Ÿงฎ. for example, a target signature with a SHA-1 hash MUST have 20 octets of hash data ๐Ÿค“.

5.2.3.26. Embedded Signature ๐Ÿงธ

(1 signature packet body)

this subpacket contains a complete Signature packet body as specified in Section 5.2 above ๐Ÿ’–. it's useful when one signature needs to refer to, or be incorporated in, another signature ๐Ÿซถ.

5.2.4. Computing Signatures โœ๏ธ

all signatures are formed by producing a hash over the signature data, and then using the resulting hash in the signature algorithm ๐Ÿ’ซ.

for binary document signatures (type 0x00), the document data is hashed directly ๐Ÿ“„. for text document signatures (type 0x01), the document is canonicalized by converting line endings to <CR><LF>, and the resulting data is hashed ๐ŸŒˆ.

when a signature is made over a key, the hash data starts with the octet 0x99, followed by a two-octet length of the key, and then body of the key packet ๐Ÿ”‘. (note that this is an old-style packet header for a key packet with two-octet length.) a subkey binding signature (type 0x18) or primary key binding signature (type 0x19) then hashes the subkey using the same format as the main key (also using 0x99 as the first octet) ๐Ÿ’ž. key revocation signatures (types 0x20 and 0x28) hash only the key being revoked ๐Ÿ’€.

a certification signature (type 0x10 through 0x13) hashes the User ID being bound to the key into the hash context after the above data ๐Ÿชช. a V3 certification hashes the contents of the User ID or attribute packet packet, without any header. a V4 certification hashes the constant 0xB4 for User ID certifications or the constant 0xD1 for User Attribute certifications, followed by a four-octet number giving the length of the User ID or User Attribute data, and then the User ID or User Attribute data ๐Ÿงพ.

when a signature is made over a Signature packet (type 0x50), the hash data starts with the octet 0x88, followed by the four-octet length of the signature, and then the body of the Signature packet ๐Ÿ–Š๏ธ. (note that this is an old-style packet header for a Signature packet with the length-of-length set to zero.) the unhashed subpacket data of the Signature packet being hashed is not included in the hash, and the unhashed subpacket data length value is set to zero ๐Ÿ™ˆ.

once the data body is hashed, then a trailer is hashed ๐ŸŽฌ. a V3 signature hashes five octets of the packet body, starting from the signature type field. this data is the signature type, followed by the four-octet signature time. a V4 signature hashes the packet body starting from its first field, the version number, through the end of the hashed subpacket data. thus, the fields hashed are the signature version, the signature type, the public-key algorithm, the hash algorithm, the hashed subpacket length, and the hashed subpacket body ๐Ÿง .

V4 signatures also hash in a final trailer of six octets: the version of the Signature packet, i.e., 0x04; 0xFF; and a four-octet, big-endian number that is the length of the hashed data from the Signature packet (note that this number does not include these final six octets) ๐Ÿฌ.

after all this has been hashed in a single hash context, the resulting hash field is used in the signature algorithm and placed at the end of the Signature packet ๐ŸŽ.

5.2.4.1. Subpacket Hints ๐Ÿ’ก

it is certainly possible for a signature to contain conflicting information in subpackets ๐Ÿ˜…. for example, a signature may contain multiple copies of a preference or multiple expiration times. in most cases, an implementation SHOULD use the last subpacket in the signature, but MAY use any conflict resolution scheme that makes more sense. please note that we are intentionally leaving conflict resolution to the implementer ๐Ÿซ ; most conflicts are simply syntax errors, and the wishy-washy language here allows a receiver to be generous in what they accept, while putting pressure on a creator to be stingy in what they generate ๐Ÿคญ.

some apparent conflicts may actually make sense ๐Ÿง. for example, suppose a keyholder has a V3 key and a V4 key that share the same RSA key material. either of these keys can verify a signature created by the other, and it may be reasonable for a signature to contain an issuer subpacket for each key, as a way of explicitly tying those keys to the signature ๐Ÿฅฐ.

5.3. Symmetric-Key Encrypted Session Key Packets (Tag 3) ๐Ÿ”โœจ

okay so this packet is giving secret-keeper energy ๐Ÿคซ It holds the symmetric-key encryption of a session key, which is the key that actually encrypts a message. Zero or more Public-Key Encrypted Session Key packets and/or Symmetric-Key Encrypted Session Key packets can come before a Symmetrically Encrypted Data packet holding the encrypted message. The message gets encrypted with a session key, and that session key is then encrypted and stored in either the Encrypted Session Key packet or the Symmetric-Key Encrypted Session Key packet. Iconic layering ๐Ÿ’…

If the Symmetrically Encrypted Data packet is preceded by one or more Symmetric-Key Encrypted Session Key packets, each one specifies a passphrase that may be used to decrypt the message. So a message can be locked to a bunch of public keys AND one or more passphrases, which is so main character ๐ŸŽ€ This packet type is new and is not generated by PGP 2.x or PGP 5.0, so it's a newer girl in town ๐ŸŒธ

The body of this packet consists of:

  • A one-octet version number. The only currently defined version is 4. ๐Ÿ”ข
  • A one-octet number describing the symmetric algorithm used. ๐Ÿง
  • A string-to-key (S2K) specifier, length as defined above. ๐Ÿ—๏ธ
  • Optionally, the encrypted session key itself, which is decrypted with the string-to-key object. ๐Ÿซง

If the encrypted session key is not present (you can tell from the packet length and the S2K specifier size), then the S2K algorithm applied to the passphrase produces the session key for decrypting the file, using the symmetric cipher algorithm from the Symmetric-Key Encrypted Session Key packet. Brb, I'm obsessed with how efficient this is ๐Ÿ˜ญ

If the encrypted session key IS present, the result of applying the S2K algorithm to the passphrase is used to decrypt just that encrypted session key field, using CFB mode with an IV of all zeros. The decryption result is a one-octet algorithm identifier that says which symmetric-key encryption algorithm was used to encrypt the following Symmetrically Encrypted Data packet, followed by the session key octets themselves. ๐Ÿ’–

Note: because an all-zero IV is used for this decryption, the S2K specifier MUST use a salt value, either a Salted S2K or an Iterated-Salted S2K. The salt value will ensure that the decryption key is not repeated even if the passphrase is reused. This is the part you cannot skip, bestie ๐Ÿšจ

5.4. One-Pass Signature Packets (Tag 4) โœ๏ธ

the One-Pass Signature packet shows up BEFORE the signed data and has enough info to let the receiver start calculating any hashes needed to verify the signature. That's the trick, because it means the Signature packet can go at the END of the message, so the signer can compute the whole signed message in one pass. Galaxy brain fr ๐ŸŒŒ

A One-Pass Signature does not interoperate with PGP 2.6.x or earlier. Those versions are giving "no thank you" ๐Ÿ™…โ€โ™€๏ธ

The body of this packet consists of:

  • A one-octet version number. The current version is 3. ๐Ÿ”ข
  • A one-octet signature type. Signature types are described in Section 5.2.1. ๐Ÿ“œ
  • A one-octet number describing the hash algorithm used. ๐ŸŒ€
  • A one-octet number describing the public-key algorithm used. ๐Ÿ”‘
  • An eight-octet number holding the Key ID of the signing key. ๐Ÿชช
  • A one-octet number holding a flag showing whether the signature is nested. A zero value indicates that the next packet is another One-Pass Signature packet that describes another signature to be applied to the same message data. ๐Ÿช†

Note that if a message contains more than one one-pass signature, then the Signature packets bracket the message. That means the first Signature packet after the message corresponds to the LAST one-pass packet, and the final Signature packet corresponds to the FIRST one-pass packet. It's like a little nesting doll situation and I'm here for it ๐Ÿช†โœจ


5.5. Key Material Packet ๐Ÿ—๏ธ๐ŸŽ€

A key material packet contains all the information about a public or private key. There are four variants of this packet type, and two major versions. So yes, this section is complex, and I'm not gonna pretend otherwise ๐Ÿ˜ฉ

5.5.1. Key Packet Variants ๐Ÿฆ‹

5.5.1.1. Public-Key Packet (Tag 6) ๐Ÿ’œ

A Public-Key packet starts a series of packets that forms an OpenPGP key (sometimes called an OpenPGP certificate). It's the opener, the main character, the one everybody follows ๐Ÿ‘‘

5.5.1.2. Public-Subkey Packet (Tag 14) ๐ŸŒท

A Public-Subkey packet (tag 14) has exactly the same format as a Public-Key packet, but denotes a subkey. One or more subkeys may be associated with a top-level key. By convention, the top-level key provides signature services, and the subkeys provide encryption services. Team roles, but make it cryptography ๐Ÿซถ

Note: in PGP 2.6.x, tag 14 was intended to indicate a comment packet. This tag got picked for reuse because no previous version of PGP ever emitted comment packets, but they did properly ignore them. Public-Subkey packets are ignored by PGP 2.6.x and do not cause it to fail, which gives a limited degree of backward compatibility. Such a clever little workaround, I'm obsessed ๐Ÿ™ˆ

5.5.1.3. Secret-Key Packet (Tag 5) ๐Ÿค

A Secret-Key packet contains all the information that is found in a Public-Key packet, including the public-key material. It also includes the secret-key material after all the public-key fields. So it's the public version PLUS the secret stuff, and yes the secret stuff is locked in ๐Ÿ”’

5.5.1.4. Secret-Subkey Packet (Tag 7) ๐ŸŒ™

A Secret-Subkey packet (tag 7) is the subkey analog of the Secret Key packet and has exactly the same format. Same vibe, smaller crew ๐Ÿ’…

5.5.2. Public-Key Packet Formats ๐Ÿ“ฆ

There are two versions of key-material packets. Version 3 packets were first generated by PGP 2.6. Version 4 keys first appeared in PGP 5.0 and are the preferred key version for OpenPGP. Version 4 is the real one, bestie โœจ

OpenPGP implementations MUST create keys with version 4 format. V3 keys are deprecated; an implementation MUST NOT generate a V3 key, but MAY accept it. So make new keys in v4, and if you get a v3 one, you can take it or leave it ๐Ÿซ 

A version 3 public key or public-subkey packet contains:

  • A one-octet version number (3). ๐Ÿ”ข
  • A four-octet number denoting the time that the key was created. โฐ
  • A two-octet number denoting the time in days that this key is valid. If this number is zero, then it does not expire. ๐Ÿ“…
  • A one-octet number denoting the public-key algorithm of this key. ๐Ÿงฎ
  • A series of multiprecision integers comprising the key material:
    • a multiprecision integer (MPI) of RSA public modulus n;
    • an MPI of RSA public encryption exponent e.

V3 keys are deprecated, and honestly they have three weaknesses, so let's roast them ๐Ÿ’€ First, it is relatively easy to construct a V3 key that has the same Key ID as any other key, because the Key ID is simply the low 64 bits of the public modulus. Second, because the fingerprint of a V3 key hashes the key material but not its length, there is an increased opportunity for fingerprint collisions. Third, there are weaknesses in the MD5 hash algorithm that make developers prefer other algorithms. See below for a fuller discussion of Key IDs and fingerprints. Not the vibe ๐Ÿ™…โ€โ™€๏ธ

V2 keys are identical to the deprecated V3 keys except for the version number. An implementation MUST NOT generate them and MAY accept or reject them as it sees fit. Basically a v2 key is just a v3 key with a different name tag, so same drama ๐ŸŽญ

The version 4 format is similar to the version 3 format except for the absence of a validity period. That has been moved to the Signature packet. In addition, fingerprints of version 4 keys are calculated differently from version 3 keys, as described in the section "Enhanced Key Formats". Glow-up energy โœจ

A version 4 packet contains:

  • A one-octet version number (4). ๐Ÿ”ข
  • A four-octet number denoting the time that the key was created. โฐ
  • A one-octet number denoting the public-key algorithm of this key. ๐Ÿงฎ
  • A series of multiprecision integers comprising the key material. This algorithm-specific portion is:

Algorithm-Specific Fields for RSA public keys:

  • multiprecision integer (MPI) of RSA public modulus n;
  • MPI of RSA public encryption exponent e.

Algorithm-Specific Fields for DSA public keys:

  • MPI of DSA prime p;
  • MPI of DSA group order q (q is a prime divisor of p-1);
  • MPI of DSA group generator g;
  • MPI of DSA public-key value y (= g**x mod p where x is secret).

Algorithm-Specific Fields for Elgamal public keys:

  • MPI of Elgamal prime p;
  • MPI of Elgamal group generator g;
  • MPI of Elgamal public key value y (= g**x mod p where x is secret). ๐Ÿง 

5.5.3. Secret-Key Packet Formats ๐Ÿ”โœจ

okay so the Secret-Key and Secret-Subkey packets are basically the public versions with a secret twist ๐Ÿซถ They contain all the data of the Public-Key and Public-Subkey packets, plus extra algorithm-specific secret-key data tacked on the end, usually in encrypted form ๐Ÿ”’ (the secret stuff is locked up, obviously).

The packet contains:

  • A Public-Key or Public-Subkey packet, as described above.
  • One octet indicating string-to-key usage conventions. Zero indicates that the secret-key data is not encrypted. 255 or 254 indicates that a string-to-key specifier is being given. Any other value is a symmetric-key encryption algorithm identifier.
  • [Optional] If string-to-key usage octet was 255 or 254, a one-octet symmetric encryption algorithm.
  • [Optional] If string-to-key usage octet was 255 or 254, a string-to-key specifier. The length of the string-to-key specifier is implied by its type, as described above.
  • [Optional] If secret data is encrypted (string-to-key usage octet not zero), an Initial Vector (IV) of the same length as the cipher's block size.
  • Plain or encrypted multiprecision integers comprising the secret key data. These algorithm-specific fields are as described below.
  • If the string-to-key usage octet is zero or 255, then a two-octet checksum of the plaintext of the algorithm-specific portion (sum of all octets, mod 65536). If the string-to-key usage octet was 254, then a 20-octet SHA-1 hash of the plaintext of the algorithm-specific portion. This checksum or hash is encrypted together with the algorithm-specific fields (if string-to-key usage octet is not zero). Note that for all other values, a two-octet checksum is required.

Algorithm-Specific Fields for RSA secret keys:

  • multiprecision integer (MPI) of RSA secret exponent d.
  • MPI of RSA secret prime value p.
  • MPI of RSA secret prime value q (p < q).
  • MPI of u, the multiplicative inverse of p, mod q.

Algorithm-Specific Fields for DSA secret keys:

  • MPI of DSA secret exponent x.

Algorithm-Specific Fields for Elgamal secret keys:

  • MPI of Elgamal secret exponent x.

Secret MPI values can be encrypted using a passphrase ๐Ÿ—๏ธ If a string-to-key specifier is given, that describes the algorithm for converting the passphrase to a key, else a simple MD5 hash of the passphrase is used. Implementations MUST use a string-to-key specifier; the simple hash is for backward compatibility and is deprecated, though implementations MAY continue to use existing private keys in the old format. The cipher for encrypting the MPIs is specified in the Secret-Key packet.

Encryption and decryption of the secret data is done in CFB mode using the key created from the passphrase and the Initial Vector from the packet. A different mode is used with V3 keys (which are only RSA) than with other key formats. With V3 keys, the MPI bit count prefix (i.e., the first two octets) is not encrypted. Only the MPI non-prefix data is encrypted. Furthermore, the CFB state is resynchronized at the beginning of each new MPI value, so that the CFB block boundary is aligned with the start of the MPI data.

With V4 keys, a simpler method is used. All secret MPI values are encrypted in CFB mode, including the MPI bitcount prefix.

The two-octet checksum that follows the algorithm-specific portion is the algebraic sum, mod 65536, of the plaintext of all the algorithm-specific octets (including MPI prefix and data). With V3 keys, the checksum is stored in the clear. With V4 keys, the checksum is encrypted like the algorithm-specific data. This value is used to check that the passphrase was correct. However, this checksum is deprecated ๐Ÿ™…โ€โ™€๏ธ an implementation SHOULD NOT use it, but should rather use the SHA-1 hash denoted with a usage octet of 254. The reason for this is that there are some attacks that involve undetectably modifying the secret key. ๐Ÿ˜ฑ


5.6. Compressed Data Packet (Tag 8) ๐Ÿ—œ๏ธ

The Compressed Data packet contains compressed data. Typically, this packet is found as the contents of an encrypted packet, or following a Signature or One-Pass Signature packet, and contains a literal data packet.

The body of this packet consists of:

  • One octet that gives the algorithm used to compress the packet.
  • Compressed data, which makes up the remainder of the packet.

A Compressed Data Packet's body contains an block that compresses some set of packets. See section "Packet Composition" for details on how messages are formed.

ZIP-compressed packets are compressed with raw RFC 1951 [RFC1951] DEFLATE blocks. Note that PGP V2.6 uses 13 bits of compression. If an implementation uses more bits of compression, PGP V2.6 cannot decompress it.

ZLIB-compressed packets are compressed with RFC 1950 [RFC1950] ZLIB-style blocks.

BZip2-compressed packets are compressed using the BZip2 [BZ2] algorithm.


5.7. Symmetrically Encrypted Data Packet (Tag 9) ๐Ÿ”๐Ÿ’œ

The Symmetrically Encrypted Data packet contains data encrypted with a symmetric-key algorithm. When it has been decrypted, it contains other packets (usually a literal data packet or compressed data packet, but in theory other Symmetrically Encrypted Data packets or sequences of packets that form whole OpenPGP messages).

The body of this packet consists of:

  • Encrypted data, the output of the selected symmetric-key cipher operating in OpenPGP's variant of Cipher Feedback (CFB) mode.

The symmetric cipher used may be specified in a Public-Key or Symmetric-Key Encrypted Session Key packet that precedes the Symmetrically Encrypted Data packet. In that case, the cipher algorithm octet is prefixed to the session key before it is encrypted. If no packets of these types precede the encrypted data, the IDEA algorithm is used with the session key calculated as the MD5 hash of the passphrase, though this use is deprecated.

The data is encrypted in CFB mode, with a CFB shift size equal to the cipher's block size. The Initial Vector (IV) is specified as all zeros. Instead of using an IV, OpenPGP prefixes a string of length equal to the block size of the cipher plus two to the data before it is encrypted. The first block-size octets (for example, 8 octets for a 64-bit block length) are random, and the following two octets are copies of the last two octets of the IV. For example, in an 8-octet block, octet 9 is a repeat of octet 7, and octet 10 is a repeat of octet 8. In a cipher of length 16, octet 17 is a repeat of octet 15 and octet 18 is a repeat of octet 16. As a pedantic clarification, in both these examples, we consider the first octet to be numbered 1.

After encrypting the first block-size-plus-two octets, the CFB state is resynchronized. The last block-size octets of ciphertext are passed through the cipher and the block boundary is reset.

The repetition of 16 bits in the random data prefixed to the message allows the receiver to immediately check whether the session key is incorrect. See the "Security Considerations" section for hints on the proper use of this "quick check". ๐Ÿ‘€


5.8. Marker Packet (Obsolete Literal Packet) (Tag 10) ๐Ÿšฉ

An experimental version of PGP used this packet as the Literal packet, but no released version of PGP generated Literal packets with this tag. With PGP 5.x, this packet has been reassigned and is reserved for use as the Marker packet.

The body of this packet consists of:

  • The three octets 0x50, 0x47, 0x50 (which spell "PGP" in UTF-8).

Such a packet MUST be ignored when received. It may be placed at the beginning of a message that uses features not available in PGP 2.6.x in order to cause that version to report that newer software is necessary to process the message. ๐Ÿ™ˆ


5.9. Literal Data Packet (Tag 11) ๐Ÿ“๐ŸŒธ

A Literal Data packet contains the body of a message; data that is not to be further interpreted.

The body of this packet consists of:

  • A one-octet field that describes how the data is formatted.

If it is a 'b' (0x62), then the Literal packet contains binary data. If it is a 't' (0x74), then it contains text data, and thus may need line ends converted to local form, or other text-mode changes. The tag 'u' (0x75) means the same as 't', but also indicates that implementation believes that the literal data contains UTF-8 text.

Early versions of PGP also defined a value of 'l' as a 'local' mode for machine-local conversions. RFC 1991 [RFC1991] incorrectly stated this local mode flag as '1' (ASCII numeral one). Both of these local modes are deprecated.

  • File name as a string (one-octet length, followed by a file name). This may be a zero-length string. Commonly, if the source of the encrypted data is a file, this will be the name of the encrypted file. An implementation MAY consider the file name in the Literal packet to be a more authoritative name than the actual file name.

If the special name "_CONSOLE" is used, the message is considered to be "for your eyes only". This advises that the message data is unusually sensitive, and the receiving program should process it more carefully, perhaps avoiding storing the received data to disk, for example. ๐Ÿคซ

  • A four-octet number that indicates a date associated with the literal data. Commonly, the date might be the modification date of a file, or the time the packet was created, or a zero that indicates no specific time.
  • The remainder of the packet is literal data.

Text data is stored with <CR><LF> text endings (i.e., network-normal line endings). These should be converted to native line endings by the receiving software.


5.10. Trust Packet (Tag 12) ๐Ÿ›ก๏ธ

The Trust packet is used only within keyrings and is not normally exported. Trust packets contain data that record the user's specifications of which key holders are trustworthy introducers, along with other information that implementing software uses for trust information. The format of Trust packets is defined by a given implementation.

Trust packets SHOULD NOT be emitted to output streams that are transferred to other users, and they SHOULD be ignored on any input other than local keyring files. ๐Ÿ”‘


5.11. User ID Packet (Tag 13) ๐Ÿชช๐Ÿ’–

A User ID packet consists of UTF-8 text that is intended to represent the name and email address of the key holder. By convention, it includes an RFC 2822 [RFC2822] mail name-addr, but there are no restrictions on its content. The packet length in the header specifies the length of the User ID. โœจ

5.12. User Attribute Packet (Tag 17) ๐ŸŽ€

okay so the User Attribute packet is basically a cousin of the User ID packet ๐Ÿ’… except it can store way more kinds of data than the User ID packet, which is limited to text ๐Ÿ“. Just like the User ID packet, a User Attribute packet may be certified by the key owner ("self-signed") or by any other key owner who cares to certify it ๐Ÿซถ. Except as noted, a User Attribute packet may be used anywhere that a User ID packet may be used โœจ.

User Attribute packets are not a required part of the OpenPGP standard, but implementations SHOULD provide at least enough compatibility to properly handle a certification signature on the User Attribute packet ๐Ÿ™ˆ. A simple way to do this is to treat the User Attribute packet as a User ID packet with opaque contents, but an implementation may use any method desired ๐Ÿฆ‹.

The User Attribute packet is made up of one or more attribute subpackets ๐Ÿงš. Each subpacket has a subpacket header and a body. The header consists of:

  • the subpacket length (1, 2, or 5 octets)
  • the subpacket type (1 octet)

and it is followed by the subpacket specific data ๐ŸŒธ.

The only currently defined subpacket type is 1, signifying an image. An implementation SHOULD ignore any subpacket of a type that it does not recognize ๐Ÿ‘€. Subpacket types 100 through 110 are reserved for private or experimental use ๐Ÿ”ฎ.

5.12.1. The Image Attribute Subpacket ๐Ÿ“ธ

The Image Attribute subpacket is used to encode an image, presumably (but not required to be) that of the key owner ๐Ÿ’–.

The Image Attribute subpacket begins with an image header. The first two octets of the image header contain the length of the image header. Note that unlike other multi-octet numerical values in this document, due to a historical accident this value is encoded as a little-endian number ๐Ÿ™ƒ. The image header length is followed by a single octet for the image header version. The only currently defined version of the image header is 1, which is a 16-octet image header. The first three octets of a version 1 image header are thus 0x10, 0x00, 0x01.

The fourth octet of a version 1 image header designates the encoding format of the image. The only currently defined encoding format is the value 1 to indicate JPEG. Image format types 100 through 110 are reserved for private or experimental use. The rest of the version 1 image header is made up of 12 reserved octets, all of which MUST be set to 0 ๐Ÿ”’.

The rest of the image subpacket contains the image itself. As the only currently defined image type is JPEG, the image is encoded in the JPEG File Interchange Format (JFIF), a standard file format for JPEG images [JFIF] ๐ŸŒˆ.

An implementation MAY try to determine the type of an image by examination of the image data if it is unable to handle a particular version of the image header or if a specified encoding format value is not recognized ๐Ÿ”.

5.13. Sym. Encrypted Integrity Protected Data Packet (Tag 18) ๐Ÿ”

The Symmetrically Encrypted Integrity Protected Data packet is a variant of the Symmetrically Encrypted Data packet. It is a new feature created for OpenPGP that addresses the problem of detecting a modification to encrypted data ๐Ÿ•ต๏ธโ€โ™€๏ธ. It is used in combination with a Modification Detection Code packet ๐Ÿฆ‹.

There is a corresponding feature in the features Signature subpacket that denotes that an implementation can properly use this packet type ๐Ÿ’…. An implementation MUST support decrypting these packets and SHOULD prefer generating them to the older Symmetrically Encrypted Data packet when possible ๐Ÿ”ฅ. Since this data packet protects against modification attacks, this standard encourages its proliferation ๐Ÿ“ฃ. While blanket adoption of this data packet would create interoperability problems, rapid adoption is nevertheless important ๐Ÿ˜ญ. An implementation SHOULD specifically denote support for this packet, but it MAY infer it from other mechanisms โœจ.

For example, an implementation might infer from the use of a cipher such as Advanced Encryption Standard (AES) or Twofish that a user supports this feature. It might place in the unhashed portion of another user's key signature a Features subpacket. It might also present a user with an opportunity to regenerate their own self-signature with a Features subpacket ๐Ÿงš.

This packet contains data encrypted with a symmetric-key algorithm and protected against modification by the SHA-1 hash algorithm. When it has been decrypted, it will typically contain other packets (often a Literal Data packet or Compressed Data packet). The last decrypted packet in this packet's payload MUST be a Modification Detection Code packet ๐ŸŽฏ.

The body of this packet consists of:

  • A one-octet version number. The only currently defined value is 1.
  • Encrypted data, the output of the selected symmetric-key cipher operating in Cipher Feedback mode with shift amount equal to the block size of the cipher (CFB-n where n is the block size).

The symmetric cipher used MUST be specified in a Public-Key or Symmetric-Key Encrypted Session Key packet that precedes the Symmetrically Encrypted Data packet. In either case, the cipher algorithm octet is prefixed to the session key before it is encrypted ๐Ÿ—๏ธ.

The data is encrypted in CFB mode, with a CFB shift size equal to the cipher's block size. The Initial Vector (IV) is specified as all zeros. Instead of using an IV, OpenPGP prefixes an octet string to the data before it is encrypted. The length of the octet string equals the block size of the cipher in octets, plus two. The first octets in the group, of length equal to the block size of the cipher, are random; the last two octets are each copies of their 2nd preceding octet. For example, with a cipher whose block size is 128 bits or 16 octets, the prefix data will contain 16 random octets, then two more octets, which are copies of the 15th and 16th octets, respectively. Unlike the Symmetrically Encrypted Data Packet, no special CFB resynchronization is done after encrypting this prefix data. See "OpenPGP CFB Mode" below for more details ๐ŸŒธ.

The repetition of 16 bits in the random data prefixed to the message allows the receiver to immediately check whether the session key is incorrect ๐Ÿ‘€.

The plaintext of the data to be encrypted is passed through the SHA-1 hash function, and the result of the hash is appended to the plaintext in a Modification Detection Code packet. The input to the hash function includes the prefix data described above; it includes all of the plaintext, and then also includes two octets of values 0xD3, 0x14. These represent the encoding of a Modification Detection Code packet tag and length field of 20 octets ๐Ÿงฎ.

The resulting hash value is stored in a Modification Detection Code (MDC) packet, which MUST use the two octet encoding just given to represent its tag and length field. The body of the MDC packet is the 20-octet output of the SHA-1 hash ๐Ÿ’œ.

The Modification Detection Code packet is appended to the plaintext and encrypted along with the plaintext using the same CFB context ๐Ÿซถ.

During decryption, the plaintext data should be hashed with SHA-1, including the prefix data as well as the packet tag and length field of the Modification Detection Code packet. The body of the MDC packet, upon decryption, is compared with the result of the SHA-1 hash ๐Ÿ”Ž.

Any failure of the MDC indicates that the message has been modified and MUST be treated as a security problem. Failures include a difference in the hash values, but also the absence of an MDC packet, or an MDC packet in any position other than the end of the plaintext. Any failure SHOULD be reported to the user. ๐Ÿšจ๐Ÿ’€

Note: future designs of new versions of this packet should consider rollback attacks since it will be possible for an attacker to change the version back to 1 ๐Ÿ™ˆ.

NON-NORMATIVE EXPLANATION ๐Ÿง 

The MDC system, as packets 18 and 19 are called, were created to provide an integrity mechanism that is less strong than a signature, yet stronger than bare CFB encryption ๐Ÿ“.

It is a limitation of CFB encryption that damage to the ciphertext will corrupt the affected cipher blocks and the block following. Additionally, if data is removed from the end of a CFB-encrypted block, that removal is undetectable. (Note also that CBC mode has a similar limitation, but data removed from the front of the block is undetectable.) ๐Ÿ˜ฌ

The obvious way to protect or authenticate an encrypted block is to digitally sign it. However, many people do not wish to habitually sign data, for a large number of reasons beyond the scope of this document. Suffice it to say that many people consider properties such as deniability to be as valuable as integrity ๐Ÿคทโ€โ™€๏ธ.

OpenPGP addresses this desire to have more security than raw encryption and yet preserve deniability with the MDC system. An MDC is intentionally not a MAC. Its name was not selected by accident. It is analogous to a checksum ๐Ÿงฉ.

Despite the fact that it is a relatively modest system, it has proved itself in the real world. It is an effective defense to several attacks that have surfaced since it has been created. It has met its modest goals admirably ๐Ÿ‘‘.

Consequently, because it is a modest security system, it has modest requirements on the hash function(s) it employs. It does not rely on a hash function being collision-free, it relies on a hash function being one-way. If a forger, Frank, wishes to send Alice a (digitally) unsigned message that says, "I've always secretly loved you, signed Bob", it is far easier for him to construct a new message than it is to modify anything intercepted from Bob. (Note also that if Bob wishes to communicate secretly with Alice, but without authentication or identification and with a threat model that includes forgers, he has a problem that transcends mere cryptography.) ๐Ÿ’Œ๐Ÿ˜ญ

Note also that unlike nearly every other OpenPGP subsystem, there are no parameters in the MDC system. It hard-defines SHA-1 as its hash function. This is not an accident. It is an intentional choice to avoid downgrade and cross-grade attacks while making a simple, fast system. (A downgrade attack would be an attack that replaced SHA-256 with SHA-1, for example. A cross-grade attack would replace SHA-1 with another 160-bit hash, such as RIPE-MD/160, for example.) ๐Ÿฆ‹

However, given the present state of hash function cryptanalysis and cryptography, it may be desirable to upgrade the MDC system to a new hash function. See Section 13.11 in the "IANA Considerations" for guidance ๐ŸŒŸ.

5.14. Modification Detection Code Packet (Tag 19) ๐Ÿ•ต๏ธโ€โ™€๏ธ

okay bestie, this packet holds a SHA-1 hash of the plaintext, and its whole job is catching anyone who messes with the message ๐Ÿ” It's only used with a Symmetrically Encrypted Integrity Protected Data packet. The Modification Detection Code packet MUST be the last packet in the plaintext data that gets encrypted inside that Symmetrically Encrypted Integrity Protected Data packet, and it MUST appear in no other place. Main character placement only ๐Ÿ‘‘

A Modification Detection Code packet MUST have a length of 20 octets. No exceptions, no debate ๐Ÿ’…

The body is just one thing:

  • A 20-octet SHA-1 hash of the preceding plaintext data of the Symmetrically Encrypted Integrity Protected Data packet, including prefix data, the tag octet, and length octet of the Modification Detection Code packet.

Also, the Modification Detection Code packet MUST always use a new format encoding of the packet tag, plus a one-octet encoding of the packet length. The reason is that the hashing rules for modification detection include a one-octet tag and a one-octet length in the data hash. It's a bit restrictive, ngl, but it keeps things simpler ๐Ÿง โœจ


6. Radix-64 Conversions ๐ŸŽ€

So, like we said in the intro, OpenPGP's native format for objects is just a stream of arbitrary octets. Some systems need those objects to survive character set translation, data conversions, and other chaos ๐Ÿ˜ญ

In theory, any printable encoding scheme that met the requirements of the unsafe channel would work, since it wouldn't change the underlying binary bit streams of the native OpenPGP data structures. OpenPGP picks one specific printable encoding scheme so everyone stays interoperable ๐Ÿซถ

OpenPGP's Radix-64 encoding has two parts: a base64 encoding of the binary data, and a checksum. The base64 part is identical to the MIME base64 content-transfer-encoding [RFC2045] ๐ŸŒธ

The checksum is a 24-bit Cyclic Redundancy Check (CRC), turned into four radix-64 characters using the same MIME base64 transformation, and it gets an equal sign (=) in front. The CRC uses the generator 0x864CFB and an initialization of 0xB704CE. The accumulation runs over the data before it gets converted to radix-64, not over the converted data. There's a sample implementation in the next section ๐Ÿ‘€

The checksum with its leading equal sign MAY appear on the first line after the base64 encoded data.

Rationale for CRC-24: 24 bits fits evenly into printable base64, and a nonzero initialization can catch more errors than a zero initialization. Smart, right? ๐Ÿงš

6.1. An Implementation of the CRC-24 in "C" ๐Ÿ’ป

   #define CRC24_INIT 0xB704CEL
   #define CRC24_POLY 0x1864CFBL

   typedef long crc24;
   crc24 crc_octets(unsigned char *octets, size_t len)
   {
       crc24 crc = CRC24_INIT;
       int i;
       while (len--) {
           crc ^= (*octets++) << 16;
           for (i = 0; i < 8; i++) {
               crc <<= 1;
               if (crc & 0x1000000)
                   crc ^= CRC24_POLY;
           }
       }
       return crc & 0xFFFFFFL;
   }

6.2. Forming ASCII Armor ๐Ÿฆ‹

When OpenPGP encodes data into ASCII Armor, it wraps the Radix-64 encoded data in specific headers, so OpenPGP can rebuild the data later. An OpenPGP implementation MAY use ASCII armor to protect raw binary data. The headers also tell the user what kind of data is inside the armor ๐Ÿ’–

Here's how you put ASCII Armor together. Concatenate the following:

  • An Armor Header Line, appropriate for the type of data
  • Armor Headers
  • A blank (zero-length, or containing only whitespace) line
  • The ASCII-Armored data
  • An Armor Checksum
  • The Armor Tail, which depends on the Armor Header Line

An Armor Header Line is the right header line text surrounded by five (5) dashes (-, 0x2D) on each side. The text depends on what kind of data is being armored and how it's being encoded. These are the header line texts:

  • BEGIN PGP MESSAGE: Used for signed, encrypted, or compressed files.
  • BEGIN PGP PUBLIC KEY BLOCK: Used for armoring public keys.
  • BEGIN PGP PRIVATE KEY BLOCK: Used for armoring private keys.
  • BEGIN PGP MESSAGE, PART X/Y: Used for multi-part messages, where the armor is split amongst Y parts, and this is the Xth part out of Y.
  • BEGIN PGP MESSAGE, PART X: Used for multi-part messages, where this is the Xth part of an unspecified number of parts. Requires the MESSAGE-ID Armor Header to be used.
  • BEGIN PGP SIGNATURE: Used for detached signatures, OpenPGP/MIME signatures, and cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE for detached signatures.

Every one of these Armor Header Lines has to be a complete line. That means there's always a line ending before the starting five dashes, and after the ending five dashes. The header lines MUST start at the beginning of a line, and MUST NOT have any text other than whitespace following them on the same line. Those line endings count as part of the Armor Header Line when working out what content they delimit, which matters a LOT for cleartext signatures (see below) ๐Ÿซ 

The Armor Headers are key-value string pairs that can tell the user or the receiving OpenPGP implementation how to decode or use the message. They're part of the armor, not part of the message, so no signatures applied to the message protect them ๐Ÿ™ˆ

Each Armor Header is a key-value pair. A colon (:, 0x3A) and a single space (0x20) separate the key and the value. Wait, let me get this exact: the separator is a colon (':' 0x38) followed by a single space (0x20). OpenPGP should consider improperly formatted Armor Headers to be corruption of the ASCII Armor. Unknown keys should be reported to the user, but OpenPGP should continue to process the message.

Note that some transport methods are picky about line length. There's a 76-character limit for the Radix-64 data (Section 6.3), but there's no limit on how long Armor Headers can be. Make sure they're short enough to survive transport. One trick is to repeat an Armor Header key several times with different values, so no single line gets overly long ๐Ÿ“

These are the Armor Header Keys defined so far:

  • "Version", which states the OpenPGP implementation and version used to encode the message.
  • "Comment", a user-defined comment. OpenPGP defines all text to be in UTF-8. A comment may be any UTF-8 string. However, the whole point of armoring is to provide seven-bit-clean data. So if a comment has characters outside the US-ASCII range of UTF, they may very well not survive transport ๐Ÿ˜ฌ
  • "MessageID", a 32-character string of printable characters. The string must be the same for all parts of a multi-part message that uses the "PART X" Armor Header. MessageID strings should be unique enough that the recipient of the mail can associate all the parts of a message with each other. A good checksum or cryptographic hash function is sufficient.

The MessageID SHOULD NOT appear unless it's in a multi-part message. If it does appear at all, it MUST be computed from the finished (encrypted, signed, etc.) message in a deterministic fashion, rather than being a purely random value. This lets the legitimate recipient confirm that the MessageID can't serve as a covert means of leaking cryptographic key information ๐Ÿ•ต๏ธโ€โ™€๏ธ๐Ÿ”

  • "Hash", a comma-separated list of hash algorithms used in this message. This is only used in cleartext signed messages.
  • "Charset", a description of the character set the plaintext is in. OpenPGP defines text to be in UTF-8, and an implementation gets the best results by translating into and out of UTF-8. Easier said than done, though! Some communities are super happy with a character set like ISO Latin-5 or a Japanese character set and have no need for UTF-8. In those cases, an implementation MAY override the UTF-8 default by using this header key. An implementation MAY implement this key and any translations it cares to, and an implementation MAY ignore it and assume all text is UTF-8 ๐ŸŒˆ

The Armor Tail Line is composed the same way as the Armor Header Line, except the string "BEGIN" is replaced by the string "END" ๐ŸŽ€

6.3. Encoding Binary in Radix-64 โœจ

okay bestie, so the encoding process takes your input in 24-bit groups and turns each group into 4 encoded characters ๐Ÿงš Going left to right, you glue three 8-bit input groups together to make one 24-bit group. Then those 24 bits get split into four 6-bit groups, and each one becomes a single digit in the Radix-64 alphabet ๐Ÿ’… When you're encoding a bit stream, the bit stream must be presumed to be ordered with the most significant bit first. So the first bit is the high-order bit in the first 8-bit octet, the eighth bit is the low-order bit in that same octet, and so on ๐Ÿซถ

      +--first octet--+-second octet--+--third octet--+
      |7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
      +-----------+---+-------+-------+---+-----------+
      |5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
      +--1.index--+--2.index--+--3.index--+--4.index--+

Each 6-bit group is used as an index into an array of 64 printable characters from the table below. The character referenced by the index gets placed in the output string ๐ŸŒธ

  Value Encoding  Value Encoding  Value Encoding  Value Encoding
      0 A            17 R            34 i            51 z
      1 B            18 S            35 j            52 0
      2 C            19 T            36 k            53 1
      3 D            20 U            37 l            54 2
      4 E            21 V            38 m            55 3
      5 F            22 W            39 n            56 4
      6 G            23 X            40 o            57 5
      7 H            24 Y            41 p            58 6
      8 I            25 Z            42 q            59 7
      9 J            26 a            43 r            60 8
     10 K            27 b            44 s            61 9
     11 L            28 c            45 t            62 +
     12 M            29 d            46 u            63 /
     13 N            30 e            47 v
     14 O            31 f            48 w         (pad) =
     15 P            32 g            49 x
     16 Q            33 h            50 y

The encoded output stream must be represented in lines of no more than 76 characters each. ๐Ÿ“

Special processing is performed if fewer than 24 bits are available at the end of the data being encoded. There are three possibilities ๐Ÿ‘€

  1. The last data group has 24 bits (3 octets). No special processing is needed.
  2. The last data group has 16 bits (2 octets). The first two 6-bit groups are processed as above. The third (incomplete) data group has two zero-value bits added to it, and is processed as above. A pad character (=) is added to the output.
  3. The last data group has 8 bits (1 octet). The first 6-bit group is processed as above. The second (incomplete) data group has four zero-value bits added to it, and is processed as above. Two pad characters (=) are added to the output.

6.4. Decoding Radix-64 ๐Ÿ”

In Radix-64 data, characters other than those in the table, line breaks, and other white space probably indicate a transmission error, which is a vibe check for a warning message or even a message rejection, depending on the situation. Decoding software must ignore all white space โœจ

Because it is used only for padding at the end of the data, the occurrence of any = characters may be taken as evidence that the end of the data has been reached (without truncation in transit). No such assurance is possible, however, when the number of octets transmitted was a multiple of three and no = characters are present ๐Ÿ™ˆ

6.5. Examples of Radix-64 ๐ŸŽ€

Input data:  0x14FB9C03D97E
Hex:     1   4    F   B    9   C     | 0   3    D   9    7   E
8-bit:   00010100 11111011 10011100  | 00000011 11011001 11111110
6-bit:   000101 001111 101110 011100 | 000000 111101 100111 111110
Decimal: 5      15     46     28       0      61     37     62
Output:  F      P      u      c        A      9      l      +
Input data:  0x14FB9C03D9
Hex:     1   4    F   B    9   C     | 0   3    D   9
8-bit:   00010100 11111011 10011100  | 00000011 11011001
                                             pad with 00
6-bit:   000101 001111 101110 011100 | 000000 111101 100100
Decimal: 5      15     46     28       0      61     36
                                                pad with =
Output:  F      P      u      c        A      9      k      =
Input data:  0x14FB9C03
Hex:     1   4    F   B    9   C     | 0   3
8-bit:   00010100 11111011 10011100  | 00000011
                                    pad with 0000
6-bit:   000101 001111 101110 011100 | 000000 110000
Decimal: 5      15     46     28       0      48
                                         pad with =      =
Output:  F      P      u      c        A      w      =      =

6.6. Example of an ASCII Armored Message ๐Ÿฆ‹

   -----BEGIN PGP MESSAGE-----
   Version: OpenPrivacy 0.99

   yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
   vBSFjNSiVHsuAA==
   =njUN
   -----END PGP MESSAGE-----

Note that this example has extra indenting; an actual armored message would have no leading whitespace. ๐Ÿ™ƒ

7. Cleartext Signature Framework ๐ŸŒˆ

It is desirable to be able to sign a textual octet stream without ASCII armoring the stream itself, so the signed text is still readable without special software ๐Ÿ“– To bind a signature to such a cleartext, this framework is used. (Note that this framework is not intended to be reversible. RFC 3156 [RFC3156] defines another way to sign cleartext messages for environments that support MIME.) The cleartext signed message consists of:

  • The cleartext header -----BEGIN PGP SIGNED MESSAGE----- on a single line,
  • One or more "Hash" Armor Headers,
  • Exactly one empty line not included into the message digest,
  • The dash-escaped cleartext that is included into the message digest,
  • The ASCII armored signature(s) including the -----BEGIN PGP SIGNATURE----- Armor Header and Armor Tail Lines.

If the "Hash" Armor Header is given, the specified message digest algorithm(s) are used for the signature. If there are no such headers, MD5 is used. If MD5 is the only hash used, then an implementation MAY omit this header for improved V2.x compatibility. If more than one message digest is used in the signature, the "Hash" armor header contains a comma-delimited list of used message digests ๐Ÿ“

Current message digest names are described below with the algorithm IDs.

An implementation SHOULD add a line break after the cleartext, but MAY omit it if the cleartext ends with a line break. This is for visual clarity ๐Ÿ‘‘

7.1. Dash-Escaped Text ๐Ÿ’…

The cleartext content of the message must also be dash-escaped.

Dash-escaped cleartext is the ordinary cleartext where every line starting with a dash - (0x2D) is prefixed by the sequence dash - (0x2D) and space ' ' (0x20). This prevents the parser from recognizing armor headers of the cleartext itself. An implementation MAY dash-escape any line, SHOULD dash-escape lines commencing "From" followed by a space, and MUST dash-escape any line commencing in a dash. The message digest is computed using the cleartext itself, not the dash-escaped form ๐Ÿ”ฅ

As with binary signatures on text documents, a cleartext signature is calculated on the text using canonical <CR><LF> line endings. The line ending (i.e., the <CR><LF>) before the -----BEGIN PGP SIGNATURE----- line that terminates the signed text is not considered part of the signed text ๐ŸŽ€

When reversing dash-escaping, an implementation MUST strip the string "- " if it occurs at the beginning of a line, and SHOULD warn on "-" and any character other than a space at the beginning of a line. โš ๏ธ

Also, any trailing whitespace -- spaces (0x20) and tabs (0x09) -- at the end of any line is removed when the cleartext signature is generated. ๐Ÿงน

8. Regular Expressions ๐Ÿงš

A regular expression is zero or more branches, separated by |. It matches anything that matches one of the branches.

A branch is zero or more pieces, concatenated. It matches a match for the first, followed by a match for the second, etc.

A piece is an atom possibly followed by *, +, or ?. An atom followed by * matches a sequence of 0 or more matches of the atom. An atom followed by + matches a sequence of 1 or more matches of the atom. An atom followed by ? matches a match of the atom, or the null string.

An atom is a regular expression in parentheses (matching a match for the regular expression), a range (see below), . (matching any single character), ^ (matching the null string at the beginning of the input string), $ (matching the null string at the end of the input string), a \ followed by a single character (matching that character), or a single character with no other significance (matching that character).

A range is a sequence of characters enclosed in []. It normally matches any single character from the sequence. If the sequence begins with ^, it matches any single character not from the rest of the sequence. If two characters in the sequence are separated by -, this is shorthand for the full list of ASCII characters between them (e.g., [0-9] matches any decimal digit). To include a literal ] in the sequence, make it the first character (following a possible ^). To include a literal -, make it the first or last character. ๐Ÿฆ„

9. Constants ๐Ÿ’–

This section describes the constants used in OpenPGP.

Note that these tables are not exhaustive lists; an implementation MAY implement an algorithm not on these lists, so long as the algorithm numbers are chosen from the private or experimental algorithm range. โœจ

See the section "Notes on Algorithms" below for more discussion of the algorithms.

9.1. Public-Key Algorithms ๐Ÿ”‘

   ID           Algorithm
   --           ---------
   1          - RSA (Encrypt or Sign) [HAC]
   2          - RSA Encrypt-Only [HAC]
   3          - RSA Sign-Only [HAC]
   16         - Elgamal (Encrypt-Only) [ELGAMAL] [HAC]
   17         - DSA (Digital Signature Algorithm) [FIPS186] [HAC]
   18         - Reserved for Elliptic Curve
   19         - Reserved for ECDSA
   20         - Reserved (formerly Elgamal Encrypt or Sign)
   21         - Reserved for Diffie-Hellman (X9.42,
                as defined for IETF-S/MIME)
   100 to 110 - Private/Experimental algorithm

Implementations MUST implement DSA for signatures, and Elgamal for encryption. Implementations SHOULD implement RSA keys (1). RSA Encrypt-Only (2) and RSA Sign-Only are deprecated and SHOULD NOT be generated, but may be interpreted. See Section 13.5. See Section 13.8 for notes on Elliptic Curve (18), ECDSA (19), Elgamal Encrypt or Sign (20), and X9.42 (21). Implementations MAY implement any other algorithm. ๐ŸŒธ

9.2. Symmetric-Key Algorithms ๐Ÿ”โœจ

okay so here's the lineup of symmetric ciphers, bestie ๐Ÿ’… each ID is the number that shows up in the packet, and it points at one specific cipher:

ID           Algorithm
--           ---------
0          - Plaintext or unencrypted data
1          - IDEA [IDEA]
2          - TripleDES (DES-EDE, [SCHNEIER] [HAC] -
             168 bit key derived from 192)
3          - CAST5 (128 bit key, as per [RFC2144])
4          - Blowfish (128 bit key, 16 rounds) [BLOWFISH]
5          - Reserved
6          - Reserved
7          - AES with 128-bit key [AES]
8          - AES with 192-bit key
9          - AES with 256-bit key
10         - Twofish with 256-bit key [TWOFISH]
100 to 110 - Private/Experimental algorithm

So the rules for what you have to build ๐ŸŽ€: TripleDES is a MUST, no cap ๐Ÿ˜ญ. AES-128 and CAST5 are a SHOULD, which means they're super recommended ๐Ÿซถ. If you need to talk to PGP 2.6 or earlier, you need IDEA, because that's the only symmetric cipher those versions use ๐Ÿ™ˆ. Anything else is a MAY, so you can go wild ๐Ÿฆ‹.

9.3. Compression Algorithms ๐Ÿ—œ๏ธ๐Ÿ’–

Here's the compression menu, and yes, the IDs work the same way:

ID           Algorithm
--           ---------
0          - Uncompressed
1          - ZIP [RFC1951]
2          - ZLIB [RFC1950]
3          - BZip2 [BZ2]
100 to 110 - Private/Experimental algorithm

Uncompressed data is a MUST, so every implementation has to handle it ๐Ÿ“. ZIP is a SHOULD, so you really want it in there โœจ. Everything else is a MAY ๐ŸŒˆ.

9.4. Hash Algorithms ๐Ÿง‚๐Ÿ”

Next up are the hash algorithms, with both their IDs and the text names you'd spell out:

ID           Algorithm                             Text Name
--           ---------                             ---------
1          - MD5 [HAC]                             "MD5"
2          - SHA-1 [FIPS180]                       "SHA1"
3          - RIPE-MD/160 [HAC]                     "RIPEMD160"
4          - Reserved
5          - Reserved
6          - Reserved
7          - Reserved
8          - SHA256 [FIPS180]                      "SHA256"
9          - SHA384 [FIPS180]                      "SHA384"
10         - SHA512 [FIPS180]                      "SHA512"
11         - SHA224 [FIPS180]                      "SHA224"
100 to 110 - Private/Experimental algorithm

SHA-1 is a MUST for implementations ๐Ÿ‘‘. Other algorithms are a MAY ๐Ÿฆ‹. And MD5 is deprecated, so please don't reach for it ๐Ÿ’€.

10. IANA Considerations ๐Ÿ›๏ธ๐ŸŒธ

OpenPGP is super parameterized, which means there are a bunch of things to think about when you're allocating parameters for extensions. This section explains how IANA should look at extensions to the protocol described in this document ๐Ÿง .

10.1. New String-to-Key Specifier Types ๐Ÿ”‘

OpenPGP S2K specifiers have a way for new algorithms to turn a string into a key ๐Ÿช„. This spec makes a registry of S2K specifier types. Each entry has the S2K type, the name of the S2K, and a reference to the spec that defines it. The starting values for this registry are in Section 3.7.1. Adding a new S2K specifier MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ“œ.

10.2. New Packets ๐Ÿ“ฆ

Big new OpenPGP features get added through new packet types ๐ŸŽ. This spec makes a registry of packet types. Each entry has the packet type, the name of the packet, and a reference to the spec that defines it. The starting values are in Section 4.3. Adding a new packet type MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ’….

10.2.1. User Attribute Types ๐Ÿชช

The User Attribute packet gives you an extensible way to identify certificates in other ways ๐Ÿ’ซ. This spec makes a registry of User Attribute types, with the User Attribute type, its name, and a reference to the defining spec. The starting values are in Section 5.12. Adding a new User Attribute type MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐ŸŽ€.

10.2.1.1. Image Format Subpacket Types ๐Ÿ–ผ๏ธ

Inside User Attribute packets there's an extensible mechanism for other kinds of image-based user attributes ๐Ÿ“ธ. This spec makes a registry of Image Attribute subpacket types, with the Image Attribute subpacket type, the name of that subpacket, and a reference to the defining spec. The starting values are in Section 5.12.1. Adding a new Image Attribute subpacket type MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐ŸŒท.

10.2.2. New Signature Subpackets โœ๏ธ

OpenPGP signatures have a mechanism where signed (or unsigned) data can be attached for all sorts of purposes, through the Signature subpackets covered in Section 5.2.3.1 ๐Ÿง. This spec makes a registry of Signature subpacket types, with the subpacket type, its name, and a reference to the defining spec. The starting values are in Section 5.2.3.1. Adding a new Signature subpacket MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ’œ.

10.2.2.1. Signature Notation Data Subpackets ๐Ÿ“

Signatures also have a way to add extensions, through the Notation Data subpackets ๐Ÿซง. Each notation is a key/value pair, and notations live in two spaces: a user space that nobody manages at all, and an IETF space ๐ŸŒˆ.

This spec makes a registry of Signature Notation Data types. It has the Signature Notation Data type, its name, its allowed values, and a reference to the defining spec. The starting values are in Section 5.2.3.16. Adding a new Signature Notation Data subpacket MUST be done through the EXPERT REVIEW method, as described in [RFC2434] ๐Ÿง‘โ€๐Ÿ”ฌ.

10.2.2.2. Key Server Preference Extensions ๐ŸŒ

Signatures can also say what they prefer about key servers ๐Ÿ—‚๏ธ. This spec makes a registry of key server preferences, with the preference value, its name, and a reference to the defining spec. The starting values are in Section 5.2.3.17. Adding a new key server preference MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿซถ.

10.2.2.3. Key Flags Extensions ๐Ÿšฉ

Signatures have a way to set flags about how a key gets used ๐Ÿ—๏ธ. This spec makes a registry of key usage flags, with the key flags value, the flag's name, and a reference to the defining spec. The starting values are in Section 5.2.3.21. Adding a new key usage flag MUST be done through the IETF CONSENSUS method, as described in [RFC2434] โœจ.

10.2.2.4. Reason for Revocation Extensions ๐Ÿšซ

Signatures can also set flags that explain why a key was revoked ๐Ÿฅ€. This spec makes a registry of "Reason for Revocation" flags, with the "Reason for Revocation" flags value, its name, and a reference to the defining spec. The starting values are in Section 5.2.3.23. Adding a new feature flag MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ’ซ.

10.2.2.5. Implementation Features ๐Ÿ› ๏ธ

Signatures have flags that say which optional features an implementation supports ๐ŸŽ›๏ธ. This spec makes a registry of feature-implementation flags, with the feature-implementation flags value, its name, and a reference to the defining spec. The starting values are in Section 5.2.3.24. Adding a new feature-implementation flag MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ”ฅ.

Also see Section 13.12 for more on when feature flags are needed ๐Ÿ‘€.

10.2.3. New Packet Versions ๐Ÿ”ข

The core OpenPGP packets all have version numbers, so you can revise one by introducing a new version of an existing packet ๐Ÿฃ. This spec makes a registry of packet types, with the packet type, the number of the version, and a reference to the defining spec. The starting values are in Section 5. Adding a new packet version MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐ŸŒธ.

10.3. New Algorithms ๐Ÿงช

Section 9 lists the core algorithms OpenPGP uses ๐Ÿ“‹. Adding a new algorithm is usually pretty simple ๐Ÿฅฐ. For example, a new symmetric cipher usually just needs a constant allocated for it. If that cipher had a block size other than 64 or 128 bits, there might need to be extra documentation about how OpenPGP-CFB mode would be adjusted ๐Ÿงฉ. Same vibe with DSA: when it went from a maximum of 1024-bit public keys to 3072-bit public keys, the newer FIPS 186 already had enough info for anyone to implement it ๐Ÿ™Œ. Changes to this document were mostly made for emphasis ๐Ÿ“ฃ.

10.3.1. Public-Key Algorithms ๐Ÿ—๏ธ

OpenPGP has a bunch of public-key algorithms ๐Ÿ”. This spec makes a registry of public-key algorithm identifiers, with the algorithm name, its key sizes and parameters, and a reference to the defining spec. The starting values are in Section 9. Adding a new public-key algorithm MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ’–.

10.3.2. Symmetric-Key Algorithms ๐Ÿ”’

OpenPGP also has a bunch of symmetric-key algorithms ๐Ÿฌ. This spec makes a registry of symmetric-key algorithm identifiers, with the algorithm name, its key sizes and block size, and a reference to the defining spec. The starting values are in Section 9. Adding a new symmetric-key algorithm MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐ŸŽ€.

10.3.3. Hash Algorithms ๐Ÿง‚

OpenPGP has a bunch of hash algorithms too ๐Ÿง. This spec makes a registry of hash algorithm identifiers, with the algorithm name, a text version of that name, its block size, an OID hash prefix, and a reference to the defining spec. The starting values for the identifiers and text names are in Section 9, and the OIDs and expanded signature prefixes are in Section 5.2.2 ๐Ÿ“š. Adding a new hash algorithm MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿซถ.

10.3.4. Compression Algorithms ๐Ÿ—œ๏ธ

OpenPGP has a bunch of compression algorithms as well ๐Ÿ’ซ. This spec makes a registry of compression algorithm identifiers, with the algorithm name and a reference to the defining spec. The starting values are in Section 9.3. Adding a new compression key algorithm MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐ŸŒˆ.

11. Packet Composition ๐Ÿงฉ๐Ÿงš

OpenPGP packets get assembled into sequences, and that's how you make messages and transfer keys ๐Ÿ“ฌ. Not every possible packet sequence makes sense or is correct ๐Ÿ™ˆ. This section lays out the rules for how packets should be put into sequences ๐Ÿ’….

11.1. Transferable Public Keys โœจ

Okay bestie, so OpenPGP users can transfer public keys ๐Ÿ’– Here are the essential elements of a transferable public key:

  • One Public-Key packet
  • Zero or more revocation signatures
  • One or more User ID packets
  • After each User ID packet, zero or more Signature packets (certifications)
  • Zero or more User Attribute packets
  • After each User Attribute packet, zero or more Signature packets (certifications)
  • Zero or more Subkey packets
  • After each Subkey packet, one Signature packet, plus optionally a revocation

The Public-Key packet comes first ๐Ÿ‘‘ Each User ID packet after it tells you who owns the key. If there are several User IDs, that's several ways of identifying the same unique individual. Like, a user might have more than one email address and make a User ID for each one ๐ŸŒธ

Right after each User ID packet, there are zero or more Signature packets. Each of those is calculated on the User ID packet right before it and on the initial Public-Key packet. The signature certifies the public key and the User ID. Basically the signer is saying they believe this public key belongs to the user named in that User ID ๐Ÿฆ‹

Zero or more User Attribute packets also live in this section. Like User IDs, each User Attribute packet is followed by zero or more Signature packets, calculated on the User Attribute packet right before them and on the initial Public-Key packet ๐Ÿ’…

User Attribute packets and User ID packets can be freely mixed in this section, as long as each signature stays attached to the right User Attribute or User ID packet. No mix-ups allowed ๐Ÿ™ˆ

After the User ID packet or Attribute packet, there may be zero or more Subkey packets. Usually subkeys show up when the top-level public key is signature-only. But ANY V4 key can have subkeys, and those subkeys can be encryption-only, signature-only, or general-purpose ๐Ÿงš V3 keys MUST NOT have subkeys, though. Not even a little bit ๐Ÿ’€

Every Subkey packet MUST be followed by one Signature packet, which should be a subkey binding signature issued by the top-level key. For subkeys that can issue signatures, the subkey binding signature MUST contain an Embedded Signature subpacket with a primary key binding signature (0x19) issued by the subkey on the top-level key. That's the cross-check that ties everything together ๐Ÿ”ฅ

Subkey and Key packets may each be followed by a revocation Signature packet to say the key is revoked. Revocation signatures only get accepted if they come from the key itself, or from a key that's allowed to issue revocations through a Revocation Key subpacket in a self-signature by the top-level key ๐ŸŒˆ

Transferable public-key packet sequences can be concatenated, so you can transfer multiple public keys in one go โœจ


11.2. Transferable Secret Keys ๐Ÿ”

OpenPGP users may transfer secret keys too. A transferable secret key has the same format as a transferable public key, except it uses secret-key and secret-subkey packets instead of public-key and public-subkey packets ๐Ÿ’œ Implementations SHOULD include self-signatures on any user IDs and subkeys, because that lets a complete public key get extracted from the transferable secret key automatically. Implementations MAY choose to omit the self-signatures, especially if a transferable public key comes along with the transferable secret key ๐Ÿซถ


11.3. OpenPGP Messages ๐Ÿ“จ

An OpenPGP message is a packet or a sequence of packets that matches these grammatical rules (comma means sequential composition, and vertical bar separates alternatives):

OpenPGP Message :- Encrypted Message | Signed Message |
                   Compressed Message | Literal Message.

Compressed Message :- Compressed Data Packet.

Literal Message :- Literal Data Packet.

ESK :- Public-Key Encrypted Session Key Packet |
       Symmetric-Key Encrypted Session Key Packet.

ESK Sequence :- ESK | ESK Sequence, ESK.

Encrypted Data :- Symmetrically Encrypted Data Packet |
      Symmetrically Encrypted Integrity Protected Data Packet

Encrypted Message :- Encrypted Data | ESK Sequence, Encrypted Data.

One-Pass Signed Message :- One-Pass Signature Packet,
            OpenPGP Message, Corresponding Signature Packet.

Signed Message :- Signature Packet, OpenPGP Message |
            One-Pass Signed Message.

Also, decrypting a Symmetrically Encrypted Data packet or a Symmetrically Encrypted Integrity Protected Data packet, as well as decompressing a Compressed Data packet, must yield a valid OpenPGP Message. No shortcuts here, babe ๐Ÿ’…


11.4. Detached Signatures ๐Ÿ“Ž

Some OpenPGP apps use so-called "detached signatures". Like, a program bundle might contain a file, plus a second file that's a detached signature of the first one ๐ŸŽ€ These are just a Signature packet stored separately from the data they sign โœจ


12. Enhanced Key Formats ๐ŸŒธ

12.1. Key Structures ๐Ÿ—๏ธ

Here's the format of an OpenPGP V3 key. Entries in square brackets are optional, and ellipses mean repetition ๐Ÿ‘‘

        RSA Public Key
           [Revocation Self Signature]
            User ID [Signature ...]
           [User ID [Signature ...] ...]

Each signature certifies the RSA public key and the User ID right before it. The RSA public key can have many User IDs, and each User ID can have many signatures ๐Ÿ’– V3 keys are deprecated. Implementations MUST NOT generate new V3 keys, but MAY continue to use existing ones ๐Ÿฅ€

A V4 key that uses multiple public keys is similar, except the other keys get added to the end as "subkeys" of the primary key ๐Ÿฆ‹

        Primary-Key
           [Revocation Self Signature]
           [Direct Key Signature...]
            User ID [Signature ...]
           [User ID [Signature ...] ...]
           [User Attribute [Signature ...] ...]
           [[Subkey [Binding-Signature-Revocation]
                   Primary-Key-Binding-Signature] ...]

A subkey always has a single signature after it, issued with the primary key to tie the two keys together ๐Ÿซถ That binding signature may be in either V3 or V4 format, but SHOULD be V4. Subkeys that can issue signatures MUST have a V4 binding signature, due to the REQUIRED embedded primary key binding signature. Big deal, very important ๐Ÿ˜ญ

In the diagram above, if a subkey's binding signature has been revoked, the revoked key may be removed, leaving only one key ๐Ÿงš

In a V4 key, the primary key MUST be a key capable of certification. The subkeys can be keys of any other type. There may be other ways to build V4 keys too. For example, there could be a single-key RSA key in V4 format, a DSA primary key with an RSA encryption key, or an RSA primary key with an Elgamal subkey, etc. ๐ŸŒˆ

It's also possible to have a signature-only subkey. This lets a primary key collect certifications (key signatures) while only certifying the subkeys that get used for encryption and signatures ๐Ÿ’…


12.2. Key IDs and Fingerprints ๐Ÿ”‘

For a V3 key, the eight-octet Key ID is made of the low 64 bits of the public modulus of the RSA key โœจ

The fingerprint of a V3 key is made by hashing the body (but not the two-octet length) of the MPIs that form the key material (public modulus n, followed by exponent e) with MD5. Note that both V3 keys and MD5 are deprecated. Bad vibes, bestie ๐Ÿ’€

A V4 fingerprint is the 160-bit SHA-1 hash of the octet 0x99, followed by the two-octet packet length, followed by the entire Public-Key packet starting with the version field. The Key ID is the low-order 64 bits of the fingerprint. Here are the fields of the hash material, using a DSA key as the example ๐ŸŒธ

a.1) 0x99 (1 octet)

a.2) high-order length octet of (b)-(e) (1 octet)
a.3) low-order length octet of (b)-(e) (1 octet)

  b) version number = 4 (1 octet);

  c) timestamp of key creation (4 octets);

  d) algorithm (1 octet): 17 = DSA (example);

  e) Algorithm-specific fields.

And the Algorithm-Specific Fields for the DSA example:

Algorithm-Specific Fields for DSA keys (example):

e.1) MPI of DSA prime p;

e.2) MPI of DSA group order q (q is a prime divisor of p-1);

e.3) MPI of DSA group generator g;

e.4) MPI of DSA public-key value y (= g**x mod p where x is secret).

It's possible for Key IDs to collide, meaning two different keys with the same Key ID. Note that there's a much smaller, but still non-zero, chance that two different keys have the same fingerprint ๐Ÿ™ˆ

Also, if V3 and V4 format keys share the same RSA key material, they'll have different Key IDs AND different fingerprints ๐Ÿฆ‹

Finally, the Key ID and fingerprint of a subkey are calculated the same way as for a primary key, including the 0x99 as the first octet (even though that isn't a valid packet ID for a public subkey) ๐Ÿ‘‘


13. Notes on Algorithms ๐Ÿงช

13.1. PKCS#1 Encoding in OpenPGP ๐Ÿ”

This standard uses the PKCS#1 functions EME-PKCS1-v1_5 and EMSA-PKCS1-v1_5. However, the calling conventions of these functions have changed in the past. To avoid confusion and interoperability problems, we include local copies in this document, adapted from those in PKCS#1 v2.1 [RFC3447]. RFC 3447 should be treated as the ultimate authority on PKCS#1 for OpenPGP. Still, we think it's valuable to have a self-contained document that avoids problems later if the conventions need changing ๐Ÿ’–

13.1.1. EME-PKCS1-v1_5-ENCODE โœจ

Input:

k  = the length in octets of the key modulus

M  = message to be encoded, an octet string of length mLen, where
     mLen <= k - 11

Output:

EM = encoded message, an octet string of length k

Error:

"message too long"

Here's how the steps go ๐Ÿงš

  1. Length checking: If mLen > k - 11, output "message too long" and stop.
  2. Generate an octet string PS of length k - mLen - 3 consisting of pseudo-randomly generated nonzero octets. The length of PS will be at least eight octets.
  3. Concatenate PS, the message M, and other padding to form an encoded message EM of length k octets as:
EM = 0x00 || 0x02 || PS || 0x00 || M
  1. Output EM.

13.1.2. EME-PKCS1-v1_5-DECODE ๐ŸŒˆ

Input:

EM = encoded message, an octet string

Output:

M  = message, an octet string

Error:

"decryption error"

To decode an EME-PKCS1_v1_5 message, split the encoded message EM into an octet string PS (made of nonzero octets) and a message M, like this:

EM = 0x00 || 0x02 || PS || 0x00 || M

If the first octet of EM does not have hexadecimal value 0x00, if the second octet of EM does not have hexadecimal value 0x02, if there is no octet with hexadecimal value 0x00 to separate PS from M, or if the length of PS is less than 8 octets, output "decryption error" and stop. See also the security note in Section 14 about the differences in reporting between a decryption error and a padding error ๐Ÿ™ˆ

13.1.3. EMSA-PKCS1-v1_5 โœจ

okay so this one is the deterministic one ๐Ÿ’… It only has an encoding operation, no decoding, zero randomness, zero drama ๐ŸŒธ

Option:

  • Hash - a hash function in which hLen denotes the length in octets of the hash function output

Input:

  • M = message to be encoded
  • mL = intended length in octets of the encoded message, at least tLen + 11, where tLen is the octet length of the DER encoding T of a certain value computed during the encoding operation

Output:

  • EM = encoded message, an octet string of length emLen

Errors: "message too long"; "intended encoded message length too short" ๐Ÿ™ˆ

Steps:

  1. Apply the hash function to the message M to produce a hash value H: H = Hash(M). If the hash function outputs "message too long," output "message too long" and stop.
  2. Using the list in Section 5.2.2, produce an ASN.1 DER value for the hash function used. Let T be the full hash prefix from Section 5.2.2, and let tLen be the length in octets of T.
  3. If emLen < tLen + 11, output "intended encoded message length too short" and stop.
  4. Generate an octet string PS consisting of emLen - tLen - 3 octets with hexadecimal value 0xFF. The length of PS will be at least 8 octets. ๐Ÿซถ
  5. Concatenate PS, the hash prefix T, and other padding to form the encoded message EM as EM = 0x00 || 0x01 || PS || 0x00 || T.
  6. Output EM.

13.2. Symmetric Algorithm Preferences

the symmetric algorithm preference is an ordered list of algorithms that the keyholder accepts ๐Ÿ’– Since it lives on a self-signature, a keyholder can have multiple, different preferences. Like, Alice might have only TripleDES for alice@work.com but CAST5, Blowfish, and TripleDES for alice@home.org. Main character energy, she's different at work and at home ๐Ÿ’… Preferences can also show up in a subkey's binding signature, so keep that in mind too.

TripleDES is the MUST-implement algorithm, so if it isn't explicitly in the list, it's tacitly at the end. It's still good form to place it there explicitly ๐ŸŽ€ Also, if an implementation doesn't implement the preference at all, it's implicitly a TripleDES-only implementation. Iconic, honestly.

An implementation MUST NOT use a symmetric algorithm that is not in the recipient's preference list. When encrypting to more than one recipient, the implementation finds a suitable algorithm by taking the intersection of the preferences of the recipients. Note that the MUST-implement algorithm, TripleDES, ensures that the intersection is not null. The implementation may use any mechanism to pick an algorithm in the intersection โœจ

If an implementation can decrypt a message that a keyholder doesn't have in their preferences, the implementation SHOULD decrypt the message anyway, but MUST warn the keyholder that the protocol has been violated. So say Alice has software that implements every algorithm in this spec, but she still prefers subsets for work or home. If someone sends her an IDEA-encrypted message (not in her preferences), her software warns her that someone sent her an IDEA-encrypted message, but ideally it would still decrypt it. Warn her, but don't leave her hanging ๐Ÿฆ‹


13.3. Other Algorithm Preferences

other algorithm preferences work similarly to the symmetric algorithm preference, in that they specify which algorithms the keyholder accepts ๐ŸŒˆ There are two interesting cases I need to talk about though: the compression preferences and the hash preferences.

13.3.1. Compression Preferences

compression has been part of PGP since its very first days ๐Ÿงš OpenPGP and every previous version of PGP have offered it. In this spec, the default is for messages to be compressed, although an implementation is not required to do so. So the compression preference is a way for a keyholder to ask that messages not be compressed, probably because they're using a minimal implementation without compression. It also lets a keyholder say it can support alternate algorithms. Clever, right? ๐Ÿ‘‘

Like the algorithm preferences, an implementation MUST NOT use an algorithm that is not in the preference vector. If the preferences aren't present, they're assumed to be [ZIP(1), Uncompressed(0)] ๐Ÿ“

Additionally, an implementation MUST implement this preference to the degree of recognizing when to send an uncompressed message. A robust implementation would satisfy this by looking at the recipient's preference and acting accordingly. A minimal implementation can satisfy it by never generating a compressed message, since all implementations can handle messages that haven't been compressed ๐Ÿ’œ

13.3.2. Hash Algorithm Preferences

typically the signer picks the hash algorithm, not the verifier, because a signer rarely knows who's going to verify the signature ๐Ÿ™ˆ This preference though allows a protocol based on digital signatures to ease negotiation.

So if Alice is authenticating herself to Bob with a signature, it makes sense for her to use a hash algorithm that Bob's software uses. This preference lets Bob state in his key which algorithms Alice may use. Very cute, very efficient ๐ŸŒธ

Since SHA1 is the MUST-implement hash algorithm, if it isn't explicitly in the list, it's tacitly at the end. Again, good form to place it there explicitly ๐Ÿ’…


13.4. Plaintext

algorithm 0, "plaintext", may only be used to denote secret keys that are stored in the clear ๐Ÿ”’ Implementations MUST NOT use plaintext in Symmetrically Encrypted Data packets. Instead they must use Literal Data packets to encode unencrypted or literal data. No plaintext in the encrypted packets, bestie, that's a hard no ๐Ÿ’€


13.5. RSA

there are algorithm types for RSA Sign-Only and RSA Encrypt-Only keys. These types are deprecated ๐Ÿฅ€ The "key flags" subpacket in a signature is a much better way to express the same idea, and it generalizes to all algorithms. An implementation SHOULD NOT create such a key, but MAY interpret it.

An implementation SHOULD NOT implement RSA keys of size less than 1024 bits โœจ


13.6. DSA

an implementation SHOULD NOT implement DSA keys of size less than 1024 bits. It MUST NOT implement a DSA key with a q size of less than 160 bits. DSA keys MUST also be a multiple of 64 bits, and the q size MUST be a multiple of 8 bits. The Digital Signature Standard (DSS) [FIPS186] specifies that DSA be used in one of the following ways:

  • 1024-bit key, 160-bit q, SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 hash
  • 2048-bit key, 224-bit q, SHA-224, SHA-256, SHA-384, or SHA-512 hash
  • 2048-bit key, 256-bit q, SHA-256, SHA-384, or SHA-512 hash
  • 3072-bit key, 256-bit q, SHA-256, SHA-384, or SHA-512 hash

The above key and q size pairs were chosen to best balance the strength of the key with the strength of the hash ๐Ÿฆ‹ Implementations SHOULD use one of the above key and q size pairs when generating DSA keys. If DSS compliance is desired, one of the specified SHA hashes must be used as well. [FIPS186] is the ultimate authority on DSS, so consult it for all questions of DSS compliance ๐Ÿ“š

Note that earlier versions of this standard only allowed a 160-bit q with no truncation allowed, so earlier implementations may not be able to handle signatures with a different q size or a truncated hash. Old versions be like "not meโ€ฆ" ๐Ÿ™ˆ


13.7. Elgamal

an implementation SHOULD NOT implement Elgamal keys of size less than 1024 bits ๐ŸŒธ


13.8. Reserved Algorithm Numbers

a bunch of algorithm IDs have been reserved for algorithms that would be useful in an OpenPGP implementation, but there are issues that stop an implementer from actually implementing them ๐Ÿฅบ These are marked in Section 9.1, "Public-Key Algorithms", as "reserved for".

The reserved public-key algorithms, Elliptic Curve (18), ECDSA (19), and X9.42 (21), don't have the necessary parameters, parameter order, or semantics defined. Not yet, babe ๐Ÿ’…

Previous versions of OpenPGP permitted Elgamal [ELGAMAL] signatures with a public-key identifier of 20. These are no longer permitted. An implementation MUST NOT generate such keys. An implementation MUST NOT generate Elgamal signatures. See [BLEICHENBACHER] ๐Ÿšซ


13.9. OpenPGP CFB Mode

OpenPGP does symmetric encryption using a variant of Cipher Feedback mode (CFB mode) ๐Ÿง This section describes the procedure it uses in detail. This mode is what's used for Symmetrically Encrypted Data Packets. The mechanism for encrypting secret-key material is similar, and it's described in the sections above.

Quick note on the math: the value BS is the block size in octets of the cipher. Most ciphers have a block size of 8 octets. AES and Twofish have a block size of 16 octets. Also, this description assumes the IV and CFB arrays start with an index of 1 (unlike C, which starts arrays at zero) ๐Ÿค“

OpenPGP CFB mode uses an initialization vector (IV) of all zeros, and prefixes the plaintext with BS+2 octets of random data, such that octets BS+1 and BS+2 match octets BS-1 and BS. It does a CFB resynchronization after encrypting those BS+2 octets.

So for an algorithm with a block size of 8 octets (64 bits), the IV is 10 octets long, and octets 7 and 8 of the IV are the same as octets 9 and 10. For an algorithm with a block size of 16 octets (128 bits), the IV is 18 octets long, and octets 17 and 18 replicate octets 15 and 16. Those extra two octets are an easy check for a correct key โœจ Iconic little safety net, honestly.

Step by step, here's the procedure ๐Ÿ’–

  1. The feedback register (FR) is set to the IV, which is all zeros.
  2. FR is encrypted to produce FRE (FR Encrypted). This is the encryption of an all-zero value.
  3. FRE is xored with the first BS octets of random data prefixed to the plaintext to produce C[1] through C[BS], the first BS octets of ciphertext.
  4. FR is loaded with C[1] through C[BS].
  5. FR is encrypted to produce FRE, the encryption of the first BS octets of ciphertext.
  6. The left two octets of FRE get xored with the next two octets of data that were prefixed to the plaintext. This produces C[BS+1] and C[BS+2], the next two octets of ciphertext.
  7. (The resynchronization step) FR is loaded with C[3] through C[BS+2].
  8. FR is encrypted to produce FRE.
  9. FRE is xored with the first BS octets of the given plaintext, now that we've finished encrypting the BS+2 octets of prefixed data. This produces C[BS+3] through C[BS+(BS+2)], the next BS octets of ciphertext.
  10. FR is loaded with C[BS+3] to C[BS + (BS+2)] (which is C11-C18 for an 8-octet block).
  11. FR is encrypted to produce FRE.
  12. FRE is xored with the next BS octets of plaintext, to produce the next BS octets of ciphertext. These are loaded into FR, and the process is repeated until the plaintext is used up. ๐ŸŽ€

13.10. Private or Experimental Parameters

S2K specifiers, Signature subpacket types, user attribute types, image format types, and algorithms described in Section 9 all reserve the range 100 to 110 for private and experimental use. Packet types reserve the range 60 to 63 for private and experimental use. These are intentionally managed with the PRIVATE USE method, as described in [RFC2434] ๐Ÿงš

However, implementations need to be careful with these and promote them to full IANA-managed parameters when they grow beyond the original, limited system. Growing up is hard, but it's necessary ๐ŸŒˆ

13.11. Extension of the MDC System ๐Ÿฆ‹

okay so, the non-normative bit in Section 5.13 explains that the MDC system is uniquely unparameterized in OpenPGP ๐Ÿงš. That was a deliberate choice to dodge cross-grade attacks ๐Ÿ™ˆ. If the MDC system ever gets extended to a stronger hash function, you have to be super careful to avoid downgrade and cross-grade attacks ๐Ÿ’€.

One simple way is to make brand-new packets for the new MDC. Like, instead of the MDC system using packets 18 and 19, a new MDC could use 20 and 21. The obvious drawback is that this burns two packet numbers for every new hash function, and the whole space is capped at a maximum of 60 ๐Ÿฅฒ.

Another simple way is to make new versions of packet 18 and reflect that in packet 19 โœจ. For example, say V2 of packet 18 implicitly used SHA-256. Then packet 19 would have to be 32 octets long. The version change in packet 18 plus the size of packet 19 are what prevent a downgrade attack ๐Ÿ›ก๏ธ.

There are two drawbacks to this approach. First, using a packet's version number to carry algorithm info is not tidy from a protocol-design standpoint ๐Ÿ˜ค. Several versions of the MDC system might end up in common use, but that messiness would just mirror the messiness in cryptographic consensus about hash function security ๐Ÿซ . Second, different versions of packet 19 would need unique sizes. If two versions each had 256-bit hashes, they couldn't both use 32-octet packet 19s without admitting the chance of a cross-grade attack ๐Ÿ˜ฑ.

Yet another approach, a complex one, would be a hybrid of the two above: create a new pair of MDC packets that are fully parameterized, and yet still protected from downgrade and cross-grade attacks ๐ŸŒˆ.

Real talk, any change to the MDC system MUST be done through the IETF CONSENSUS method, as described in [RFC2434] ๐Ÿ“œ.

13.12. Meta-Considerations for Expansion ๐Ÿงฉ

If OpenPGP gets extended in a way that isn't backwards-compatible, meaning old implementations won't gracefully handle the absence of a new feature, the extension proposal can be declared in the key holder's self-signature as part of the Features signature subpacket ๐Ÿ”‘.

We cannot state definitively what extensions will not be upwards-compatible. Typically, though, new algorithms are upwards-compatible, whereas new packets are not ๐ŸŽ€.

If an extension proposal does not update the Features system, it SHOULD include an explanation of why this is unnecessary. If the proposal contains neither an extension to the Features system nor an explanation of why such an extension is unnecessary, the proposal SHOULD be rejected ๐Ÿ™…โ€โ™€๏ธ.

14. Security Considerations โœจ

Okay bestie, this part is serious business. Read it all, no skipping ๐Ÿ˜ญ๐Ÿ”ฅ
  • As with any technology involving cryptography, you should check the current literature to determine if any algorithms used here have been found to be vulnerable to attack ๐Ÿ”.
  • This specification uses Public-Key Cryptography technologies. It is assumed that the private key portion of a public-private key pair is controlled and secured by the proper party or parties ๐Ÿ”.
  • Certain operations in this specification involve the use of random numbers. An appropriate entropy source should be used to generate these numbers (see [RFC4086]) ๐ŸŽฒ.
  • The MD5 hash algorithm has been found to have weaknesses, with collisions found in a number of cases. MD5 is deprecated for use in OpenPGP. Implementations MUST NOT generate new signatures using MD5 as a hash function. They MAY continue to consider old signatures that used MD5 as valid ๐Ÿ’€.
  • SHA-224 and SHA-384 require the same work as SHA-256 and SHA-512, respectively. In general, there are few reasons to use them outside of DSS compatibility. You need a situation where one needs more security than smaller hashes, but does not want to have the full 256-bit or 512-bit data length ๐Ÿค”.
  • Many security protocol designers think it is a bad idea to use a single key for both privacy (encryption) and integrity (signatures). In fact, this was one of the motivating forces behind the V4 key format with separate signature and encryption keys. If you as an implementer promote dual-use keys, you should at least be aware of this controversy ๐Ÿ‘€.
  • The DSA algorithm will work with any hash, but is sensitive to the quality of the hash algorithm. Verifiers should be aware that even if the signer used a strong hash, an attacker could have modified the signature to use a weak one. Only signatures using acceptably strong hash algorithms should be accepted as valid ๐Ÿง.
  • As OpenPGP combines many different asymmetric, symmetric, and hash algorithms, each with different measures of strength, care should be taken that the weakest element of an OpenPGP message is still sufficiently strong for the purpose at hand. While consensus about the strength of a given algorithm may evolve, NIST Special Publication 800-57 [SP800-57] recommends the following list of equivalent strengths ๐Ÿ’ช:
     Asymmetric  |  Hash  |  Symmetric
      key size   |  size  |   key size
     ------------+--------+-----------
        1024        160         80
        2048        224        112
        3072        256        128
        7680        384        192
       15360        512        256
  • There is a somewhat-related potential security problem in signatures. If an attacker can find a message that hashes to the same hash with a different algorithm, a bogus signature structure can be constructed that evaluates correctly ๐Ÿ˜ฑ.

For example, suppose Alice DSA signs message M using hash algorithm H. Suppose that Mallet finds a message M' that has the same hash value as M with H'. Mallet can then construct a signature block that verifies as Alice's signature of M' with H'. However, this would also constitute a weakness in either H or H' or both. Should this ever occur, a revision will have to be made to this document to revise the allowed hash algorithms ๐Ÿšจ.

  • If you are building an authentication system, the recipient may specify a preferred signing algorithm. However, the signer would be foolish to use a weak algorithm simply because the recipient requests it ๐Ÿ™„.
  • Some of the encryption algorithms mentioned in this document have been analyzed less than others. For example, although CAST5 is presently considered strong, it has been analyzed less than TripleDES. Other algorithms may have other controversies surrounding them ๐Ÿ•ต๏ธโ€โ™€๏ธ.
  • In late summer 2002, Jallad, Katz, and Schneier published an interesting attack on the OpenPGP protocol and some of its implementations [JKS02]. In this attack, the attacker modifies a message and sends it to a user who then returns the erroneously decrypted message to the attacker. The attacker is thus using the user as a random oracle, and can often decrypt the message ๐Ÿ˜ญ.

Compressing data can ameliorate this attack. The incorrectly decrypted data nearly always decompresses in ways that defeat the attack. However, this is not a rigorous fix, and leaves open some small vulnerabilities. For example, if an implementation does not compress a message before encryption (perhaps because it knows it was already compressed), then that message is vulnerable. Because of this happenstance, that modification attacks can be thwarted by decompression errors, an implementation SHOULD treat a decompression error as a security problem, not merely a data problem ๐Ÿง .

This attack can be defeated by the use of Modification Detection, provided that the implementation does not let the user naively return the data to the attacker. An implementation MUST treat an MDC failure as a security problem, not merely a data problem ๐Ÿ›ก๏ธ.

In either case, the implementation MAY allow the user access to the erroneous data, but MUST warn the user as to potential security problems should that data be returned to the sender โš ๏ธ.

While this attack is somewhat obscure, requiring a special set of circumstances to create it, it is nonetheless quite serious, as it permits someone to trick a user into decrypting a message. Consequently, it is important that:

  1. Implementers treat MDC errors and decompression failures as security problems.
  2. Implementers implement Modification Detection with all due speed and encourage its spread.
  3. Users migrate to implementations that support Modification Detection with all due speed.
  • PKCS#1 has been found to be vulnerable to attacks in which a system that reports errors in padding differently from errors in decryption becomes a random oracle that can leak the private key in mere millions of queries. Implementations must be aware of this attack and prevent it from happening. The simplest solution is to report a single error code for all variants of decryption errors so as not to leak information to an attacker ๐Ÿ™€.
  • Some technologies mentioned here may be subject to government control in some countries ๐ŸŒ.
  • In winter 2005, Serge Mister and Robert Zuccherato from Entrust released a paper describing a way that the "quick check" in OpenPGP CFB mode can be used with a random oracle to decrypt two octets of every cipher block [MZ05]. They recommend as prevention not using the quick check at all ๐Ÿซฃ.

Many implementers have taken this advice to heart for any data that is symmetrically encrypted and for which the session key is public-key encrypted. In this case, the quick check is not needed, as the public-key encryption of the session key should guarantee that it is the right session key. In other cases, the implementation should use the quick check with care ๐Ÿ’….

On the one hand, there is a danger to using it if there is a random oracle that can leak information to an attacker. In plainer language, there is a danger to using the quick check if timing information about the check can be exposed to an attacker, particularly via an automated service that allows rapidly repeated queries ๐Ÿšซ.

On the other hand, it is inconvenient to the user to be informed that they typed in the wrong passphrase only after a petabyte of data is decrypted ๐Ÿ˜ฉ. There are many cases in cryptographic engineering where the implementer must use care and wisdom, and this is one ๐ŸŒˆโœจ.

15. Implementation Nits โœจ

Okay bestie, this section is a grab bag of tips for implementers who want to stay backward-compatible ๐Ÿ’… Old PGP versions aren't OpenPGP compliant, and the tiny differences are honestly the most annoying ones ๐Ÿ˜ญ So this is a non-comprehensive list of gotchas, the ones that'll trip you up if you're trying to play nice with older stuff ๐Ÿ™ˆ

  • The IDEA algorithm is patented, and yet it's still required for PGP 2.x interoperability ๐Ÿ’€ It's also the de-facto preferred algorithm for a V3 key with a V3 self-signature (or no self-signature). ๐Ÿ”ฅ
  • When exporting a private key, PGP 2.x generates the header BEGIN PGP SECRET KEY BLOCK instead of BEGIN PGP PRIVATE KEY BLOCK. All previous versions ignore the implied data type and look directly at the packet data type ๐Ÿงš
  • PGP 2.0 through 2.5 generated V2 Public-Key packets. These are identical to the deprecated V3 keys except for the version number. An implementation MUST NOT generate them and may accept or reject them as it sees fit ๐Ÿ™…โ€โ™€๏ธ Some older PGP versions generated V2 PKESK packets (Tag 1) as well. An implementation may accept or reject V2 PKESK packets as it sees fit, and MUST NOT generate them ๐Ÿ’–
  • PGP 2.6.x will not accept key-material packets with versions greater than 3 ๐Ÿ˜ค
  • There are many ways two keys can have the same key material but different fingerprints (and thus Key IDs) ๐Ÿฆ‹ Perhaps the most interesting one is an RSA key that has been "upgraded" to V4 format. A V4 fingerprint is constructed by hashing the key creation time along with other things, so two V4 keys created at different times, yet with the same key material, will have different fingerprints. It's giving plot twist ๐ŸŒ€
  • If an implementation is using zlib to interoperate with PGP 2.x, then the "windowBits" parameter should be set to -13 โญ
  • The 0x19 back signatures weren't required for signing subkeys until relatively recently. Consequently, there may be keys out in the wild that don't have these back signatures. Implementing software may handle these keys as it sees fit ๐ŸŒธ
  • OpenPGP doesn't put limits on the size of public keys. However, bigger keys aren't necessarily better keys! Larger keys take more computation time to use, and this can quickly become impractical ๐Ÿ˜ฉ Different OpenPGP implementations may also use different upper bounds for public key sizes, so care should be taken when choosing sizes to maintain interoperability. As of 2007, most implementations have an upper bound of 4096 bits ๐Ÿ“
  • ASCII armor is an optional feature of OpenPGP. The OpenPGP working group strives for a minimal set of mandatory-to-implement features, and since there could be useful implementations that only use binary object formats, this is not a "MUST" feature for an implementation ๐Ÿซถ For example, an implementation that's using OpenPGP as a mechanism for file signatures may find ASCII armor unnecessary. OpenPGP lets an implementation declare what features it does and doesn't support, but ASCII armor is not one of those features. Since most implementations allow binary and armored objects to be used indiscriminately, an implementation that doesn't implement ASCII armor may find itself with compatibility issues with general-purpose implementations ๐Ÿ˜ฌ Moreover, implementations of OpenPGP-MIME [RFC3156] already have a requirement for ASCII armor, so those implementations will necessarily have support ๐Ÿ’œ

16. References ๐Ÿ“š

16.1. Normative References

[AES]            NIST, FIPS PUB 197, "Advanced Encryption Standard
                 (AES)," November 2001.
                 http://csrc.nist.gov/publications/fips/fips197/fips-
                 197.{ps,pdf}

[BLOWFISH]       Schneier, B. "Description of a New Variable-Length
                 Key, 64-Bit Block Cipher (Blowfish)" Fast Software
                 Encryption, Cambridge Security Workshop Proceedings
                 (December 1993), Springer-Verlag, 1994, pp191-204
                 <http://www.counterpane.com/bfsverlag.html>

[BZ2]            J. Seward, jseward@acm.org, "The Bzip2 and libbzip2
                 home page" <http://www.bzip.org/>

[ELGAMAL]        T. Elgamal, "A Public-Key Cryptosystem and a
                 Signature Scheme Based on Discrete Logarithms," IEEE
                 Transactions on Information Theory, v. IT-31, n. 4,
                 1985, pp. 469-472.

[FIPS180]        Secure Hash Signature Standard (SHS) (FIPS PUB 180-
                 2).
                 <http://csrc.nist.gov/publications/fips/fips180-
                 2/fips180-2withchangenotice.pdf>

[FIPS186]        Digital Signature Standard (DSS) (FIPS PUB 186-2).
                 <http://csrc.nist.gov/publications/fips/fips186-2/
                  fips186-2-change1.pdf> FIPS 186-3 describes keys
                 greater than 1024 bits.  The latest draft is at:
                 <http://csrc.nist.gov/publications/drafts/
                 fips_186-3/Draft-FIPS-186-3%20_March2006.pdf>

[HAC]            Alfred Menezes, Paul van Oorschot, and Scott
                 Vanstone, "Handbook of Applied Cryptography," CRC
                 Press, 1996.
                 <http://www.cacr.math.uwaterloo.ca/hac/>

[IDEA]           Lai, X, "On the design and security of block
                 ciphers", ETH Series in Information Processing, J.L.
                 Massey (editor), Vol. 1, Hartung-Gorre Verlag
                 Knostanz, Technische Hochschule (Zurich), 1992

[ISO10646]       ISO/IEC 10646-1:1993. International Standard --
                 Information technology -- Universal Multiple-Octet
                 Coded Character Set (UCS) -- Part 1: Architecture
                 and Basic Multilingual Plane.

[JFIF]           JPEG File Interchange Format (Version 1.02).  Eric
                 Hamilton, C-Cube Microsystems, Milpitas, CA,
                 September 1, 1992.

[RFC1950]        Deutsch, P. and J-L. Gailly, "ZLIB Compressed Data
                 Format Specification version 3.3", RFC 1950, May
                 1996.

[RFC1951]        Deutsch, P., "DEFLATE Compressed Data Format
                 Specification version 1.3", RFC 1951, May 1996.

[RFC2045]        Freed, N. and N. Borenstein, "Multipurpose Internet
                 Mail Extensions (MIME) Part One: Format of Internet
                 Message Bodies", RFC 2045, November 1996

[RFC2119]        Bradner, S., "Key words for use in RFCs to Indicate
                 Requirement Levels", BCP 14, RFC 2119, March 1997.

[RFC2144]        Adams, C., "The CAST-128 Encryption Algorithm", RFC
                 2144, May 1997.

[RFC2434]        Narten, T. and H. Alvestrand, "Guidelines for
                 Writing an IANA Considerations Section in RFCs", BCP
                 26, RFC 2434, October 1998.

[RFC2822]        Resnick, P., "Internet Message Format", RFC 2822,
                 April 2001.

[RFC3156]        Elkins, M., Del Torto, D., Levien, R., and T.
                 Roessler, "MIME Security with OpenPGP", RFC 3156,
                 August 2001.

[RFC3447]        Jonsson, J. and B. Kaliski, "Public-Key Cryptography
                 Standards (PKCS) #1: RSA Cryptography Specifications
                 Version 2.1", RFC 3447, February 2003.

[RFC3629]        Yergeau, F., "UTF-8, a transformation format of ISO
                 10646", STD 63, RFC 3629, November 2003.

[RFC4086]        Eastlake, D., 3rd, Schiller, J., and S. Crocker,
                 "Randomness Requirements for Security", BCP 106, RFC
                 4086, June 2005.

[SCHNEIER]       Schneier, B., "Applied Cryptography Second Edition:
                 protocols, algorithms, and source code in C", 1996.

[TWOFISH]        B. Schneier, J. Kelsey, D. Whiting, D. Wagner, C.
                 Hall, and N. Ferguson, "The Twofish Encryption
                 Algorithm", John Wiley & Sons, 1999.

16.2. Informative References

[BLEICHENBACHER] Bleichenbacher, Daniel, "Generating Elgamal
                 signatures without knowing the secret key,"
                 Eurocrypt 96. Note that the version in the
                 proceedings has an error. A revised version is
                 available at the time of writing from
                 <ftp://ftp.inf.ethz.ch/pub/publications/papers/ti
                 /isc/ElGamal.ps>

[JKS02]          Kahil Jallad, Jonathan Katz, Bruce Schneier
                 "Implementation of Chosen-Ciphertext Attacks against
                 PGP and GnuPG" http://www.counterpane.com/pgp-
                 attack.html

[MAURER]         Ueli Maurer, "Modelling a Public-Key
                 Infrastructure", Proc. 1996 European Symposium on
                 Research in Computer Security (ESORICS' 96), Lecture
                 Notes in Computer Science, Springer-Verlag, vol.
                 1146, pp. 325-350, Sep 1996.

[MZ05]           Serge Mister, Robert Zuccherato, "An Attack on CFB
                 Mode Encryption As Used By OpenPGP," IACR ePrint
                 Archive: Report 2005/033, 8 Feb 2005
                 http://eprint.iacr.org/2005/033

[REGEX]          Jeffrey Friedl, "Mastering Regular Expressions,"
                 O'Reilly, ISBN 0-596-00289-0.

[RFC1423]        Balenson, D., "Privacy Enhancement for Internet
                 Electronic Mail: Part III: Algorithms, Modes, and
                 Identifiers", RFC 1423, February 1993.

[RFC1991]        Atkins, D., Stallings, W., and P. Zimmermann, "PGP
                 Message Exchange Formats", RFC 1991, August 1996.

[RFC2440]        Callas, J., Donnerhacke, L., Finney, H., and R.
                 Thayer, "OpenPGP Message Format", RFC 2440, November
                 1998.

[SP800-57]       NIST Special Publication 800-57, Recommendation on
                 Key Management
                 <http://csrc.nist.gov/publications/nistpubs/ 800-
                 57/SP800-57-Part1.pdf>
                 <http://csrc.nist.gov/publications/nistpubs/ 800-
                 57/SP800-57-Part2.pdf>

Acknowledgements ๐Ÿซถ

This memo also draws on loads of previous work from a bunch of other authors, including: Derek Atkins, Charles Breed, Dave Del Torto, Marc Dyksterhouse, Gail Haspert, Gene Hoffman, Paul Hoffman, Ben Laurie, Raph Levien, Colin Plumb, Will Price, David Shaw, William Stallings, Mark Weaver, and Philip R. Zimmermann ๐ŸŒˆ The people who came before really laid the groundwork, no cap ๐Ÿ‘‘

Authors' Addresses ๐ŸŽ€

The working group can be contacted via the current chair:

   Derek Atkins
   IHTFP Consulting, Inc.
   4 Farragut Ave
   Somerville, MA  02144  USA

   EMail: derek@ihtfp.com
   Tel: +1 617 623 3745

The principal authors of this document are as follows:

   Jon Callas
   EMail: jon@callas.org

   Lutz Donnerhacke
   IKS GmbH
   Wildenbruchstr. 15
   07745 Jena, Germany
   EMail: lutz@iks-jena.de

   Hal Finney
   EMail: hal@finney.org

   David Shaw
   EMail: dshaw@jabberwocky.com

   Rodney Thayer
   EMail: rodney@canola-jones.com

okay so the copyright bit is up first ๐Ÿ“œ The IETF Trust owns the copyright for 2007, and this document is covered by the rights, licenses, and restrictions in BCP 78. Except for what BCP 78 says, the authors keep all their own rights ๐Ÿฆ‹

The document and everything in it come "AS IS," no guarantees attached ๐Ÿ’€ The contributor, the organization they represent or are sponsored by (if there is one), the Internet Society, the IETF Trust, and the Internet Engineering Task Force all say no to every warranty, stated or implied. That includes no promise that using the info won't infringe anyone's rights, and no promise it's merchantable or fit for a particular purpose ๐Ÿ™ˆ

Intellectual Property ๐Ÿ’–

the IETF is not picking a side on whether any IPR or other rights claimed over the tech in this document are valid, how far they reach, or whether anyone can get a license for them. It also hasn't done any independent digging to find such rights ๐Ÿ” If you want the details on how rights work for RFC documents, look in BCP 78 and BCP 79 ๐Ÿ“š

Copies of IPR disclosures sent to the IETF Secretariat, plus any promised licenses, and the outcome of any attempt to get a general license or permission for implementers or users to use those proprietary rights, can be found in the IETF online IPR repository at http://www.ietf.org/ipr ๐ŸŒธ

The IETF invites anyone who knows about copyrights, patents, patent applications, or other proprietary rights that might cover technology needed to implement this standard to tell them about it. Send that info to the IETF at ietf-ipr@ietf.org ๐Ÿซถ

๐Ÿ“ฆ where this page comes from