postgresql_16:9.6.28.3 PGP Encryption Functions
PostgreSQL 16 > Appendixes > Additional Supplied Modules and Extensions > pgcrypto — cryptographic functions > PGP Encryption Functions
The functions here implement the encryption part of the OpenPGP (RFC 4880) standard. Supported are both symmetric-key and public-key encryption.
An encrypted PGP message consists of 2 parts, or *packets*:
- Packet containing a session key — either symmetric-key or public-key encrypted.
- Packet containing data encrypted with the session key.
When encrypting with a symmetric key (i.e., a password):
1. The given password is hashed using a String2Key (S2K) algorithm. This is rather similar to crypt() algorithms — purposefully slow and with random salt — but it produces a full-length binary key. 2. If a separate session key is requested, a new random key will be generated. Otherwise the S2K key will be used directly as the session key. 3. If the S2K key is to be used directly, then only S2K settings will be put into the session key packet. Otherwise the session key will be encrypted with the S2K key and put into the session key packet.
When encrypting with a public key:
1. A new random session key is generated. 2. It is encrypted using the public key and put into the session key packet.
In either case the data to be encrypted is processed as follows:
1. Optional data-manipulation: compression, conversion to UTF-8, and/or conversion of line-endings. 2. The data is prefixed with a block of random bytes. This is equivalent to using a random IV. 3. A SHA1 hash of the random prefix and data is appended. 4. All this is encrypted with the session key and placed in the data packet.
pgp_sym_encrypt()
pgp_sym_encrypt(data text, psw text [, options text ]) returns bytea pgp_sym_encrypt_bytea(data bytea, psw text [, options text ]) returns bytea
Encrypt data with a symmetric PGP key psw. The options parameter can contain option settings, as described below.
pgp_sym_decrypt()
pgp_sym_decrypt(msg bytea, psw text [, options text ]) returns text pgp_sym_decrypt_bytea(msg bytea, psw text [, options text ]) returns bytea
Decrypt a symmetric-key-encrypted PGP message.
Decrypting bytea data with pgp_sym_decrypt is disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data with pgp_sym_decrypt_bytea is fine.
The options parameter can contain option settings, as described below.
pgp_pub_encrypt()
pgp_pub_encrypt(data text, key bytea [, options text ]) returns bytea pgp_pub_encrypt_bytea(data bytea, key bytea [, options text ]) returns bytea
Encrypt data with a public PGP key key. Giving this function a secret key will produce an error.
The options parameter can contain option settings, as described below.
pgp_pub_decrypt()
pgp_pub_decrypt(msg bytea, key bytea [, psw text [, options text ]]) returns text pgp_pub_decrypt_bytea(msg bytea, key bytea [, psw text [, options text ]]) returns bytea
Decrypt a public-key-encrypted message. key must be the secret key corresponding to the public key that was used to encrypt. If the secret key is password-protected, you must give the password in psw. If there is no password, but you want to specify options, you need to give an empty password.
Decrypting bytea data with pgp_pub_decrypt is disallowed. This is to avoid outputting invalid character data. Decrypting originally textual data with pgp_pub_decrypt_bytea is fine.
The options parameter can contain option settings, as described below.
pgp_key_id()
pgp_key_id(bytea) returns text
pgp_key_id extracts the key ID of a PGP public or secret key. Or it gives the key ID that was used for encrypting the data, if given an encrypted message.
It can return 2 special key IDs:
SYMKEY
The message is encrypted with a symmetric key.
ANYKEY
The message is public-key encrypted, but the key ID has been removed. That means you will need to try all your secret keys on it to see which one decrypts it. pgcrypto itself does not produce such messages.
Note that different keys may have the same ID. This is rare but a normal event. The client application should then try to decrypt with each one, to see which fits — like handling ANYKEY.
armor(), dearmor()
armor(data bytea [ , keys text[], values text[] ]) returns text dearmor(data text) returns bytea
These functions wrap/unwrap binary data into PGP ASCII-armor format, which is basically Base64 with CRC and additional formatting.
If the keys and values arrays are specified, an *armor header* is added to the armored format for each key/value pair. Both arrays must be single-dimensional, and they must be of the same length. The keys and values cannot contain any non-ASCII characters.
pgp_armor_headers
pgp_armor_headers(data text, key out text, value out text) returns setof record
pgp_armor_headers() extracts the armor headers from data. The return value is a set of rows with two columns, key and value. If the keys or values contain any non-ASCII characters, they are treated as UTF-8.
Options for PGP Functions
Options are named to be similar to GnuPG. An option's value should be given after an equal sign; separate options from each other with commas. For example: pgp_sym_encrypt(data, psw, 'compress-algo=1, cipher-algo=aes256')
All of the options except convert-crlf apply only to encrypt functions. Decrypt functions get the parameters from the PGP data.
The most interesting options are probably compress-algo and unicode-mode. The rest should have reasonable defaults.
cipher-algo
Which cipher algorithm to use.
Values: bf, aes128, aes192, aes256, 3des, cast5 Default: aes128 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
compress-algo
Which compression algorithm to use. Only available if PostgreSQL was built with zlib.
Values: 0 - no compression 1 - ZIP compression 2 - ZLIB compression (= ZIP plus meta-data and block CRCs) Default: 0 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
compress-level
How much to compress. Higher levels compress smaller but are slower. 0 disables compression.
Values: 0, 1-9 Default: 6 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
convert-crlf
Whether to convert \n into \r\n when encrypting and \r\n to \n when decrypting. RFC 4880 specifies that text data should be stored using \r\n line-feeds. Use this to get fully RFC-compliant behavior.
Values: 0, 1 Default: 0 Applies to: pgp_sym_encrypt, pgp_pub_encrypt, pgp_sym_decrypt, pgp_pub_decrypt
disable-mdc
Do not protect data with SHA-1. The only good reason to use this option is to achieve compatibility with ancient PGP products, predating the addition of SHA-1 protected packets to RFC 4880. Recent gnupg.org and pgp.com software supports it fine.
Values: 0, 1 Default: 0 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
sess-key
Use separate session key. Public-key encryption always uses a separate session key; this option is for symmetric-key encryption, which by default uses the S2K key directly.
Values: 0, 1 Default: 0 Applies to: pgp_sym_encrypt
s2k-mode
Which S2K algorithm to use.
Values: 0 - Without salt. Dangerous! 1 - With salt but with fixed iteration count. 3 - Variable iteration count. Default: 3 Applies to: pgp_sym_encrypt
s2k-count
The number of iterations of the S2K algorithm to use. It must be a value between 1024 and 65011712, inclusive.
Default: A random value between 65536 and 253952 Applies to: pgp_sym_encrypt, only with s2k-mode=3
s2k-digest-algo
Which digest algorithm to use in S2K calculation.
Values: md5, sha1 Default: sha1 Applies to: pgp_sym_encrypt
s2k-cipher-algo
Which cipher to use for encrypting separate session key.
Values: bf, aes, aes128, aes192, aes256 Default: use cipher-algo Applies to: pgp_sym_encrypt
unicode-mode
Whether to convert textual data from database internal encoding to UTF-8 and back. If your database already is UTF-8, no conversion will be done, but the message will be tagged as UTF-8. Without this option it will not be.
Values: 0, 1 Default: 0 Applies to: pgp_sym_encrypt, pgp_pub_encrypt
ignore-cipher-failure
Dangerous! Instructs pgcrypto to use an incorrect decryption algorithm matching the historical behavior prior to the fix for CVE-2026-14663, by completely ignoring failures from the OpenSSL cipher in use. This is intended only for users who need to recover incorrectly-encrypted messages created when the cipher-algo was unavailable under the OpenSSL configuration in use. Such faulty messages do not require the correct decryption key when ignore-cipher-failure is enabled, so there is no guarantee that the decrypted plaintext actually originated from a holder of the key.
Contrast the case of a message which was correctly encrypted, but the cipher that produced it is unavailable under the current OpenSSL configuration. Recovering such plaintext via pgcrypto requires making the actual cipher available to OpenSSL by, for example, enabling the appropriate provider. ignore-cipher-failure is not necessary or helpful for that scenario. If “decryption” of a correctly encrypted message with this option happens to pass PGP integrity checks, that result is coincidental and does not make the recovered plaintext trustworthy.
Values: 0, 1 Default: 0 Applies to: pgp_sym_decrypt, pgp_pub_decrypt
Generating PGP Keys with GnuPG
To generate a new key: gpg --gen-key
The preferred key type is “DSA and Elgamal”.
For RSA encryption you must create either DSA or RSA sign-only key as master and then add an RSA encryption subkey with gpg --edit-key.
To list keys: gpg --list-secret-keys
To export a public key in ASCII-armor format: gpg -a --export KEYID > public.key
To export a secret key in ASCII-armor format: gpg -a --export-secret-keys KEYID > secret.key
You need to use dearmor() on these keys before giving them to the PGP functions. Or if you can handle binary data, you can drop -a from the command.
For more details see man gpg, The GNU Privacy Handbook and other documentation on https://www.gnupg.org/.
Limitations of PGP Code
- No support for signing. That also means that it is not checked whether the encryption subkey belongs to the master key.
- No support for encryption key as master key. As such practice is generally discouraged, this should not be a problem.
- No support for several subkeys. This may seem like a problem, as this is common practice. On the other hand, you should not use your regular GPG/PGP keys with
pgcrypto, but create new ones, as the usage scenario is rather different.