Transparent Data Encryption
This page describes the transparent data encryption feature proposed in pgsql-hackers.
- 1 Overview
- 2 When to encrypt/decrypt
- 3 How to encrypt
- 4 TODO for Cluster File Encryption
- 5 List of the contains of user data for PostgreSQL files
- 6 TDE in other systems
- 7 Links
There has been continual discussion about whether and how to implement Transparent Data Encryption (TDE) in Postgres. Many other relational databases support TDE, and some security standards require it. However, it is also debatable how much security value TDE provides.
Fundamentally, TDE must meet three criteria — it must be secure, obviously, but it also must be done in a way that has minimal impact on the rest of the Postgres code. This has value for two reasons — first, only a small number of users will use TDE, so the less code that is added, the less testing is required. Second, the less code that is added, the less likely TDE will break because of future Postgres changes. Finally, TDE should meet regulatory requirements.
The first patch was proposed in 2016  and implemented cluster-wide encryption with a single key. In 2018 table-level transparent data encryption was proposed , together with a method to integrate with key management systems; that first patch was submitted in 2019 . The patch implemented both tablespace-level encryption using a 2-tier key architecture and generic key management API to communicate with external key management systems.
Scope of TDE
The scope of TDE is:
- Internal key management system (KMS), storing keys in the database
- Cluster-wide encryption
- encrypt everything that is persistent
- not encrypting shared buffers or data in memory
The benefit of cluster wide encryption is:
- Simple architecture
- Suitable for the requirement of encrypting all data
Cluster-wide encryption meets the compliance requirements and checks the box as far as TDE is concerned. It also meets the criteria of encrypting the data at rest i.e., persistent data.
When to encrypt/decrypt
It encrypts buffer data during disk I/O
- Processes encrypt data when writing it to disk
- Decrypt when reading from disk
- Data in the shared buffer is not encrypted
In cluster encryption
- Processes insert WAL data to WAL buffers in non-encrypted state
- WAL buffers are encrypted when writing to the file system
- WAL would use a dedicated encryption key
Temporary files would use a temporary key that is randomly generated at postmaster start and lives only for the postmaster lifetime. For parallel queries, especially parallel hash joins, since it's possible that multiple parallel workers use the same temporary files, the the temporary key should be shared with parallel workers.
In cluster-wide encryption, there will be an option in pg_basebackup to change the heap/index key for key rotation purposes. After failover to a standby, the WAL key can be changed too.
How to encrypt
We will use Advanced Encryption Standard (AES) . We will use AES-CTR  for heap/index and WAL encryption. We will offer three key length options (128, 192, and 256-bits) selected at initdb time with
IV for WAL encryption
We will use a different IV(nonce) 16MB WAL file, so we will be OK there too. What about timelines?
IV for temporary files
It is unclear how to set the nonce for temporary files. We will probably use a data encryption key generated at postmaster start, and mix that with the time of day, process id, and maybe file path.
Checksum and Encryption
Encrypt and then CRC, and store the CRC decrypted.
Prepared Transaction Encryption
During the discussion, the point about prepared transaction encryption also came up since they are also persisted. Sawada-san mentioned that we aren’t storing any important data for prepared transactions so we might not need to encrypt it. However we need to have it as part of the todo list.
- Buffer encryption proof of concept by Masahiko Sawada 
- Cybertec version based on Postgres 12.3 
- General feature documentation 
- Implementation documentation 
- Cluster key retrieval scripts 
- /common functions 
- /backend/crypto functions 
- Other /backend changes 
- /bin changes 
- pg_alterckey utility 
- Testing 
- Hint bit handling 
- GiST index handling 
- Relation buffer encryption 
All patches must be applied to compile; they are logically ordered, not dependency-ordered. The full patch is at . Some information from this wiki has been moved to relevant places in these patches.
TODO for Cluster File Encryption
Here is list of ongoing tasks with there assignment and status for cluster-wide encryption:
- WAL encryption - Assigned to Moon/Swada Status : Needs to update based on the latest KMS patch.
- use WAL key
- use CTR mode
- for WAL, don't use OpenSSL's EVP interface so the offset can be specified?
- WAL nonce is segment number (no timeline?)
- Encrypt whole WAL records
- Need to make sure that we don't encrypt the different data with the same key and nonce, and write it to the disk, especially when encrypting data which is not multiple of 16 bytes.
- add Asssert() code to check that there are no WAL record types that modify more than one relation (already written)
- Temporary file encryption - Assigned to Moon Status : Work in progress, will be posted to hackers soon.
- Encryption key: A hash value is randomly generated for each temporary file, and a temporary key is generated by a combination of the hash value and the master key. (will use HMAC256)
- IV value: The encryption key will be used separately for each file. So that the IV is simply generated(64bit = hash value, 32bit = counter) as it should not exceed 1GB per file(pgsql_tmp).
- Need to check if some new temporary files that could have user data are introduced by recent changes for PG13.
- Front end tools encryption - Assigned to Cary Status : Pending due to the shift of focus on KMS for PG13. Some front-end patches have been shared with community that illustrates the interactions with KMS. Development can resume once the focus is shifted back to TDE.
- Allow pg_rewind and pg_waldump to work, add --cluster-passphrase-command option
- does pg_rewind need to work across WAL key changes?
- offline tool to allow changing the data encryption key of current WAL and PITR WAL files, must be crash-safe
- changing the pass phrase will require --old-passphrase-command and --new-passphrase-command options
- modify pg_basebackup to store relations with a different relation key so standby servers can be used for relation key rotation
Here are some tasks or areas that we need to research, some of these are being worked as part of main to do's listed above. This is a exhaustive list to ensure that we don't skip any todo required for the first phase of TDE.
- TDE for replication
- wal sender (especially xlogreader) needs to take TDE-wal key to decrypt wal data.
- Physical replication
- WAL records are sent in a form of plaintext?
- Allow primary and the standby servers to use different heap/index keys
- Logical replication (and decoding)
- We decrypt and decode WAL data and send these changes in a form of plaintext.
- The subscriber will use different encryption keys or even can disable TDE.
- TDE for backup
- During physical backup (pg_basebackup or copying OS files), table/index data are transferred in a form of encrypted text and all three internal keys are replicated
- Maybe we need to have the ability to change some internal keys during basebackup? This can be done by generating new internal keys, re-encrypting database files with the new key during the transfer (we already do checksum verification for every page), generate new control file having new internal keys and sending it.
- For logical backup (pg_dump), all data are dumped in a form of plaintext because pg_dump simply fetches data via SQL.
- How does TDE work with backup manifests?
- Regression test cases for TDE
- if a standby is promoted to a primary and the old primary continues writing, one must be re-keyed to avoid using the same IV
- Comprehensive testing
List of the contains of user data for PostgreSQL files
If there are any added files, incorrect note or user data, please correct it.
|Num||Database cluster||Contains of user data||Single Sequential Write||Single Process Write-then-Read||note|
|1||PG_VERSION||not contain||Only PostgreSQL version information is written|
|3||base/NNNNN/NNNNN_vm||not contain||VM file|
|4||base/NNNNN/NNNNN_fsm||not contain||FSM file|
|6||base/NNNNN/PG_VERSION||not contain||Only PostgreSQL version information is written|
|9||base/pgsql_tmp/pgsql_tmpPID.tempFileCounter||contain||Temporary file that creates user data temporarily when work_mem size is insufficient|
|11||global/NNNN||contain||Database name and user name|
|12||global/NNNN_vm||not contain||vm file|
|13||global/NNNN_fsm||not contain||fsm file|
|26||pg_replslot/Slotname/xid-NNN-lsn-0-NNNNNNNN.snap||contain||Includes user data decoded from WAL files|
|29||pg_stat/db_NNNNN.stat||contain||Statistics collector includes user data|
|30||pg_stat/global.stat||contain||Statistics collector includes user data|
|31||pg_stat_tmp/db_NNNNN.stat||contain||Statistics collector includes user data|
|32||pg_stat_tmp/global.stat||contain||Statistics collector includes user data|
|35||pg_twophase/NNNNNNNN||not contain||Exclude user data|
|37||pg_wal/*.backup||not contain||Exclude user data|
|38||pg_wal/*.history||not contain||Exclude user data|
|40||pg_wal/archive_status/NNNNNNNNNNNNNNNNNN.done||not contain||Exclude user data|
|41||pg_wal/archive_status/NNNNNNNNNNNNNNNNNN.ready||not contain||Exclude user data|
|43||postgresql.auto.conf||not contain||setting file|
|46||postgresql.conf||not contain||Setting file|
TDE in other systems
MySQL supports per tablespace, data at rest encryption . Please note that in MySQL the tablespace refers to a data file that can hold data for one or more InnoDB tables and associated indexes, while tablespace refers to a directory in PostgreSQL. innodb_file_per_table option allows tables to be created in their own tablespace. As of MySQL 8.0.16 it supports redo log and undo log encryption  and system tables encryption . It supports 2 tier key architecture; it has tablespace keys for each tablespace which are located on the header of tablespace file. The master key can be obtained from external systems via a key ring plugin 
MySQL encrypts each page of both redo log and undo log with dedicated keys, not with the keys used for table encryption. The encryption key is stored in the header of the first redo/undo log file in encrypted state.
Oracle DB supports column-level and tablespace-level TDE, both approaches use a two-tiered key-based architecture . The Master Encryption Key (MEK) is stored in an external key store with both hardware and software key stores supported . The MEK is used to secure the column- and tablespace-level keys. Column-level TDE uses one key per table, tablespace-level TDE uses one key per tablespace. Oracle TDE supports Triple-DES (3DES168) and AES (128, 192, 256 bit). Column-level TDE defaults to AES-192, tablespace-level TDE defaults to AES-128. Both methods add a salt to the plaintext before encryption by default . Column-level TDE supports a NOMAC parameter to improve performance.
MS SQL Server
MS SQL Server supports database-level TDE with a three-tiered architecture using both symmetric and asymmetric key encryption . The Service Master Key (SMK) is generated automatically during installation (e.g. initdb in PostgreSQL). The Database Master Key (DMK) is created in the `master` database (e.g. postgres default database) and is encrypted by the SMK. The DMK is then used to generate the certificates actually used to secure the Database Encryption Key (DEK). The DEK is the per-database symmetricly used to encrypt data and log files.