-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from nyonson/chore/break-up-workspaces
Break up packages into workspace
- Loading branch information
Showing
18 changed files
with
119 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,3 @@ | ||
[package] | ||
name = "bip324" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license-file = "LICENSE" | ||
description = "Encrypted messaging over the Bitcoin P2P Protocol as specified by BIP 324" | ||
repository = "https://github.com/rustaceanrob/bip324" | ||
readme = "README.md" | ||
rust-version = "1.56.1" | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["secp256k1/std", "rand/std", "rand/std_rng"] | ||
|
||
[dependencies] | ||
secp256k1 = { version="0.28.2", default-features = false} | ||
rand = { version = "0.8.4", default-features = false } | ||
bitcoin_hashes = { version = "0.13.0", default-features = false } | ||
|
||
[dev-dependencies] | ||
bitcoin = "0.31.1" | ||
tokio = { version = "1.36.0", features = ["full"] } | ||
hex = { package = "hex-conservative", version = "0.2.0" } | ||
|
||
[lib] | ||
name = "bip324" | ||
path = "src/lib.rs" | ||
|
||
[workspace] | ||
members = ["protocol", "proxy"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "bip324" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license-file = "LICENSE" | ||
description = "Encrypted messaging over the Bitcoin P2P Protocol as specified by BIP 324" | ||
repository = "https://github.com/rustaceanrob/bip324" | ||
readme = "README.md" | ||
rust-version = "1.56.1" | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["secp256k1/std", "rand/std", "rand/std_rng"] | ||
|
||
[dependencies] | ||
secp256k1 = { version="0.28.2", default-features = false} | ||
rand = { version = "0.8.4", default-features = false } | ||
bitcoin_hashes = { version = "0.13.0", default-features = false } | ||
|
||
[dev-dependencies] | ||
hex = { package = "hex-conservative", version = "0.2.0" } | ||
|
||
[lib] | ||
name = "bip324" | ||
path = "src/lib.rs" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Protocol | ||
|
||
Alice and Bob initiate a connection by sending three messages to each other to derive a number of shared secrets. Alice begins the connection by deriving a public/private keypair over `secp256k1`, the typical Bitcoin curve. Alice is known as the initiator. She encodes the public key in the [Elligator Swift](https://eprint.iacr.org/2022/759.pdf) format (64-bytes), optionally pads it with some random garbage bytes, and sends the message to Bob. Bob, known as the responder, decodes the Elligator Swift public key, and derives an ephemeral public/private keypair himself. Using his public and private keys, as well as Alice's public key, Bob performs a variation of the Elliptic Curve Diffie Hellman algorithm to derive a shared key. From this shared key, Bob derives multiple keys and a session ID using the HKDF algorithm. Next, Bob creates garbage data, and sends his public key, garbage data, an encrypted packet using the garbage data, and a version negotiation to Alice. With Bob's public key, Alice derives the shared secret and ensures the decrypted packet is authenticated with the garbage Bob sent her. Finally, Alice sends a "garbage terminator" and an encrypted packet using her garbage data, so Bob may authenticate she derived the correct secret and he can decode her messages. Alice and Bob may now freely exchange encrypted messages over the Bitcoin P2P protocol. | ||
|
||
The crate exposes 4 functions, of which each party need to call only two for a complete handshake. For encrypting and decrypting messages, a `PacketHandler` struct is exposed with two methods. All messages are expected to be a `Vec<u8>` arrays of bytes, as this structure works well with `TcpStream` from the standard library and Bitcoin P2P messages. To initiate a handshake Alice calls `initialize_v2_handshake` and `initiator_complete_v2_handshake`. Similarly, to respond to a V2 handshake, Bob calls `receive_v2_handshake` and `responder_complete_v2_handshake`. Each function creates the appropriate message as well as additional data or structures to complete the handshake. Errors thrown by each of these functions should result in disconnection from the peer. | ||
|
||
```rust | ||
use bip324::{initialize_v2_handshake, initiator_complete_v2_handshake, receive_v2_handshake, responder_complete_v2_handshake}; | ||
fn main() { | ||
// Alice starts a connection with Bob by making a pub/priv keypair and sending a message to Bob. | ||
let handshake_init = initialize_v2_handshake(None).unwrap(); | ||
// Bob parses Alice's message, generates his pub/priv key, and sends a message back. | ||
let mut bob_handshake = receive_v2_handshake(handshake_init.message.clone()).unwrap(); | ||
// Alice finishes her handshake by using her keys from earlier, and sending a final message to Bob. | ||
let alice_completion = initiator_complete_v2_handshake(bob_handshake.message.clone(), handshake_init).unwrap(); | ||
// Bob checks Alice derived the correct keys for the session by authenticating her first message. | ||
let _bob_completion = responder_complete_v2_handshake(alice_completion.message.clone(), &mut bob_handshake).unwrap(); | ||
// Alice and Bob can freely exchange encrypted messages using the packet handler returned by each handshake. | ||
let mut alice = alice_completion.packet_handler; | ||
let mut bob = bob_handshake.packet_handler; | ||
let message = b"Hello world".to_vec(); | ||
let encrypted_message_to_alice = bob.prepare_v2_packet(message.clone(), None, false).unwrap(); | ||
let messages = alice.receive_v2_packets(encrypted_message_to_alice, None).unwrap(); | ||
let secret_message = messages.first().unwrap().message.clone().unwrap(); | ||
assert_eq!(message, secret_message); | ||
let message = b"Goodbye!".to_vec(); | ||
let encrypted_message_to_bob = alice.prepare_v2_packet(message.clone(), None, false).unwrap(); | ||
let messages = bob.receive_v2_packets(encrypted_message_to_bob, None).unwrap(); | ||
let secret_message = messages.first().unwrap().message.clone().unwrap(); | ||
assert_eq!(message, secret_message); | ||
} | ||
``` | ||
|
||
There are also `no_std` compliant versions of these functions which require an RNG to be initialized by the consumer. | ||
|
||
## ChaCha20Poly1305 | ||
|
||
BIP324 elects to use the ChaCha20Poly1305 Authenticated Encryption with Addition Data (AEAD) algorithm under the hood. This is a combination of the ChaCha20 stream cipher and the Poly1305 message authentication code (MAC). In this context, "authentication" refers to the encrypted message's integrity, not to the identity of either party communicating. | ||
|
||
Poly1305 is a purpose-built MAC, as opposed to something like an HMAC using SHA256 which leverages an existing hash scheme to build a message authentication code. Purpose-built introduces new complexity, but also allows for increased performance. | ||
|
||
ChaCha20 and Poly1305 are both implemented in this crate to keep dependencies to a minimum. | ||
|
||
## Development | ||
|
||
The implementation is tested against vectors from the BIP324 reference and a number of additional library tests. | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "bip324-proxy" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license-file = "LICENSE" | ||
description = "BIP324 proxy enabling v1-only clients to use the v2 Bitcoin P2P Protocol" | ||
repository = "https://github.com/rustaceanrob/bip324" | ||
readme = "README.md" | ||
rust-version = "1.56.1" | ||
|
||
[dependencies] | ||
bitcoin = "0.31.1" | ||
tokio = { version = "1.36.0", features = ["full"] } | ||
hex = { package = "hex-conservative", version = "0.2.0" } | ||
bip324 = { path = "../protocol", version = "0.1.0" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Proxy | ||
|
||
A simple proxy process to show off the BIP324 protocol. |
File renamed without changes.
File renamed without changes.