Field-Level Envelope Encryption & Key Management
Storage-volume encryption protects disks at rest, whereas field-level authenticated envelope encryption ensures restricted secrets and clinical narrative remain encrypted inside database dumps, query replicas, log archives, and administrative exports unless explicitly unsealed by an authorized API service.
1. Authenticated AES-256-GCM Envelope Format
Marking an attribute with encrypted: true (often paired with MultiTenant, Auditable, or ePHIAudited traits) directs the backend compiler to populate private readonly encryptedFields on the generated controller and wire CryptoService automatically:
- NIST SP 800-38D Cipher Suite: Uses
aes-256-gcmwith a freshly sampled random 96-bit initialization vector (iv) per write and a 128-bit GCM authentication tag (authTag) preventing silent ciphertext tampering. - Local Key & Versioned Keyring Envelopes (
PERLITE_CRYPTO_PROVIDER=local):Single-key local sealing via$perlite$v1$gcm$<iv>$<tag>$<cipher>$perlite$v1$local:<version>$<iv>$<tag>$<cipher>PERLITE_ENCRYPTION_KEYemits$perlite$v1$gcm$..., while configuring a multi-versionPERLITE_ENCRYPTION_KEYRING(v1:<key>,v2:<key>) emits$perlite$v1$local:<version>$...for zero-downtime local key rotation. - Multi-Cloud KMS / HSM Envelope Format (
$perlite$v2$):Supported$perlite$v2$<provider>$<keyIdB64Url>$<wrappedDekB64>$<iv>$<tag>$<cipher>provideridentifiers emitted byEnvelopeKeyProviderareawskms(PERLITE_CRYPTO_PROVIDER=aws-kms),azurekv(azure-kv),gcpkms(gcp-kms), andvault(vault/openbao), with<provider>:<keyId>bound as AES-256-GCM AAD and each wrapped DEK cached in memory with bounded TTL/usage limits. - Key Environment Configuration: Configured via
PERLITE_CRYPTO_PROVIDER,PERLITE_ENCRYPTION_KEY/PERLITE_ENCRYPTION_KEYRING, and cloud provider references (AWS_KMS_KEY_ID, Azure Key Vault, GCP Cloud KMS, or Vault/OpenBao Transit settings attached per target environment).
2. Bi-Directional Write Sealing & Authorized Read Decryption
Compiled controllers apply cryptographic lifecycle hooks symmetrically across every create, update, bulk ingestion, single-record fetch, paginated list, and relational view response:
- Idempotent Write Sealing (
encryptNestedPayload): Walks incoming DTO payloads (including nesteddata,rows,items, andrecordsarrays). Values starting with$perlite$skip double-encryption, ensuring retries and partial updates stay idempotent. - Fail-Safe Read Unsealing (
decryptRecord): Every read endpoint decryptsencryptedFieldsin-memory immediately before serialization so authorized screens and API consumers work cleanly with plaintext while database rows stay sealed. Unrecognized legacy values preserve safely without crashing the request.
3. Cloud Key Providers & Zero-Downtime Rolling Key Rotation
Every ciphertext envelope embeds its protocol version, provider, and key identifier directly in the $perlite$v2$... (or local $perlite$v1$...) header:
- Live Key Rollover: Updating the active cloud KEK (
AWS_KMS_KEY_ID, Azure Key Vault key/version, GCP KMS key, or Vault Transit key) or adding a higher version toPERLITE_ENCRYPTION_KEYRINGimmediately encrypts new writes under$perlite$v2$<provider>$<keyIdB64Url>$...(or$perlite$v1$local:<version>$...) whileCryptoService.decryptcontinues unsealing olderv2and legacyv1records by inspecting each embedded header. - Re-Wrapping Batches:
CryptoService.needsReencryptionandrewrapCiphertextdetect stale key versions or providers, re-encrypting targeted values under the active KEK/DEK without application downtime or table lockups.