Skip to content

Commit

Permalink
Add nonce to wrap_key and unwrap_key syscalls
Browse files Browse the repository at this point in the history
This patch adds a nonce argument to the wrap_key and unwrap_key syscalls
to be able to use the Aes256Cbc mechanism with a non-zero IV in the
future.
  • Loading branch information
robin-nitrokey committed Mar 1, 2024
1 parent 2a2b209 commit 6492aba
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 3 deletions.
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)
}
}

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

0 comments on commit 6492aba

Please sign in to comment.