Database Encryption in Trustpoint¶
Overview¶
Trustpoint implements a robust database encryption system to protect sensitive data such as EST passwords, CMP shared secrets, and private keys for credentials created in Trustpoint. The system uses a two-tier key management approach with PKCS#11 hardware security module (HSM) integration, employing AES-256-GCM encryption.
Key Management Architecture¶
The encryption system follows a hierarchical key structure:
Key Encryption Key (KEK) - A 256-bit AES key stored in the PKCS#11 HSM
Data Encryption Key (DEK) - A 256-bit AES key encrypted by the KEK and stored in the database
Field Encryption - Individual database fields encrypted using the DEK with AES-256-GCM
Key Generation Process¶
KEK Generation¶
During the setup wizard HSM configuration phase:
The system generates a 256-bit AES key directly in the PKCS#11 token
The KEK is stored with the label
trustpoint-kekThe key is marked as: -
SENSITIVE- Cannot be extracted from the HSM -TOKEN- Persistent across sessions -EXTRACTABLE=False- Cannot be exported
DEK Generation and Wrapping¶
The DEK is generated and wrapped during initial setup:
Generate a random 256-bit key
Open a session with the PKCS#11 token
Use the KEK to wrap the DEK using AES-ECB encryption
Prepend an 8-byte random IV to the encrypted DEK
Store the wrapped DEK (40 bytes: 8-byte IV + 32-byte encrypted DEK) in the database
encrypted_dekfield
Runtime Key Management¶
Container Startup¶
When the Trustpoint container starts:
The system attempts to retrieve the cached DEK from Django’s cache
If not cached, it unwraps the DEK using the PKCS#11 KEK:
Open session with the PKCS#11 token
Retrieve the KEK using label
trustpoint-kekExtract the 8-byte IV and 32-byte encrypted DEK from
encrypted_dekDecrypt using AES-ECB with the KEK
Cache the decrypted DEK indefinitely
The DEK remains in memory cache for the container’s lifetime
DEK Caching Strategy¶
Cache Key:
trustpoint-dek-chache-{token_label}(token-specific)Cache Duration: Indefinite (
Nonetimeout)Cache Backend: Django’s configured cache
Security: DEK can be manually cleared using
clear_dek_cache()method
Database Field Encryption¶
Encrypted Field Types¶
Two field types are provided for database encryption:
EncryptedCharField- For short sensitive strings (passwords, secrets)EncryptedTextField- For longer sensitive text content
Encryption Process¶
When saving data to encrypted fields:
Check Encryption: Verify if HSM-based encryption is enabled via
KeyStorageConfigRetrieve DEK: Get the cached DEK from the PKCS#11 token
Generate Nonce: Create a random 12-byte nonce for GCM mode
Padding: Add random padding (0-15 bytes) to obscure length patterns
Encryption: Encrypt using AES-256-GCM with the DEK and nonce
Combine: Concatenate nonce (12 bytes) + authentication tag (16 bytes) + ciphertext
Encoding: Base64 encode the combined data
Storage: Store the encoded result in the database
Decryption Process¶
When reading data from encrypted fields:
Check Encryption: Verify if HSM-based encryption is enabled
Retrieve DEK: Get the cached DEK
Decode: Base64 decode the stored value
Extract Components: Separate nonce (first 12 bytes), authentication tag (next 16 bytes), and ciphertext
Decryption: Decrypt using AES-256-GCM and verify authentication tag
Unpadding: Remove random padding based on last byte value
Return: Return the original plaintext
Protected Data Types¶
The following sensitive fields use database encryption:
Device Model Fields¶
est_password(EncryptedCharField, max_length=128) - EST authentication passwordscmp_shared_secret(EncryptedCharField, max_length=128) - CMP protocol shared secrets
Credential Model Fields¶
private_key(EncryptedCharField, max_length=65536) - PEM-encoded private keys for credentials created in Trustpoint
Note: Encryption is only active when KeyStorageConfig.storage_type is set to SOFTHSM or PHYSICAL_HSM. When using software-based key storage, data is stored in plaintext.
UML Sequence Diagram¶
Encryption Implementation Details¶
Cryptographic Algorithm¶
The system uses AES-256-GCM (Advanced Encryption Standard with 256-bit keys in Galois/Counter Mode) for field-level encryption:
Algorithm: AES-256
Mode: GCM (Galois/Counter Mode)
Key Size: 256 bits (32 bytes)
Nonce Size: 96 bits (12 bytes)
Authentication Tag: 128 bits (16 bytes)
Padding: Not required (GCM is a stream cipher mode)
Security Properties¶
- Nonce
12-byte random nonce generated for each encryption operation using
os.urandom(12)Ensures identical plaintexts produce different ciphertexts
- Authentication
Built-in authentication prevents tampering
128-bit authentication tag provides strong integrity protection
Eliminates padding oracle attacks
- Key Management
256-bit DEK provides strong cryptographic security
KEK stored in HSM prevents key extraction
AES-ECB encryption used for DEK wrapping (8-byte IV prepended for format consistency)
Field Encryption/Decryption Workflow¶
Error Handling and Recovery¶
Corrupted DEK¶
If the wrapped DEK becomes corrupted:
The system detects invalid wrapped data during unwrapping
Error messages indicate potential data corruption
Manual DEK regeneration using
generate_and_wrap_dek()will be requiredWarning: Regenerating the DEK will make all previously encrypted data unrecoverable
Key Rotation¶
Currently, key rotation is not implemented. Future versions may include:
Automated KEK rotation with dual-key support
DEK re-wrapping with new KEKs
Gradual field re-encryption with new DEKs