rename ciphers to stream cipher

This commit is contained in:
Y. T. Chung
2017-02-14 23:13:56 +08:00
parent 3ff5c6ecb2
commit cb051a2bb5
10 changed files with 37 additions and 37 deletions

View File

@@ -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<u8>) -> CipherResult<()>;
fn finalize(&mut self, out: &mut Vec<u8>) -> 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<C>(cipher: C) -> CipherVariant
where CipherVariant: From<C>
pub fn new<C>(cipher: C) -> StreamCipherVariant
where StreamCipherVariant: From<C>
{
From::from(cipher)
}
}
impl Cipher for CipherVariant {
impl StreamCipher for StreamCipherVariant {
fn update(&mut self, data: &[u8], out: &mut Vec<u8>) -> 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<u8>) -> 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)),
}
}

View File

@@ -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<u8>) -> CipherResult<()> {
out.reserve(data.len());
let orig_len = out.len();

View File

@@ -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<u8>) -> CipherResult<()> {
out.extend_from_slice(data);
Ok(())

View File

@@ -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;

View File

@@ -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<u8>) -> CipherResult<()> {
self.worker.update(data, out)
}

View File

@@ -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<u8>) -> CipherResult<()> {
self.crypto.update(data, out)
}

View File

@@ -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<u8>) -> CipherResult<()> {
self.process(data, out)
}

View File

@@ -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<R>
{
reader: R,
buffer: Vec<u8>,
cipher: CipherVariant,
cipher: StreamCipherVariant,
pos: usize,
sent_final: bool,
}
@@ -41,7 +41,7 @@ pub struct DecryptedReader<R>
impl<R> DecryptedReader<R>
where R: Read
{
pub fn new(r: R, cipher: CipherVariant) -> DecryptedReader<R> {
pub fn new(r: R, cipher: StreamCipherVariant) -> DecryptedReader<R> {
DecryptedReader {
reader: r,
buffer: Vec::new(),
@@ -132,14 +132,14 @@ pub struct EncryptedWriter<W>
where W: Write
{
writer: W,
cipher: CipherVariant,
cipher: StreamCipherVariant,
}
impl<W> EncryptedWriter<W>
where W: Write
{
/// Creates a new EncryptedWriter
pub fn new(w: W, cipher: CipherVariant) -> EncryptedWriter<W> {
pub fn new(w: W, cipher: StreamCipherVariant) -> EncryptedWriter<W> {
EncryptedWriter {
writer: w,
cipher: cipher,

View File

@@ -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};

View File

@@ -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};