Using pgcrypto to Encrypt GaussDB(DWS) Data

GaussDB(DWS) 8.2.0 and later provides a built-in cryptographic module pgcrypto. The pgcrypto module allows database users to store certain columns of data after encryption, enhancing sensitive data security. Users without the encryption key cannot read the encrypted data stored in GaussDB(DWS).

The pgcrypto function runs inside database servers, which means that all data and passwords are transmitted in plaintext between pgcrypto and client applications. For security purposes, you are advised to use the SSL connection between the client and the GaussDB(DWS) server.

The functions in the pgcrypto module are as follows.

General Hash Functions

Cryptographic Hash Functions

The crypt() and gen_salt() functions are used for password hashing. crypt() executes hashes to encrypt data, and gen_salt() generates salted hashes.

The algorithms in crypt() differ from the common MD5 and SHA1 hash algorithms in the following aspects:

The following table lists the algorithms supported by the crypt() function.

Table 1 Algorithms supported by crypt()

Algorithm

Maximum Password Length

Adaptability

Salt Bits

Standard Output Length

Description

bf

72

128

60

Blowfish-based 2a variation

md5

unlimited

×

48

34

MD5-based algorithm

xdes

8

24

20

Extended DES

des

8

×

12

13

Native UNIX algorithm

PGP Encryption Functions

The PGP encryption function of GaussDB(DWS) complies with the OpenPGP (RFC 4880) standard, which includes requirements for symmetric key (private key) encryption and asymmetric key (public key) encryption.

An encrypted PGP message consists of the following parts:

For symmetric key (password) encryption:

  1. The key is encrypted using the String2Key (S2K) algorithm, which is like a slowed down crypt() algorithm with a random salt. A full-length binary key will be generated.
  2. If a separate session key is required, a random key will be generated. If it is not required, the S2K key will be used as the session key.
  3. If the S2K key is directly used for a session, this key will be put in the session key packet. Otherwise, the S2K key will be used to encrypt the session key, and the encryption result will be put in the session key packet.

For public key encryption:

  1. A random session key is generated.
  2. This random key is encrypted using the public key and then put in the session key packet.

In either case, the data encryption process is as follows:

  1. (Optional) Compress data, convert data to UTF-8, or convert newline characters.
  2. A block consisting of random bytes is added before the data, serving as a random initial value (IV).
  3. A random prefix and the SHA1 hash value suffix are added to the data.
  4. The entire content is encrypted using the session key and then placed in the data packet.

Supported PGP encryption functions

Raw Encryption Functions

Raw encryption functions only run a cipher over data. They do not support any advanced functions of PGP encryption. Therefore, the following problems exist:

With the introduction of PGP encryption, these raw encryption functions are not recommended.

encrypt(data bytea, key bytea, type text) returns bytea
decrypt(data bytea, key bytea, type text) returns bytea  
encrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea 
decrypt_iv(data bytea, key bytea, iv bytea, type text) returns bytea 

data indicates the data to be encrypted, and type indicates the encryption/decryption method. The syntax of the type parameter is as follows:

algorithm [ - mode ] [ /pad: padding ] 

The options of algorithm are as follows:

The options of mode are as follows:

The options of padding are as follows:

For example, the encryption results of the following functions are the same:

encrypt(data, 'fooz', 'bf') 
encrypt(data, 'fooz', 'bf-cbc/pad:pkcs') 

For the encrypt_iv and decrypt_iv functions, the iv parameter indicates the initial value for the CBC mode. This parameter is ignored for ECB. It is truncated or padded with zeroes if not exactly block size. It defaults to all zeroes in the functions without this parameter.

Random Data Functions