Encryption
On this page
- Overview
- When to Use Field-Level Encryption
- Basic Field Encryption
- Setup
- Encrypt Before Insert
- Decrypt on Read
- Schema Design for Encrypted Fields
- Searchable Encryption (Hash-Based)
- Key Management
- Option 1: Environment Variables (Development)
- Option 2: AWS KMS (Production)
- Option 3: HashiCorp Vault (Production)
- Key Rotation
- Helper: Encryption Wrapper
- Performance Considerations
- Compliance
- HIPAA
- PCI DSS
- Testing
- Related Documentation
Encrypt sensitive data in Kimberlite applications.
Overview
Kimberlite provides multiple layers of encryption:
- Encryption at rest - Per-tenant envelope encryption (automatic)
- Encryption in transit - TLS 1.3 (configure once)
- Field-level encryption - Encrypt specific columns (manual)
This recipe focuses on field-level encryption for sensitive data like SSNs, credit cards, or personal notes.
When to Use Field-Level Encryption
| Data Type | Encryption Needed? | Why |
|---|---|---|
| Patient name | No | Already encrypted at rest, needs to be searchable |
| Social Security Number | Yes | Extra protection, not searchable |
| Credit card number | Yes | PCI DSS requirement |
| Personal notes | Yes | Highly sensitive, user-controlled |
| Phone number | No | Already encrypted at rest, needs to be searchable |
| Date of birth | No | Already encrypted at rest, needs to be queryable |
Rule of thumb: Encrypt fields that are:
- Highly sensitive
- Don’t need to be searchable/queryable
- Subject to extra regulations (PCI DSS, etc.)
Basic Field Encryption
Setup
use EncryptionKey;
// Generate a per-field encryption key
let field_key = generate;
// Store key securely (KMS, HSM, or environment variable)
set_var;
Encrypt Before Insert
use Client;
use EncryptionKey;
Decrypt on Read
Schema Design for Encrypted Fields
(
id BIGINT PRIMARY KEY,
name TEXT NOT NULL, -- Searchable (not encrypted at app level)
date_of_birth DATE, -- Searchable (not encrypted at app level)
ssn_encrypted TEXT, -- Encrypted at app level (not searchable)
credit_card_encrypted TEXT, -- Encrypted at app level (not searchable)
notes_encrypted TEXT -- Encrypted at app level (not searchable)
);
-- No index on encrypted fields (they're not searchable)
Key point: Encrypted fields cannot be queried. Store a hash if you need to verify without decrypting:
(
id BIGINT PRIMARY KEY,
ssn_encrypted TEXT, -- Encrypted SSN
ssn_hash TEXT -- SHA-256 hash for verification
);
ON patients(ssn_hash);
Searchable Encryption (Hash-Based)
To verify an encrypted field without decrypting:
use ;
Key Management
Option 1: Environment Variables (Development)
# .env (DO NOT commit to git)
FIELD_ENCRYPTION_KEY=base64_encoded_key_here
use env;
Only for development. Never commit keys to version control.
Option 2: AWS KMS (Production)
use Client as KmsClient;
async
Option 3: HashiCorp Vault (Production)
use ;
async
Key Rotation
Rotate encryption keys periodically:
Run during maintenance window. Large tables may take hours.
Helper: Encryption Wrapper
Encapsulate encryption logic:
use EncryptionKey;
use base64;
Usage:
let field = new;
// Insert
let encrypted_ssn = field.encrypt?;
client.execute?;
// Query
let row = client.query_one?;
let encrypted: String = row.get;
let plaintext_ssn = field.decrypt?;
Performance Considerations
Encryption overhead:
- AES-256-GCM: ~2 GB/s (hardware-accelerated)
- Per-field overhead: <1ms for typical field sizes
Best practices:
- Encrypt/decrypt only when needed (not on every query)
- Cache decrypted values in memory (with TTL)
- Use connection pooling to amortize key loading
Compliance
HIPAA
- Encryption at rest (per-tenant, automatic)
- Encryption in transit (TLS 1.3)
- Field-level encryption (manual, for extra-sensitive data)
PCI DSS
Requires encryption for:
- Credit card numbers (PAN)
- CVV codes
- Track data
// PCI DSS: Encrypt credit card data
let field = new;
let encrypted_pan = field.encrypt?;
let encrypted_cvv = field.encrypt?;
client.execute?;
Never log decrypted credit card data.
Testing
Related Documentation
- Cryptography - Encryption algorithms
- Compliance - Encryption requirements
- Multi-tenancy - Per-tenant encryption
Key Takeaway: Use field-level encryption for highly sensitive data that doesn’t need to be searchable. Kimberlite’s per-tenant encryption protects all data at rest, but field-level encryption adds defense-in-depth.