From b1c4512457e1b05de0cd9624dce96d2d13b1933f Mon Sep 17 00:00:00 2001 From: "Arvid E. Picciani" Date: Sat, 31 Aug 2019 11:19:59 +0200 Subject: [PATCH] wip: no_std --- Cargo.toml | 5 +++-- src/builder.rs | 3 +++ src/cipherstate.rs | 1 + src/error.rs | 3 ++- src/handshakestate.rs | 3 ++- src/lib.rs | 6 ++++++ src/params/mod.rs | 6 ++++-- src/params/patterns.rs | 3 ++- src/resolvers/default.rs | 7 ++++++- src/resolvers/mod.rs | 1 + src/stateless_transportstate.rs | 2 +- src/symmetricstate.rs | 1 + src/transportstate.rs | 2 +- src/utils.rs | 2 +- 14 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5fa72076..7eb2abf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] vector-tests = [] +std = ["subtle/std"] [[bench]] name = "benches" @@ -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 } diff --git a/src/builder.rs b/src/builder.rs index b0a193f7..075e945b 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -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()`] /// diff --git a/src/cipherstate.rs b/src/cipherstate.rs index b35afcee..f57656af 100644 --- a/src/cipherstate.rs +++ b/src/cipherstate.rs @@ -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, diff --git a/src/error.rs b/src/error.rs index 4899466f..fa646858 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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)] @@ -126,4 +126,5 @@ impl fmt::Display for Error { } } +#[cfg(feature = "std")] impl std::error::Error for Error {} diff --git a/src/handshakestate.rs b/src/handshakestate.rs index ba47d9b7..fb94f26c 100644 --- a/src/handshakestate.rs +++ b/src/handshakestate.rs @@ -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. /// diff --git a/src/lib.rs b/src/lib.rs index 55ebd998..f28d6de4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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; diff --git a/src/params/mod.rs b/src/params/mod.rs index dbbaca03..1df7fc87 100644 --- a/src/params/mod.rs +++ b/src/params/mod.rs @@ -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, @@ -143,7 +145,7 @@ impl FromStr for NoiseParams { fn from_str(s: &str) -> Result { 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()?, diff --git a/src/params/patterns.rs b/src/params/patterns.rs index 43054bfb..b93de8db 100644 --- a/src/params/patterns.rs +++ b/src/params/patterns.rs @@ -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. diff --git a/src/resolvers/default.rs b/src/resolvers/default.rs index a895f80d..ff14a578 100644 --- a/src/resolvers/default.rs +++ b/src/resolvers/default.rs @@ -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; @@ -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]; @@ -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); } } diff --git a/src/resolvers/mod.rs b/src/resolvers/mod.rs index e753644f..0dfaf76a 100644 --- a/src/resolvers/mod.rs +++ b/src/resolvers/mod.rs @@ -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; diff --git a/src/stateless_transportstate.rs b/src/stateless_transportstate.rs index aca70bb1..a4c82cd5 100644 --- a/src/stateless_transportstate.rs +++ b/src/stateless_transportstate.rs @@ -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 diff --git a/src/symmetricstate.rs b/src/symmetricstate.rs index ea93b018..cbc8367b 100644 --- a/src/symmetricstate.rs +++ b/src/symmetricstate.rs @@ -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 { diff --git a/src/transportstate.rs b/src/transportstate.rs index eb6cc820..bf05520e 100644 --- a/src/transportstate.rs +++ b/src/transportstate.rs @@ -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 diff --git a/src/utils.rs b/src/utils.rs index d1b4fcad..178a5477 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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