Skip to content

Commit

Permalink
chore: change by comments
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jan 19, 2021
1 parent 85b1223 commit c4f0f76
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
4 changes: 2 additions & 2 deletions secio/src/codec/secure_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum RecvBuf {
}

impl RecvBuf {
fn copy_to(&mut self, buf: &mut [u8], size: usize) {
fn drain_to(&mut self, buf: &mut [u8], size: usize) {
match self {
RecvBuf::Vec(ref mut b) => {
buf[..size].copy_from_slice(b.drain(..size).as_slice());
Expand Down Expand Up @@ -142,7 +142,7 @@ where
let n = ::std::cmp::min(buf.len(), self.recv_buf.len());

// Copy data to the output buffer
self.recv_buf.copy_to(buf, n);
self.recv_buf.drain_to(buf, n);

n
}
Expand Down
2 changes: 1 addition & 1 deletion secio/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait StreamCipher {
false
}
/// Feeds data from input through the cipher, in place decrypted.
fn decrypt_in_place(&mut self, _input: &mut bytes::BytesMut) -> Result<(), SecioError> {
fn decrypt_in_place(&mut self, _input: &mut [u8]) -> Result<(), SecioError> {
Err(SecioError::InvalidProposition(
"don't support in place decrypted",
))
Expand Down
14 changes: 4 additions & 10 deletions secio/src/crypto/ring_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,22 @@ impl RingAeadCipher {
buf.copy_from_slice(input);

if let RingAeadCryptoVariant::Open(ref mut key) = self.cipher {
match key.open_in_place(Aad::empty(), &mut buf) {
Ok(_) => (),
Err(e) => return Err(e.into()),
}
key.open_in_place(Aad::empty(), &mut buf)?;
} else {
unreachable!("encrypt is called on a non-open cipher")
}
buf.truncate(output_len);
Ok(buf)
}

pub fn decrypt_in_place(&mut self, input: &mut bytes::BytesMut) -> Result<(), SecioError> {
pub fn decrypt_in_place(&mut self, input: &mut [u8]) -> Result<(), SecioError> {
let output_len = input
.len()
.checked_sub(self.cipher_type.tag_size())
.ok_or(SecioError::FrameTooShort)?;

if let RingAeadCryptoVariant::Open(ref mut key) = self.cipher {
match key.open_in_place(Aad::empty(), input) {
Ok(_) => (),
Err(e) => return Err(e.into()),
}
key.open_in_place(Aad::empty(), input)?;
} else {
unreachable!("encrypt is called on a non-open cipher")
}
Expand All @@ -148,7 +142,7 @@ impl StreamCipher for RingAeadCipher {
true
}

fn decrypt_in_place(&mut self, input: &mut bytes::BytesMut) -> Result<(), SecioError> {
fn decrypt_in_place(&mut self, input: &mut [u8]) -> Result<(), SecioError> {
self.decrypt_in_place(input)
}
}
Expand Down

0 comments on commit c4f0f76

Please sign in to comment.