Documentation Index
Fetch the complete documentation index at: https://mintlify.com/bitwarden/clients/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheKeyService manages all cryptographic keys in Bitwarden, including user keys, organization keys, provider keys, and key pairs. It handles key generation, derivation, storage, and rotation.
Location
Interface
User Key Management
userKey$()
Returns an observable stream of the user’s encryption key.
userId- The user ID
Observable<UserKey | null> - Stream of user keys (null if user is locked/logged out)
Example:
getUserKey()
Retrieves the user’s encryption key.
userId- Optional user ID
Promise<UserKey | null> - The user key or null
setUserKey()
Sets the user key and stores any additional versions (auto, biometrics, pin).
key- The user key to setuserId- The user ID
Error when key or userId is null. Lock the account to clear a key.
hasUserKey()
Checks if a user key is available in memory.
userId- The user ID
Promise<boolean> - True if user key is available
clearStoredUserKey()
Clears the stored user key from storage.
userId- The user ID
Error when userId is null or undefined
Key Generation
makeUserKey()
Generates a new user key and encrypts it with the master key.
masterKey- The user’s master key
Promise<[UserKey, EncString]> - Tuple of new user key and encrypted version
Throws: Error when master key is null or undefined
makeMasterKey()
Derives a master key from password using KDF.
password- The user’s master passwordemail- The user’s email addresskdfConfig- Key derivation function configuration
Promise<MasterKey> - Derived master key
makeCipherKey()
Generates a new cipher encryption key.
Promise<CipherKey> - A new cipher key
Example:
makeDataEncKey()
Generates a new data encryption key and wraps it with the provided key.
key- User key or organization key to wrap with
Promise<[SymmetricCryptoKey, EncString]> - New key and wrapped version
makeSendKey()
Creates a Send encryption key from key material.
keyMaterial- Key material to derive from
Promise<SymmetricCryptoKey> - Send key
Asymmetric Key Operations
makeKeyPair()
Generates a new RSA key pair.
key- Symmetric key to wrap the private key with
Promise<[string, EncString]> - Tuple of [publicKey, wrappedPrivateKey]
Throws: Error if the provided key is null
makeOrgKey()
Creates a new organization key encrypted with the user’s public key.
userId- User ID for public key lookup
Promise<[EncString, T]> - Encrypted org/provider key and decrypted key
Throws:
Errorwhen userId is null or undefinedErrorwhen no public key is found for the user
userPrivateKey$()
Returns an observable of the user’s decrypted private key.
userId- The user ID
Observable<UserPrivateKey | null> - Stream of private keys
userPublicKey$()
Returns an observable of the user’s public key.
userId- The user ID
Observable<Uint8Array | null> - Stream of public keys
userEncryptionKeyPair$()
Returns an observable of the user’s key pair (guaranteed to be consistent).
userId- The user ID
Observable<{privateKey, publicKey} | null> - Key pair or null
getFingerprint()
Generates a fingerprint phrase for a public key.
fingerprintMaterial- Material to include in fingerprintpublicKey- The public key
Promise<string[]> - Array of fingerprint words
Throws: Error when publicKey is null or undefined
Master Key Operations
getOrDeriveMasterKey()
Retrieves or derives the master key from a password.
password- The master passworduserId- The user ID
Promise<MasterKey> - The master key
Throws:
Errorwhen userId is null/undefinedErrorwhen email or KDF config cannot be found
encryptUserKeyWithMasterKey()
Encrypts the user key with the master key.
masterKey- The master keyuserKey- The user key to encrypt
Promise<[UserKey, EncString]> - User key and encrypted version
Throws: Error when userKey or masterKey is null/undefined
hashMasterKey()
Creates a master password hash for authentication.
password- The master passwordkey- The master keyhashPurpose- Hash purpose (defaults toHashPurpose.ServerAuthorization)
Promise<string> - Password hash
Hash Purposes:
Organization Key Management
setOrgKeys()
Stores encrypted organization keys.
orgs- Organization dataproviderOrgs- Provider organization datauserId- The user ID
getOrgKey()
Retrieves an organization’s symmetric key.
orgId- The organization ID
Promise<OrgKey | null> - The organization key
Throws: Error when not active user
orgKeys$()
Returns an observable of all organization keys for a user.
userId- The user ID
Observable<Record<OrganizationId, OrgKey> | null> - Map of org IDs to keys
Throws: Error if an invalid user ID is passed
Provider Key Management
setProviderKeys()
Stores provider keys for a user.
providers- Provider organization datauserId- The user ID
providerKeys$()
Returns an observable of provider keys.
userId- The user ID
Observable<Record<ProviderId, ProviderKey> | null> - Map of provider IDs to keys
Throws: Error if an invalid user ID is passed
Cipher Decryption Keys
cipherDecryptionKeys$()
Returns all keys needed for decrypting ciphers.
userId- The user IDlegacySupport- Support legacy key format (default:false)
Observable<CipherDecryptionKeys | null> - Decryption keys
Types:
Error if an invalid user ID is passed
Account Initialization
initAccount()
Initializes all necessary crypto keys for a new account.
userId- The user ID
Promise<{userKey, publicKey, privateKey}> - Newly created keys
Throws:
Errorif userId is null or undefinedErrorif user already has a user key
Key Validation
validateUserKey()
Validates that a user key is correct for a given user.
key- The key to validateuserId- The user ID
Promise<boolean> - True if key is valid
Cleanup
clearKeys()
Clears all of the user’s keys from storage.
userId- The user ID
Error when userId is null or undefined
Key Types
All key types are branded types built on top ofSymmetricCryptoKey or Uint8Array:
libs/common/src/types/key.ts
KDF Configuration
Key Derivation Function (KDF) configurations control how master keys are derived from passwords.KdfConfig
Union type for KDF configurations:
PBKDF2KdfConfig
PBKDF2-SHA256 configuration:
Argon2KdfConfig
Argon2id configuration:
KdfType
libs/key-management/src/models/kdf-config.ts
Usage Examples
Generating and Setting User Key
Accessing User Keys
Deriving Master Key
Creating Organization Key
Getting Cipher Decryption Keys
Generating Fingerprint
Related Services
- EncryptService - Encryption/decryption operations
- CryptoFunctionService - Low-level crypto primitives
Security Considerations
-
Master Key Deprecation: Direct interaction with master keys is deprecated. Use
MasterPasswordServicefor high-level operations. - Key Storage: User keys should be cleared from memory when the user locks their vault.
-
KDF Configuration: Use appropriate KDF parameters:
- PBKDF2: Minimum 600,000 iterations
- Argon2id: Minimum 2 iterations, 16 MB memory, 1 parallelism
-
Key Validation: Always validate keys before use with
validateUserKey(). - Observable Cleanup: Subscribe to key observables carefully to avoid memory leaks.
- SDK Migration: New cryptographic features should use SDK methods instead of these low-level functions.
- Error Handling: Many methods throw errors. Always wrap in try-catch blocks.
-
User Context: Most operations require a
UserId. Never use keys from one user for another user’s data.