Created
December 8, 2022 17:25
-
-
Save hMihaiDavid/dd2c58378b64b6ae9062cd1a743fe618 to your computer and use it in GitHub Desktop.
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
/* | |
[dependencies] | |
snow = "0.9.0" | |
clap = "4.0.27" | |
**/ | |
use clap::{arg, Command}; | |
use snow::{params::NoiseParams, Builder}; | |
use std::{ | |
io::{self, Read, Write}, | |
net::{TcpListener, TcpStream}, | |
}; | |
// These keys have been pre-generated with Builder::generate_keypair with the | |
// script in the comment at the end of this file. | |
static STATIC_PRIVKEY_CLIENT: [u8; 32] = [ | |
52, 18, 25, 179, 157, 119, 221, 39, 189, 53, 115, 58, 182, 224, 141, 74, 87, | |
145, 180, 253, 227, 25, 109, 202, 213, 65, 55, 143, 112, 185, 166, 27 | |
]; | |
static STATIC_PUBKEY_CLIENT: [u8; 32]= [ | |
160, 244, 254, 226, 58, 148, 38, 56, 41, 161, 244, 175, 23, 33, 50, 199, | |
213, 141, 70, 204, 247, 190, 58, 165, 19, 137, 84, 41, 230, 101, 162, | |
87 | |
]; | |
static STATIC_PRIVKEY_SERVER: [u8; 32] = [ | |
136, 238, 164, 3, 190, 116, 47, 145, 129, 76, 125, 37, 49, 4, 135, 41, 198, | |
94, 198, 190, 70, 136, 146, 167, 202, 81, 109, 142, 119, 143, 170, 103 | |
]; | |
static STATIC_PUBKEY_SERVER: [u8; 32]= [ | |
171, 232, 231, 4, 67, 146, 198, 47, 139, 164, 103, 99, 4, 80, 169, 147, 238, | |
243, 104, 25, 107, 250, 227, 87, 237, 108, 18, 85, 119, 91, 201, 62 | |
]; | |
fn main() { | |
let matches = Command::new("simple") | |
.arg(arg!(-s --server "Server mode")) | |
.get_matches(); | |
if matches.get_flag("server") { | |
println!("Running server..."); | |
run_server(); | |
} else { | |
println!("Running client..."); | |
run_client(); | |
} | |
} | |
fn run_server() { | |
let mut buf = vec![0u8; 65535]; | |
let noise_params: NoiseParams = "Noise_XX_25519_ChaChaPoly_BLAKE2s" | |
.parse().unwrap(); | |
let noise_builder = Builder::new(noise_params); | |
let mut noise = noise_builder | |
.local_private_key(&STATIC_PRIVKEY_SERVER) | |
//.remote_public_key(&STATIC_PUBKEY_CLIENT) | |
.build_responder().unwrap(); | |
let (mut stream, _) = TcpListener::bind("127.0.0.1:9999").unwrap().accept().unwrap(); | |
println!("server accepted connection..."); | |
// <- e | |
let msg = recv(&mut stream).unwrap(); | |
let payload_len = noise.read_message(&msg, &mut []).unwrap(); | |
println!("<- e (payload_len: {})", payload_len); | |
// -> e, ee, s, es | |
let len = noise.write_message(&[0u8; 0], &mut buf).unwrap(); | |
println!("-> e, ee, s, es (payload_len: 0)"); | |
send(&mut stream, &buf[..len]); | |
// <- s, se | |
let msg = recv(&mut stream).unwrap(); | |
let payload_len = noise.read_message(&msg, &mut buf).unwrap(); | |
println!("<- s, se (payload_len: {})", payload_len); | |
let mut noise = noise.into_transport_mode().unwrap(); | |
println!("server session established..."); | |
let remote_pubkey_from_transport_state = noise.get_remote_static(); | |
println!("server: remote_pubkey_from_transport_state: {:?}", remote_pubkey_from_transport_state); | |
while let Ok(msg) = recv(&mut stream) { | |
let len = noise.read_message(&msg, &mut buf).unwrap(); | |
println!("client said: {}", String::from_utf8_lossy(&buf[..len])); | |
} | |
println!("======== SERVER QUITS ========"); | |
} | |
fn run_client() { | |
let mut buf = vec![0u8; 65535]; | |
let noise_params: NoiseParams = "Noise_XX_25519_ChaChaPoly_BLAKE2s" | |
.parse().unwrap(); | |
let noise_builder = Builder::new(noise_params); | |
let mut noise = noise_builder | |
.local_private_key(&STATIC_PRIVKEY_CLIENT) | |
//.remote_public_key(&STATIC_PUBKEY_SERVER) | |
.build_initiator().unwrap(); | |
let mut stream = TcpStream::connect("127.0.0.1:9999").unwrap(); | |
println!("client connected..."); | |
// -> e | |
let msg_len = noise.write_message(&[], &mut buf).unwrap(); | |
println!("-> e (payload_len: 0)"); | |
send(&mut stream, &buf[..msg_len]); | |
// <- e, ee, s, es | |
let msg = recv(&mut stream).unwrap(); | |
let payload_len = noise.read_message(&msg, &mut buf).unwrap(); | |
println!("<- e, ee, s, es (payload_len: {})", payload_len); | |
// -> s, se | |
let len = noise.write_message(&[], &mut buf).unwrap(); | |
println!("->s, se (payload_len: 0)"); | |
send(&mut stream, &buf[..len]); | |
let mut noise = noise.into_transport_mode().unwrap(); | |
println!("client session established..."); | |
let remote_pubkey_from_transport_state = noise.get_remote_static(); | |
println!("client: remote_pubkey_from_transport_state: {:?}", remote_pubkey_from_transport_state); | |
for _ in 0..10 { | |
let data = b"some data AAAAAAAAAAA"; | |
println!("-> data ({})", data.len()); | |
let len = noise.write_message(data, &mut buf).unwrap(); | |
send(&mut stream, &buf[..len]); | |
} | |
println!("======== CLIENT QUITS ========"); | |
} | |
// Copy pasta from snow crate example | |
/// Hyper-basic stream transport receiver. 16-bit BE size followed by payload. | |
fn recv(stream: &mut TcpStream) -> io::Result<Vec<u8>> { | |
let mut msg_len_buf = [0u8; 2]; | |
stream.read_exact(&mut msg_len_buf)?; | |
let msg_len = ((msg_len_buf[0] as usize) << 8) + (msg_len_buf[1] as usize); | |
let mut msg = vec![0u8; msg_len]; | |
stream.read_exact(&mut msg[..])?; | |
println!("recv 2+{} {:?}", msg.len(), msg); | |
Ok(msg) | |
} | |
/// Hyper-basic stream transport sender. 16-bit BE size followed by payload. | |
fn send(stream: &mut TcpStream, buf: &[u8]) { | |
println!("send 2+{} {:?}", buf.len(), buf); | |
let msg_len_buf = [(buf.len() >> 8) as u8, (buf.len() & 0xff) as u8]; | |
stream.write_all(&msg_len_buf).unwrap(); | |
stream.write_all(buf).unwrap(); | |
} | |
// Little script to generate keypairs: | |
/* | |
use snow::{params::NoiseParams, Builder, Keypair}; | |
use std::fmt; | |
struct WrapKeypair(Keypair); | |
impl fmt::Debug for WrapKeypair { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "public: {:?} private: {:?}", self.0.public, self.0.private) | |
} | |
} | |
fn main() { | |
let noise_params: NoiseParams = "Noise_XXpsk3_25519_ChaChaPoly_BLAKE2s" | |
.parse().unwrap(); | |
let noise_builder = Builder::new(noise_params); | |
let keypair = noise_builder.generate_keypair().unwrap(); | |
println!("{:?}", WrapKeypair(keypair)); | |
} | |
*/ |
Same but uncommenting lines 61 and 107 (.remote_public_key(...))
Server:
Running server...
server accepted connection...
recv 2+32 [112, 119, 143, 37, 146, 209, 54, 45, 209, 13, 6, 63, 24, 249, 111, 86, 189, 228, 82, 93, 12, 67, 185, 96, 84, 254, 34, 28, 251, 186, 213, 28]
<- e (payload_len: 0)
-> e, ee, s, es (payload_len: 0)
send 2+96 [240, 153, 60, 213, 206, 120, 3, 251, 107, 74, 249, 69, 51, 228, 31, 29, 91, 147, 3, 12, 171, 207, 92, 167, 129, 185, 252, 181, 80, 91, 160, 26, 89, 209, 103, 174, 57, 113, 207, 57, 94, 101, 127, 13, 82, 113, 112, 212, 215, 52, 197, 125, 255, 115, 54, 224, 184, 249, 1, 43, 179, 159, 185, 160, 180, 49, 18, 84, 173, 219, 189, 46, 228, 243, 127, 230, 2, 10, 233, 26, 154, 3, 229, 141, 105, 94, 155, 149, 10, 42, 205, 156, 139, 190, 98, 163]
recv 2+64 [123, 80, 91, 249, 249, 110, 225, 235, 49, 234, 130, 192, 39, 185, 60, 1, 77, 254, 88, 139, 39, 58, 181, 216, 106, 220, 90, 149, 85, 217, 191, 17, 211, 219, 135, 99, 15, 163, 175, 60, 30, 118, 2, 47, 13, 130, 152, 239, 106, 76, 170, 84, 67, 148, 212, 204, 59, 208, 157, 239, 35, 169, 20, 96]
<- s, se (payload_len: 0)
server session established...
server: remote_pubkey_from_transport_state: Some([117, 128, 194, 127, 206, 190, 207, 254, 6, 153, 92, 189, 163, 188, 72, 85, 154, 208, 248, 225, 6, 73, 127, 125, 177, 188, 120, 86, 192, 210, 29, 8])
recv 2+37 [119, 162, 41, 50, 255, 8, 119, 196, 132, 59, 132, 218, 103, 125, 211, 209, 13, 154, 153, 250, 241, 76, 152, 44, 101, 224, 127, 202, 179, 213, 141, 167, 169, 173, 129, 151, 220]
client said: some data AAAAAAAAAAA
recv 2+37 [90, 133, 120, 231, 245, 69, 1, 22, 135, 116, 208, 64, 1, 140, 174, 115, 214, 119, 231, 210, 230, 198, 144, 2, 151, 234, 36, 41, 195, 194, 22, 106, 240, 29, 241, 252, 1]
client said: some data AAAAAAAAAAA
recv 2+37 [104, 58, 228, 209, 86, 75, 186, 105, 170, 189, 167, 52, 97, 165, 199, 73, 129, 188, 9, 141, 128, 161, 113, 34, 248, 176, 252, 71, 208, 21, 44, 240, 77, 192, 48, 229, 217]
client said: some data AAAAAAAAAAA
recv 2+37 [204, 234, 75, 220, 163, 114, 143, 134, 69, 221, 58, 145, 138, 0, 149, 13, 131, 157, 218, 39, 154, 157, 223, 234, 84, 3, 238, 245, 187, 180, 211, 100, 45, 248, 119, 159, 137]
client said: some data AAAAAAAAAAA
recv 2+37 [61, 133, 31, 33, 43, 159, 75, 108, 1, 244, 253, 181, 59, 203, 28, 182, 19, 89, 250, 228, 1, 43, 95, 185, 79, 89, 101, 47, 152, 24, 246, 84, 127, 24, 215, 175, 174]
client said: some data AAAAAAAAAAA
recv 2+37 [184, 85, 112, 98, 250, 20, 118, 30, 163, 56, 149, 205, 164, 69, 243, 12, 85, 96, 69, 137, 167, 226, 2, 46, 108, 168, 201, 73, 220, 115, 188, 32, 118, 160, 10, 8, 143]
client said: some data AAAAAAAAAAA
recv 2+37 [153, 158, 189, 212, 42, 94, 89, 242, 31, 123, 231, 24, 215, 184, 178, 218, 101, 176, 76, 43, 221, 79, 84, 249, 33, 18, 221, 145, 213, 190, 69, 147, 182, 77, 175, 44, 144]
client said: some data AAAAAAAAAAA
recv 2+37 [91, 227, 42, 234, 10, 115, 238, 143, 176, 121, 233, 177, 188, 94, 251, 61, 212, 169, 9, 130, 180, 23, 215, 160, 55, 164, 42, 61, 208, 247, 67, 121, 215, 142, 171, 15, 73]
client said: some data AAAAAAAAAAA
recv 2+37 [64, 163, 155, 180, 180, 66, 101, 174, 107, 244, 172, 124, 225, 225, 102, 81, 184, 127, 37, 206, 108, 14, 100, 105, 180, 58, 61, 36, 163, 9, 107, 218, 175, 200, 43, 35, 0]
client said: some data AAAAAAAAAAA
recv 2+37 [117, 108, 89, 131, 194, 227, 222, 242, 160, 117, 214, 165, 246, 190, 179, 254, 50, 96, 230, 124, 78, 136, 224, 106, 204, 63, 60, 165, 113, 116, 150, 63, 74, 133, 194, 175, 80]
client said: some data AAAAAAAAAAA
======== SERVER QUITS ========
Client:
Running client...
client connected...
-> e (payload_len: 0)
send 2+32 [112, 119, 143, 37, 146, 209, 54, 45, 209, 13, 6, 63, 24, 249, 111, 86, 189, 228, 82, 93, 12, 67, 185, 96, 84, 254, 34, 28, 251, 186, 213, 28]
recv 2+96 [240, 153, 60, 213, 206, 120, 3, 251, 107, 74, 249, 69, 51, 228, 31, 29, 91, 147, 3, 12, 171, 207, 92, 167, 129, 185, 252, 181, 80, 91, 160, 26, 89, 209, 103, 174, 57, 113, 207, 57, 94, 101, 127, 13, 82, 113, 112, 212, 215, 52, 197, 125, 255, 115, 54, 224, 184, 249, 1, 43, 179, 159, 185, 160, 180, 49, 18, 84, 173, 219, 189, 46, 228, 243, 127, 230, 2, 10, 233, 26, 154, 3, 229, 141, 105, 94, 155, 149, 10, 42, 205, 156, 139, 190, 98, 163]
<- e, ee, s, es (payload_len: 0)
->s, se (payload_len: 0)
send 2+64 [123, 80, 91, 249, 249, 110, 225, 235, 49, 234, 130, 192, 39, 185, 60, 1, 77, 254, 88, 139, 39, 58, 181, 216, 106, 220, 90, 149, 85, 217, 191, 17, 211, 219, 135, 99, 15, 163, 175, 60, 30, 118, 2, 47, 13, 130, 152, 239, 106, 76, 170, 84, 67, 148, 212, 204, 59, 208, 157, 239, 35, 169, 20, 96]
client session established...
client: remote_pubkey_from_transport_state: Some([52, 18, 25, 179, 157, 119, 221, 39, 189, 53, 115, 58, 182, 224, 141, 74, 87, 145, 180, 253, 227, 25, 109, 202, 213, 65, 55, 143, 112, 185, 166, 27])
-> data (21)
send 2+37 [119, 162, 41, 50, 255, 8, 119, 196, 132, 59, 132, 218, 103, 125, 211, 209, 13, 154, 153, 250, 241, 76, 152, 44, 101, 224, 127, 202, 179, 213, 141, 167, 169, 173, 129, 151, 220]
-> data (21)
send 2+37 [90, 133, 120, 231, 245, 69, 1, 22, 135, 116, 208, 64, 1, 140, 174, 115, 214, 119, 231, 210, 230, 198, 144, 2, 151, 234, 36, 41, 195, 194, 22, 106, 240, 29, 241, 252, 1]
-> data (21)
send 2+37 [104, 58, 228, 209, 86, 75, 186, 105, 170, 189, 167, 52, 97, 165, 199, 73, 129, 188, 9, 141, 128, 161, 113, 34, 248, 176, 252, 71, 208, 21, 44, 240, 77, 192, 48, 229, 217]
-> data (21)
send 2+37 [204, 234, 75, 220, 163, 114, 143, 134, 69, 221, 58, 145, 138, 0, 149, 13, 131, 157, 218, 39, 154, 157, 223, 234, 84, 3, 238, 245, 187, 180, 211, 100, 45, 248, 119, 159, 137]
-> data (21)
send 2+37 [61, 133, 31, 33, 43, 159, 75, 108, 1, 244, 253, 181, 59, 203, 28, 182, 19, 89, 250, 228, 1, 43, 95, 185, 79, 89, 101, 47, 152, 24, 246, 84, 127, 24, 215, 175, 174]
-> data (21)
send 2+37 [184, 85, 112, 98, 250, 20, 118, 30, 163, 56, 149, 205, 164, 69, 243, 12, 85, 96, 69, 137, 167, 226, 2, 46, 108, 168, 201, 73, 220, 115, 188, 32, 118, 160, 10, 8, 143]
-> data (21)
send 2+37 [153, 158, 189, 212, 42, 94, 89, 242, 31, 123, 231, 24, 215, 184, 178, 218, 101, 176, 76, 43, 221, 79, 84, 249, 33, 18, 221, 145, 213, 190, 69, 147, 182, 77, 175, 44, 144]
-> data (21)
send 2+37 [91, 227, 42, 234, 10, 115, 238, 143, 176, 121, 233, 177, 188, 94, 251, 61, 212, 169, 9, 130, 180, 23, 215, 160, 55, 164, 42, 61, 208, 247, 67, 121, 215, 142, 171, 15, 73]
-> data (21)
send 2+37 [64, 163, 155, 180, 180, 66, 101, 174, 107, 244, 172, 124, 225, 225, 102, 81, 184, 127, 37, 206, 108, 14, 100, 105, 180, 58, 61, 36, 163, 9, 107, 218, 175, 200, 43, 35, 0]
-> data (21)
send 2+37 [117, 108, 89, 131, 194, 227, 222, 242, 160, 117, 214, 165, 246, 190, 179, 254, 50, 96, 230, 124, 78, 136, 224, 106, 204, 63, 60, 165, 113, 116, 150, 63, 74, 133, 194, 175, 80]
======== CLIENT QUITS ========
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Server:
Client: