diff --git a/Cargo.toml b/Cargo.toml index a515882..f07e43d 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ hpke-test = [] hpke-test-prng = [] # ⚠️ Enable testing PRNG - DO NOT USE [dev-dependencies] +hpke-rs-crypto = { version = "0.1.3", path = "./traits", features = ["std"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } lazy_static = "1.4" diff --git a/traits/Cargo.toml b/traits/Cargo.toml index 321aa4b..48433d3 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -16,3 +16,4 @@ getrandom = { version = "0.2", features = ["js"] } [features] serde = ["dep:serde"] +std = [] diff --git a/traits/src/error.rs b/traits/src/error.rs index 8ca20fb..14edd80 100644 --- a/traits/src/error.rs +++ b/traits/src/error.rs @@ -2,7 +2,8 @@ //! //! Errors thrown by crypto functions implementing the [`crate::HpkeCrypto`] traits. -use std::fmt::Display; +use alloc::string::String; +use core::fmt::Display; /// Errors thrown by [`crate::HpkeCrypto`] trait implementations. #[derive(Debug)] @@ -41,10 +42,11 @@ pub enum Error { CryptoLibraryError(String), } +#[cfg(feature = "std")] impl std::error::Error for Error {} impl Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "HPKE Crypto Error: {:?}", self) } } diff --git a/traits/src/lib.rs b/traits/src/lib.rs index fa1d2bb..90d684b 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -1,4 +1,12 @@ #![doc = include_str!("../Readme.md")] +#![no_std] + +extern crate alloc; +#[cfg(feature = "std")] +extern crate std; + +use alloc::string::String; +use alloc::vec::Vec; use error::Error; use types::{AeadAlgorithm, KemAlgorithm}; diff --git a/traits/src/types.rs b/traits/src/types.rs index 5b48dc3..c5c3282 100644 --- a/traits/src/types.rs +++ b/traits/src/types.rs @@ -28,13 +28,13 @@ pub enum KemAlgorithm { DhKem448 = 0x0021, } -impl std::fmt::Display for KemAlgorithm { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for KemAlgorithm { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "{:?}", self) } } -impl std::convert::TryFrom for KemAlgorithm { +impl core::convert::TryFrom for KemAlgorithm { type Error = error::Error; fn try_from(x: u16) -> Result { match x { @@ -90,13 +90,13 @@ pub enum AeadAlgorithm { HpkeExport = 0xFFFF, } -impl std::fmt::Display for AeadAlgorithm { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for AeadAlgorithm { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "{:?}", self) } } -impl std::convert::TryFrom for AeadAlgorithm { +impl core::convert::TryFrom for AeadAlgorithm { type Error = error::Error; fn try_from(x: u16) -> Result { match x { @@ -171,13 +171,13 @@ pub enum KdfAlgorithm { HkdfSha512 = 0x0003, } -impl std::fmt::Display for KdfAlgorithm { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for KdfAlgorithm { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { write!(f, "{:?}", self) } } -impl std::convert::TryFrom for KdfAlgorithm { +impl core::convert::TryFrom for KdfAlgorithm { type Error = error::Error; fn try_from(x: u16) -> Result { match x {