Overview
TheCryptoFunctionService provides low-level cryptographic primitives for encryption, decryption, hashing, and key generation operations. This service is the foundation of Bitwarden’s cryptographic operations.
Location
Interface
Password-Based Key Derivation
pbkdf2()
Derives a key from a password using PBKDF2 (Password-Based Key Derivation Function 2).
password- The password to derive a key fromsalt- Salt value to use in derivationalgorithm- Hash algorithm ("sha256"or"sha512")iterations- Number of iterations
Promise<Uint8Array> - The derived key
Key Derivation Functions
hkdf()
Performs HKDF (HMAC-based Key Derivation Function) key derivation.
ikm- Input keying materialsalt- Salt valueinfo- Context and application specific informationoutputByteSize- Desired output length in bytesalgorithm- Hash algorithm ("sha256"or"sha512")
Promise<Uint8Array> - The derived key
hkdfExpand()
Performs the expand step of HKDF.
prk- Pseudorandom key from HKDF extract stepinfo- Context and application specific informationoutputByteSize- Desired output length in bytesalgorithm- Hash algorithm
Promise<Uint8Array> - The expanded key
Hashing
hash()
Generates a cryptographic hash of the input value.
value- The value to hashalgorithm- Hash algorithm to use
Promise<Uint8Array> - The hash output
Supported Algorithms:
sha1- SHA-1 (deprecated, avoid for new code)sha256- SHA-256sha512- SHA-512md5- MD5 (deprecated, avoid for new code)
hmacFast()
Computes HMAC (Hash-based Message Authentication Code) for a value.
value- The value to compute HMAC forkey- The HMAC keyalgorithm- Hash algorithm
Promise<Uint8Array | string> - The HMAC result
Comparison
compareFast()
Performs constant-time comparison of two values to prevent timing attacks.
a- First value to compareb- Second value to compare
Promise<boolean> - true if values are equal, false otherwise
AES Encryption/Decryption
aesDecryptFastParameters()
Prepares parameters for fast AES decryption.
data- Encrypted data (base64)iv- Initialization vector (base64)mac- Message authentication code (base64)key- Symmetric crypto key
CbcDecryptParameters<Uint8Array | string> - Decryption parameters
aesDecryptFast()
Performs fast AES decryption.
mode- Cipher mode ("cbc"or"ecb")parameters- Mode-specific decryption parameters
Promise<string> - Decrypted plaintext
aesDecrypt()
Performs AES decryption.
data- Encrypted dataiv- Initialization vectorkey- Encryption keymode- Cipher mode
Promise<Uint8Array> - Decrypted data
RSA Operations
rsaEncrypt()
Encrypts data using RSA with OAEP padding.
data- Data to encryptpublicKey- RSA public keyalgorithm- Hash algorithm for OAEP ("sha1")
Promise<Uint8Array> - Encrypted data
rsaDecrypt()
Decrypts RSA-encrypted data.
data- Encrypted dataprivateKey- RSA private keyalgorithm- Hash algorithm for OAEP
Promise<Uint8Array> - Decrypted data
rsaExtractPublicKey()
Extracts the public key from an RSA private key.
privateKey- RSA private key
Promise<Uint8Array> - Extracted public key
rsaGenerateKeyPair()
Generates an RSA key pair.
length- Key length in bits (must be2048)
Promise<[Uint8Array, Uint8Array]> - Tuple of [publicKey, privateKey]
Key Generation
aesGenerateKey()
Generates a cryptographically secure random AES key.
bitLength- Key length in bits
Promise<CsprngArray> - The generated key suitable for AES encryption
Supported Key Lengths:
128- AES-128192- AES-192256- AES-256512- AES-256 with HMAC-SHA256 (64 bytes total)
randomBytes()
Generates cryptographically secure random bytes.
length- Number of bytes to generate
Promise<CsprngArray> - Random bytes from a cryptographically secure random number generator
Do not use this for generating encryption keys. Use
aesGenerateKey() or rsaGenerateKeyPair() instead.Usage Example
Related Services
- EncryptService - High-level encryption/decryption operations
- KeyService - Key management and generation
Security Considerations
- Deprecation Warnings: Most methods are deprecated for direct use. Always consult the Key Management team before using low-level crypto functions.
- Use SDK Methods: New cryptographic features should be implemented in the SDK rather than using these low-level primitives directly.
-
Algorithm Selection:
- Prefer SHA-256 or SHA-512 over SHA-1 or MD5
- Use appropriate key sizes (AES-256 recommended)
-
Constant-Time Operations: Use
compareFast()when comparing secrets to prevent timing attacks. -
Random Number Generation: Always use
aesGenerateKey()orrandomBytes()for cryptographic operations, never useMath.random().