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

Add nonce to wrap_key and unwrap_key syscalls #148

Merged
merged 1 commit into from
Mar 1, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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.

### 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
5 changes: 5 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,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 @@ -556,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 @@ -564,6 +568,7 @@ pub trait CryptoClient: PollClient {
wrapping_key,
key,
associated_data,
nonce,
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/client/mechanisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait Aes256Cbc: CryptoClient {
wrapping_key: KeyId,
key: KeyId,
) -> ClientResult<'_, reply::WrapKey, Self> {
self.wrap_key(Mechanism::Aes256Cbc, wrapping_key, key, &[])
self.wrap_key(Mechanism::Aes256Cbc, wrapping_key, key, &[], None)
}
Comment on lines 19 to 21
Copy link
Contributor

@sosthene-nitrokey sosthene-nitrokey Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use None here but add it to client::wrap_key_chacha8poly1305 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because currently it does not have any effect for Aes256Cbc. I would implement that in a separate PR that then also updates the shortcut functions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put both in the same PR.

}

Expand Down Expand Up @@ -81,6 +81,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 +91,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
2 changes: 1 addition & 1 deletion src/mechanisms/aes256cbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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
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
Loading