Skip to content

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-gcm with 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):
    $perlite$v1$gcm$<iv>$<tag>$<cipher>
    $perlite$v1$local:<version>$<iv>$<tag>$<cipher>
    Single-key local sealing via PERLITE_ENCRYPTION_KEY emits $perlite$v1$gcm$..., while configuring a multi-version PERLITE_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$):
    $perlite$v2$<provider>$<keyIdB64Url>$<wrappedDekB64>$<iv>$<tag>$<cipher>
    Supported provider identifiers emitted by EnvelopeKeyProvider are awskms (PERLITE_CRYPTO_PROVIDER=aws-kms), azurekv (azure-kv), gcpkms (gcp-kms), and vault (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:

  1. Idempotent Write Sealing (encryptNestedPayload): Walks incoming DTO payloads (including nested data, rows, items, and records arrays). Values starting with $perlite$ skip double-encryption, ensuring retries and partial updates stay idempotent.
  2. Fail-Safe Read Unsealing (decryptRecord): Every read endpoint decrypts encryptedFields in-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 to PERLITE_ENCRYPTION_KEYRING immediately encrypts new writes under $perlite$v2$<provider>$<keyIdB64Url>$... (or $perlite$v1$local:<version>$...) while CryptoService.decrypt continues unsealing older v2 and legacy v1 records by inspecting each embedded header.
  • Re-Wrapping Batches: CryptoService.needsReencryption and rewrapCiphertext detect stale key versions or providers, re-encrypting targeted values under the active KEK/DEK without application downtime or table lockups.

4. Guided Example — AES-256-GCM Sealing & KMS Integration

Security & Operations · Guide 3 of 5
AES-256-GCM Envelope Formats, KMS Providers & Bi-Directional Controller Hooks
Previously in this build: In Guide 2, Customer received MultiTenant tenant isolation and Confidential classification on siteAccessPin.
✅ Checkpoint:Customer.siteAccessPin persists strictly as authenticated AES-256-GCM ciphertext and unseals cleanly for permitted callers.
Primary TrackSeal Customer.siteAccessPin with AES-256-GCM envelope encryption so plant security keypad codes remain ciphertext at rest in SQLite and PostgreSQL while OperationsManager unseals them transparently on dispatch.
  1. Enable Encryption on Customer.siteAccessPin: Mark siteAccessPin with encrypted: true under Customer so CustomerController compiles encryptedFields = ['siteAccessPin'].
  2. Inspect Write Sealing (encryptNestedPayload): Verify encryptNestedPayload checks !value.startsWith('$perlite$') before sealing with CryptoService, guaranteeing idempotent inserts and updates.
  3. Inspect Authorized Decryption (decryptRecord): Confirm CustomerController.findOne and findAll decrypt siteAccessPin on read paths before returning JSON to authorized dispatchers.