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

Introduced a basic no_std implementation heavily relying on alloc #179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ edition = "2021"
# Features with a -resolver suffix simply enables the existence of a specific resolver,
# and -accelerated suffix means that this resolver will be the default used by the Builder.
[features]
default = ["default-resolver"]
default = ["default-resolver", "std"]
default-resolver = ["aes-gcm", "chacha20poly1305", "blake2", "sha2", "curve25519-dalek"]
nightly = ["blake2/simd_opt", "subtle/nightly"]
ring-resolver = ["ring"]
Expand All @@ -28,6 +28,7 @@ hfs = []
pqclean_kyber1024 = ["pqcrypto-kyber", "pqcrypto-traits", "hfs", "default-resolver"]
xchachapoly = ["chacha20poly1305", "default-resolver"]
risky-raw-split = []
std = []

[[bench]]
name = "benches"
Expand Down
7 changes: 5 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Debug;
use core::fmt::Debug;

#[cfg(feature = "hfs")]
use crate::params::HandshakeModifier;
Expand All @@ -13,6 +13,9 @@ use crate::{
};
use subtle::ConstantTimeEq;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec, vec::Vec};

/// The maximum number of PSKs we will allocate for.
const MAX_PSKS: usize = 10;

Expand Down Expand Up @@ -65,7 +68,7 @@ pub struct Builder<'builder> {
}

impl<'builder> Debug for Builder<'builder> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Builder").field("params", &self.params.name).finish_non_exhaustive()
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/cipherstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::{
types::Cipher,
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

pub(crate) struct CipherState {
cipher: Box<dyn Cipher>,
n: u64,
Expand Down
3 changes: 2 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! All error types used by Snow operations.

use std::fmt;
use core::fmt;

/// `snow` provides decently detailed errors, exposed as the [`Error`] enum,
/// to allow developers to react to errors in a more actionable way.
Expand Down Expand Up @@ -181,4 +181,5 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
6 changes: 5 additions & 1 deletion src/handshakestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ use crate::{
types::{Dh, Hash, Random},
utils::Toggle,
};
use std::{
use core::{
convert::{TryFrom, TryInto},
fmt,
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;


/// A state machine encompassing the handshake phase of a Noise session.
///
/// **Note:** you are probably looking for [`Builder`](struct.Builder.html) to
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@
//!
//! See `examples/simple.rs` for a more complete TCP client/server example with static keys.

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]


#[cfg(not(feature = "std"))]
extern crate alloc;

macro_rules! copy_slices {
($inslice:expr, $outslice:expr) => {
$outslice[..$inslice.len()].copy_from_slice(&$inslice[..])
Expand Down
6 changes: 5 additions & 1 deletion src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
//! patterns/names)

use crate::error::{Error, PatternProblem};
use std::str::FromStr;
use core::str::FromStr;
mod patterns;

#[cfg(not(feature = "std"))]
use alloc::{string::String, borrow::ToOwned};


pub use self::patterns::{
HandshakeChoice, HandshakeModifier, HandshakeModifierList, HandshakePattern,
SUPPORTED_HANDSHAKE_PATTERNS,
Expand Down
5 changes: 4 additions & 1 deletion src/params/patterns.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#![allow(clippy::enum_glob_use)]

use crate::error::{Error, PatternProblem};
use std::{convert::TryFrom, str::FromStr};
use core::{convert::TryFrom, str::FromStr};

#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

/// A small helper macro that behaves similar to the `vec![]` standard macro,
/// except it allocates a bit extra to avoid resizing.
Expand Down
3 changes: 3 additions & 0 deletions src/resolvers/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use crate::{
Error,
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

/// The default resolver provided by snow. This resolver is designed to
/// support as many of the Noise spec primitives as possible with
/// pure-Rust (or nearly pure-Rust) implementations.
Expand Down
3 changes: 3 additions & 0 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub use self::libsodium::SodiumResolver;
#[cfg(feature = "ring-resolver")]
pub use self::ring::RingResolver;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

/// Boxed `CryptoResolver`
pub type BoxedCryptoResolver = Box<dyn CryptoResolver + Send>;

Expand Down
3 changes: 3 additions & 0 deletions src/resolvers/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use ring::{
rand::{SecureRandom, SystemRandom},
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

/// A resolver that chooses [ring](https://github.com/briansmith/ring)-backed
/// primitives when available.
#[allow(clippy::module_name_repetitions)]
Expand Down
2 changes: 1 addition & 1 deletion src/stateless_transportstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
params::HandshakePattern,
utils::Toggle,
};
use std::{convert::TryFrom, fmt};
use core::{convert::TryFrom, fmt};

/// A state machine encompassing the transport phase of a Noise session, using the two
/// `CipherState`s (for sending and receiving) that were spawned from the `SymmetricState`'s
Expand Down
3 changes: 3 additions & 0 deletions src/symmetricstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::{
types::Hash,
};

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;

#[derive(Copy, Clone)]
pub(crate) struct SymmetricStateData {
h: [u8; MAXHASHLEN],
Expand Down
2 changes: 1 addition & 1 deletion src/transportstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
params::HandshakePattern,
utils::Toggle,
};
use std::{convert::TryFrom, fmt};
use core::{convert::TryFrom, fmt};

/// A state machine encompassing the transport phase of a Noise session, using the two
/// `CipherState`s (for sending and receiving) that were spawned from the `SymmetricState`'s
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Deref, DerefMut};
use core::ops::{Deref, DerefMut};

/// Toggle is similar to Option, except that even in the Off/"None" case, there is still
/// an owned allocated inner object. This is useful for holding onto pre-allocated objects
Expand Down
Loading