Skip to main content

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:
  • 256-bit master key
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:
  1. Generate 512-bit random key using CSPRNG
  2. Encrypt user key with master key
  3. 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:
  1. Generate 512-bit user key (CSPRNG)
  2. Generate 2048-bit RSA key pair
  3. Encrypt private key with user key
  4. Store user key in memory
  5. 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:
  1. Clear master key hash
  2. Clear user key from memory
  3. Clear organization keys
  4. Clear provider keys
  5. 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:
  1. Generate random 512-bit organization key
  2. Encrypt with user’s RSA public key (key encapsulation)
  3. 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:
  1. Generate 2048-bit RSA key pair
  2. Base64-encode public key
  3. Encrypt private key with user key
  4. 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:
  1. Retrieve encrypted private key from state
  2. Decrypt using current user key
  3. Return decrypted private key (or null if locked)

Key Validation

Keys are validated before use:
Validation checks:
  1. Key is not null
  2. Can decrypt user’s private key
  3. 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

  1. Generation: Use CSPRNG for all key material
  2. Storage: Encrypt before persisting
  3. Usage: Decrypt only in memory
  4. Rotation: Support key updates without data loss
  5. Destruction: Clear keys from memory on lock/logout

KDF Configuration

  1. Use Argon2id for new accounts (better security)
  2. Never reduce PBKDF2 iterations below 600,000
  3. Validate KDF parameters before use
  4. 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