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
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;
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, )+) => {
pub enum StreamCipherVariant {
$(
$(#[$attr])*
$name($cipher),
)+
}
impl StreamCipherVariant {
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)
}
}
)+
}
}
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,
}
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),
}
}