Documentation Index
Fetch the complete documentation index at: https://resources.devweekends.com/llms.txt
Use this file to discover all available pages before exploring further.
Encryption: Data at Rest & In Transit
Encryption is the foundation of PHI protection. This module covers everything from basic cryptographic concepts to production-ready implementations for healthcare applications. Real-world compliance scenario: In 2017, Advocate Medical Group paid 200 total. The cost of not encrypting them? $5.55 million in fines, plus years of remediation and reputational damage. Encryption is the single highest-ROI compliance investment you can make.- Understand symmetric vs asymmetric encryption
- Implement AES-256-GCM for data at rest
- Configure TLS 1.3 for data in transit
- Design key management systems
- Implement field-level and database encryption
Why Encryption Matters for HIPAA
Cryptography Fundamentals
Symmetric vs Asymmetric Encryption
Choosing the Right Algorithm
| Use Case | Algorithm | Key Size | Notes |
|---|---|---|---|
| Data at rest | AES-256-GCM | 256 bits | HIPAA recommended |
| File encryption | AES-256-GCM | 256 bits | With authenticated encryption |
| Key wrapping | AES-256-KW | 256 bits | For protecting DEKs |
| Key exchange | ECDH P-256 | 256 bits | For TLS |
| Digital signatures | Ed25519 | 256 bits | For audit logs |
| Password hashing | Argon2id | - | For user passwords |
Data at Rest Encryption
Envelope Encryption Pattern
- Master Key (KEK): Stored in HSM / AWS KMS / HashiCorp Vault
- Data Encryption Keys (DEKs): Unique per record, stored encrypted
- PHI Data: Encrypted with the DEK
Implementation with AWS KMS
Field-Level Encryption
Database Encryption
PostgreSQL Transparent Data Encryption (TDE)
MongoDB Client-Side Field-Level Encryption
Data in Transit Encryption
TLS 1.3 Configuration
NGINX TLS Configuration
Certificate Management with Let’s Encrypt
Key Management
HashiCorp Vault Integration
Key Rotation Strategy
Key Takeaways
Encrypt Everything
Use Envelope Encryption
TLS 1.2+ Required
Manage Keys Properly
Practice Exercise
Next Steps
E2E Encryption with AI
Implementation Guide
Interview Deep-Dive
Explain envelope encryption and why it is the standard pattern for encrypting PHI at rest. Why not just encrypt everything directly with a master key?
Explain envelope encryption and why it is the standard pattern for encrypting PHI at rest. Why not just encrypt everything directly with a master key?
- Envelope encryption uses a two-tier key hierarchy: a Master Key (KEK) that lives in an HSM or KMS, and Data Encryption Keys (DEKs) that are unique per record or per logical unit of data. The DEK encrypts the actual PHI, and the KEK encrypts the DEK. The encrypted DEK is stored alongside the ciphertext.
- The reason you do not encrypt everything directly with the master key comes down to three concerns. First, performance: calling out to an HSM or KMS for every encrypt/decrypt operation introduces latency. With envelope encryption, you generate a DEK locally (one KMS call), encrypt potentially megabytes of data with that local key (fast AES operations), then store the encrypted DEK. Decryption reverses this: one KMS call to unwrap the DEK, then local decryption.
- Second, blast radius: if a single DEK is compromised, only the data encrypted with that specific DEK is at risk. If your master key is compromised, everything is exposed. By generating unique DEKs per patient record or per session, you limit the damage of any single key compromise.
- Third, key rotation: rotating the master key with envelope encryption is straightforward. You re-wrap all the encrypted DEKs with the new master key (a KMS-side operation that never exposes plaintext DEKs) without re-encrypting the underlying data. Without envelope encryption, rotating a key means decrypting and re-encrypting every single record.
- In a HIPAA context, this also helps with the safe harbor provision. If your master key in the HSM is never compromised but an encrypted DEK blob leaks, the attacker still cannot decrypt the PHI without access to the KMS.
A junior engineer proposes using AES-256-CBC for encrypting PHI fields. You recommend AES-256-GCM instead. Explain why and what could go wrong with CBC in this context.
A junior engineer proposes using AES-256-CBC for encrypting PHI fields. You recommend AES-256-GCM instead. Explain why and what could go wrong with CBC in this context.
- AES-256-GCM is an authenticated encryption mode — it provides both confidentiality and integrity in a single operation. When you decrypt, GCM verifies that the ciphertext has not been tampered with. If someone flips a bit in the ciphertext, decryption fails with an authentication error.
- AES-256-CBC provides confidentiality only. It does not tell you whether the ciphertext was modified. This makes it vulnerable to padding oracle attacks: if the application returns different error messages for “invalid padding” versus “invalid data,” an attacker can iteratively recover the plaintext without knowing the key. This is not theoretical — it has been exploited in production systems.
- CBC also requires careful IV management. The IV must be unique and unpredictable for each encryption operation. If you reuse an IV with the same key, an attacker can XOR two ciphertexts to derive information about the plaintexts. GCM uses a nonce that must be unique but does not need to be unpredictable, and GCM explicitly fails if you accidentally reuse a nonce.
- For HIPAA compliance, the Security Rule requires integrity controls (Section 164.312(c)(1)). GCM gives you integrity for free. With CBC, you need to add a separate HMAC computation (encrypt-then-MAC), which adds complexity and more opportunities for implementation errors.
Your HIPAA-compliant application needs to support searching for patients by SSN, but SSNs are encrypted at rest. How do you design this without decrypting the entire table?
Your HIPAA-compliant application needs to support searching for patients by SSN, but SSNs are encrypted at rest. How do you design this without decrypting the entire table?
- The standard approach is blind indexing. For each searchable encrypted field, you compute a deterministic, one-way hash (typically HMAC-SHA256 with a dedicated search key) and store it alongside the encrypted value. To search, you compute the same HMAC of the search input and compare it against the stored hashes.
- This works because the same plaintext always produces the same HMAC with the same key, enabling exact-match lookups. But the HMAC is not reversible — an attacker who obtains the hash column cannot recover SSNs from it (assuming the HMAC key is secured separately).
- The tradeoff is that blind indexing only supports exact matches. You cannot do range queries, partial matches, or LIKE searches. For SSNs, exact match is usually sufficient. For names, you might index normalized forms (lowercase, stripped of punctuation).
- A more advanced approach is deterministic encryption for searchable fields and randomized encryption for non-searchable fields. MongoDB’s Client-Side Field-Level Encryption offers exactly this split.
- Security implication: deterministic encryption or blind indexes leak equality information. An attacker can tell which records have the same SSN. For high-cardinality fields like SSN this is acceptable, but for low-cardinality fields like gender or blood type, the leakage is significant. Never use deterministic encryption on low-cardinality PHI fields.
Walk me through your key rotation strategy for a HIPAA-compliant system. How often do you rotate, what happens to old data, and what are the failure modes?
Walk me through your key rotation strategy for a HIPAA-compliant system. How often do you rotate, what happens to old data, and what are the failure modes?
- Key rotation frequency depends on the key tier. Master keys in an HSM or KMS rotate annually, aligning with NIST guidelines. Data encryption keys rotate every 90 days or after a configurable number of encryptions. Session keys rotate per session or per message.
- With envelope encryption, master key rotation is a “rewrap” operation: the KMS decrypts each stored DEK with the old master key version and re-encrypts it with the new version. The underlying data is never decrypted or re-encrypted, making master key rotation fast and safe.
- Old key versions must be retained until all data encrypted under them has been re-wrapped. Never delete a key version while ciphertext encrypted under it still exists. Most KMS systems handle this with key versioning — new encryptions use the latest version, but old versions remain available for decryption.
- Failure modes to plan for: (1) Incomplete rotation — some DEKs re-wrapped but the process fails partway. Each ciphertext must store which key version encrypted it. (2) Accidental key version deletion renders data permanently unrecoverable. Implement a “minimum decryption version” policy. (3) Rotation during high load — schedule during maintenance windows and make the re-wrap process idempotent and resumable. (4) Every rotation event must be audit logged.