cleaned up buffer pruning code

transfer
Avril 4 years ago
parent a12a20c410
commit dca3e1cc19
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -1,7 +1,7 @@
[package]
name = "chacha20"
description = "chacha20_poly1305 encryption tool"
version = "1.1.1"
version = "1.1.2"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
license = "gpl-3.0-or-later"

@ -57,7 +57,7 @@ pub trait HexStringSliceIterExt
}
impl<S> HexStringSliceIterExt for S
where S: AsRef<[u8]>
where S: AsRef<[u8]>
{
fn hex(&self) -> HexStringSliceIter<'_>
{
@ -120,3 +120,25 @@ impl<I: Iterator<Item = u8> + Clone> fmt::Display for HexStringIter<I>
($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());
}
}
}

@ -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"))]

Loading…
Cancel
Save