Skip to content

Commit

Permalink
Cleanup TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbert Fabritius committed Sep 13, 2023
1 parent 8009698 commit ae6e251
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 48 deletions.
4 changes: 2 additions & 2 deletions examples/stm32h745i/cm7/src/bin/rng_single_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a> RequestSink<'a> for RequestQueueSink<'_, 'a> {
fn send(&mut self, request: Request<'a>) -> Result<(), queues::Error> {
self.producer
.enqueue(request)
.map_err(|_| queues::Error::QueueFull)
.map_err(|_| queues::Error::Enqueue)
}

fn ready(&self) -> bool {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl<'a> ResponseSink<'a> for ResponseQueueSink<'_, 'a> {
fn send(&mut self, response: Response<'a>) -> Result<(), queues::Error> {
self.producer
.enqueue(response)
.map_err(|_| queues::Error::QueueFull)
.map_err(|_| queues::Error::Enqueue)
}
fn ready(&self) -> bool {
self.producer.ready()
Expand Down
33 changes: 15 additions & 18 deletions heimlig/src/client/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,37 @@ use crate::common::queues::RequestSink;

/// An interface to send [Request]s to the HSM core and receive [Response]es from it.
pub struct Api<'a, Req: RequestSink<'a>, Resp: Iterator<Item = Response<'a>>> {
requests_sink: Req,
responses_source: Resp,
requests: Req,
responses: Resp,
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum Error {
SinkNotReady,
Send(queues::Error),
Queue(queues::Error),
}

impl<'a, Req: RequestSink<'a>, Resp: Iterator<Item = Response<'a>>> Api<'a, Req, Resp> {
/// Create a new instance of the HSM API.
pub fn new(requests_sink: Req, responses_source: Resp) -> Self {
pub fn new(requests: Req, responses: Resp) -> Self {
Api {
requests_sink,
responses_source,
requests,
responses,
}
}

/// Request `size` many random bytes.
pub fn get_random(&mut self, output: &'a mut [u8]) -> Result<(), Error> {
if self.requests_sink.ready() {
self.requests_sink
.send(Request::GetRandom { output })
.map_err(Error::Send)?
} else {
Err(Error::SinkNotReady)?
if !self.requests.ready() {
return Err(Error::Queue(queues::Error::NotReady));
}
Ok(())
self.requests
.send(Request::GetRandom { output })
.map_err(Error::Queue)
}

/// Attempt to poll a response and return it.
pub fn recv_response(&mut self) -> Option<Response> {
self.responses_source.next()
self.responses.next()
}
}

Expand Down Expand Up @@ -90,15 +87,15 @@ mod test {
let (requests_tx, mut requests_rx) = requests.split();
let (mut responses_tx, responses_rx) = responses.split();

let requests_sink = RequestQueueSink {
let requests = RequestQueueSink {
producer: requests_tx,
};

let responses_source = ResponseQueueSource {
let responses = ResponseQueueSource {
consumer: responses_rx,
};

let mut api = Api::new(requests_sink, responses_source);
let mut api = Api::new(requests, responses);

// Send request
api.get_random(&mut random_output)
Expand Down
2 changes: 0 additions & 2 deletions heimlig/src/common/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ pub enum Error {
Crypto(crate::crypto::Error),
/// A key store error occurred.
KeyStore(keystore::Error),
/// Attempted to push to a full queue.
QueueFull,
}

/// A request for the HSM to perform a cryptographic task.
Expand Down
20 changes: 11 additions & 9 deletions heimlig/src/common/queues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ use crate::common::jobs::{Request, Response};
pub enum Error {
/// No [Channel] found for given ID.
UnknownChannelId,
/// Attempted to push to a full queue.
QueueFull,
/// Failed to enqueue into a queue
Enqueue,
/// Sink was not ready
/// Queue was not ready
NotReady,
}

/// Sink where the requests to the Core can be pushed to
pub trait RequestSink<'data> {
/// Send a [Request] to the client through this sink.
fn send(&mut self, request: Request<'data>) -> Result<(), Error>;
/// todo!
fn ready(&self) -> bool;
}

/// Sink where the responses from the Core can be pushed to
pub trait ResponseSink<'data> {
/// Send a [Response] to the client through this sink.
fn send(&mut self, response: Response<'data>) -> Result<(), Error>;
/// todo!
fn ready(&self) -> bool;
}

/// Sink where the requests to the Core can be pushed to
pub trait RequestSink<'data> {
/// Send a [Request] to the client through this sink.
fn send(&mut self, request: Request<'data>) -> Result<(), Error>;
fn ready(&self) -> bool;
}
// TODO: Add dedicated RequestSource and ResponseSource here instead of using Iterators directly?
7 changes: 2 additions & 5 deletions heimlig/src/hsm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ impl<
MAX_WORKERS,
>
{
// TODO: Create builder
/// Create a new HSM core.
/// The core accepts requests and forwards the responses once they are ready.
///
Expand Down Expand Up @@ -122,8 +121,7 @@ impl<
Ok(())
}

// TODO: Make private?
pub fn process_worker_responses(&mut self) -> Result<(), Error> {
fn process_worker_responses(&mut self) -> Result<(), Error> {
for channel in &mut self.worker_channels {
if self.client_responses.ready() {
if let Some((_id, response)) = channel.responses.next() {
Expand All @@ -134,7 +132,6 @@ impl<
Ok(())
}

// TODO: Make private?
/// Search all input channels for a new request and process it.
/// Channels are processed in a round-robin fashion.
///
Expand All @@ -143,7 +140,7 @@ impl<
/// * `Ok(true)` if a [Request] was found and successfully processed.
/// * `Ok(false)` if no [Request] was found in any input [Channel].
/// * `Err(core::Error)` if a processing error occurred.
pub fn process_client_requests(&mut self) -> Result<(), Error> {
fn process_client_requests(&mut self) -> Result<(), Error> {
if !self.client_responses.ready() {
return Err(Error::NotReady);
}
Expand Down
3 changes: 1 addition & 2 deletions heimlig/src/hsm/workers/chachapoly_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl<
RespSink: ResponseSink<'data>,
> ChaChaPolyWorker<'data, 'keystore, M, K, ReqSrc, RespSink>
{
// TODO: Do not use core errors here? Export errors in trait as typedef?
pub fn execute(&mut self) -> Result<(), queues::Error> {
if self.responses.ready() {
let mut key_buffer = Zeroizing::new([0u8; MAX_KEY_SIZE]);
Expand Down Expand Up @@ -102,7 +101,7 @@ impl<
}
Ok(())
} else {
Err(queues::Error::QueueFull)
Err(queues::Error::NotReady)
}
}
pub fn encrypt_external_key<'a>(
Expand Down
3 changes: 1 addition & 2 deletions heimlig/src/hsm/workers/rng_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl<
RespSink: ResponseSink<'data>,
> RngWorker<'data, E, ReqSrc, RespSink>
{
// TODO: Do not use core errors here? Export errors in trait as typedef?
pub fn execute(&mut self) -> Result<(), queues::Error> {
if self.responses.ready() {
match self.requests.next() {
Expand All @@ -40,7 +39,7 @@ impl<
}
}
} else {
Err(queues::Error::QueueFull)
Err(queues::Error::NotReady)
}
}

Expand Down
11 changes: 3 additions & 8 deletions heimlig/tests/api_core_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ mod tests {

impl<'data> ResponseSink<'data> for ResponseQueueSink<'_, 'data> {
fn send(&mut self, response: Response<'data>) -> Result<(), Error> {
self.producer
.enqueue(response)
.map_err(|_| Error::QueueFull)
self.producer.enqueue(response).map_err(|_| Error::Enqueue)
}
fn ready(&self) -> bool {
self.producer.ready()
Expand All @@ -82,9 +80,7 @@ mod tests {

impl<'data> RequestSink<'data> for RequestQueueSink<'_, 'data> {
fn send(&mut self, response: Request<'data>) -> Result<(), Error> {
self.producer
.enqueue(response)
.map_err(|_| Error::QueueFull)
self.producer.enqueue(response).map_err(|_| Error::Enqueue)
}
fn ready(&self) -> bool {
self.producer.ready()
Expand Down Expand Up @@ -345,8 +341,7 @@ mod tests {
req_client_tx
.enqueue(request)
.expect("failed to send request");
core.process_client_requests()
.expect("failed to process next request");
core.execute().expect("failed to process next request");
let response = resp_client_rx
.dequeue()
.expect("Failed to receive expected response");
Expand Down

0 comments on commit ae6e251

Please sign in to comment.