Skip to content

Commit 92bb5c3

Browse files
authored
[PM-24377] Adds decrypt_user_key_with_master_key into PureCrypto (#465)
## 🎟️ Tracking https://bitwarden.atlassian.net/browse/PM-24377 ## 📔 Objective Create a new SDK method in `PureCrypto.decrypt_user_key_with_master_key`. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 1ca5a58 commit 92bb5c3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

crates/bitwarden-wasm-internal/src/pure_crypto.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,20 @@ impl PureCrypto {
323323
#[allow(deprecated)]
324324
dangerous_derive_kdf_material(password, salt, &kdf)
325325
}
326+
327+
pub fn decrypt_user_key_with_master_key(
328+
encrypted_user_key: String,
329+
master_key: Vec<u8>,
330+
) -> Result<Vec<u8>, CryptoError> {
331+
let master_key = &BitwardenLegacyKeyBytes::from(master_key);
332+
let master_key = &SymmetricCryptoKey::try_from(master_key)?;
333+
let master_key = MasterKey::try_from(master_key)?;
334+
let encrypted_user_key = EncString::from_str(&encrypted_user_key)?;
335+
let result = master_key
336+
.decrypt_user_key(encrypted_user_key)
337+
.map_err(|_| CryptoError::InvalidKey)?;
338+
Ok(result.to_encoded().to_vec())
339+
}
326340
}
327341

328342
#[cfg(test)]
@@ -665,4 +679,25 @@ DnqOsltgPomWZ7xVfMkm9niL2OA=
665679
let derived_key = PureCrypto::derive_kdf_material(password, email, kdf).unwrap();
666680
assert_eq!(derived_key, DERIVED_KDF_MATERIAL_ARGON2ID);
667681
}
682+
683+
#[test]
684+
fn test_decrypt_user_key_with_master_key() {
685+
let password = "test_password";
686+
let email = "test_email@example.com";
687+
let kdf = &Kdf::Argon2id {
688+
iterations: NonZero::try_from(3).unwrap(),
689+
memory: NonZero::try_from(64).unwrap(),
690+
parallelism: NonZero::try_from(4).unwrap(),
691+
};
692+
let master_key = MasterKey::derive(password, email, kdf).unwrap();
693+
let (user_key, encrypted_user_key) = master_key.make_user_key().unwrap();
694+
let master_key_bytes = master_key.to_base64().into_bytes();
695+
696+
let decrypted_user_key = PureCrypto::decrypt_user_key_with_master_key(
697+
encrypted_user_key.to_string(),
698+
master_key_bytes,
699+
)
700+
.unwrap();
701+
assert_eq!(user_key.0.to_encoded().to_vec(), decrypted_user_key);
702+
}
668703
}

0 commit comments

Comments
 (0)