1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
//! # Symmetric Cipher Layer //! //! This library intends to encrypt a bidirectional stream with two symmetric ciphers of //! the same type. //! //! Currently the only cipher available is [Salsa20](https://github.com/RustCrypto/stream-ciphers). //! ```edition2018 //! # use std::{net::SocketAddr, str::FromStr}; //! # use tokio::net::{TcpStream, TcpListener}; //! # use ciph::salsa::{Psk, Randomness, Connector, Acceptor}; //! # const PSK_B64: &str = include_str!("../examples/example.psk"); //! # async fn stunted_serve(listener: TcpListener, acceptor: Acceptor) { //! # let (stream, _) = listener //! # .accept() //! # .await //! # .expect("Can't accept new TCP connection."); //! # let establish = acceptor.accept(stream).await.expect("Can't setup cipher."); //! # } //! # async fn spawn_server( //! # psk: Psk, randomness: Randomness //! # ) -> Result<SocketAddr, Box<dyn std::error::Error>> { //! # let acceptor = Acceptor::new(psk, randomness); //! # let addr = SocketAddr::from(([127, 0, 0, 1], 0)); //! # let listener = TcpListener::bind(&addr).await?; //! # let bound_addr = listener.local_addr()?; //! # //! # tokio::spawn(stunted_serve(listener, acceptor)); //! # //! # Ok(bound_addr) //! # } //! # #[tokio::main] //! # async fn main() -> Result<(), Box<dyn std::error::Error>> { //! let psk = Psk::from_str(PSK_B64)?; //! let addr = spawn_server(psk.clone(), Randomness::Entropy).await?; //! let connector = Connector::new(psk, Randomness::Entropy); //! let tcp_stream = TcpStream::connect(addr).await?; //! let connect = connector.connect(tcp_stream); //! let mut encrypted_stream = connect.await?; //! # Ok(()) //! # } //! ``` //! //! Unix timestamp is used for initial handshake cipher nonce. This is the cipher produced //! from the key in the `Psk`. CLient and Server must have synchronized clocks. A variance //! of 60 seconds is tolerated. pub mod salsa;