The “column level encryption” feature described on this page specifically refers to symmetric-key encryption of data, not hashing functions operating on data.
Available Functions
encrypt and encrypt_iv
The encrypt and encrypt_iv functions encrypt a column’s data with a given key and cipher method. For more information, see .
For usage examples, see:
If you do not have a , you will see an error message like the following if you try to use them:
For more information about whether to use the
encrypt or encrypt_iv variants of this function, see Whether to use encrypt and decrypt or encrypt_iv and decrypt_iv.decrypt and decrypt_iv
The decrypt and decrypt_iv functions decrypt an encrypted column’s data with a given key and cipher method. For more information, see .
For usage examples, see:
If you do not have a , you will see an error message like the following if you try to use them:
For more information about whether to use the
decrypt or decrypt_iv variants of this function, see Whether to use encrypt and decrypt or encrypt_iv and decrypt_iv.Security considerations
Whether to use encrypt and decrypt or encrypt_iv and decrypt_iv
Both encrypt and decrypt have *_iv variants: encrypt_iv and decrypt_iv. You will need to assess your risk profile to determine which functions to use.
The benefits of using encrypt_iv and decrypt_iv include:
- Avoid repetition: If you use the same encryption key and method to encrypt the same plaintext multiple times without an initialization vector (IV), you’ll get the same ciphertext every time. This repetition can provide a point of attack for someone trying to break the encryption. By using an IV, even the same plaintext will produce different ciphertexts, provided a different IV is used each time.
- Defend against pattern analysis: Without an IV, if two users have the same piece of data (such as an SSN), their encrypted values will also be the same. An attacker can exploit these patterns. By using different IVs for each encryption, the encrypted values will be different even if the plaintext values are the same.
- Cipher block chaining (CBC) mode: Many encryption algorithms, like AES, operate on blocks of data. In modes like CBC, the previous block of ciphertext is used as an IV for the encryption of the next block. This means that even if there are patterns in the plaintext, they won’t appear in the ciphertext. However, for the first block, there is no previous block of ciphertext, so an IV is used. This is another way IVs help in breaking up patterns in the ciphertext.
- Mitigate replay attacks: Since the IV is typically random and changed for every encryption, it makes replay attacks more difficult. An attacker can’t simply take an old piece of encrypted data and send it again, as the IV will likely have changed.
encrypt_iv and decrypt_iv include:
- Storage: You need to store the IV alongside the ciphertext. It’s common practice to prepend or append the IV to the ciphertext before storing it. Unlike the encryption key, the IV doesn’t need to be kept secret, but it does need to be known for decryption.
- Randomness: It’s crucial that IVs are random and not predictable. If an attacker can predict the next IV, some of the security benefits are negated.
- Unique IVs with the Same Key: While IVs need to be random, it’s also essential that the same IV isn’t used twice with the same encryption key. Doing so can leak information about the plaintext.
How AES variants are determined
The actual AES variant (AES-128, AES-192, or AES-256) is determined by the length of the encryption key you provide in the functions:- AES-128: 16-byte key
- AES-192: 24-byte key
- AES-256: 32-byte key
Performance considerations
Use of theencrypt built-in function can have anywhere from 10-40% overhead depending on the length of the data being encrypted and the hardware provisioned for CockroachDB.
Cockroach Labs measured baseline performance in a 3-node CockroachDB cluster running on three n1-standard-4 machines on GCP.
Without using encrypt or decrypt, the following statement (which represents 10,000 operations) generally ran in 60-80 ms:
encrypt and decrypt, the following statement generally ran in 80-100 ms:
encrypt only, the following statement generally ran in 80-100 ms:
It is important to benchmark these built-in functions on your particular CockroachDB setup to establish the performance implications for your workloads. This is necessary because performance can vary depending on your hardware (CPU type), the typical amount of load on the cluster, etc.
Examples
Setup
The examples in this section operate on the following table.The columns that will store the encrypted values must be of type as shown below.

