Skip to content

Commit

Permalink
Support multiple client channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Fabritius committed Sep 22, 2023
1 parent 5b14888 commit f60da12
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 117 deletions.
16 changes: 11 additions & 5 deletions examples/linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use heimlig::client::api::Api;
use heimlig::common::jobs::{Request, RequestType, Response};
use heimlig::crypto::rng;
use heimlig::crypto::rng::Rng;
use heimlig::hsm::core::Core;
use heimlig::hsm::core::{Builder, Core};
use heimlig::hsm::workers::rng_worker::RngWorker;
use heimlig::integration::embassy::{
RequestQueueSink, RequestQueueSource, ResponseQueueSink, ResponseQueueSource,
Expand Down Expand Up @@ -124,7 +124,11 @@ async fn client_task(
None => Timer::after(Duration::from_millis(10)).await, // Continue waiting for response
Some(response) => {
match response {
Response::GetRandom { request_id, data } => {
Response::GetRandom {
client_id: _client_id,
request_id,
data,
} => {
info!(target: "CLIENT",
"<-- response: random data (id={request_id}) (size={}): {}",
data.len(),
Expand Down Expand Up @@ -173,14 +177,16 @@ async fn main(spawner: Spawner) {
.try_lock()
.expect("Failed to lock RNG_WORKER")
.replace(Some(rng_worker));
let mut core: Core<
let core: Core<
CriticalSectionRawMutex,
RequestQueueSource<'_, '_, CriticalSectionRawMutex, QUEUE_SIZE>,
ResponseQueueSink<'_, '_, CriticalSectionRawMutex, QUEUE_SIZE>,
RequestQueueSink<'_, '_, CriticalSectionRawMutex, QUEUE_SIZE>,
ResponseQueueSource<'_, '_, CriticalSectionRawMutex, QUEUE_SIZE>,
> = Core::new(None, client_requests, client_responses);
core.add_worker_channel(&[RequestType::GetRandom], rng_requests_tx, rng_responses_rx);
> = Builder::new()
.with_client(client_requests, client_responses)
.with_worker(&[RequestType::GetRandom], rng_requests_tx, rng_responses_rx)
.build();
CORE.try_lock()
.expect("Failed to lock CORE")
.replace(Some(core));
Expand Down
14 changes: 10 additions & 4 deletions examples/stm32h745i/cm7/src/bin/rng_single_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use heapless::spsc::{Consumer, Producer, Queue};
use heimlig::client::api::Api;
use heimlig::common::jobs::{Request, RequestType, Response};
use heimlig::crypto::rng;
use heimlig::hsm::core::Core;
use heimlig::hsm::core::{Builder, Core};
use heimlig::hsm::workers::rng_worker::RngWorker;
use heimlig::integration::embassy::{
RequestQueueSink, RequestQueueSource, ResponseQueueSink, ResponseQueueSource,
Expand Down Expand Up @@ -87,8 +87,10 @@ async fn hsm_task(
ResponseQueueSink<'_, '_, NoopRawMutex, QUEUE_SIZE>,
RequestQueueSink<'_, '_, NoopRawMutex, QUEUE_SIZE>,
ResponseQueueSource<'_, '_, NoopRawMutex, QUEUE_SIZE>,
> = Core::new(None, client_requests, client_responses);
core.add_worker_channel(&[RequestType::GetRandom], rng_requests_tx, rng_responses_rx);
> = Builder::new()
.with_client(client_requests, client_responses)
.with_worker(&[RequestType::GetRandom], rng_requests_tx, rng_responses_rx)
.build();

loop {
core.execute().await.expect("failed to forward request");
Expand Down Expand Up @@ -155,7 +157,11 @@ async fn client_task(
loop {
if let Some(response) = api.recv_response().await {
match response {
Response::GetRandom { request_id, data } => {
Response::GetRandom {
client_id: _,
request_id,
data,
} => {
info!(
"<-- response: random data (id={}) (size={}): {}",
request_id,
Expand Down
11 changes: 10 additions & 1 deletion heimlig/src/client/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl<
pub async fn get_random(&mut self, output: &'data mut [u8]) -> Result<RequestId, Error> {
let request_id = self.next_request_id();
self.requests
.send(Request::GetRandom { request_id, output })
.send(Request::GetRandom {
client_id: 0,
request_id,
output,
})
.await
.map_err(|_e| Error::Send)?;
Ok(request_id)
Expand All @@ -57,6 +61,7 @@ impl<
let request_id = self.next_request_id();
self.requests
.send(Request::ImportKey {
client_id: 0,
request_id,
key_id,
data,
Expand All @@ -80,6 +85,7 @@ impl<
SymmetricEncryptionAlgorithm::ChaCha20Poly1305 => self
.requests
.send(Request::EncryptChaChaPoly {
client_id: 0,
request_id,
key_id,
nonce,
Expand Down Expand Up @@ -107,6 +113,7 @@ impl<
SymmetricEncryptionAlgorithm::ChaCha20Poly1305 => self
.requests
.send(Request::EncryptChaChaPolyExternalKey {
client_id: 0,
request_id,
key,
nonce,
Expand Down Expand Up @@ -134,6 +141,7 @@ impl<
SymmetricEncryptionAlgorithm::ChaCha20Poly1305 => self
.requests
.send(Request::DecryptChaChaPoly {
client_id: 0,
request_id,
key_id,
nonce,
Expand Down Expand Up @@ -161,6 +169,7 @@ impl<
SymmetricEncryptionAlgorithm::ChaCha20Poly1305 => self
.requests
.send(Request::DecryptChaChaPolyExternalKey {
client_id: 0,
request_id,
key,
nonce,
Expand Down
50 changes: 50 additions & 0 deletions heimlig/src/common/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub enum Error {
Send,
}

/// Used to distinguish multiple clients
pub type ClientId = u32;

/// Used to match requests and responses
pub type RequestId = u32;

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
Expand All @@ -31,15 +35,18 @@ pub enum RequestType {
#[derive(Eq, PartialEq, Debug)]
pub enum Request<'a> {
ImportKey {
client_id: ClientId,
request_id: RequestId,
key_id: KeyId,
data: &'a [u8],
},
GetRandom {
client_id: ClientId,
request_id: RequestId,
output: &'a mut [u8],
},
EncryptChaChaPoly {
client_id: ClientId,
request_id: RequestId,
key_id: KeyId,
nonce: &'a [u8],
Expand All @@ -48,6 +55,7 @@ pub enum Request<'a> {
tag: &'a mut [u8],
},
EncryptChaChaPolyExternalKey {
client_id: ClientId,
request_id: RequestId,
key: &'a [u8],
nonce: &'a [u8],
Expand All @@ -56,6 +64,7 @@ pub enum Request<'a> {
tag: &'a mut [u8],
},
DecryptChaChaPoly {
client_id: ClientId,
request_id: RequestId,
key_id: KeyId,
nonce: &'a [u8],
Expand All @@ -64,6 +73,7 @@ pub enum Request<'a> {
tag: &'a [u8],
},
DecryptChaChaPolyExternalKey {
client_id: ClientId,
request_id: RequestId,
key: &'a [u8],
nonce: &'a [u8],
Expand All @@ -88,29 +98,69 @@ impl<'data> Request<'data> {
}
}
}

pub fn set_client_id(&mut self, new_client_id: ClientId) {
match self {
Request::ImportKey {
ref mut client_id, ..
} => *client_id = new_client_id,
Request::GetRandom {
ref mut client_id, ..
} => *client_id = new_client_id,
Request::EncryptChaChaPoly {
ref mut client_id, ..
} => *client_id = new_client_id,
Request::EncryptChaChaPolyExternalKey {
ref mut client_id, ..
} => *client_id = new_client_id,
Request::DecryptChaChaPoly {
ref mut client_id, ..
} => *client_id = new_client_id,
Request::DecryptChaChaPolyExternalKey {
ref mut client_id, ..
} => *client_id = new_client_id,
}
}
}

/// A response from the HSM containing the results of a cryptographic task.
#[derive(Eq, PartialEq, Debug)]
pub enum Response<'a> {
ImportKey {
client_id: ClientId,
request_id: RequestId,
},
Error {
client_id: ClientId,
request_id: RequestId,
error: Error,
},
GetRandom {
client_id: ClientId,
request_id: RequestId,
data: &'a mut [u8],
},
EncryptChaChaPoly {
client_id: ClientId,
request_id: RequestId,
ciphertext: &'a mut [u8],
tag: &'a mut [u8],
},
DecryptChaChaPoly {
client_id: ClientId,
request_id: RequestId,
plaintext: &'a mut [u8],
},
}

impl<'data> Response<'data> {
pub fn get_client_id(&self) -> ClientId {
*match self {
Response::ImportKey { client_id, .. } => client_id,
Response::Error { client_id, .. } => client_id,
Response::GetRandom { client_id, .. } => client_id,
Response::EncryptChaChaPoly { client_id, .. } => client_id,
Response::DecryptChaChaPoly { client_id, .. } => client_id,
}
}
}
Loading

0 comments on commit f60da12

Please sign in to comment.