diff options
| author | Gab <24553253+gabrix73@users.noreply.github.com> | 2025-03-08 14:59:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-08 14:59:35 +0100 |
| commit | 4a3ae9929f4657a0f2e9ccf393205655895bb501 (patch) | |
| tree | cc4e8089f178b791875a9a3b34f842cf7466ba4f /security/memory.py | |
| parent | 489f247cffd1a63936a884c6d336b318179b39d8 (diff) | |
| download | securewhisper-4a3ae9929f4657a0f2e9ccf393205655895bb501.tar.gz securewhisper-4a3ae9929f4657a0f2e9ccf393205655895bb501.tar.xz securewhisper-4a3ae9929f4657a0f2e9ccf393205655895bb501.zip | |
Add files via upload
Diffstat (limited to 'security/memory.py')
| -rw-r--r-- | security/memory.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/security/memory.py b/security/memory.py new file mode 100644 index 0000000..6a6e96e --- /dev/null +++ b/security/memory.py @@ -0,0 +1,38 @@ + +import nacl.bindings +import ctypes +import platform + +class SecureMemory: + def __init__(self): + self.protected_blocks = [] + + def protect_memory(self, data: bytes) -> bytearray: + """Protect memory block from being swapped to disk""" + protected = bytearray(data) + self.protected_blocks.append(protected) + + if platform.system() != 'Windows': + try: + # Lock memory to prevent swapping + libc = ctypes.CDLL('libc.so.6') + libc.mlock( + ctypes.c_void_p(protected.__array_interface__['data'][0]), + len(protected) + ) + except: + pass + + return protected + + def secure_wipe(self, data: bytearray): + """Securely wipe memory block""" + if data in self.protected_blocks: + self.protected_blocks.remove(data) + + nacl.bindings.sodium_memzero(data) + + def wipe_all(self): + """Wipe all protected memory blocks""" + for block in self.protected_blocks[:]: + self.secure_wipe(block) |
