Skip to content

Commit

Permalink
Implement Display for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Santiago Cingolani authored and inorick committed Aug 21, 2024
1 parent cca2eea commit 82b8509
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
12 changes: 12 additions & 0 deletions heimlig/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion heimlig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ chacha20poly1305 = { version = "0.10.1", default-features = false }
cmac = { version = "0.7.2", default-features = false }
critical-section = { version = "1.1.2", default-features = false }
dbl = { version = "0.3.2", default-features = false }
displaydoc = { version = "0.2.4", default-features = false }
ecdsa = { version = "0.16.8", default-features = false }
ed25519-dalek = { version = "2.1.1", default-features = false, features = ["zeroize"] }
elliptic-curve = { version = "0.13.5", default-features = false }
Expand Down Expand Up @@ -47,4 +48,4 @@ ed25519-dalek = { version = "2.1.1", default-features = false, features = ["zero
cbindgen = { version = "0.26.0", default-features = false }

[lints.clippy]
undocumented_unsafe_blocks = "warn"
undocumented_unsafe_blocks = "warn"
10 changes: 6 additions & 4 deletions heimlig/src/common/jobs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use displaydoc::Display;

use crate::hsm::keystore;
use crate::hsm::keystore::{Curve, KeyId};

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
pub enum Error {
/// No worker found for received request type.
NoWorkerForRequest,
Expand All @@ -13,11 +15,11 @@ pub enum Error {
NoKeyStore,
/// Failed to send through channel.
Send,
/// Futures Stream was terminated
/// Futures Stream was terminated.
StreamTerminated,
/// A cryptographic error occurred.
/// A cryptographic error occurred: {0}
Crypto(crate::crypto::Error),
/// A key store error occurred.
/// A key store error occurred: {0}
KeyStore(keystore::Error),
}

Expand Down
4 changes: 3 additions & 1 deletion heimlig/src/crypto/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use displaydoc::Display;

pub mod aes;
pub mod chacha20poly1305;
pub mod ecc;
Expand All @@ -10,7 +12,7 @@ pub mod rng;
pub mod x25519;

/// Common errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
pub enum Error {
/// Error during encryption.
Encrypt,
Expand Down
19 changes: 10 additions & 9 deletions heimlig/src/hsm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use crate::hsm::keystore;
use core::future::poll_fn;
use core::ops::DerefMut;
use core::pin::Pin;
use displaydoc::Display;
use embassy_futures::select::select_slice;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::mutex::Mutex;
use futures::{FutureExt, Sink, SinkExt, Stream, StreamExt};
use heapless::Vec;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
pub enum Error {
/// Error sending message through queue
Send,
Expand All @@ -26,24 +27,24 @@ pub enum Error {
ChannelForRequestExists,
/// Maximum number of request types for a single worker exceeded
TooManyRequestTypes,
/// An internal error occurred
/// An internal error occurred: {0}
Internal(InternalError),
}

/// Internal errors that a client should not be able to trigger.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
pub enum InternalError {
// The internal client ID was invalid for the internal list of client channels.
/// The internal client ID ({0:?}) was invalid for the internal list of client channels.
InvalidClientId(ClientId),
// The internal worker ID was invalid for the internal list of worker channels.
/// The internal worker ID ({0:?}) was invalid for the internal list of worker channels.
InvalidWorkerId(WorkerId),
/// An empty client request queue was encountered even though a previous check made sure that it was non-empty.
/// An empty client ({0:?}) request queue was encountered even though a previous check made sure that it was non-empty.
EmptyClientRequestQueue(ClientId),
/// An empty worker response queue was encountered even though a previous check made sure that it was non-empty.
/// An empty worker ({0:?}) response queue was encountered even though a previous check made sure that it was non-empty.
EmptyWorkerResponseQueue(WorkerId),
/// The core encountered a request type it cannot handle
/// The core encountered a request ({0:?}) type it cannot handle.
UnexpectedCoreRequest(RequestType),
// The client ID of the response that was determined to be processed next did not match the one in the response queue.
/// The client ID of the response that was determined to be processed next ({0:?}) did not match the one in the response queue ({1:?}).
ClientIdMismatch(ClientId, ClientId),
}

Expand Down
6 changes: 4 additions & 2 deletions heimlig/src/hsm/keystore.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use displaydoc::Display;

/// Identifier to reference HSM keys
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct KeyId(pub u32);

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Display)]
pub enum Error {
/// The operation is not permitted
NotAllowed,
/// The requested key was not found.
KeyNotFound,
/// The operation attempted to overwrite an existing key when it was not permitted
/// The operation attempted to overwrite an existing key when it was not permitted.
KeyAlreadyExists,
/// The key store cannot handle the number of requested keys.
KeyStoreTooSmall,
Expand Down

0 comments on commit 82b8509

Please sign in to comment.