From 82b8509a7f56d7ee4a37854775422387feee6d39 Mon Sep 17 00:00:00 2001 From: Santiago Cingolani Date: Wed, 17 Apr 2024 21:18:04 +0200 Subject: [PATCH] Implement Display for errors --- heimlig/Cargo.lock | 12 ++++++++++++ heimlig/Cargo.toml | 3 ++- heimlig/src/common/jobs.rs | 10 ++++++---- heimlig/src/crypto/mod.rs | 4 +++- heimlig/src/hsm/core.rs | 19 ++++++++++--------- heimlig/src/hsm/keystore.rs | 6 ++++-- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/heimlig/Cargo.lock b/heimlig/Cargo.lock index 63d4b799..3e9037f2 100644 --- a/heimlig/Cargo.lock +++ b/heimlig/Cargo.lock @@ -494,6 +494,17 @@ dependencies = [ "subtle", ] +[[package]] +name = "displaydoc" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "ecdsa" version = "0.16.8" @@ -818,6 +829,7 @@ dependencies = [ "cmac", "critical-section", "dbl", + "displaydoc", "ecdsa", "ed25519-dalek", "elliptic-curve", diff --git a/heimlig/Cargo.toml b/heimlig/Cargo.toml index f3c006b0..89c8b543 100644 --- a/heimlig/Cargo.toml +++ b/heimlig/Cargo.toml @@ -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 } @@ -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" \ No newline at end of file +undocumented_unsafe_blocks = "warn" diff --git a/heimlig/src/common/jobs.rs b/heimlig/src/common/jobs.rs index 1ec1c462..fb5e73d6 100644 --- a/heimlig/src/common/jobs.rs +++ b/heimlig/src/common/jobs.rs @@ -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, @@ -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), } diff --git a/heimlig/src/crypto/mod.rs b/heimlig/src/crypto/mod.rs index 00fc3fdc..99e3e57a 100644 --- a/heimlig/src/crypto/mod.rs +++ b/heimlig/src/crypto/mod.rs @@ -1,3 +1,5 @@ +use displaydoc::Display; + pub mod aes; pub mod chacha20poly1305; pub mod ecc; @@ -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, diff --git a/heimlig/src/hsm/core.rs b/heimlig/src/hsm/core.rs index 0fe3e1ba..8d1a18bc 100644 --- a/heimlig/src/hsm/core.rs +++ b/heimlig/src/hsm/core.rs @@ -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, @@ -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), } diff --git a/heimlig/src/hsm/keystore.rs b/heimlig/src/hsm/keystore.rs index 444042e4..b2cb1855 100644 --- a/heimlig/src/hsm/keystore.rs +++ b/heimlig/src/hsm/keystore.rs @@ -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,