Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge upstream changes #37

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
by default).
- Change store implementations to use littlefs2’s `DynFilesystem` trait instead
of being generic over the storage implementation.
- Add `nonce` argument to `wrap_key` and `unwrap_key` syscalls.
- Use nonce as IV for Aes256Cbc mechanism.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ pub mod request {
- wrapping_key: KeyId
- wrapped_key: Message
- associated_data: Message
- nonce: ShortData
- attributes: StorageAttributes

Verify:
Expand All @@ -327,6 +328,7 @@ pub mod request {
- wrapping_key: KeyId
- key: KeyId
- associated_data: ShortData
- nonce: Option<ShortData>

RequestUserConsent:
- level: consent::Level
Expand Down
6 changes: 6 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub trait PollClient {
}
}

#[must_use = "Syscalls must be polled with the `syscall` macro"]
pub struct FutureResult<'c, T, C: ?Sized>
where
C: PollClient,
Expand Down Expand Up @@ -536,15 +537,18 @@ pub trait CryptoClient: PollClient {
wrapping_key: KeyId,
wrapped_key: Message,
associated_data: &[u8],
nonce: &[u8],
attributes: StorageAttributes,
) -> ClientResult<'c, reply::UnwrapKey, Self> {
let associated_data =
Message::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
let nonce = ShortData::from_slice(nonce).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::UnwrapKey {
mechanism,
wrapping_key,
wrapped_key,
associated_data,
nonce,
attributes,
})
}
Expand All @@ -555,6 +559,7 @@ pub trait CryptoClient: PollClient {
wrapping_key: KeyId,
key: KeyId,
associated_data: &[u8],
nonce: Option<ShortData>,
) -> ClientResult<'_, reply::WrapKey, Self> {
let associated_data =
Bytes::from_slice(associated_data).map_err(|_| ClientError::DataTooLarge)?;
Expand All @@ -563,6 +568,7 @@ pub trait CryptoClient: PollClient {
wrapping_key,
key,
associated_data,
nonce,
})
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/client/mechanisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ pub trait Aes256Cbc: CryptoClient {
&'c mut self,
key: KeyId,
message: &[u8],
iv: &[u8],
) -> ClientResult<'c, reply::Decrypt, Self> {
self.decrypt(Mechanism::Aes256Cbc, key, message, &[], &[], &[])
self.decrypt(Mechanism::Aes256Cbc, key, message, &[], iv, &[])
}

fn wrap_key_aes256cbc(
&mut self,
wrapping_key: KeyId,
key: KeyId,
iv: Option<&[u8; 16]>,
) -> ClientResult<'_, reply::WrapKey, Self> {
self.wrap_key(Mechanism::Aes256Cbc, wrapping_key, key, &[])
self.wrap_key(
Mechanism::Aes256Cbc,
wrapping_key,
key,
&[],
iv.and_then(|iv| ShortData::from_slice(iv).ok()),
)
}
}

Expand Down Expand Up @@ -81,6 +89,7 @@ pub trait Chacha8Poly1305: CryptoClient {
wrapping_key,
Message::from_slice(wrapped_key).map_err(|_| ClientError::DataTooLarge)?,
associated_data,
&[],
StorageAttributes::new().set_persistence(location),
)
}
Expand All @@ -90,12 +99,14 @@ pub trait Chacha8Poly1305: CryptoClient {
wrapping_key: KeyId,
key: KeyId,
associated_data: &[u8],
nonce: Option<&[u8; 12]>,
) -> ClientResult<'c, reply::WrapKey, Self> {
self.wrap_key(
Mechanism::Chacha8Poly1305,
wrapping_key,
key,
associated_data,
nonce.and_then(|nonce| ShortData::from_slice(nonce).ok()),
)
}
}
Expand Down
25 changes: 20 additions & 5 deletions src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ impl Encrypt for super::Aes256Cbc {
.try_into()
.map_err(|_| Error::InternalError)?;

let zero_iv = [0u8; 16];
let cipher = Aes256CbcEnc::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let iv = if let Some(nonce) = &request.nonce {
nonce
.as_slice()
.try_into()
.map_err(|_| Error::MechanismParamInvalid)?
} else {
[0u8; 16]
};
let cipher = Aes256CbcEnc::new_from_slices(&symmetric_key, &iv).unwrap();

// buffer must have enough space for message+padding
let mut buffer = request.message.clone();
Expand Down Expand Up @@ -83,7 +90,7 @@ impl WrapKey for super::Aes256Cbc {
key: request.wrapping_key,
message,
associated_data: request.associated_data.clone(),
nonce: None,
nonce: request.nonce.clone(),
};
let encryption_reply = <super::Aes256Cbc>::encrypt(keystore, &encryption_request)?;

Expand Down Expand Up @@ -117,8 +124,16 @@ impl Decrypt for super::Aes256Cbc {
.try_into()
.map_err(|_| Error::InternalError)?;

let zero_iv = [0u8; 16];
let cipher = Aes256CbcDec::new_from_slices(&symmetric_key, &zero_iv).unwrap();
let iv = if request.nonce.is_empty() {
[0u8; 16]
} else {
request
.nonce
.as_slice()
.try_into()
.map_err(|_| Error::MechanismParamInvalid)?
};
let cipher = Aes256CbcDec::new_from_slices(&symmetric_key, &iv).unwrap();

// buffer must have enough space for message+padding
let mut buffer = request.message.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/mechanisms/chacha8poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl WrapKey for super::Chacha8Poly1305 {
key: request.wrapping_key,
message,
associated_data: request.associated_data.clone(),
nonce: None,
nonce: request.nonce.clone(),
};
let encryption_reply = <super::Chacha8Poly1305>::encrypt(keystore, &encryption_request)?;

Expand Down
6 changes: 1 addition & 5 deletions src/mechanisms/hmacblake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ impl DeriveKey for super::HmacBlake2s {
if let Some(additional_data) = &request.additional_data {
mac.update(additional_data);
}
let derived_key: [u8; 32] = mac
.finalize()
.into_bytes()
.try_into()
.map_err(|_| Error::InternalError)?;
let derived_key: [u8; 32] = mac.finalize().into_bytes().into();
let key = keystore.store_key(
request.attributes.persistence,
key::Secrecy::Secret,
Expand Down
6 changes: 1 addition & 5 deletions src/mechanisms/hmacsha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ impl DeriveKey for super::HmacSha1 {
if let Some(additional_data) = &request.additional_data {
mac.update(additional_data);
}
let derived_key: [u8; 20] = mac
.finalize()
.into_bytes()
.try_into()
.map_err(|_| Error::InternalError)?;
let derived_key: [u8; 20] = mac.finalize().into_bytes().into();
let key_id = keystore.store_key(
request.attributes.persistence,
key::Secrecy::Secret,
Expand Down
6 changes: 1 addition & 5 deletions src/mechanisms/hmacsha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ impl DeriveKey for super::HmacSha256 {
if let Some(additional_data) = &request.additional_data {
mac.update(additional_data);
}
let derived_key: [u8; 32] = mac
.finalize()
.into_bytes()
.try_into()
.map_err(|_| Error::InternalError)?;
let derived_key: [u8; 32] = mac.finalize().into_bytes().into();
let key_id = keystore.store_key(
request.attributes.persistence,
key::Secrecy::Secret,
Expand Down
4 changes: 2 additions & 2 deletions src/mechanisms/hmacsha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ impl DeriveKey for super::HmacSha512 {
if let Some(additional_data) = &request.additional_data {
mac.update(additional_data);
}
let mut derived_key = [0u8; 64];
derived_key.copy_from_slice(&mac.finalize().into_bytes()); //.try_into().map_err(|_| Error::InternalError)?;
let derived_key: [u8; 64] = mac.finalize().into_bytes().into();

let key = keystore.store_key(
request.attributes.persistence,
key::Secrecy::Secret,
Expand Down
1 change: 1 addition & 0 deletions src/serde_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ where
/// A result returned by [`ExtensionClient`][] and clients using it.
pub type ExtensionResult<'a, E, T, C> = Result<ExtensionFutureResult<'a, E, T, C>, ClientError>;

#[must_use = "Syscalls must be polled with the `syscall` macro"]
/// A future of an [`ExtensionResult`][].
pub struct ExtensionFutureResult<'c, E, T, C: ?Sized> {
client: &'c mut C,
Expand Down
Loading
Loading