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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! Stream ciphers

use std::ops::{Deref, DerefMut};

#[cfg(feature = "rc4")]
use crate::crypto::rc4_md5;
#[cfg(feature = "sodium")]
use crate::crypto::sodium;
use crate::crypto::{
    cipher::{CipherCategory, CipherResult, CipherType},
    dummy,
    openssl,
    table,
    CryptoMode,
};

use bytes::BufMut;

/// Basic operation of Cipher, which is a Symmetric Cipher.
///
/// The `update` method could be called multiple times, and the `finalize` method will
/// encrypt the last block
pub trait StreamCipher {
    fn update<B: BufMut>(&mut self, data: &[u8], out: &mut B) -> CipherResult<()>;
    fn finalize<B: BufMut>(&mut self, out: &mut B) -> CipherResult<()>;
    fn buffer_size(&self, data: &[u8]) -> usize;
}

macro_rules! define_stream_ciphers {
    ($( $(#[$attr:meta])* pub $name:ident => $cipher:ty, )+) => {
        /// Variant cipher which contains all possible ciphers
        pub enum StreamCipherVariant {
            $(
                $(#[$attr])*
                $name($cipher),
            )+
        }

        impl StreamCipherVariant {
            /// Creates from an actual cipher
            pub fn new<C>(cipher: C) -> StreamCipherVariant
                where StreamCipherVariant: From<C>
            {
                From::from(cipher)
            }
        }

        impl StreamCipher for StreamCipherVariant {
            fn update<B: BufMut>(&mut self, data: &[u8], out: &mut B) -> CipherResult<()> {
                match *self {
                    $(
                        $(#[$attr])*
                        StreamCipherVariant::$name(ref mut cipher) => cipher.update(data, out),
                    )+
                }
            }

            fn finalize<B: BufMut>(&mut self, out: &mut B) -> CipherResult<()> {
                match *self {
                    $(
                        $(#[$attr])*
                        StreamCipherVariant::$name(ref mut cipher) => cipher.finalize(out),
                    )+
                }
            }

            fn buffer_size(&self, data: &[u8]) -> usize {
                match *self {
                    $(
                        $(#[$attr])*
                        StreamCipherVariant::$name(ref cipher) => cipher.buffer_size(data),
                    )+
                }
            }
        }

        $(
            $(#[$attr])*
            impl From<$cipher> for StreamCipherVariant {
                fn from(cipher: $cipher) -> StreamCipherVariant {
                    StreamCipherVariant::$name(cipher)
                }
            }
        )+
    }
}

/// Variant cipher which contains all possible stream ciphers
pub struct BoxStreamCipher {
    cipher: Box<StreamCipherVariant>,
}

impl Deref for BoxStreamCipher {
    type Target = StreamCipherVariant;

    fn deref(&self) -> &StreamCipherVariant {
        &*self.cipher
    }
}

impl DerefMut for BoxStreamCipher {
    fn deref_mut(&mut self) -> &mut StreamCipherVariant {
        &mut *self.cipher
    }
}

define_stream_ciphers! {
    pub TableCipher => table::TableCipher,
    pub DummyCipher => dummy::DummyCipher,
    #[cfg(feature = "rc4")]
    pub Rc4Md5Cipher => rc4_md5::Rc4Md5Cipher,
    pub OpenSSLCipher => openssl::OpenSSLCipher,
    #[cfg(feature = "sodium")]
    pub SodiumStreamCipher => sodium::SodiumStreamCipher,
}

/// Generate a specific Cipher with key and initialize vector
pub fn new_stream(t: CipherType, key: &[u8], iv: &[u8], mode: CryptoMode) -> BoxStreamCipher {
    assert!(
        t.category() == CipherCategory::Stream,
        "only allow initializing with stream cipher"
    );

    let cipher = match t {
        CipherType::Table => StreamCipherVariant::new(table::TableCipher::new(key, mode)),
        CipherType::Plain => StreamCipherVariant::new(dummy::DummyCipher),

        #[cfg(feature = "sodium")]
        CipherType::ChaCha20 | CipherType::Salsa20 | CipherType::XSalsa20 | CipherType::ChaCha20Ietf => {
            StreamCipherVariant::new(sodium::SodiumStreamCipher::new(t, key, iv))
        }

        #[cfg(feature = "rc4")]
        CipherType::Rc4Md5 => StreamCipherVariant::new(rc4_md5::Rc4Md5Cipher::new(key, iv, mode)),

        _ => StreamCipherVariant::new(openssl::OpenSSLCipher::new(t, key, iv, mode)),
    };
    BoxStreamCipher {
        cipher: Box::new(cipher),
    }
}