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
142
143
144
145
146
147
148
149
150
151
152
153
use futures::{
io::{self, AsyncWrite},
ready,
task::{Context, Poll},
};
use log::trace;
use pin_project::pin_project;
use salsa20::{cipher::SyncStreamCipher, XSalsa20};
use std::{fmt, pin::Pin};
#[pin_project]
pub struct CryptWriter<W> {
#[pin]
inner: W,
buf: Vec<u8>,
cipher: XSalsa20,
}
impl<W: AsyncWrite> CryptWriter<W> {
pub fn with_capacity(capacity: usize, inner: W, cipher: XSalsa20) -> CryptWriter<W> {
CryptWriter {
inner,
buf: Vec::with_capacity(capacity),
cipher,
}
}
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
self.project().inner
}
}
fn poll_flush_buf<W: AsyncWrite>(
inner: &mut Pin<&mut W>,
buf: &mut Vec<u8>,
cx: &mut Context<'_>,
) -> Poll<io::Result<()>> {
let mut ret = Poll::Ready(Ok(()));
let mut written = 0;
let len = buf.len();
while written < len {
match inner.as_mut().poll_write(cx, &buf[written..]) {
Poll::Ready(Ok(n)) => {
if n > 0 {
written += n;
} else {
ret = Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero,
"Failed to write buffered data",
)));
break;
}
}
Poll::Ready(Err(e)) => {
if e.kind() != io::ErrorKind::Interrupted {
ret = Poll::Ready(Err(e));
break;
}
}
Poll::Pending => {
ret = Poll::Pending;
break;
}
}
}
if written > 0 {
buf.drain(..written);
}
if let Poll::Ready(Ok(())) = ret { debug_assert!(buf.is_empty()); }
ret
}
impl<W: AsyncWrite> AsyncWrite for CryptWriter<W> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let mut this = self.project();
ready!(poll_flush_buf(&mut this.inner, this.buf, cx))?;
debug_assert!(this.buf.is_empty());
let res = Pin::new(&mut *this.buf).poll_write(cx, buf);
if let Poll::Ready(Ok(count)) = res {
this.cipher.apply_keystream(&mut this.buf[0..count]);
trace!("encrypted {} bytes", count);
} else {
debug_assert!(false);
};
if let Poll::Ready(Err(e)) = poll_flush_buf(&mut this.inner, this.buf, cx) {
Poll::Ready(Err(e))
} else {
res
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();
ready!(poll_flush_buf(&mut this.inner, this.buf, cx))?;
this.inner.poll_flush(cx)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();
ready!(poll_flush_buf(&mut this.inner, this.buf, cx))?;
this.inner.poll_close(cx)
}
}
impl<W: AsyncWrite + fmt::Debug> fmt::Debug for CryptWriter<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CryptWriter")
.field("writer", &self.inner)
.field("buf", &self.buf)
.finish()
}
}