The Case for Defense-in-Depth Encryption
Context data in AI systems represents a concentrated target for attackers. Unlike scattered application logs or fragmented database records, context stores contain rich, pre-aggregated information—customer profiles, behavioral patterns, business intelligence, and proprietary knowledge graphs. A single breach of an unencrypted context store can expose an entire organization's most sensitive data in a readily usable format.
Encryption provides defense-in-depth: even when access controls, network segmentation, or physical security fails, properly encrypted data remains protected. However, "encrypt everything" is not a strategy—it is a starting point. Effective encryption requires thoughtful decisions about what to encrypt, where, with what algorithms, and how to manage keys throughout their lifecycle.
Encryption without proper key management is like installing a vault door and leaving the combination taped to the wall. The algorithm is only as strong as your operational discipline around key handling.
This guide covers the three layers of encryption relevant to AI context systems—at rest, in transit, and at the field level—along with key management practices that make encryption operationally sustainable.
Encryption at Rest
Storage-Level Encryption (Transparent Data Encryption)
Every major cloud provider and database platform offers transparent data encryption (TDE) or storage-level encryption. This encrypts data on disk without requiring application changes. It should be enabled by default on all context stores, but it is critical to understand its limitations:
- What it protects against: Physical theft of storage media, unauthorized access to raw storage volumes, and certain classes of storage-side vulnerabilities.
- What it does not protect against: Anyone with legitimate database credentials can read data in plaintext. A compromised application, a rogue administrator, or a SQL injection attack bypasses storage-level encryption entirely because decryption happens transparently at the storage layer.
Storage-level encryption is a necessary floor, not a sufficient ceiling. Treat it as your baseline and layer additional encryption on top for sensitive context categories.
Application-Level Encryption
For context data that demands stronger protection—PII, financial information, health records, authentication tokens—encrypt at the application layer before writing to storage. This means the database never sees plaintext for these fields, protecting against:
- Database administrator access
- Database vulnerabilities and injection attacks
- Backup and snapshot exposure
- Accidental logging of sensitive values
Implementation approaches include:
- Envelope encryption: Generate a data encryption key (DEK) for each context record or batch. Encrypt the context with the DEK, then encrypt the DEK with a key encryption key (KEK) stored in your key management service. Store the encrypted DEK alongside the encrypted data. This pattern allows key rotation without re-encrypting all data.
- Deterministic encryption: When you need to query or index encrypted fields (e.g., looking up context by an encrypted email address), use deterministic encryption that produces the same ciphertext for the same plaintext. This sacrifices some security (it leaks equality information) but preserves queryability.
- Order-preserving encryption: For range queries on encrypted fields, order-preserving encryption maintains sort order. Use this sparingly and only when range queries are genuinely required, as it leaks ordering information.
Searchable Encryption and Encrypted Context Queries
A major challenge with application-level encryption is maintaining query capabilities. Several approaches exist, each with different security-performance tradeoffs:
| Approach | Query Support | Security Level | Performance Impact | Best For |
|---|---|---|---|---|
| Deterministic encryption | Exact match only | Moderate (leaks equality) | Low | Lookup by encrypted identifier |
| Order-preserving encryption | Range queries | Lower (leaks order) | Low | Date ranges on encrypted timestamps |
| Blind index | Exact match via HMAC | High | Low | Searching encrypted fields without leaking plaintext |
| Homomorphic encryption | Computation on ciphertext | Very high | Very high (100-1000x) | Research and specialized use cases |
| Client-side filtering | Full query flexibility | High | High (fetch and decrypt all) | Small datasets with strong security needs |
For most AI context systems, the blind index approach offers the best balance. Generate an HMAC of the field value using a separate key and store the HMAC alongside the encrypted field. Queries match against the HMAC without ever decrypting the actual data.
Encryption in Transit
TLS Configuration for Context Services
All context data in transit must be encrypted using TLS 1.3 or, at minimum, TLS 1.2 with strong cipher suites. TLS 1.3 is preferred because it eliminates vulnerable cipher suites, reduces handshake latency, and provides forward secrecy by default. Key configuration practices:
- Disable TLS 1.0 and 1.1: These versions have known vulnerabilities and are deprecated by all major standards bodies.
- Prefer AEAD cipher suites: Use AES-256-GCM or ChaCha20-Poly1305. Avoid CBC-mode ciphers, which are vulnerable to padding oracle attacks.
- Enable HSTS: For any HTTP-based context APIs, enforce HTTP Strict Transport Security with a long max-age and include subdomains.
- Certificate management: Automate certificate provisioning and renewal using ACME (Let's Encrypt) or your cloud provider's certificate manager. Expired certificates cause outages; manual renewal processes are a ticking time bomb.
Mutual TLS Between Services
In a zero-trust architecture, one-way TLS is insufficient. Mutual TLS (mTLS) requires both the client and server to present certificates, providing bidirectional authentication. This is essential for service-to-service communication within your context infrastructure:
- Deploy a private certificate authority (CA) for internal service certificates
- Automate certificate rotation with short lifetimes (24-72 hours)
- Use a service mesh to handle mTLS transparently, removing the burden from application code
- Monitor certificate expiration and revocation across all services
Encryption for Context Synchronization
When replicating context data across regions or availability zones, ensure that replication channels use encrypted transport. Database-native replication protocols (e.g., PostgreSQL streaming replication, MongoDB replica set communication) should be configured with TLS. For real-time context synchronization over message brokers like Kafka, enable TLS for both broker-to-broker and client-to-broker communication.
Field-Level Encryption
When and Why to Encrypt Individual Fields
Field-level encryption protects specific high-sensitivity attributes within a context record while leaving other fields in plaintext for indexing and querying. This is the right approach when:
- Context records contain a mix of sensitive and non-sensitive fields
- You need to query on non-sensitive fields while protecting sensitive ones
- Different fields have different access authorization requirements
- Regulatory requirements mandate encryption of specific data categories (e.g., HIPAA for health information, PCI DSS for cardholder data)
Implementation Patterns
Implement field-level encryption in your context access layer so that encryption and decryption are transparent to consuming services. The access layer:
- Receives context write requests with plaintext sensitive fields
- Identifies fields marked for encryption in the context schema
- Encrypts those fields using the appropriate key (potentially different keys for different sensitivity levels)
- Stores the encrypted context record
- On read, decrypts fields only if the requesting service has authorization for that sensitivity level
This pattern integrates naturally with multi-tenant isolation—use tenant-specific encryption keys so that even if isolation boundaries fail, cross-tenant data remains encrypted with inaccessible keys.
Key Management
Key Management Service (KMS) Architecture
Never manage encryption keys in application code or configuration files. Use a dedicated key management service:
- Cloud KMS options: AWS KMS, Google Cloud KMS, Azure Key Vault. These provide hardware security module (HSM) backing, automatic rotation, and IAM-integrated access control.
- Self-managed options: HashiCorp Vault provides a mature, cloud-agnostic key management solution with dynamic secrets, lease-based access, and detailed audit logging.
Key Rotation
Rotate encryption keys on a regular schedule and immediately after any suspected compromise. Design your encryption scheme to support rotation without downtime:
- Use envelope encryption so that rotating the KEK does not require re-encrypting all data—only the DEKs need re-wrapping
- Maintain a key version registry so that data encrypted with older key versions can still be decrypted
- Implement lazy re-encryption: when a record encrypted with an old key version is accessed, re-encrypt it with the current key version before returning it
Key Access Controls
Apply the principle of least privilege to key access. Context services should have access only to the keys they need for their specific context types. Separate encryption and decryption permissions—a service that writes encrypted context may not need the ability to decrypt it. Log all key access operations and alert on anomalous patterns.
Compliance Mapping
Different regulatory frameworks have specific encryption requirements. When designing your encryption strategy, map your approach to the frameworks relevant to your context system:
- GDPR (Article 32): Requires "appropriate technical measures" including encryption. No specific algorithm mandates, but expects state-of-the-art protection.
- HIPAA (45 CFR 164.312): Requires encryption for electronic protected health information (ePHI) at rest and in transit. AES-256 is the de facto standard.
- PCI DSS (Requirement 3): Mandates strong cryptography for stored cardholder data with documented key management procedures.
- SOC 2 (CC6.1): Requires logical and physical access controls, which encryption supports. Encryption key management practices are commonly examined during audits.
For a broader view of compliance requirements, see our GDPR compliance guide and ensure your encryption strategy aligns with your audit trail architecture.
Frequently Asked Questions
Should we encrypt all context data or only sensitive fields?
Use a tiered approach. Enable storage-level encryption for all context data as a baseline. Apply application-level or field-level encryption to sensitive categories: PII, financial data, health information, and authentication credentials. Encrypting everything at the application level introduces unnecessary performance overhead and operational complexity for data that does not warrant it.
How does encryption affect context retrieval performance?
Modern hardware-accelerated AES encryption adds negligible overhead—typically under 1 microsecond per operation on processors with AES-NI instructions. The real performance impact comes from the inability to index or query encrypted fields. Design your schema so that fields you need to query remain in plaintext or use blind indexes, and encrypt only fields that are retrieved, not searched.
What happens to encrypted context data when encryption keys are rotated?
With envelope encryption, key rotation is lightweight. The key encryption key (KEK) is rotated, and only the data encryption keys (DEKs) need to be re-wrapped—the data itself is not re-encrypted. Old key versions are retained in a deprecated state for decrypting existing DEKs. Implement lazy re-encryption to gradually migrate data to the new key version during normal read operations.
How do we handle encryption for context data that needs to be shared with third-party AI model providers?
Never send encryption keys to third parties. Instead, decrypt context at your boundary, apply data minimization (send only the fields the provider needs), transmit over TLS, and ensure your data processing agreement with the provider specifies their encryption obligations. For highly sensitive context, consider techniques like differential privacy or synthetic data generation to avoid sharing real data entirely.