Skip to content

Commit

Permalink
Merge pull request #50 from japaric/crypto-no-std
Browse files Browse the repository at this point in the history
no-std-ify hpke-rs-crypto
  • Loading branch information
franziskuskiefer authored Nov 27, 2023
2 parents aae8d0a + 26aabba commit 9cfc218
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ getrandom = { version = "0.2", features = ["js"] }

[features]
serde = ["dep:serde"]
std = []
6 changes: 4 additions & 2 deletions traits/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
}
8 changes: 8 additions & 0 deletions traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
18 changes: 9 additions & 9 deletions traits/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16> for KemAlgorithm {
impl core::convert::TryFrom<u16> for KemAlgorithm {
type Error = error::Error;
fn try_from(x: u16) -> Result<KemAlgorithm, Self::Error> {
match x {
Expand Down Expand Up @@ -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<u16> for AeadAlgorithm {
impl core::convert::TryFrom<u16> for AeadAlgorithm {
type Error = error::Error;
fn try_from(x: u16) -> Result<AeadAlgorithm, Self::Error> {
match x {
Expand Down Expand Up @@ -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<u16> for KdfAlgorithm {
impl core::convert::TryFrom<u16> for KdfAlgorithm {
type Error = error::Error;
fn try_from(x: u16) -> Result<KdfAlgorithm, Self::Error> {
match x {
Expand Down

0 comments on commit 9cfc218

Please sign in to comment.