@enterprise_search/storage_crypto
v0.8.137
Published
Crypto for storage: ensuring each user only sees their own data
Readme
Storage Crypto
Problem
- Container-level RBAC in blob storage can’t restrict access per user.
- We need “crypto boxes” so only the creator or admin can decrypt each blob.
Features
- A single 'current' global secret with key derivation allows for cryptographically strong keys for each id
- While only one global secret is 'active' for writing, multiple reads are allowed, which means we don't have an outage when we change global keys
- Each record can only be decrypted by the user or the admin
- All the metadata (apart from the secret) needed to decrypt the blob is stored in parallel to the blob
- Very resiliant to crypto attacks: even if a single blob is 'hacked' and the key found, this key does not help find the global secret, and is not similar to other keys for the same user, so is of no help when decypting other
Components
- Global secrets → derive per-ID wrap keys via HKDF + salt
- Client ops: DEK generation, wrapping, payload encryption, metadata headers
- API: holds master secrets, validates JWT, exposes wrap/unwrap endpoints
- Storage: blob body + user-metadata (
x-ms-meta-…)
Encryption Flow
- Generate random DEK & payload IV.
- Generate salt.
- HKDF-derive user wrap-key (
info = userId) and admin wrap-key (info = "admin"). - AES-GCM wrap DEK under each key (stores salt+IV+wrappedDEK in metadata).
- AES-GCM encrypt payload with DEK (store in blob).
Decryption Flow
- Read blob metadata & body.
- HKDF-derive wrap-key for user or admin.
- AES-GCM decrypt wrapped DEK.
- AES-GCM decrypt payload.
Example code to encrypt:
//setup
const globalSecret1 = '12345678123123123123321123' // probably injected via env variables.
const globalSecret2 = 'ffffffffff3123123123321123'
const cryptoConfig = cryptoConfig({
globalSecrets: {'one': globalSecret1, 'two': globalSecret2}, //First one is used to write, any can be used to read
adminName: 'admin'
})
const encryptor = defaultStorageEncodeFn(config1)
const decryptor = defaultStorageDecodeFn(config1)
//To encrypt
const {metadata, encoded} = await encryptor('someUserId', 'someplaintext')
//to decypt for a user or admin
const plainTextForUser = await decryptor(metadata, {userId: 'someUserId'}, encoded)
const plainTextForAdmin = await decryptor(metadata, 'admin', encoded)