Field notes. Opinions marked as opinions.
Encryption algorithms are not where organisations fail. AES is fine. Nobody is breaking AES-256 on your database. What fails is the key: where it lives, who can read it, how long it has been the same, and whether anyone can prove it was not copied. If the key sits in the same place as the data, you have bought a very expensive padlock and taped the key to it.
The problem envelope encryption solves
Say you have 40 TB of customer documents. You want them encrypted, and you want to be able to change the master key without re-encrypting 40 TB. You also do not want the master key to ever leave a protected device.
The standard answer is two layers of keys:
- A data encryption key (DEK) encrypts the actual data. Often one per file, per object or per table.
- A key encryption key (KEK) encrypts the DEKs. The KEK lives inside a key management service or a hardware security module and is never handed out.
To write: the application asks the key service for a fresh DEK, receives it in two forms (plain, and wrapped by the KEK), encrypts the data locally, stores the wrapped DEK next to the ciphertext, and discards the plain DEK from memory. To read: send the wrapped DEK to the key service, get the plain DEK back if policy allows, decrypt, discard. This pattern is called envelope encryption. Major cloud providers and most enterprise key services use some version of it.
Why bother? Three reasons, all practical. Bulk data never travels to the key service, so it is fast and cheap. Rotating the KEK means re-wrapping small DEKs, not re-encrypting terabytes. And access to the KEK is a single choke point you can log and restrict.
KMS versus HSM, without the brochure
A key management service is software (or a managed cloud service) that creates keys, stores them, applies policy, and records every use. A hardware security module is a tamper-resistant device that performs cryptographic operations inside itself so that the key material cannot be exported in the clear. Many cloud KMS products are backed by HSMs; you can often choose a higher assurance tier, validated against a standard such as FIPS 140-2 or 140-3, when a regulation or customer contract demands it.
My opinion: most teams do not need to run their own physical HSM. They need to use the managed service properly. Buying hardware while leaving the application’s service account able to decrypt anything is theatre.
Access to decrypt is the real control
Here is the thing people miss. If a role is allowed to call “decrypt” on the key, then anyone who compromises that role reads the data in plain form, with encryption at rest working exactly as designed. Disk-level and database-level “encryption at rest” mainly protects against stolen disks, decommissioned hardware and some backup mishandling. It does little against a stolen application credential, because the platform decrypts transparently for anyone authorised.
So write key policies the way you write database permissions. Separate who can administer keys (create, disable, schedule deletion) from who can use them (encrypt, decrypt). Give each application its own key or grant, scoped to its own data. Alert on unusual decrypt volumes. A burst of ten thousand decrypt calls at 3 a.m. from a service that normally makes two hundred a day is a better breach signal than most firewall logs.
Rotation: what it does and does not do
Rotation means replacing a key with a new version on a schedule or after suspected exposure. With envelope encryption, rotating the KEK is cheap: new wraps use the new version, old DEKs can be re-wrapped gradually, and many services keep old versions available to decrypt only. Rotating a DEK means re-encrypting the data it protects, which is expensive, so it is usually done for specific reasons.
Be honest about benefit. Annual rotation limits how much data sits under one key and shortens the window in which a leaked old key is useful, but it does not rescue you from a key that was copied and used last week. Compliance frameworks and the PCI DSS standard ask for defined crypto-periods and for procedures when a key is suspected compromised. Whatever interval you choose, test that you can actually do an emergency rotation. The untested emergency procedure is the one that fails.
Mistakes I would flag on sight
- Keys in source code, configuration files in the same repository, or container images.
- One key for everything, so a single compromise exposes every dataset.
- Backups of the key service stored beside the backups of the data it protects.
- Nobody owns key deletion. Scheduled deletion of a key makes all data under it permanently unreadable, so it needs a waiting period and a second approver.
- Treating “customer-managed keys” as a magic shield. They give you control over revocation and audit, which matters, but the cloud provider’s running service still handles plaintext at the moment of processing unless you use special confidential-computing features.
A worked loss scenario
Consider an invented lender in Vadodara storing scanned KYC documents. Each file has its own DEK, wrapped by a KEK in the cloud key service. An attacker steals a copy of the storage bucket contents. They get ciphertext and wrapped DEKs, which are useless without the KEK. Good. Now suppose instead they steal the credentials of the document-processing service. That identity may call decrypt, so they can ask the key service to unwrap each DEK, and read everything. The defence here is not stronger crypto. It is narrower permissions, rate limits and alerts on decrypt calls, and short-lived credentials.
Also worth a line: India’s DPDP Act expects reasonable security safeguards for personal data. Encryption is a common safeguard, but regulators will care whether it was implemented in a way that actually limits exposure.
Rule of thumb from these notes: protect the key more carefully than the data, because the data is only as safe as the key.
Figures and names in the examples are illustrative.
Leave a Reply