Skip to content

Commit

Permalink
Fix windows api
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyob94 committed Sep 8, 2024
1 parent 67be39a commit f799eab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ zeroize = "1.8.1"
libc = "0.2.158"
errno = "0.3.9"


[target.'cfg(windows)'.dependencies]
windows-sys = "0.59.0"
windows-sys = { version = "0.59.0", default-features = false, features = [
"Win32_System_SystemInformation",
"Win32_System_Memory",
"Win32_Foundation",
"Win32_System_Diagnostics_Debug",
"Win32_System",
"Win32_System_Diagnostics_Debug_Extensions",
], optional = true }
14 changes: 3 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl<S: Zeroize> Zeroize for SecretBox<S> {
impl<S: Zeroize> Drop for SecretBox<S> {
fn drop(&mut self) {
let len = size_of_val(&*self.inner_secret);

let secret_ptr = self.inner_secret.as_ref() as *const S;

#[cfg(unix)]
Expand All @@ -66,12 +65,8 @@ impl<S: Zeroize> Drop for SecretBox<S> {

#[cfg(windows)]
unsafe {
#[cfg(feature = "Win32")]
if windows_sys::Win32::System::Memory::VirtualUnlock(secret_ptr.cast(), len) == 0 {
panic!(
"VirtualUnlock failed: {:?}",
windows_sys::core::HRESULT_FROM_WIN32(windows_sys::core::GetLastError())
);
panic!("VirtualUnlock failed",);
}
}

Expand All @@ -91,6 +86,7 @@ impl<S: Zeroize> SecretBox<S> {
/// Create a secret value using a pre-boxed value.
pub fn new(boxed_secret: Box<S>) -> Self {
let len = size_of_val(&*boxed_secret);

let secret_ptr = Box::into_raw(boxed_secret);

#[cfg(unix)]
Expand All @@ -115,12 +111,8 @@ impl<S: Zeroize> SecretBox<S> {

#[cfg(windows)]
unsafe {
#[cfg(feature = "Win32")]
if windows_sys::Win32::System::Memory::VirtualLock(secret_ptr.cast(), len) == 0 {
panic!(
"VirtualLock failed: {:?}",
windows_sys::core::HRESULT_FROM_WIN32(windows_sys::core::GetLastError())
);
panic!("VirtualLock failed",);
}
}

Expand Down

0 comments on commit f799eab

Please sign in to comment.