Skip to content

Commit

Permalink
use sodiumoxide for mlock
Browse files Browse the repository at this point in the history
  • Loading branch information
radu committed Jun 23, 2024
1 parent e695866 commit b303bec
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 11 deletions.
100 changes: 100 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ zeroize_rs = { version = "1.8.1", package = "zeroize"}
numpy = "0.21"
memsec = "0.7.0"
region = "3.0.2"
sodiumoxide = "0.2.7"

[profile.release]
panic = "abort"
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ fn zeroize1(arr: &Bound<'_, PyAny>, py: Python<'_>) -> PyResult<()> {
#[pyfunction]
fn mlock(arr: &Bound<'_, PyAny>, py: Python<'_>) -> PyResult<()> {
let arr = as_array_mut(arr, py)?;
unsafe {
if !_mlock(arr.as_mut_ptr(), arr.len()) {
return Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(
"mlock failed",
));
}
}
// if !_mlock(arr.as_mut_ptr(), arr.len()) {
sodiumoxide::utils::mlock(arr).map_err(|_|PyErr::new::<pyo3::exceptions::PyTypeError, _>(
"mlock failed",
))?;
Ok(())
}

Expand Down
9 changes: 5 additions & 4 deletions tests/test_zeroize.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def unlock_memory(address, size):
0.5,
1,
2,
# 2.97,
4,
2.83,
]


Expand Down Expand Up @@ -111,9 +110,11 @@ def test_zeroize1_sizes(self):
print(f"Length of the array: {size2}")

print("lock arr")
lock_memory(address, size)
mlock(arr)
# lock_memory(address, size)
print("lock arr2")
lock_memory(address2, size2)
mlock(arr2)
# lock_memory(address2, size2)

zeroize1(arr)
zeroize1(arr_np)
Expand Down

0 comments on commit b303bec

Please sign in to comment.