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

RFC: no_std #63

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
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ edition = "2018"
# 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"]
nightly = ["blake2-rfc/simd_opt", "chacha20-poly1305-aead/simd_opt", "x25519-dalek/nightly", "subtle/nightly"]
default-resolver = ["chacha20-poly1305-aead", "blake2-rfc", "sha2", "x25519-dalek", "rand"]
ring-resolver = ["ring"]
ring-accelerated = ["ring-resolver", "default-resolver"]
default-resolver = ["chacha20-poly1305-aead", "blake2-rfc", "sha2", "x25519-dalek", "rand", "std"]
ring-resolver = ["ring", "std"]
ring-accelerated = ["ring-resolver", "default-resolver", "std"]
vector-tests = []
std = ["subtle/std"]

[[bench]]
name = "benches"
Expand All @@ -34,7 +35,7 @@ appveyor = { repository = "mcginty/snow", branch = "master", service = "github"
[dependencies]
arrayref = "0.3.5"
rand_core = "0.5"
subtle = "2.1"
subtle = { version = "2.1", default-features = false}

# default crypto provider
chacha20-poly1305-aead = { version = "0.1", optional = true }
Expand Down
3 changes: 3 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::params::NoiseParams;
use crate::resolvers::CryptoResolver;
use crate::error::{Error, InitStage, Prerequisite};
use subtle::ConstantTimeEq;
use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::vec;

/// A keypair object returned by [`Builder::generate_keypair()`]
///
Expand Down
1 change: 1 addition & 0 deletions src/cipherstate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::constants::TAGLEN;
use crate::error::{Error, InitStage, StateProblem};
use crate::types::Cipher;
use alloc::boxed::Box;

pub(crate) struct CipherState {
cipher : Box<dyn Cipher>,
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;

/// All errors in snow will include an `ErrorKind`.
#[allow(missing_docs)]
Expand Down Expand Up @@ -126,4 +126,5 @@ impl fmt::Display for Error {
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
3 changes: 2 additions & 1 deletion src/handshakestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::params::{HandshakeTokens, MessagePatterns, NoiseParams, Token};
use crate::transportstate::TransportState;
use crate::stateless_transportstate::StatelessTransportState;
use crate::error::{Error, InitStage, StateProblem};
use std::{convert::{TryFrom, TryInto}, fmt};
use core::{convert::{TryFrom, TryInto}, fmt};
use alloc::boxed::Box;

/// A state machine encompassing the handshake phase of a Noise session.
///
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![no_std]
//! The `snow` crate is a straightforward, Hard To Fuck Up™ Noise Protocol implementation.
//!
//! Read the [Noise Protocol Framework Spec](http://noiseprotocol.org/noise.html) for more
Expand Down Expand Up @@ -71,6 +72,11 @@ macro_rules! bail {
return Err(($e).into());
};
}
extern crate alloc;

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

pub mod error;
mod utils;
Expand Down
6 changes: 4 additions & 2 deletions src/params/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
//! patterns/names)

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


pub use self::patterns::{
HandshakeChoice,
HandshakeModifier,
Expand Down Expand Up @@ -143,7 +145,7 @@ impl FromStr for NoiseParams {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut split = s.split('_');
Ok(NoiseParams::new(s.to_owned(),
Ok(NoiseParams::new(String::from(s),
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
split.next().ok_or(PatternProblem::TooFewParameters)?.parse()?,
Expand Down
3 changes: 2 additions & 1 deletion src/params/patterns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::{Error, PatternProblem};
use std::{convert::TryFrom, str::FromStr};
use core::{convert::TryFrom, str::FromStr};
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
7 changes: 6 additions & 1 deletion src/resolvers/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use x25519_dalek as x25519;
use crate::types::{Cipher, Dh, Hash, Random};
use crate::constants::TAGLEN;
use crate::params::{CipherChoice, DHChoice, HashChoice};
use alloc::boxed::Box;

use std::io::{Cursor, Write};
use super::CryptoResolver;

Expand Down Expand Up @@ -317,6 +319,9 @@ mod tests {
use super::*;
use self::hex::FromHex;

use alloc::{vec::Vec, vec};
use alloc::string::String;

#[test]
fn test_sha256() {
let mut output = [0u8; 32];
Expand Down Expand Up @@ -470,6 +475,6 @@ mod tests {
6d206f74686572207468616e20617320\
2fe2809c776f726b20696e2070726f67\
726573732e2fe2809d";
assert!(hex::encode(out[..ciphertext.len()].to_owned()) == desired_plaintext);
assert!(String::from(hex::encode(&out[..ciphertext.len()])) == desired_plaintext);
}
}
1 change: 1 addition & 0 deletions src/resolvers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! The wrappers around the default collection of cryptography and entropy providers.

use alloc::boxed::Box;

/// The default primitive resolver.
#[cfg(feature = "default-resolver")] mod default;
Expand Down
2 changes: 1 addition & 1 deletion src/stateless_transportstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::cipherstate::StatelessCipherStates;
use crate::constants::{MAXDHLEN, MAXMSGLEN, TAGLEN};
use crate::handshakestate::HandshakeState;
use crate::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
1 change: 1 addition & 0 deletions src/symmetricstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::Error;
use crate::constants::{CIPHERKEYLEN, MAXHASHLEN};
use crate::types::Hash;
use crate::cipherstate::CipherState;
use alloc::boxed::Box;

#[derive(Copy, Clone)]
pub(crate) struct SymmetricStateData {
Expand Down
2 changes: 1 addition & 1 deletion src/transportstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::cipherstate::CipherStates;
use crate::constants::{MAXDHLEN, MAXMSGLEN, TAGLEN};
use crate::utils::Toggle;
use crate::handshakestate::HandshakeState;
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