Key management consists of four parts: key generation, key preservation, key exchange, and key rotation.
Only for the study of symmetric encryption, so I mainly introduce symmetric encryption.
The symmetric password generation method is:
- A random number is a key
- Password-based key generation
- HKDF (HMAC-based extraction and extended key derivation)
A random number is a key
Random number as a key: Using a powerful random number generator to generate the key, this is easy to understand.
Password-based key generation
Password-based key generation: A method of generating a key based on a user password and using it for encryption and decryption. The specific method is as follows:
- A user enters a password;
- The system generates a random number, performs a hash calculation with the user password, and obtains a key encryption key;
- Store the key encryption key to a secure location;
- The system generates a random number as a data encryption key;
- The key encryption key encrypts the data key and the encrypted data key is also stored in a secure place;
- Use data encryption keys to encrypt and decrypt data.
The Key Derivation Function (KDF) is an essential part of the cryptosystem. Its goal is to obtain some initial key material and derive one or more keys that are very strong in security.
- Extracting, using strong random numbers and input information, using the HMAC method for hashing;
- Extend, through multiple hash calculations, extend the above results to the length we need.
The generation of keys, whether random or password-based, is partially difficult to remember, so it is necessary to store the key (maybe it is a salt).
The key cannot be stored in the same location as the data, otherwise, it is equal to the key hanging on the door, meaning nothing.
We generally need to store the key in a file and put it in a secure storage location such as a secure key distribution center.
When the number of keys reached a certain amount, we needed another key — KEK (Key encryption key).
This is because if the data key is not encrypted, the stealer can easily decrypt the data using the key after stealing. Also, managing multiple keys is much more difficult than a single key.
So you can consider a way to use KEK to save keys, of course, KEK also needs to be stored in a secure location.
There are currently four types of key exchanges: pre-shared keys, public-key ciphers, key distribution centers, and Diffie-Hellman key exchange methods.
Here’s a quick description of the four methods:
The two sides exchange keys by secure means before encryption, but in the database, the two sides are in the same area as the server and the disk, that is, the key and the disk are insecure, so the database does not consider this way.
Since we use symmetric encryption system here, we do not consider the public key. But this can be used as part of key exchange.
Key Distribution Center
Store the key to a trusted third party and through it to obtain the key. When a key is needed, communication is made to a third party, a session key is generated, and the session key is used to encrypt the key to transmit the key.
Diffie-Hellman key exchange
Diffie-Hellman was an algorithm invented in 1976 by Whitfield Diffie and Martin Hellman. The algorithm generate shared secret numbers by exchanging information that can be disclosed, so as to achieve the purpose of sharing keys. The process is as follows:
- The DB server sends two prime numbers P, G to the Key server (hereinafter referred to as D and K);
- D generates a random number A;
- K generates a random number B;
- D sends G^A mod P to K;
- K sends G^B mod P to D;
- D uses the number B’ sent by K to calculate B’ mod P, which is the data encryption key;
- K uses the number A’ sent by D to calculate A’ mod P, which is equal to the encryption key calculated by D;
Of course, when you add it to the database, you also need to use a secure third party as the information exchange, but this reduces the possibility of the key being eavesdropped.
The key rotation is divided into two parts: key update and invalidation.
Key update can improve the difficulty of key brute force cracking. Secondly, even if the past key is cracked, the current data cannot be obtained.
The key invalidated. After the update, the key should be invalidated in time. The invalidation here refers not only to the deletion but also make the key can not be able to restore the key.
Shawn Wang is a developer of PostgreSQL Database Core. He has been working in HighGo Software for about eight years.
He did some work for Full Database encryption, Oracle Compatible function, Monitor tool for PostgreSQL, Just in time for PostgreSQL and so on.
Now he has joined the HighGo community team and hopes to make more contributions to the community in the future.