Skip to content
Closed
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
6 changes: 4 additions & 2 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ rust-version = "1.63.0"
default = ["std"]
# High-level wrappers using tokio traits - may affect MSRV requirements.
tokio = ["std", "dep:tokio"]
std = ["bitcoin/rand-std", "bitcoin_hashes/std", "chacha20-poly1305/std"]
std = ["secp256k1/rand-std", "bitcoin_hashes/std", "chacha20-poly1305/std"]

[dependencies]
# The tokio feature may increase the MSRV beyond 1.63.0
# depending on which version of tokio is selected by the caller.
tokio = { version = "1", default-features = false, optional = true, features = ["io-util"] }
bitcoin = { version = "0.32.4", default-features = false }
secp256k1 = { version = "0.29.0", default-features = false }
# Depending on hashes directly for HKDF, can drop this and
# use the re-exported version in bitcoin > 0.32.*.
bitcoin_hashes = { version =">=0.15.0, <0.17.0", default-features = false }
Expand All @@ -28,6 +28,8 @@ chacha20-poly1305 = { version = "0.1.1", default-features = false }
[dev-dependencies]
# bitcoind version 26.0 includes support for BIP-324's V2 protocol, but it is disabled by default.
bitcoind = { package = "corepc-node", version = "0.7.1", default-features = false, features = ["26_0","download"] }
bitcoin = { git = "https://github.com/rust-bitcoin/rust-bitcoin", rev = "16cc257c3695dea0e7301a5fa9cab44b8ed60598" }
p2p = { package = "bitcoin-p2p-messages", git = "https://github.com/rust-bitcoin/rust-bitcoin", rev = "16cc257c3695dea0e7301a5fa9cab44b8ed60598" }
hex = { package = "hex-conservative", version = "0.2.0" }
tokio = { version = "1", features = ["io-util", "net", "rt-multi-thread", "macros"] }

Expand Down
7 changes: 4 additions & 3 deletions protocol/benches/cipher_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
extern crate test;

use bip324::{
CipherSession, GarbageResult, Handshake, InboundCipher, Initialized, Network, OutboundCipher,
CipherSession, GarbageResult, Handshake, InboundCipher, Initialized, OutboundCipher,
PacketType, ReceivedKey, Role, VersionResult, NUM_LENGTH_BYTES,
};
use p2p::Magic;
use test::{black_box, Bencher};

fn create_cipher_session_pair() -> (CipherSession, CipherSession) {
// Send Alice's key.
let alice_handshake = Handshake::<Initialized>::new(Network::Bitcoin, Role::Initiator).unwrap();
let alice_handshake = Handshake::<Initialized>::new(Magic::BITCOIN, Role::Initiator).unwrap();
let mut alice_key_buffer = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
let alice_handshake = alice_handshake
.send_key(None, &mut alice_key_buffer)
.unwrap();

// Send Bob's key
let bob_handshake = Handshake::<Initialized>::new(Network::Bitcoin, Role::Responder).unwrap();
let bob_handshake = Handshake::<Initialized>::new(Magic::BITCOIN, Role::Responder).unwrap();
let mut bob_key_buffer = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
let bob_handshake = bob_handshake.send_key(None, &mut bob_key_buffer).unwrap();

Expand Down
8 changes: 4 additions & 4 deletions protocol/examples/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use bip324::futures::Protocol;
use bip324::io::Payload;
use bip324::{Network, Role};
use bip324::Role;
use std::fmt;
use std::time::{Duration, Instant};
use tokio::io::BufReader;
Expand Down Expand Up @@ -127,7 +127,7 @@ async fn start_server(
let (reader, writer) = stream.into_split();

let mut protocol = Protocol::new(
Network::Bitcoin,
p2p::Magic::BITCOIN,
Role::Responder,
None,
None,
Expand Down Expand Up @@ -180,7 +180,7 @@ impl Client {
Client::Buffered => {
let buffered_reader = BufReader::new(reader);
let mut protocol = Protocol::new(
Network::Bitcoin,
p2p::Magic::BITCOIN,
Role::Initiator,
None,
None,
Expand All @@ -196,7 +196,7 @@ impl Client {
}
Client::NonBuffered => {
let mut protocol = Protocol::new(
Network::Bitcoin,
p2p::Magic::BITCOIN,
Role::Initiator,
None,
None,
Expand Down
10 changes: 5 additions & 5 deletions protocol/fuzz/fuzz_targets/receive_garbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//! Fuzz test for the receive_garbage function.

#![no_main]
use bip324::{Handshake, Initialized, Network, ReceivedKey, Role};
use bip324::{Handshake, Initialized, ReceivedKey, Role};
use libfuzzer_sys::fuzz_target;
use rand::SeedableRng;

const MAGIC: [u8; 4] = [0xF9, 0xBE, 0xB4, 0xD9];

fuzz_target!(|data: &[u8]| {
// Cap input size to avoid wasting time on obviously invalid large inputs
// The protocol limit is 4095 garbage bytes + 16 terminator bytes = 4111 total
Expand All @@ -22,15 +24,13 @@ fuzz_target!(|data: &[u8]| {

// Set up a valid handshake in the SentVersion state
let initiator =
Handshake::<Initialized>::new_with_rng(Network::Bitcoin, Role::Initiator, &mut rng, &secp)
.unwrap();
Handshake::<Initialized>::new_with_rng(MAGIC, Role::Initiator, &mut rng, &secp).unwrap();
let mut initiator_key = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
let initiator = initiator.send_key(None, &mut initiator_key).unwrap();

let mut rng2 = rand::rngs::StdRng::from_seed([43u8; 32]);
let responder =
Handshake::<Initialized>::new_with_rng(Network::Bitcoin, Role::Responder, &mut rng2, &secp)
.unwrap();
Handshake::<Initialized>::new_with_rng(MAGIC, Role::Responder, &mut rng2, &secp).unwrap();
let mut responder_key = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
let responder = responder.send_key(None, &mut responder_key).unwrap();

Expand Down
7 changes: 4 additions & 3 deletions protocol/fuzz/fuzz_targets/receive_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
//! This focused test fuzzes the elliptic curve point validation and ECDH logic.

#![no_main]
use bip324::{Handshake, Initialized, Network, Role};
use bip324::{Handshake, Initialized, Role};
use libfuzzer_sys::fuzz_target;
use rand::SeedableRng;

const MAGIC: [u8; 4] = [0xF9, 0xBE, 0xB4, 0xD9];

fuzz_target!(|data: &[u8]| {
// We need exactly 64 bytes for an ElligatorSwift key.
// This gives the fuzzer a clear signal about the expected input size.
Expand All @@ -24,8 +26,7 @@ fuzz_target!(|data: &[u8]| {

// Set up a handshake in the SentKey state.
let handshake =
Handshake::<Initialized>::new_with_rng(Network::Bitcoin, Role::Initiator, &mut rng, &secp)
.unwrap();
Handshake::<Initialized>::new_with_rng(MAGIC, Role::Initiator, &mut rng, &secp).unwrap();
let mut key_buffer = vec![0u8; Handshake::<Initialized>::send_key_len(None)];
let handshake = handshake.send_key(None, &mut key_buffer).unwrap();

Expand Down
23 changes: 12 additions & 11 deletions protocol/src/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! use bip324::futures::Protocol;
//! use bip324::io::Payload;
//! use bip324::{Network, Role};
//! use tokio::net::TcpStream;

Check failure on line 19 in protocol/src/futures.rs

View workflow job for this annotation

GitHub Actions / features (ubuntu-latest)

unresolved import `bip324::Network`
//! use tokio::io::BufReader;
//!
//! # #[tokio::main]
Expand All @@ -30,8 +30,8 @@
//!
//! // Establish BIP-324 encrypted connection
//! let mut protocol = Protocol::new(
//! Network::Bitcoin,
//! MAGIC,
//! Role::Initiator,

Check failure on line 34 in protocol/src/futures.rs

View workflow job for this annotation

GitHub Actions / features (ubuntu-latest)

cannot find value `MAGIC` in this scope
//! None, // no garbage bytes
//! None, // no decoy packets
//! reader,
Expand All @@ -46,12 +46,12 @@
//! # }
//! ```

use core::borrow::Borrow;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::vec;
use std::vec::Vec;

use bitcoin::Network;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::{
Expand Down Expand Up @@ -146,7 +146,7 @@
///
/// * `Io` - Includes a flag for if the remote probably only understands the V1 protocol.
pub async fn handshake<R, W>(
network: Network,
magic: impl Borrow<[u8; 4]>,
role: Role,
garbage: Option<Vec<u8>>,
decoys: Option<Vec<Vec<u8>>>,
Expand All @@ -166,7 +166,7 @@
.map(|vecs| vecs.iter().map(Vec::as_slice).collect());
let decoys_ref = decoy_refs.as_deref();

let handshake = Handshake::<handshake::Initialized>::new(network, role)?;
let handshake = Handshake::<handshake::Initialized>::new(magic, role)?;

// Send local public key and optional garbage.
let key_buffer_len = Handshake::<handshake::Initialized>::send_key_len(garbage_ref);
Expand Down Expand Up @@ -284,15 +284,15 @@
///
/// * `Io` - Includes a flag for if the remote probably only understands the V1 protocol.
pub async fn new(
network: Network,
magic: impl Borrow<[u8; 4]>,
role: Role,
garbage: Option<Vec<u8>>,
decoys: Option<Vec<Vec<u8>>>,
reader: R,
mut writer: W,
) -> Result<Protocol<R, W>, ProtocolError> {
let (inbound_cipher, outbound_cipher, session_reader) =
handshake(network, role, garbage, decoys, reader, &mut writer).await?;
handshake(magic, role, garbage, decoys, reader, &mut writer).await?;

Ok(Protocol {
reader: ProtocolReader {
Expand Down Expand Up @@ -480,7 +480,8 @@
#[cfg(test)]
mod tests {
use super::*;
use bitcoin::Network;

const MAGIC: [u8; 4] = [0xF9, 0xBE, 0xB4, 0xD9];

#[tokio::test]
async fn test_async_handshake_functions() {
Expand All @@ -493,7 +494,7 @@

let local_handshake = tokio::spawn(async move {
handshake(
Network::Bitcoin,
MAGIC,
Role::Initiator,
Some(b"local garbage".to_vec()),
Some(vec![b"local decoy".to_vec()]),
Expand All @@ -505,7 +506,7 @@

let remote_handshake = tokio::spawn(async move {
handshake(
Network::Bitcoin,
MAGIC,
Role::Responder,
Some(b"remote garbage".to_vec()),
Some(vec![b"remote decoy 1".to_vec(), b"remote decoy 2".to_vec()]),
Expand All @@ -532,7 +533,7 @@

let local_handshake = tokio::spawn(async move {
handshake(
Network::Bitcoin,
MAGIC,
Role::Initiator,
None,
None,
Expand All @@ -545,7 +546,7 @@
let remote_handshake = tokio::spawn(async move {
let large_decoy = vec![0u8; MAX_PACKET_SIZE_FOR_ALLOCATION + 1];
handshake(
Network::Bitcoin,
MAGIC,
Role::Responder,
None,
Some(vec![large_decoy]),
Expand Down
Loading
Loading