Skip to content

Commit

Permalink
use into_out methods
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 20, 2024
1 parent 0c74a0e commit 472a462
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
12 changes: 10 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aead/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rust-version = "1.81"

[dependencies]
crypto-common = "0.2.0-rc.0"
inout = "0.2.0-rc.1"
inout = { version = "0.2.0-rc.1", git = "https://github.com/RustCrypto/utils" }

# optional dependencies
arrayvec = { version = "0.7", optional = true, default-features = false }
Expand Down
19 changes: 3 additions & 16 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,8 @@ pub trait Aead {
let res_len = msg.len() + tag_len;
let tag = self.detached_encrypt_inout(nonce, associated_data, msg)?;
tag_dst.copy_from_slice(&tag);

let out_buf = into_out_buf2(buffer);
Ok(&mut out_buf[..res_len])
let res = &mut buffer.into_out()[..res_len];
Ok(res)
}

/// Decrypt the [`InOutBuf`] data, verify the appended authentication tag, and return
Expand All @@ -199,7 +198,7 @@ pub trait Aead {
let (mut buf, tag) = buffer.split_at(ct_len);
let tag = tag.get_in().try_into().expect("tag has correct length");
self.detached_decrypt_inout(nonce, associated_data, buf.reborrow(), tag)?;
Ok(into_out_buf(buf))
Ok(buf.into_out())
}

/// Encrypt the plaintext data of length `plaintext_len` residing at the beggining of `buffer`
Expand Down Expand Up @@ -475,18 +474,6 @@ fn split_reserved<'a>(
}
}

fn into_out_buf<'out>(buf: InOutBuf<'_, 'out, u8>) -> &'out mut [u8] {
let out_len = buf.len();
let (_, out_ptr) = buf.into_raw();
unsafe { core::slice::from_raw_parts_mut(out_ptr, out_len) }
}

fn into_out_buf2<'out>(buf: InOutBufReserved<'_, 'out, u8>) -> &'out mut [u8] {
let out_len = buf.get_out_len();
let (_, out_ptr) = buf.into_raw();
unsafe { core::slice::from_raw_parts_mut(out_ptr, out_len) }
}

/// AEAD payloads (message + AAD).
///
/// Combination of a message (plaintext or ciphertext) and
Expand Down

0 comments on commit 472a462

Please sign in to comment.