Re: [Proposal] Table-level Transparent Data Encryption (TDE) and KeyManagement Service (KMS) - Mailing list pgsql-hackers

From Sehrope Sarkuni
Subject Re: [Proposal] Table-level Transparent Data Encryption (TDE) and KeyManagement Service (KMS)
Date
Msg-id CAH7T-aq65qKbkspNyHH8vqCGeFxEw9MdARaJvDxWSWLoomxDdA@mail.gmail.com
Whole thread Raw
In response to Re: [Proposal] Table-level Transparent Data Encryption (TDE) and KeyManagement Service (KMS)  (Bruce Momjian <bruce@momjian.us>)
Responses Re: [Proposal] Table-level Transparent Data Encryption (TDE) and KeyManagement Service (KMS)  (Bruce Momjian <bruce@momjian.us>)
List pgsql-hackers
On Tue, Jul 30, 2019 at 4:48 PM Bruce Momjian <bruce@momjian.us> wrote:
I had more time to think about the complexity of adding relfilenode to
the IV.  Since relfilenode is only unique within a database/tablespace,
we would need to have pg_upgrade preserve database/tablespace oids
(which I assume are the same as the directory and tablespace symlinks).
Then, to decode a page, you would need to look up those values.  This is
in addition to the new complexity of CREATE DATABASE and moving files
between tablespaces.  I am also concerned that crash recovery operations
and cluster forensics and repair would need to also deal with this.

I am not even clear if pg_upgrade preserving relfilenode is possible ---
when we wrap the relfilenode counter, does it start at 1 or at the
first-user-relation-oid?  If the former, it could conflict with oids
assigned to new system tables in later major releases.  Tying the
preservation of relations to two restrictions seems risky.

Agreed. Unless you know for sure the input is going to immutable across copies or upgrades, including anything in either the IV or key derivation gets risky and could tie you down for the future. That's partly why I like the idea separate salt (basically you directly pay for the complexity by tracking that).

Even if we do not include a separate per-relation salt or things like relfilenode when generating a derived key, we can still include other types of immutable attributes. For example the fork type could be included to eventually allow multiple forks for the same relation to be encrypted with the same IV = LSN + Page Number as the derived key per-fork would be distinct.
 
Using just the page LSN and page number allows a page to be be
decrypted/encrypted independently of its file name, tablespace, and
database, and I think that is a win for simplicity.  Of course, if it is
insecure we will not do it.

As LSN + Page Number combo is unique for all relations (not just one relation) I think we're good for pages.

I am thinking for the heap/index IV, it would be:

        uint64 lsn;
        unint32 page number;
        /* only uses 11 bits for a zero-based CTR counter for 32k pages */
        uint32 counter;

Looks good. 
 
and for WAL it would be:

        uint64 segment_number;
        uint32    counter;
        /* guarantees this IV doesn't match any relation IV */
        uint32   2^32-1 /* all 1's */   

I need to read up more on the structure of the WAL records but here's some high level thoughts:

WAL encryption should not use the same key as page encryption so there's no need to design the IV to try to avoid matching the page IVs. Even a basic derivation with a single fixed WDEK = HKDF(MDEK, "WAL") and TDEK = HKDF(MDEK, "PAGE") would ensure separate keys. That's the the literal string "WAL" or "PAGE" being added as a salt to generate the respective keys, all that matters is they're different.

Ideally WAL encryption would generating new derived keys as part of the WAL stream. The WAL stream is not fixed so you have the luxury of being able to add a "Use new random salt XZY going forward" records. Forcing generation of a new salt/key upon promotion of a replica would ensure that at least the WAL is unique going forward. Could also generate a new upon server startup, after every N bytes, or a new one for each new WAL file. There's much more flexibility compared to page encryption.

As WAL is a single continuous stream, we can start the IV for each derived WAL key from zero. There's no need to complicate it further as Key + IV will never be reused.

If WAL is always written as full pages we need to ensure that the empty parts of the page are actual zeros and not "encrypted zeroes". Otherwise an XOR of the empty section of the first write of a page against a subsequent one would give you the plain text.

The non-fixed size of the WAL allows for the addition of a MAC though I'm not sure yet the best way to incorporate it. It could be part of each encrypted record or its own summary record (providing a MAC for a series of WAL records). After I've gone through this a bit more I'm looking to put together a write up with this and some other thoughts in one place.

Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/ 

pgsql-hackers by date:

Previous
From: Amit Kapila
Date:
Subject: Re: POC: Cleaning up orphaned files using undo logs
Next
From: Fabien COELHO
Date:
Subject: Re: proposal - patch: psql - sort_by_size