Skip to content

Commit

Permalink
tests and exmaples
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Jun 4, 2024
1 parent 84ad3d4 commit fe33565
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zeroize"
version = "0.1.8"
version = "0.1.9"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ It can work with `bytearray` and `numpy array`.

> [!WARNING]
> **In the case of [Copy-on-write fork](https://en.wikipedia.org/wiki/Copy-on-write) you need to zeroize the memory before forking the child process, see example below.
> Also by itself it doesn't work if memory is moved or moved to swap. You can use `crypes` with `libc.mlock()` to lock the memory, see example below.**
> Also by itself it doesn't work if memory is moved or moved to swap. You can use `crypes` with `libc.mlock()` to lock the memory, max size you can lock is 4MB, at least on Linux, see example below.**
# Examples

Expand Down
2 changes: 2 additions & 0 deletions examples/lock_and_zeroize.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ def unlock_memory(buffer):
print("allocate memory")

# regular array
# max size you can lock is 4MB, at least on Linux
arr = bytearray(b"1234567890")

# numpy array
# max size you can lock is 4MB, at least on Linux
arr_np = np.array([0] * 10, dtype=np.uint8)
arr_np[:] = arr
assert arr_np.tobytes() == b"1234567890"
Expand Down
1 change: 1 addition & 0 deletions examples/zeroize_before_fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def unlock_memory(buffer):

if __name__ == "__main__":
try:
# max size you can lock is 4MB, at least on Linux
sensitive_data = bytearray(b"Sensitive Information")
lock_memory(sensitive_data)

Expand Down
17 changes: 4 additions & 13 deletions tests/test_zeroize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest
import zeroize
import numpy as np


import ctypes


Expand Down Expand Up @@ -41,16 +39,6 @@ def unlock_memory(buffer):
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2 * 1024,
4 * 1024,
]


Expand All @@ -71,6 +59,7 @@ def test_zeroize1(self):
def test_zeroize_np(self):
try:
arr = np.array([0] * 10, dtype=np.uint8)
lock_memory(arr)
arr[:] = bytes(b"1234567890")
zeroize.zeroize_np(arr)
self.assertEqual(True, all(arr == 0))
Expand All @@ -82,17 +71,19 @@ def test_zeroize1_sizes(self):
for size in SIZES_MB:
try:
arr = bytearray(int(size * 1024 * 1024))
lock_memory(arr)
zeroize.zeroize1(arr)
self.assertEqual(arr, bytearray(int(size * 1024 * 1024)))

finally:
unlock_memory(arr)

def test_zeroize_np_sizes(self):
for size in [size for size in SIZES_MB if size < 4]:
for size in SIZES_MB:
try:
array_size = int(size * 1024 * 1024)
random_array = np.random.randint(0, 256, array_size, dtype=np.uint8)
lock_memory(random_array)
zeroize.zeroize_np(random_array)
self.assertEqual(True, all(random_array == 0))
finally:
Expand Down

0 comments on commit fe33565

Please sign in to comment.