Encryption at rest
Database files, uploaded objects, logs and backups all end up as bytes on a storage medium. Encryption at rest keeps those bytes encrypted, so a copy of the disk or a file taken without the key is unreadable. Encryption in transit does the same for data moving over a network. Sensitive information left in cleartext where another party might reach it is a catalogued weakness in its own right.1 The data that most often needs this protection is passwords, card numbers, health records, personal information and business secrets.2
Layers
Each level of the stack where encryption can be applied stops a different attacker.3 For a database server the threats are theft of the machine, administrators who misuse their access, and insecure networks.4
| Layer | Examples | Protects against |
|---|---|---|
| Hardware | Self-encrypting SSDs, encrypted RAID cards | A stolen drive or server |
| Disk or file system | dm-crypt with LUKS on Linux, BitLocker on Windows | A stolen drive or computer |
| Database | Transparent data encryption (TDE), managed database encryption | Copies of data files, backups and snapshots |
| Application | Fields encrypted in code before they are written | Anyone who reads the database without the application's key |
| Client | Data encrypted before it leaves the user's device | The server operator |
The lower layers decrypt automatically for any program running on the machine. A mounted file system presents an unencrypted view of its data, so disk encryption does nothing against an attacker who is logged in to the running server.4 Hardware encryption has the same limit: it covers physical theft of the server and does nothing against a remote compromise.3 Only encryption at the application or client layer keeps data unreadable to someone who can query the database.
Keys
Encrypted data is as safe as its key, and storing the key is one of the hardest problems in the design: an application that decrypts data must be able to reach it.3 Keys are generated with a cryptographically secure random function (crypto.randomBytes() in Node.js, never Math.random()), kept out of source code and version control, and stored apart from the data, so that an attacker who reads the database through SQL injection or files through Path traversal does not obtain both.3 A key committed to a repository is a case of Exposed credentials in source code repositories; Secrets management covers how keys are supplied and rotated.
Most managed services use envelope encryption. The data is encrypted with a data encryption key (DEK), and the DEK is encrypted with a key encryption key (KEK). The encrypted DEK can be stored next to the data, because it is useless without the KEK.5 At the top of the chain one root key must stay in plaintext. In AWS this root key sits in a key management service whose hardware security modules, validated to FIPS 140-3 Level 3, never release it unencrypted; a program must call the service to use it.5 If the KEK and the DEK end up on the same shared host, the scheme adds little.3
Algorithms
For stored data the standard choice is AES with a key of at least 128 bits, preferably 256, in an authenticated mode such as GCM or CCM, which detects changes to the data as well as hiding it. ECB mode and home-made algorithms are ruled out.3 The initialization vector (IV) must never repeat under the same key.2 Passwords are a separate case: they are stored as a slow, salted hash such as Argon2 or scrypt, never with reversible encryption.23
Managed services
Hosted databases and storage services encrypt at the storage layer, some of them by default. Since 5 January 2023 Amazon S3 encrypts every new object with its own key under AES-256-GCM, and wraps that key in a regularly rotated key; the object metadata stays unencrypted.6 An encrypted Amazon RDS instance covers its storage, logs, automated backups, read replicas and snapshots, and decrypts transparently for the application. Encryption cannot be switched off on an encrypted instance, and an unencrypted one is converted through a blue/green deployment. If the KMS key is disabled, RDS loses access to the data, and after seven days the instance can only be restored from a snapshot or backup.7 Supabase states that all customer data is encrypted at rest with AES-256, and that it encrypts access tokens and keys at the application level before writing them to the database.8
Field encryption in an application
For a column that should stay unreadable even to someone with database access, such as a stored third-party token, the application encrypts the value before saving it. A Node.js version with AES-256-GCM, where the 32-byte key comes from a key store:9
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
const key = Buffer.from(process.env.FIELD_KEY, "base64"); // 32 bytes
function encrypt(text) {
const iv = randomBytes(12); // new IV for every value
const c = createCipheriv("aes-256-gcm", key, iv);
const data = Buffer.concat([c.update(text, "utf8"), c.final()]);
return Buffer.concat([iv, c.getAuthTag(), data]).toString("base64");
}
function decrypt(b64) {
const buf = Buffer.from(b64, "base64");
const d = createDecipheriv("aes-256-gcm", key, buf.subarray(0, 12));
d.setAuthTag(buf.subarray(12, 28));
return Buffer.concat([d.update(buf.subarray(28)), d.final()]).toString("utf8");
}The IV and the 16-byte authentication tag are stored with the ciphertext. If either the tag or the data has been altered, d.final() throws and no plaintext is returned.9 PostgreSQL offers a database-side alternative in the pgcrypto module, but there the key and the decrypted value are briefly present on the database server.4
Limits
Encryption at rest does not stop an attacker who acts with the permissions of the application or of an account allowed to use the key. In its July 2019 notice, Capital One wrote that it encrypted its data as a standard, but that in the intrusion of 22 and 23 March 2019, which affected about 100 million people in the United States and 6 million in Canada, "the unauthorized access also enabled the decrypting of data".10 Selected fields, among them Social Security and account numbers, were also tokenized with a different method and different keys, and the company stated that the tokenized data "remained protected".10
Encryption is therefore one layer of Defence in depth and still needs access control around it. The strongest protection is not to store the data at all.3 Old keys are kept after rotation for as long as old backups encrypted with them may need to be read.3
In SecFlash
On the Security page, SF-SEC-730 reports a sign-in token, session or password saved in unencrypted app storage such as localStorage or AsyncStorage, and SF-SEC-749 reports an encryption key or a fixed IV written in the app's code. SF-SEC-707 and SF-SEC-787 report broken algorithms and modes, including DES, RC4 and ECB.
References
- back "CWE-312: Cleartext Storage of Sensitive Information". MITRE. Common Weakness Enumeration, version 5.0.
- a b c "A04:2025 Cryptographic Failures". OWASP Top 10:2025. OWASP Foundation.
- a b c d e f g h i OWASP Foundation. "Cryptographic Storage Cheat Sheet". OWASP Cheat Sheet Series.
- a b c PostgreSQL Global Development Group. "18.8. Encryption Options". PostgreSQL 19 Documentation.
- a b Amazon Web Services. "AWS KMS cryptography essentials". AWS KMS Developer Guide.
- back Amazon Web Services. "Using server-side encryption with Amazon S3 managed keys (SSE-S3)". Amazon S3 User Guide.
- back Amazon Web Services. "Encrypting Amazon RDS resources". Amazon RDS User Guide.
- back Supabase. "Security". supabase.com.
- a b Node.js. "Crypto". Node.js documentation.
- a b Capital One (29 July 2019). "Capital One Announces Data Security Incident". capitalone.com.
See also
- Encryption in transit
- Secrets management
- Backup and restore
- Exposed backups
- Database dumping
- Principle of least privilege
- OWASP Top 10
External links
- Cryptographic Storage Cheat Sheet, OWASP
- Encryption Options, PostgreSQL documentation