From cb051a2bb584cd849f8f5d7bfbbf2bbb9542284f Mon Sep 17 00:00:00 2001 From: "Y. T. Chung" Date: Tue, 14 Feb 2017 23:13:56 +0800 Subject: [PATCH] rename ciphers to stream cipher --- src/crypto/cipher.rs | 38 ++++++++++++++++++------------------ src/crypto/crypto.rs | 4 ++-- src/crypto/dummy.rs | 4 ++-- src/crypto/mod.rs | 2 +- src/crypto/openssl.rs | 4 ++-- src/crypto/rc4_md5.rs | 4 ++-- src/crypto/table.rs | 4 ++-- src/relay/tcprelay/stream.rs | 10 +++++----- src/relay/udprelay/local.rs | 2 +- src/relay/udprelay/server.rs | 2 +- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/crypto/cipher.rs b/src/crypto/cipher.rs index 3c4f96a8..620786e0 100644 --- a/src/crypto/cipher.rs +++ b/src/crypto/cipher.rs @@ -42,7 +42,7 @@ use openssl::symm; /// /// The `update` method could be called multiple times, and the `finalize` method will /// encrypt the last block -pub trait Cipher { +pub trait StreamCipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()>; fn finalize(&mut self, out: &mut Vec) -> CipherResult<()>; } @@ -368,29 +368,29 @@ impl Display for CipherType { } } -macro_rules! define_ciphers { +macro_rules! define_stream_ciphers { ($($name:ident => $cipher:ty,)+) => { /// Variant cipher which contains all possible ciphers - pub enum CipherVariant { + pub enum StreamCipherVariant { $( $name($cipher), )+ } - impl CipherVariant { + impl StreamCipherVariant { /// Creates from an actual cipher - pub fn new(cipher: C) -> CipherVariant - where CipherVariant: From + pub fn new(cipher: C) -> StreamCipherVariant + where StreamCipherVariant: From { From::from(cipher) } } - impl Cipher for CipherVariant { + impl StreamCipher for StreamCipherVariant { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { match *self { $( - CipherVariant::$name(ref mut cipher) => cipher.update(data, out), + StreamCipherVariant::$name(ref mut cipher) => cipher.update(data, out), )+ } } @@ -398,23 +398,23 @@ macro_rules! define_ciphers { fn finalize(&mut self, out: &mut Vec) -> CipherResult<()> { match *self { $( - CipherVariant::$name(ref mut cipher) => cipher.finalize(out), + StreamCipherVariant::$name(ref mut cipher) => cipher.finalize(out), )+ } } } $( - impl From<$cipher> for CipherVariant { - fn from(cipher: $cipher) -> CipherVariant { - CipherVariant::$name(cipher) + impl From<$cipher> for StreamCipherVariant { + fn from(cipher: $cipher) -> StreamCipherVariant { + StreamCipherVariant::$name(cipher) } } )+ } } -define_ciphers! { +define_stream_ciphers! { TableCipher => table::TableCipher, DummyCipher => dummy::DummyCipher, Rc4Md5Cipher => rc4_md5::Rc4Md5Cipher, @@ -423,19 +423,19 @@ define_ciphers! { } /// Generate a specific Cipher with key and initialize vector -pub fn with_type(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> CipherVariant { +pub fn with_type(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> StreamCipherVariant { match t { - CipherType::Table => CipherVariant::new(table::TableCipher::new(key, mode)), - CipherType::Dummy => CipherVariant::new(dummy::DummyCipher), + CipherType::Table => StreamCipherVariant::new(table::TableCipher::new(key, mode)), + CipherType::Dummy => StreamCipherVariant::new(dummy::DummyCipher), #[cfg(feature = "cipher-chacha20")] CipherType::ChaCha20 | - CipherType::Salsa20 => CipherVariant::new(CryptoCipher::new(t, key, iv)), + CipherType::Salsa20 => StreamCipherVariant::new(CryptoCipher::new(t, key, iv)), #[cfg(feature = "cipher-rc4")] - CipherType::Rc4Md5 => CipherVariant::new(rc4_md5::Rc4Md5Cipher::new(key, iv, mode)), + CipherType::Rc4Md5 => StreamCipherVariant::new(rc4_md5::Rc4Md5Cipher::new(key, iv, mode)), - _ => CipherVariant::new(openssl::OpenSSLCipher::new(t, key, iv, mode)), + _ => StreamCipherVariant::new(openssl::OpenSSLCipher::new(t, key, iv, mode)), } } diff --git a/src/crypto/crypto.rs b/src/crypto/crypto.rs index ded123cc..60b943d4 100644 --- a/src/crypto/crypto.rs +++ b/src/crypto/crypto.rs @@ -25,7 +25,7 @@ use rust_crypto::symmetriccipher::SynchronousStreamCipher; use rust_crypto::chacha20::ChaCha20; use rust_crypto::salsa20::Salsa20; -use crypto::cipher::{Cipher, CipherType, CipherResult}; +use crypto::cipher::{StreamCipher, CipherType, CipherResult}; /// Cipher provided by Rust-Crypto pub enum CryptoCipher { @@ -44,7 +44,7 @@ impl CryptoCipher { } } -impl Cipher for CryptoCipher { +impl StreamCipher for CryptoCipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { out.reserve(data.len()); let orig_len = out.len(); diff --git a/src/crypto/dummy.rs b/src/crypto/dummy.rs index ec49875a..09654616 100644 --- a/src/crypto/dummy.rs +++ b/src/crypto/dummy.rs @@ -21,14 +21,14 @@ //! Dummy cipher, encrypt and decrypt nothing -use super::{Cipher, CipherResult}; +use super::{StreamCipher, CipherResult}; /// Dummy cipher /// /// Copies data directly to output, very dummy pub struct DummyCipher; -impl Cipher for DummyCipher { +impl StreamCipher for DummyCipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { out.extend_from_slice(data); Ok(()) diff --git a/src/crypto/mod.rs b/src/crypto/mod.rs index 37b787a7..b6b59aca 100644 --- a/src/crypto/mod.rs +++ b/src/crypto/mod.rs @@ -26,7 +26,7 @@ use std::convert::From; use openssl::symm; -pub use self::cipher::{CipherType, Cipher, CipherVariant, CipherResult}; +pub use self::cipher::{CipherType, StreamCipher, StreamCipherVariant, CipherResult}; pub mod cipher; pub mod openssl; diff --git a/src/crypto/openssl.rs b/src/crypto/openssl.rs index f0096534..2d16ffd6 100644 --- a/src/crypto/openssl.rs +++ b/src/crypto/openssl.rs @@ -23,7 +23,7 @@ use std::convert::From; -use crypto::cipher::{Cipher, CipherType, CipherResult}; +use crypto::cipher::{StreamCipher, CipherType, CipherResult}; use crypto::cipher; use crypto::CryptoMode; @@ -135,7 +135,7 @@ impl OpenSSLCipher { unsafe impl Send for OpenSSLCipher {} -impl Cipher for OpenSSLCipher { +impl StreamCipher for OpenSSLCipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { self.worker.update(data, out) } diff --git a/src/crypto/rc4_md5.rs b/src/crypto/rc4_md5.rs index 7ce9e46d..39cdd6b8 100644 --- a/src/crypto/rc4_md5.rs +++ b/src/crypto/rc4_md5.rs @@ -22,7 +22,7 @@ //! Rc4Md5 cipher definition use crypto::openssl::OpenSSLCrypto; -use crypto::cipher::{Cipher, CipherType, CipherResult}; +use crypto::cipher::{StreamCipher, CipherType, CipherResult}; use crypto::digest::{self, Digest, DigestType}; use crypto::CryptoMode; @@ -43,7 +43,7 @@ impl Rc4Md5Cipher { } } -impl Cipher for Rc4Md5Cipher { +impl StreamCipher for Rc4Md5Cipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { self.crypto.update(data, out) } diff --git a/src/crypto/table.rs b/src/crypto/table.rs index f6fd0b55..7b295781 100644 --- a/src/crypto/table.rs +++ b/src/crypto/table.rs @@ -23,7 +23,7 @@ use std::io::BufReader; -use crypto::cipher::{Cipher, CipherResult}; +use crypto::cipher::{StreamCipher, CipherResult}; use crypto::digest::{self, DigestType, Digest}; use crypto::CryptoMode; @@ -78,7 +78,7 @@ impl TableCipher { } } -impl Cipher for TableCipher { +impl StreamCipher for TableCipher { fn update(&mut self, data: &[u8], out: &mut Vec) -> CipherResult<()> { self.process(data, out) } diff --git a/src/relay/tcprelay/stream.rs b/src/relay/tcprelay/stream.rs index 78d3e6cd..d6265f64 100644 --- a/src/relay/tcprelay/stream.rs +++ b/src/relay/tcprelay/stream.rs @@ -22,7 +22,7 @@ use std::io::{self, Read, BufRead, Write}; use std::cmp; -use crypto::{Cipher, CipherVariant}; +use crypto::{StreamCipher, StreamCipherVariant}; use super::BUFFER_SIZE; use super::{EncryptedWrite, DecryptedRead}; @@ -33,7 +33,7 @@ pub struct DecryptedReader { reader: R, buffer: Vec, - cipher: CipherVariant, + cipher: StreamCipherVariant, pos: usize, sent_final: bool, } @@ -41,7 +41,7 @@ pub struct DecryptedReader impl DecryptedReader where R: Read { - pub fn new(r: R, cipher: CipherVariant) -> DecryptedReader { + pub fn new(r: R, cipher: StreamCipherVariant) -> DecryptedReader { DecryptedReader { reader: r, buffer: Vec::new(), @@ -132,14 +132,14 @@ pub struct EncryptedWriter where W: Write { writer: W, - cipher: CipherVariant, + cipher: StreamCipherVariant, } impl EncryptedWriter where W: Write { /// Creates a new EncryptedWriter - pub fn new(w: W, cipher: CipherVariant) -> EncryptedWriter { + pub fn new(w: W, cipher: StreamCipherVariant) -> EncryptedWriter { EncryptedWriter { writer: w, cipher: cipher, diff --git a/src/relay/udprelay/local.rs b/src/relay/udprelay/local.rs index f7dbeabc..5b3f808c 100644 --- a/src/relay/udprelay/local.rs +++ b/src/relay/udprelay/local.rs @@ -77,7 +77,7 @@ use relay::{BoxIoFuture, boxed_future}; use relay::loadbalancing::server::{LoadBalancer, RoundRobin}; use relay::dns_resolver::DnsResolver; use relay::socks5::{Address, UdpAssociateHeader}; -use crypto::cipher::{self, Cipher}; +use crypto::cipher::{self, StreamCipher}; use crypto::CryptoMode; use super::{MAXIMUM_ASSOCIATE_MAP_SIZE, MAXIMUM_UDP_PAYLOAD_SIZE}; diff --git a/src/relay/udprelay/server.rs b/src/relay/udprelay/server.rs index 13991372..28a8d366 100644 --- a/src/relay/udprelay/server.rs +++ b/src/relay/udprelay/server.rs @@ -41,7 +41,7 @@ use config::{Config, ServerConfig}; use relay::{BoxIoFuture, boxed_future}; use relay::dns_resolver::DnsResolver; use relay::socks5::Address; -use crypto::cipher::{self, Cipher}; +use crypto::cipher::{self, StreamCipher}; use crypto::CryptoMode; use super::{MAXIMUM_ASSOCIATE_MAP_SIZE, MAXIMUM_UDP_PAYLOAD_SIZE};