Skip to content

Commit b913762

Browse files
committed
Cargo format
1 parent 616786e commit b913762

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

crates/bitwarden-crypto/examples/protect_key_with_password.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! This example demonstrates how to securely protect keys with a password using the [PasswordProtectedKeyEnvelope].
1+
//! This example demonstrates how to securely protect keys with a password using the
2+
//! [PasswordProtectedKeyEnvelope].
23
34
use bitwarden_crypto::{
45
key_ids, KeyStore, KeyStoreContext, PasswordProtectedKeyEnvelope,

crates/bitwarden-crypto/src/safe/password_protected_key_envelope.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ use crate::{
1616
KeyStoreContext, SymmetricCryptoKey,
1717
};
1818

19-
/// A password-protected key envelope can seal a symmetric key, and protect it with a password. It does so
20-
/// by using a Key Derivation Function (KDF), to increase the difficulty of brute-forcing the password.
19+
/// A password-protected key envelope can seal a symmetric key, and protect it with a password. It
20+
/// does so by using a Key Derivation Function (KDF), to increase the difficulty of brute-forcing
21+
/// the password.
2122
///
22-
/// The KDF parameters such as iterations and salt are stored in the key-envelope and do not have to be provided.
23+
/// The KDF parameters such as iterations and salt are stored in the key-envelope and do not have to
24+
/// be provided.
2325
pub struct PasswordProtectedKeyEnvelope<Ids: KeyIds> {
2426
_phantom: PhantomData<Ids>,
2527
cose_encrypt: coset::CoseEncrypt,
2628
}
2729

2830
impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
29-
/// Seals a symmetric key with a password, using the current default KDF parameters and a random salt.
31+
/// Seals a symmetric key with a password, using the current default KDF parameters and a random
32+
/// salt.
3033
///
3134
/// This should never fail, except for memory allocation error, when running the KDF.
3235
pub fn seal(
@@ -60,11 +63,12 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
6063
kdf_settings: &Argon2RawSettings,
6164
) -> Result<Self, PasswordProtectedKeyEnvelopeError> {
6265
// Cose does not yet have a standardized way to protect a key using a password.
63-
// This implements content encryption using direct encryption with a KDF derived key, similar to
64-
// "Direct Key with KDF". The KDF settings are placed in a single recipient struct.
66+
// This implements content encryption using direct encryption with a KDF derived key,
67+
// similar to "Direct Key with KDF". The KDF settings are placed in a single
68+
// recipient struct.
6569

66-
// The envelope key is directly derived from the KDF and used as the key to encrypt the key that should
67-
// be sealed.
70+
// The envelope key is directly derived from the KDF and used as the key to encrypt the key
71+
// that should be sealed.
6872
let envelope_key = derive_key(kdf_settings, password)
6973
.map_err(|_| PasswordProtectedKeyEnvelopeError::KdfError)?;
7074

@@ -100,7 +104,8 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
100104
})
101105
}
102106

103-
/// Unseals a symmetric key from the password-protected envelope, and stores it in the key store context.
107+
/// Unseals a symmetric key from the password-protected envelope, and stores it in the key store
108+
/// context.
104109
pub fn unseal(
105110
&self,
106111
target_keyslot: Ids::Symmetric,
@@ -117,7 +122,8 @@ impl<Ids: KeyIds> PasswordProtectedKeyEnvelope<Ids> {
117122
&self,
118123
password: &str,
119124
) -> Result<SymmetricCryptoKey, PasswordProtectedKeyEnvelopeError> {
120-
// There must be exactly one recipient in the COSE Encrypt object, which contains the KDF parameters.
125+
// There must be exactly one recipient in the COSE Encrypt object, which contains the KDF
126+
// parameters.
121127
if self.cose_encrypt.recipients.len() != 1 {
122128
return Err(PasswordProtectedKeyEnvelopeError::ParsingError(
123129
"Invalid number of recipients".to_string(),
@@ -209,8 +215,8 @@ impl<Ids: KeyIds> TryFrom<&Vec<u8>> for PasswordProtectedKeyEnvelope<Ids> {
209215
}
210216

211217
/// Raw argon2 settings differ from the KDF struct defined for existing master-password unlock.
212-
/// The memory is represented in kibibytes (KiB) instead of mebibytes (MiB), and the salt is a fixed size of 32 bytes,
213-
/// and randomly generated, instead of being derived from the email.
218+
/// The memory is represented in kibibytes (KiB) instead of mebibytes (MiB), and the salt is a fixed
219+
/// size of 32 bytes, and randomly generated, instead of being derived from the email.
214220
struct Argon2RawSettings {
215221
iterations: u32,
216222
memory: u32,
@@ -322,20 +328,20 @@ pub enum PasswordProtectedKeyEnvelopeError {
322328
/// The envelope could not be parsed correctly, or the KDF parameters are invalid
323329
#[error("Parsing error {0}")]
324330
ParsingError(String),
325-
/// The KDF failed to derive a key, possibly due to invalid parameters or memory allocation issues
331+
/// The KDF failed to derive a key, possibly due to invalid parameters or memory allocation
332+
/// issues
326333
#[error("Kdf error")]
327334
KdfError,
328335
}
329336

330337
#[cfg(test)]
331338
mod tests {
339+
use super::*;
332340
use crate::{
333341
traits::tests::{TestIds, TestSymmKey},
334342
KeyStore,
335343
};
336344

337-
use super::*;
338-
339345
#[test]
340346
fn test_make_envelope() {
341347
let key_store = KeyStore::<TestIds>::default();

0 commit comments

Comments
 (0)