snow/
lib.rs

1//! The `snow` crate is a straightforward, Hard To Fuck Up™ Noise Protocol implementation.
2//!
3//! Read the [Noise Protocol Framework Spec](https://noiseprotocol.org/noise.html) for more
4//! information.
5//!
6//! The typical usage flow is to use [`Builder`] to construct a [`HandshakeState`], where you
7//! will complete the handshake phase and convert into either a [`TransportState`] or
8//! [`StatelessTransportState`].
9//!
10//! # Examples
11//! See `examples/simple.rs` for a more complete TCP client/server example with static keys.
12//!
13//! ```
14//! # use snow::Error;
15//! #
16//! # #[cfg(any(feature = "default-resolver", feature = "ring-accelerated"))]
17//! # fn try_main() -> Result<(), Error> {
18//! static PATTERN: &'static str = "Noise_NN_25519_ChaChaPoly_BLAKE2s";
19//!
20//! let mut initiator = snow::Builder::new(PATTERN.parse()?)
21//!     .build_initiator()?;
22//! let mut responder = snow::Builder::new(PATTERN.parse()?)
23//!     .build_responder()?;
24//!
25//! let (mut read_buf, mut first_msg, mut second_msg) =
26//!     ([0u8; 1024], [0u8; 1024], [0u8; 1024]);
27//!
28//! // -> e
29//! let len = initiator.write_message(&[], &mut first_msg)?;
30//!
31//! // responder processes the first message...
32//! responder.read_message(&first_msg[..len], &mut read_buf)?;
33//!
34//! // <- e, ee
35//! let len = responder.write_message(&[], &mut second_msg)?;
36//!
37//! // initiator processes the response...
38//! initiator.read_message(&second_msg[..len], &mut read_buf)?;
39//!
40//! // NN handshake complete, transition into transport mode.
41//! let initiator = initiator.into_transport_mode();
42//! let responder = responder.into_transport_mode();
43//! #     Ok(())
44//! # }
45//! #
46//! # #[cfg(not(any(feature = "default-resolver", feature = "ring-accelerated")))]
47//! # fn try_main() -> Result<(), ()> { Ok(()) }
48//! #
49//! # fn main() {
50//! #     try_main().unwrap();
51//! # }
52//! ```
53
54#![warn(missing_docs)]
55
56macro_rules! copy_slices {
57    ($inslice:expr, $outslice:expr) => {
58        $outslice[..$inslice.len()].copy_from_slice(&$inslice[..])
59    };
60}
61
62macro_rules! static_slice {
63    ($_type:ty: $($item:expr),*) => ({
64        static STATIC_SLICE: &'static [$_type] = &[$($item),*];
65        STATIC_SLICE
66    });
67}
68
69mod builder;
70mod cipherstate;
71mod constants;
72pub mod error;
73mod handshakestate;
74mod stateless_transportstate;
75mod symmetricstate;
76mod transportstate;
77mod utils;
78
79pub mod params;
80pub mod resolvers;
81pub mod types;
82
83pub use crate::{
84    builder::{Builder, Keypair},
85    error::Error,
86    handshakestate::HandshakeState,
87    stateless_transportstate::StatelessTransportState,
88    transportstate::TransportState,
89};