diff --git a/Cargo.toml b/Cargo.toml index c864615..9d09f23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "chacha20" description = "chacha20_poly1305 encryption tool" -version = "1.1.1" +version = "1.1.2" authors = ["Avril "] edition = "2018" license = "gpl-3.0-or-later" diff --git a/src/ext.rs b/src/ext.rs index a8917cd..cd9a0f9 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -57,7 +57,7 @@ pub trait HexStringSliceIterExt } impl HexStringSliceIterExt for S - where S: AsRef<[u8]> +where S: AsRef<[u8]> { fn hex(&self) -> HexStringSliceIter<'_> { @@ -120,3 +120,25 @@ impl + Clone> fmt::Display for HexStringIter ($first, $( $rest ),+).0 } } + +#[cfg(feature="explicit_clear")] +#[inline(never)] +pub fn explicit_prune(buffer : &mut[u8]) { + use std::ffi::c_void; + unsafe { + std::ptr::write_bytes(buffer.as_mut_ptr() as * mut c_void, 0, buffer.len()); + #[cfg(nightly)] + if cfg!(target_arch = "x86_64") || cfg!(target_arch = "x86") { + asm!("clflush [{}]", in(reg)buffer.as_mut_ptr()); + } else { + asm!("") + } + #[cfg(not(nightly))] { + extern "C" { + fn explicit_bzero(_: *mut c_void, _:usize); + } + explicit_bzero(buffer.as_mut_ptr() as *mut c_void, buffer.len()); + + } + } +} diff --git a/src/stream.rs b/src/stream.rs index 66e01b9..70e7b05 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -97,35 +97,11 @@ where W: Write /// Clear the internal buffer while keeping it allocated for further use. /// /// This does not affect operations at all, all it does is 0 out the left-over temporary buffer from the last operation(s). - #[cfg_attr(all(nightly, feature="explicit_clear"), inline(never))] // We don't want this asm! being inlined and preventing other optimisations on caller functions. pub fn prune(&mut self) { #[cfg(feature="explicit_clear")] { - use std::ffi::c_void; - - #[cfg(nightly)] - #[inline(never)] unsafe fn explicit_bzero(p: *mut c_void, s: usize) - { - std::ptr::write_bytes(p, 0, s); - - asm!(""); - } - #[cfg(not(nightly))] - extern "C" { - fn explicit_bzero(_: *mut c_void, _:usize); - } - unsafe { - explicit_bzero(self.buffer.as_mut_ptr() as *mut c_void, self.buffer.len()); - - #[cfg(nightly)] - if cfg!(target_arch = "x86_64") || cfg!(target_arch = "x86"){ - asm!( - "clflush [{}]", - in(reg) self.buffer.as_mut_ptr() - ); - } - } + explicit_prune(&mut self.buffer[..]); return; } #[cfg(not(feature="explicit_clear"))]