Skip to content

Commit

Permalink
Add dummy cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Oct 26, 2016
1 parent b24f4c9 commit f302298
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/crypto/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crypto::openssl;
use crypto::table;
use crypto::CryptoMode;
use crypto::rc4_md5;
use crypto::dummy;
use crypto::crypto::CryptoCipher;

use crypto::digest::{self, DigestType};
Expand Down Expand Up @@ -120,9 +121,12 @@ const CIPHER_CHACHA20: &'static str = "chacha20";
#[cfg(feature = "cipher-salsa20")]
const CIPHER_SALSA20: &'static str = "salsa20";

const CIPHER_DUMMY: &'static str = "dummy";

#[derive(Clone, Debug, Copy)]
pub enum CipherType {
Table,
Dummy,

#[cfg(feature = "cipher-aes-cfb")]
Aes128Cfb,
Expand Down Expand Up @@ -157,6 +161,7 @@ impl CipherType {
pub fn block_size(&self) -> usize {
match *self {
CipherType::Table => 0,
CipherType::Dummy => 0,

#[cfg(feature = "cipher-aes-cfb")]
CipherType::Aes128Cfb => symm::Type::AES_128_CFB128.block_size(),
Expand Down Expand Up @@ -186,6 +191,7 @@ impl CipherType {
pub fn key_size(&self) -> usize {
match *self {
CipherType::Table => 0,
CipherType::Dummy => 0,

#[cfg(feature = "cipher-aes-cfb")]
CipherType::Aes128Cfb => symm::Type::AES_128_CFB128.key_len(),
Expand Down Expand Up @@ -243,6 +249,7 @@ impl CipherType {
pub fn iv_size(&self) -> usize {
match *self {
CipherType::Table => 0,
CipherType::Dummy => 0,

#[cfg(feature = "cipher-aes-cfb")]
CipherType::Aes128Cfb => symm::Type::AES_128_CFB128.iv_len().unwrap_or(0),
Expand Down Expand Up @@ -286,6 +293,7 @@ impl FromStr for CipherType {
fn from_str(s: &str) -> Result<CipherType, Error> {
match s {
CIPHER_TABLE | "" => Ok(CipherType::Table),
CIPHER_DUMMY => Ok(CipherType::Dummy),
#[cfg(feature = "cipher-aes-cfb")]
CIPHER_AES_128_CFB => Ok(CipherType::Aes128Cfb),
#[cfg(feature = "cipher-aes-cfb")]
Expand Down Expand Up @@ -323,6 +331,7 @@ impl Display for CipherType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
CipherType::Table => write!(f, "{}", CIPHER_TABLE),
CipherType::Dummy => write!(f, "{}", CIPHER_DUMMY),
#[cfg(feature = "cipher-aes-cfb")]
CipherType::Aes128Cfb => write!(f, "{}", CIPHER_AES_128_CFB),
#[cfg(feature = "cipher-aes-cfb")]
Expand Down Expand Up @@ -400,6 +409,7 @@ macro_rules! define_ciphers {

define_ciphers! {
TableCipher => table::TableCipher,
DummyCipher => dummy::DummyCipher,
Rc4Md5Cipher => rc4_md5::Rc4Md5Cipher,
OpenSSLCipher => openssl::OpenSSLCipher,
CryptoCipher => CryptoCipher,
Expand All @@ -409,6 +419,7 @@ define_ciphers! {
pub fn with_type(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> CipherVariant {
match t {
CipherType::Table => CipherVariant::new(table::TableCipher::new(key, mode)),
CipherType::Dummy => CipherVariant::new(dummy::DummyCipher),

#[cfg(feature = "cipher-chacha20")]
CipherType::ChaCha20 => CipherVariant::new(CryptoCipher::new(t, key, iv)),
Expand Down
35 changes: 35 additions & 0 deletions src/crypto/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// The MIT License (MIT)

// Copyright (c) 2014 Y. T. CHUNG <zonyitoo@gmail.com>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use super::{Cipher, CipherResult};

pub struct DummyCipher;

impl Cipher for DummyCipher {
fn update(&mut self, data: &[u8], out: &mut Vec<u8>) -> CipherResult<()> {
out.extend_from_slice(data);
Ok(())
}

fn finalize(&mut self, _: &mut Vec<u8>) -> CipherResult<()> {
Ok(())
}
}
3 changes: 2 additions & 1 deletion src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ use std::convert::From;

use openssl::crypto::symm;

pub use self::cipher::{CipherType, Cipher, CipherVariant};
pub use self::cipher::{CipherType, Cipher, CipherVariant, CipherResult};

pub mod cipher;
pub mod openssl;
pub mod digest;
pub mod table;
pub mod rc4_md5;
pub mod crypto;
pub mod dummy;

#[derive(Clone, Copy)]
pub enum CryptoMode {
Expand Down

0 comments on commit f302298

Please sign in to comment.