From 472a462329aa15f113a329c723849df271f6a3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 20 Nov 2024 20:20:58 +0300 Subject: [PATCH] use `into_out` methods --- Cargo.lock | 12 ++++++++++-- aead/Cargo.toml | 2 +- aead/src/lib.rs | 19 +++---------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 03e744fe..1ddc0ee1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,7 +21,7 @@ dependencies = [ "bytes", "crypto-common 0.2.0-rc.1", "heapless", - "inout 0.2.0-rc.2", + "inout 0.2.0-rc.2 (git+https://github.com/RustCrypto/utils)", ] [[package]] @@ -223,7 +223,7 @@ version = "0.5.0-pre.7" dependencies = [ "blobby", "crypto-common 0.2.0-rc.1", - "inout 0.2.0-rc.2", + "inout 0.2.0-rc.2 (registry+https://github.com/rust-lang/crates.io-index)", "zeroize", ] @@ -736,6 +736,14 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "inout" +version = "0.2.0-rc.2" +source = "git+https://github.com/RustCrypto/utils#8bb6c50988ba6c32d92e4a8e640027ab3c0920cd" +dependencies = [ + "hybrid-array", +] + [[package]] name = "itoa" version = "1.0.11" diff --git a/aead/Cargo.toml b/aead/Cargo.toml index 95237586..c1315371 100644 --- a/aead/Cargo.toml +++ b/aead/Cargo.toml @@ -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 } diff --git a/aead/src/lib.rs b/aead/src/lib.rs index 5b26fb31..9c0dcc4a 100644 --- a/aead/src/lib.rs +++ b/aead/src/lib.rs @@ -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 @@ -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` @@ -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