From 75e0368fdb9d3e07917c6896aaf1ae88f86301ea Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Thu, 11 Apr 2024 13:06:51 -0700 Subject: [PATCH 1/3] No std best practice --- protocol/src/chacha20poly1305.rs | 1 + protocol/src/chacha20poly1305/chacha20.rs | 2 ++ protocol/src/chacha20poly1305/poly1305.rs | 1 + protocol/src/hkdf.rs | 2 ++ protocol/src/lib.rs | 30 +++++++++++++++++++---- 5 files changed, 31 insertions(+), 5 deletions(-) diff --git a/protocol/src/chacha20poly1305.rs b/protocol/src/chacha20poly1305.rs index 9fc77bb..24ba89b 100644 --- a/protocol/src/chacha20poly1305.rs +++ b/protocol/src/chacha20poly1305.rs @@ -135,6 +135,7 @@ impl ChaCha20Poly1305 { #[cfg(test)] mod tests { use super::*; + use alloc::vec::Vec; use hex::prelude::*; #[test] diff --git a/protocol/src/chacha20poly1305/chacha20.rs b/protocol/src/chacha20poly1305/chacha20.rs index 821697d..bd3038c 100644 --- a/protocol/src/chacha20poly1305/chacha20.rs +++ b/protocol/src/chacha20poly1305/chacha20.rs @@ -232,9 +232,11 @@ fn keystream_at_slice(key: [u8; 32], nonce: [u8; 12], count: u32, seek: usize) - kstream } +#[cfg(feature = "std")] #[cfg(test)] mod tests { use super::*; + use alloc::vec::Vec; use hex::prelude::*; use rand::Rng; diff --git a/protocol/src/chacha20poly1305/poly1305.rs b/protocol/src/chacha20poly1305/poly1305.rs index a1a26f0..ded5607 100644 --- a/protocol/src/chacha20poly1305/poly1305.rs +++ b/protocol/src/chacha20poly1305/poly1305.rs @@ -231,6 +231,7 @@ fn _print_acc(num: &[u32; 5]) { #[cfg(test)] mod tests { use super::*; + use alloc::vec::Vec; use hex::prelude::*; #[test] diff --git a/protocol/src/hkdf.rs b/protocol/src/hkdf.rs index 5eaad6e..5b359a8 100644 --- a/protocol/src/hkdf.rs +++ b/protocol/src/hkdf.rs @@ -86,8 +86,10 @@ impl Hkdf { } #[cfg(test)] +#[cfg(feature = "std")] mod tests { use super::*; + use alloc::vec::Vec; use bitcoin_hashes::sha256; use hex::prelude::*; diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 0f0c574..a6e9fd3 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -1,8 +1,9 @@ //! BIP 324 encrypted transport for exchanging Bitcoin P2P messages. Read more about the [specification](https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki). - -#![cfg_attr(all(not(feature = "std"), not(test)), no_std)] +#![no_std] extern crate alloc; +#[cfg(feature = "std")] +extern crate std; mod chacha20poly1305; mod error; @@ -747,24 +748,30 @@ fn split_garbage_and_version<'a>( #[cfg(test)] mod tests { + //! Any tests requiring a random number generator are currently + //! gated with the std feature flag. + use super::*; use core::str::FromStr; use hex::prelude::*; + #[cfg(feature = "std")] fn gen_garbage(garbage_len: u32, rng: &mut impl Rng) -> Vec { let buffer: Vec = (0..garbage_len).map(|_| rng.gen()).collect(); buffer } #[test] + #[cfg(feature = "std")] fn test_sec_keygen() { let mut rng = rand::thread_rng(); gen_key(&mut rng).unwrap(); } #[test] + #[cfg(feature = "std")] fn test_initial_message() { - let mut message = vec![0u8; 64]; + let mut message = [0u8; 64]; let handshake = Handshake::new(Network::Mainnet, Role::Initiator, None, &mut message).unwrap(); let message = message.to_lower_hex_string(); @@ -773,11 +780,12 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_message_response() { - let mut message = vec![0u8; 64]; + let mut message = [0u8; 64]; Handshake::new(Network::Mainnet, Role::Initiator, None, &mut message).unwrap(); - let mut response_message = vec![0u8; 100]; + let mut response_message = [0u8; 100]; let mut response = Handshake::new( Network::Mainnet, Role::Responder, @@ -792,6 +800,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_expand_extract() { let ikm = Vec::from_hex("c6992a117f5edbea70c3f511d32d26b9798be4b81a62eaee1a5acaa8459a3592") .unwrap(); @@ -883,6 +892,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_fuzz_packets() { let mut rng = rand::thread_rng(); let alice = @@ -922,6 +932,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_authenticated_garbage() { let mut rng = rand::thread_rng(); let alice = @@ -948,6 +959,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_full_handshake() { // The initiator's handshake writes its 64 byte elligator swift key to the buffer to send to the responder. let mut init_message = vec![0u8; 64]; @@ -1007,6 +1019,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_decode_multiple_messages() { let mut init_message = vec![0u8; 64]; let mut init_handshake = @@ -1045,6 +1058,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_fuzz_decode_multiple_messages() { let mut rng = rand::thread_rng(); @@ -1086,6 +1100,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_partial_decodings() { let mut rng = rand::thread_rng(); @@ -1130,6 +1145,7 @@ mod tests { // The rest are sourced from: https://github.com/bitcoin/bips/blob/master/bip-0324/packet_encoding_test_vectors.csv #[test] + #[cfg(feature = "std")] fn test_vector_1() { let mut rng = rand::thread_rng(); let alice = @@ -1164,6 +1180,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_vector_2() { let alice = SecretKey::from_str("1f9c581b35231838f0f17cf0c979835baccb7f3abbbb96ffcc318ab71e6e126f") @@ -1201,6 +1218,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_vector_3() { let alice = SecretKey::from_str("0286c41cd30913db0fdff7a64ebda5c8e3e7cef10f2aebc00a7650443cf4c60d") @@ -1226,6 +1244,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_vector_4() { let alice = SecretKey::from_str("6c77432d1fda31e9f942f8af44607e10f3ad38a65f8a4bddae823e5eff90dc38") @@ -1261,6 +1280,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_vector_5() { let alice = SecretKey::from_str("a6ec25127ca1aa4cf16b20084ba1e6516baae4d32422288e9b36d8bddd2de35a") From 5067f7175f2fa2bec2998bca5b0f1d90c751eb4f Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Thu, 11 Apr 2024 13:09:42 -0700 Subject: [PATCH 2/3] Don't need to turn off test. --- protocol/src/hkdf.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/protocol/src/hkdf.rs b/protocol/src/hkdf.rs index 5b359a8..ed43894 100644 --- a/protocol/src/hkdf.rs +++ b/protocol/src/hkdf.rs @@ -86,7 +86,6 @@ impl Hkdf { } #[cfg(test)] -#[cfg(feature = "std")] mod tests { use super::*; use alloc::vec::Vec; From 12dd2957e3511e55c660a00aeebf8dc486bac86c Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Thu, 11 Apr 2024 13:11:57 -0700 Subject: [PATCH 3/3] Gate random test. --- protocol/src/chacha20poly1305/chacha20.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/protocol/src/chacha20poly1305/chacha20.rs b/protocol/src/chacha20poly1305/chacha20.rs index bd3038c..09f5e2e 100644 --- a/protocol/src/chacha20poly1305/chacha20.rs +++ b/protocol/src/chacha20poly1305/chacha20.rs @@ -232,12 +232,12 @@ fn keystream_at_slice(key: [u8; 32], nonce: [u8; 12], count: u32, seek: usize) - kstream } -#[cfg(feature = "std")] #[cfg(test)] mod tests { use super::*; use alloc::vec::Vec; use hex::prelude::*; + #[cfg(feature = "std")] use rand::Rng; #[test] @@ -426,6 +426,7 @@ mod tests { assert_eq!(binding, to); } + #[cfg(feature = "std")] fn gen_garbage(garbage_len: u32) -> Vec { let mut rng = rand::thread_rng(); let buffer: Vec = (0..garbage_len).map(|_| rng.gen()).collect(); @@ -433,6 +434,7 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_fuzz_other() { for _ in 0..100 { let garbage_key = gen_garbage(32);