Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-std-ify hpke-rs-crypto #50

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading