Overview
The @bitwarden/key-management library provides comprehensive key management services including key generation, derivation, storage, and rotation. All cryptographic keys follow a strict lifecycle to ensure security.
Key Management Library
The key management library is located at libs/key-management/ and exports services through the @bitwarden/key-management module.
Core Services
KeyService
The primary service for all key operations:
Location: libs/key-management/src/key.service.ts:73
KdfConfigService
Manages Key Derivation Function configuration:
Location: libs/key-management/src/kdf-config.service.ts:27
Key Derivation
Master Key Derivation
The master key is derived from the user’s password using a KDF:
Inputs:
- Password (user’s master password)
- Email (normalized as salt)
- KDF configuration (PBKDF2 or Argon2id settings)
Output:
Master key derivation is intentionally slow (typically 600,000+ PBKDF2 iterations or Argon2id with 64MB memory) to resist brute-force attacks. Do not reduce iteration counts.
KDF Configurations
PBKDF2-SHA256
Parameters:
- Iterations: 600,000 to 2,000,000 (default: 600,000)
- Algorithm: SHA-256
- Salt: User’s email (normalized to lowercase)
Argon2id
Parameters:
- Iterations: 2 to 10 (default: 3)
- Memory: 16 to 1024 MiB (default: 64 MiB)
- Parallelism: 1 to 16 threads (default: 4)
Argon2id provides better resistance to GPU/ASIC attacks due to its memory-hard properties. It’s recommended over PBKDF2 for new accounts.
User Key Generation
The user key is randomly generated, not derived:
Process:
- Generate 512-bit random key using CSPRNG
- Encrypt user key with master key
- Return both plaintext user key and encrypted version
Location: libs/key-management/src/key.service.ts:187
Key Storage
In-Memory Storage
Active keys are stored in memory during user sessions:
Storage locations:
- Memory: Active user key (cleared on lock)
- State provider: User-specific encrypted state
- Additional keys: Auto-unlock keys, biometric keys
Persistent Storage
Encrypted keys can be stored for auto-unlock:
Auto-unlock storage:
- Only stored if vault timeout is “Never”
- Encrypted with platform-specific protection
- CLI always stores for auto-unlock
Location: libs/key-management/src/key.service.ts:550
Auto-unlock keys stored on disk reduce security. Only enable for trusted devices with full-disk encryption.
Key Lifecycle
Account Initialization
When creating a new account, all cryptographic keys are initialized:
Initialization steps:
- Generate 512-bit user key (CSPRNG)
- Generate 2048-bit RSA key pair
- Encrypt private key with user key
- Store user key in memory
- Store encrypted private key in state
Location: libs/key-management/src/key.service.ts:502
Key Rotation
User keys can be rotated without losing access to vault data:
Refresh process:
- Re-encrypts additional keys (auto-unlock, biometric)
- Maintains same user key
- Updates storage with new encryption
Key Clearing
All keys are cleared on logout or lock:
Clearing process:
- Clear master key hash
- Clear user key from memory
- Clear organization keys
- Clear provider keys
- Clear all cryptographic state
Location: libs/key-management/src/key.service.ts:448
Always use clearKeys() when the user locks or logs out. Never leave keys in memory after the session ends.
Organization Key Management
Creating Organization Keys
Process:
- Generate random 512-bit organization key
- Encrypt with user’s RSA public key (key encapsulation)
- Return encrypted and plaintext versions
Organization Key Distribution
Distribution:
- Each member gets org key encrypted with their public key
- Organization admins can add/remove member access
- Provider organizations use provider keys for indirect access
Location: libs/key-management/src/key.service.ts:310
Asymmetric Key Management
Key Pair Generation
Process:
- Generate 2048-bit RSA key pair
- Base64-encode public key
- Encrypt private key with user key
- Return public key (plain) and private key (encrypted)
Location: libs/key-management/src/key.service.ts:425
Private Key Storage
Private keys are always stored encrypted:
Access pattern:
- Retrieve encrypted private key from state
- Decrypt using current user key
- Return decrypted private key (or null if locked)
Key Validation
Keys are validated before use:
Validation checks:
- Key is not null
- Can decrypt user’s private key
- Can derive public key from private key
Location: libs/key-management/src/key.service.ts:462
Security Best Practices
Key Storage Security
Never store master keys or unencrypted user keys in persistent storage. These should only exist in memory during active sessions.
Storage guidelines:
- Master key: Never stored, always derived
- User key: Stored encrypted with master key
- Private key: Stored encrypted with user key
- Organization keys: Stored encrypted with user’s public key
Key Lifecycle Management
- Generation: Use CSPRNG for all key material
- Storage: Encrypt before persisting
- Usage: Decrypt only in memory
- Rotation: Support key updates without data loss
- Destruction: Clear keys from memory on lock/logout
KDF Configuration
- Use Argon2id for new accounts (better security)
- Never reduce PBKDF2 iterations below 600,000
- Validate KDF parameters before use
- Store KDF config securely with user account
References
libs/key-management/src/key.service.ts - Key service implementation
libs/key-management/src/kdf-config.service.ts - KDF configuration
libs/key-management/src/models/kdf-config.ts - KDF configuration models
libs/key-management/src/abstractions/key.service.ts - Key service interface