天天看點

PostgreSQL 透明加密(TDE,FDE) - 塊級加密

digoal

2016-10-31

postgresql , 透明加密 , tde , fde , 塊級加密

在資料庫應用中,為了提高資料的安全性,可以選擇很多中安全加強的方法。

例如,

1. 可以對敏感字段進行加密,可以使用服務端加密,也可以使用資料庫端加密,例如pgcrypto加密插件。

服務端加解密相對來說比較安全,因為在資料庫端存儲的是加密後的資料,隻要服務端的秘鑰保護好,基本上資料都是安全的。

但是服務端加密有一個痛點,失去了對資料的高效通路能力,例如資料庫無法對加密後的資料很好的建構索引,或者處理函數。(通常隻能對未加密的資料進行處理)。

當然pg是一個比較開放的平台,使用者可以自定義對加密資料類型操作的udf,索引方法,操作符等。

使用者也可以自定義加密的資料類型,基于此建構新的op, am, func。

2. 有些商業資料庫支援tde,類似我前面說的(自定義加密類型,同時還自定義了對應的op, am, func)。

3. 網絡層加密,這種加密方法主要是防止網絡層洩露資料。

4. 資料塊層面的加密,這種加密方法主要是防止資料檔案被拷貝後洩露資料。

加密會損失一定的計算資源,損耗性能。

本文要講的則是資料塊層面的加密,這是cybertec開源的一個postgresql版本,支援所有資料檔案的加密(fde),采用industry standard 128-bit xts-aes block cipher。

the idea behind the patch is to store all the files making up a postgresql cluster securely on disk in encrypted format (data-at-rest encryption) and then decrypt blocks as they are read from disk.

this only requires that the database is initialized with encryption in mind and that the key used for initializing the database is accessible to the server during startup.

the encryption-key can be provided in two ways –

1. through an environment variable or

2. through a special configuration parameter specifying a custom key setup command for implementing special security requirements.

PostgreSQL 透明加密(TDE,FDE) - 塊級加密

加密解密是在讀寫檔案時完成的,是以如果資料已經在shared buffer中了,對性能的影響就很小了。

for encryption 128-bit aes algorithm in xts mode is used, sometimes called also xts-aes.

it’s a block cipher with a “tweak” for extra security and adheres to ieee p1619 standard.

the key needs to be provided to the server during every startup and when it doesn’t match, the server will refuse to start.

encrypted will be more or less everything – heap files (tables, indexes, sequences), xlog (as they also contain data), clog, temporary files being generated during execution of a larger query.

performance penalty incurred by encryption/decryption depends heavily on concrete use cases.

for cases where working set fits well into postgresql shared buffers, it is practically negligible though.

下載下傳

編譯, 與普通版本pg沒什麼不同

設定環境變量

設定加密秘鑰, 初始化資料庫叢集, 需要讀取pgencryptionkey這個環境變量的值.

初始化時已經對所有的檔案進行了塊級别的加密, 建議打開checksum.

初始化好之後,postgresql.conf會自動新增一個guc配置

啟動資料庫,注意每次啟動資料庫前,必須設定好 pgencryptionkey這個環境變量的值.

加解密會對性能帶來一定的損耗,簡單的測試如下。

加密版本postgresql

profile

非加密版本postgresql

除了使用pgencryptionkey環境變量,還可以使用pgcrypto.keysetup_command參數來指定

秘鑰傳輸相關的代碼