#![cfg(all(feature="cipher_chacha20poly1305ietf", feature="cipher_stream_noisesocket"))] extern crate crypto_proto; use crypto_proto::{ cipher::ChaCha20Poly1305Ietf, cipher_stream::{ CipherStream, NoiseSocket } }; macro_rules! include_tests { ($prefix:expr $(, $file:expr)*) => ( ($(include_bytes!(concat!($prefix, "/", $file))), *) ); } #[test] fn protocols_noisesocket_ok() { let test_vectors: [(&[u8], &[u8], &[u8]); 5] = [ include_tests!("protocols_noisesocket", "ok.0.key", "ok.0.plain", "ok.0.sealed"), include_tests!("protocols_noisesocket", "ok.1.key", "ok.1.plain", "ok.1.sealed"), include_tests!("protocols_noisesocket", "ok.2.key", "ok.2.plain", "ok.2.sealed"), include_tests!("protocols_noisesocket", "ok.3.key", "ok.3.plain", "ok.3.sealed"), include_tests!("protocols_noisesocket", "ok.4.key", "ok.4.plain", "ok.4.sealed") ]; test_vectors.iter().skip(0).take(1).for_each(|t| { // Encrypt & decrypt let encrypted = { let mut stream: Box = Box::new(NoiseSocket::init_for_encryption(&t.0.to_vec().into(), Box::new(ChaCha20Poly1305Ietf::default())).unwrap()); let mut encrypted = vec![0u8; stream.encrypted_size(t.1.len()).unwrap()]; let encrypted_len = stream.encrypt_multiple_blocks(&mut encrypted, t.1).unwrap(); encrypted.truncate(encrypted_len); encrypted }; let decrypted = { let mut stream: Box = Box::new(NoiseSocket::init_for_decryption(&t.0.to_vec().into(), Box::new(ChaCha20Poly1305Ietf::default())).unwrap()); let mut decrypted = vec![0u8; encrypted.len()]; let decrypted_len = stream.decrypt_multiple_blocks(&mut decrypted, &encrypted).unwrap(); decrypted.truncate(decrypted_len); decrypted }; assert_eq!(decrypted, t.1); // Decrypt predefined texts let decrypted = { let mut stream: Box = Box::new(NoiseSocket::init_for_decryption(&t.0.to_vec().into(), Box::new(ChaCha20Poly1305Ietf::default())).unwrap()); let mut decrypted = vec![0u8; t.2.len()]; let decrypted_len = stream.decrypt_multiple_blocks(&mut decrypted, t.2).unwrap(); decrypted.truncate(decrypted_len); decrypted }; assert_eq!(decrypted, t.1); }); } #[test] fn protocols_noisesocket_err() { let test_vectors: [(&[u8], &[u8]); 5] = [ include_tests!("protocols_noisesocket", "err.0.key", "err.0.sealed"), include_tests!("protocols_noisesocket", "err.1.key", "err.1.sealed"), include_tests!("protocols_noisesocket", "err.2.key", "err.2.sealed"), include_tests!("protocols_noisesocket", "err.3.key", "err.3.sealed"), include_tests!("protocols_noisesocket", "err.4.key", "err.4.sealed") ]; test_vectors.iter().for_each(|t| { let decryption_err = { let mut stream: Box = Box::new(NoiseSocket::init_for_decryption(&t.0.to_vec().into(), Box::new(ChaCha20Poly1305Ietf::default())).unwrap()); let mut decrypted = vec![0u8; t.1.len()]; stream.decrypt_multiple_blocks(&mut decrypted, t.1).unwrap_err() }; assert_eq!(decryption_err.kind, crypto_proto::CryptoProtoError::InvalidData); }); }