Skip to content

Commit 690c6df

Browse files
committed
Cleanup and add tests
1 parent 25700ef commit 690c6df

File tree

7 files changed

+260
-99
lines changed

7 files changed

+260
-99
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"encryptable",
1616
"Hkdf",
1717
"Hmac",
18+
"keyslot",
1819
"Maybeable",
1920
"Oaep",
2021
"Pbkdf",

crates/bitwarden-crypto/examples/protect_key_with_password.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
//! [PasswordProtectedKeyEnvelope].
33
44
use bitwarden_crypto::{
5-
key_ids, KeyStore, KeyStoreContext, PasswordProtectedKeyEnvelope,
6-
PasswordProtectedKeyEnvelopeError,
5+
key_ids,
6+
safe::{PasswordProtectedKeyEnvelope, PasswordProtectedKeyEnvelopeError},
7+
KeyStore, KeyStoreContext,
78
};
89

910
fn main() {

crates/bitwarden-crypto/src/cose.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
66
use coset::{
77
iana::{self, CoapContentFormat},
8-
CborSerializable, ContentType, Label,
8+
CborSerializable, ContentType, Header, Label,
99
};
1010
use generic_array::GenericArray;
11+
use thiserror::Error;
1112
use typenum::U32;
1213

1314
use crate::{
@@ -227,6 +228,52 @@ pub trait CoseSerializable<T: CoseContentFormat + ConstContentFormat> {
227228
where
228229
Self: Sized;
229230
}
231+
232+
pub(crate) fn extract_integer(
233+
header: &Header,
234+
target_label: i64,
235+
value_name: &str,
236+
) -> Result<i128, CoseExtractError> {
237+
Ok(header
238+
.rest
239+
.iter()
240+
.find_map(|(label, value)| match (label, value) {
241+
(Label::Int(label_value), ciborium::Value::Integer(int_value))
242+
if *label_value == target_label =>
243+
{
244+
Some(*int_value)
245+
}
246+
_ => None,
247+
})
248+
.ok_or(CoseExtractError::MissingValue(value_name.to_string()))?
249+
.into())
250+
}
251+
252+
pub(crate) fn extract_bytes(
253+
header: &Header,
254+
target_label: i64,
255+
value_name: &str,
256+
) -> Result<Vec<u8>, CoseExtractError> {
257+
header
258+
.rest
259+
.iter()
260+
.find_map(|(label, value)| match (label, value) {
261+
(Label::Int(label_value), ciborium::Value::Bytes(byte_value))
262+
if *label_value == target_label =>
263+
{
264+
Some(byte_value.clone())
265+
}
266+
_ => None,
267+
})
268+
.ok_or(CoseExtractError::MissingValue(value_name.to_string()))
269+
}
270+
271+
#[derive(Debug, Error)]
272+
pub(crate) enum CoseExtractError {
273+
#[error("Missing value {0}")]
274+
MissingValue(String),
275+
}
276+
230277
#[cfg(test)]
231278
mod test {
232279
use super::*;

crates/bitwarden-crypto/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ pub use store::{
3636
};
3737
mod cose;
3838
pub use cose::CoseSerializable;
39-
mod safe;
40-
pub use safe::*;
39+
pub mod safe;
4140
mod signing;
4241
pub use signing::*;
4342
mod traits;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Bitwarden-crypto safe module
2+
3+
The safe module contains a high-level set of tools used in building protocols and features involving
4+
cryptography. Whenever possible, a feature should be build with features from this module, before
5+
opting to build with any other, more lower-level primitives in the `bitwarden-crypto` crate.
6+
7+
## Password-protected key envelope
8+
9+
The password protected key envelope should be used, when the goal is to protect a symmetric key with
10+
a password, for example for locking a vault with a PIN/Password, for protecting exports with a
11+
password, etc. Internally, a KDF is used to protect against brute-forcing, but this is not exposed
12+
to the consumer. The consumer only provides a password and key.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
#![doc = include_str!("./README.md")]
2+
13
mod password_protected_key_envelope;
24
pub use password_protected_key_envelope::*;

0 commit comments

Comments
 (0)