Skip to main content

Module crypto

Module crypto 

Source
Expand description

The unlock pipeline and the encryption primitives, on bitwarden-crypto.

The shapes here are the ones the live spike proved against Vaultwarden 1.37.4: the wrapped user key and private key decrypt to bytes (the private key is PKCS#8 DER, not PEM), the organization key and the RSA-wrapped attachment keys unwrap with RSA-OAEP-SHA1, and an attachment is the 0x02 | IV | MAC | ciphertext buffer the official clients upload.

Re-exportsΒ§

use std::collections::HashMap; πŸ”’
use std::num::NonZeroU32; πŸ”’
use anyhow::Context; πŸ”’
use anyhow::Result; πŸ”’
use anyhow::anyhow; πŸ”’
use anyhow::bail; πŸ”’
use base64::Engine; πŸ”’
use bitwarden_crypto::BitwardenLegacyKeyBytes; πŸ”’
use bitwarden_crypto::EncString; πŸ”’
use bitwarden_crypto::HashPurpose; πŸ”’
use bitwarden_crypto::Kdf; πŸ”’
use bitwarden_crypto::KeyDecryptable; πŸ”’
use bitwarden_crypto::KeyEncryptable; πŸ”’
use bitwarden_crypto::MasterKey; πŸ”’
use bitwarden_crypto::OctetStreamBytes; πŸ”’
use hmac::Hmac; πŸ”’
use hmac::KeyInit; πŸ”’
use hmac::Mac; πŸ”’
use rsa::pkcs8::DecodePrivateKey; πŸ”’
use sha2::Digest; πŸ”’
use sha2::Sha256; πŸ”’
use zeroize::Zeroizing; πŸ”’
use crate::vault::VaultError; πŸ”’
use crate::vault::models::Prelogin; πŸ”’
use crate::vault::models::Profile; πŸ”’
use crate::vault::models::TokenResponse; πŸ”’

StructsΒ§

Totp
One computed TOTP code and the window it belongs to.
Unlocked
An unlocked account’s keys: the user key every personal cipher inherits, the account private key, and any organization keys the profile carries.

EnumsΒ§

SymmetricCryptoKey
The key type an Unlocked holds, re-exported so a caller building one for a test can name it. A symmetric encryption key. Used to encrypt and decrypt EncString

FunctionsΒ§

cipher_key
A cipher’s own key, when the item carries one, else the user/org key it inherits.
decode_base32 πŸ”’
decrypt_attachment_blob
Decrypt a downloaded attachment blob.
decrypt_bytes
decrypt_str
derive_master_key
Derive the master key from the password and the configured email.
enc_string_type
The EncString type byte of a string, without decoding it: "2...." gives Some(2).
encrypt_attachment_blob
Encrypt an attachment into the uploaded wire form: 0x02 | IV | MAC | ciphertext, identical to EncString::to_buffer.
encrypt_str
kdf_from_prelogin
The KDF a prelogin answer names. An unknown type is an error rather than a guess, because guessing the KDF derives the wrong key.
kdf_label
The KDF in one line, for a log line or a refusal message.
make_symmetric_key
A fresh symmetric key for one attachment, in the algorithm the live server stores its blobs with.
master_password_hash
The hash the token endpoint takes as password.
org_keys πŸ”’
Unwrap every organization key the profile carries, naming the ones that failed. A failed org is not fatal to the unlock - the account is still usable - but its ciphers must not be touched with another key.
parse_private_key πŸ”’
The account private key is stored as raw PKCS#8 DER by the Bitwarden clients; older exports and other clients may hold PEM or base64 DER, so every shape is tried.
rsa_oaep_unwrap πŸ”’
sha256_hex
totp_from_uri
Compute a TOTP code from the stored login.totp value: an otpauth URI, or a bare base32 secret (Bitwarden accepts both). The seed never leaves the process.
unlock
Unwrap the user key, the account private key and the organization keys.
unlock_with_user_key
Unlock from a user key a client released through its platform authenticator, rather than one derived from the master password.
unwrap_attachment_key
Unwrap an attachment key by its EncString type.
unwrap_org_key
Unwrap an organization key: EncString type 4, RSA-2048-OAEP-SHA1 with the user’s private key. Type 6 is named rather than guessed at.
unwrap_symmetric_key
verify_type6_mac πŸ”’
wire_from_enc_string πŸ”’
with_user_key πŸ”’
The half of an unlock that starts from a known user key.
wrap_symmetric_key
wrapped_user_key πŸ”’
The wrapped user key this unlock will report the fingerprint of. An empty string is a field a client left unset, not a wrapped key; it must not shadow the profile’s real one.