From 74fe12f61d8ca33066d415005315783c4f9a12e0 Mon Sep 17 00:00:00 2001 From: soralit Date: Tue, 28 Mar 2023 11:06:21 +0800 Subject: [PATCH] support no_std --- Cargo.toml | 6 ++++-- rust-toolchain | 1 + src/derivation/mod.rs | 3 +++ src/hex.rs | 6 ++++++ src/key.rs | 3 +++ src/lib.rs | 10 +++++++++- src/securemem.rs | 10 +++------- src/signature.rs | 3 +++ 8 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 rust-toolchain diff --git a/Cargo.toml b/Cargo.toml index 7be305f..f616e6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,9 @@ categories = [ "cryptography" ] [dependencies] cryptoxide = "0.4" +zeroize = "1.6.0" [features] -default = [] -with-bench = [] +default = ["std"] +std = [] +with-bench = [] \ No newline at end of file diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 0000000..07ade69 --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly \ No newline at end of file diff --git a/src/derivation/mod.rs b/src/derivation/mod.rs index e90f4a6..314fa9f 100644 --- a/src/derivation/mod.rs +++ b/src/derivation/mod.rs @@ -1,3 +1,6 @@ +#[cfg(not(feature = "std"))] +use core as std; + mod common; pub mod v2; diff --git a/src/hex.rs b/src/hex.rs index 73cfae6..7854afa 100644 --- a/src/hex.rs +++ b/src/hex.rs @@ -1,3 +1,9 @@ +#[cfg(not(feature = "std"))] +use alloc::string::String; + +#[cfg(not(feature = "std"))] +use alloc::vec::Vec; + const ALPHABET: &'static [u8] = b"0123456789abcdef"; pub fn encode(input: &[u8]) -> String { diff --git a/src/key.rs b/src/key.rs index 5e61650..668dd4c 100644 --- a/src/key.rs +++ b/src/key.rs @@ -1,3 +1,6 @@ +#[cfg(not(feature = "std"))] +use core as std; + use std::fmt; use cryptoxide::constant_time::CtEqual; diff --git a/src/lib.rs b/src/lib.rs index ad3fcab..2b64d08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,12 +10,20 @@ //! In soft derivation, the important property is that given the parent public key, //! one can derive all softly derived children public key. -#![cfg_attr(feature = "with-bench", feature(test))] +#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(not(feature = "std"), feature(error_in_core))] +#![cfg_attr(feature = "with-bench", feature(test))] #[cfg(test)] #[cfg(feature = "with-bench")] extern crate test; +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +extern crate core; + mod derivation; mod hex; mod key; diff --git a/src/securemem.rs b/src/securemem.rs index 109a608..602db10 100644 --- a/src/securemem.rs +++ b/src/securemem.rs @@ -1,9 +1,5 @@ +use zeroize::Zeroize; + pub fn zero(to_zero: &mut [u8]) { - // the unsafety of this call is bounded to the existence of the pointer - // and the accuracy of the length of the array. - // - // since to_zero existence is bound to live at least as long as the call - // of this function and that we use the length (in bytes) of the given - // slice, this call is safe. - unsafe { ::std::ptr::write_bytes(to_zero.as_mut_ptr(), 0, to_zero.len()) } + to_zero.zeroize() } diff --git a/src/signature.rs b/src/signature.rs index 657ccc1..35845c2 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,3 +1,6 @@ +#[cfg(not(feature = "std"))] +use core as std; + use super::hex; use std::error::Error; use std::fmt;