Skip to content

Commit

Permalink
Merge pull request #37 from trussed-dev/main
Browse files Browse the repository at this point in the history
Merge upstream changes
  • Loading branch information
robin-nitrokey authored Mar 4, 2024
2 parents 2583e09 + cff2e66 commit cb7bd32
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
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
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

0 comments on commit cb7bd32

Please sign in to comment.