Crates.io | snowstorm |
lib.rs | snowstorm |
version | 0.4.0 |
source | src |
created_at | 2021-12-23 12:18:04.281154 |
updated_at | 2022-05-20 15:02:09.447882 |
description | A minimalistic encryption protocol based on Noise protocol (snow) |
homepage | |
repository | https://github.com/black-binary/snowstorm |
max_upload_size | |
id | 502211 |
size | 87,481 |
A minimalistic encryption protocol for rust async streams / packets, based on noise protocol and snow.
Snowstorm allows you to secure any streams implemented AsyncRead + AsyncWrite + Unpin
. For example, TcpStream
in Tokio. Note that the underlying connections need to be reliable.
// Noise protocol params, see: http://www.noiseprotocol.org/noise.html#protocol-names-and-modifiers
// Use `KK` to enable bidirectional identity verification
static PATTERN: &str = "Noise_KK_25519_ChaChaPoly_BLAKE2s";
// Generate a private / public key pair
let key_pair = snowstorm::Builder::new(PATTERN.parse()?).generate_keypair().unwrap()
// Connect to the peer
let stream = TcpStream::connect("127.0.0.1:12345").await?;
// The client should build an initiator to launch the handshake process
let initiator = snowstorm::Builder::new(PATTERN.parse()?)
.local_private_key(local_private_key)
.remote_public_key(remote_public_key)
.build_initiator()?;
// Start handshaking
let mut secured_stream = NoiseStream::handshake(stream, initiator).await?;
// A secured stream `NoiseStream<T>` will be return once the handshake is done
secured_stream.write_all(b"hello world").await?;
// Accept a `TcpStream` from the listener
let listener = TcpListener::bind("127.0.0.1:12345").await?;
let (stream, _) = listener.accept().await?;
// The server needs a responder to handle handshake reqeusts from clients
let responder = snowstorm::Builder::new(PATTERN.parse()?)
.local_private_key(local_private_key)
.remote_public_key(remote_public_key)
.build_responder()?;
// Start handshaking
let mut secured_stream = NoiseStream::handshake(stream, responder).await?;
let mut buf = [0; 1024];
secured_stream.read(&mut buf).await?;
[ length
(2 bytes, little endian) ] [ noise message
(length
bytes) ]
[ nonce
(8 bytes) ] [ noise message
]