Skip to content

Commit 0d9a4d7

Browse files
committed
feat(crypto): ✨ add mlock and munlock in sodium
1 parent 721907b commit 0d9a4d7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Runtime/Algorand.Unity.Crypto/Interop/sodium.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ internal static unsafe partial class sodium
1919

2020
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)]
2121
internal static extern void sodium_free(IntPtr handle);
22+
23+
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)]
24+
internal static extern void sodium_mlock(IntPtr handle, UIntPtr size);
25+
26+
[DllImport(Library, CallingConvention = CallingConvention.Cdecl)]
27+
internal static extern void sodium_munlock(IntPtr handle, UIntPtr size);
2228
}
2329
}

Runtime/Algorand.Unity.Crypto/SecureString.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,21 @@ public struct SecureString : INativeDisposable
2727
public SecureString(ReadOnlySpan<char> source)
2828
{
2929
totalCapacity = source.Length * 2;
30-
handle = SecureMemoryHandle.Create((UIntPtr)totalCapacity);
30+
var totalCapacityNUint = (UIntPtr)totalCapacity;
31+
handle = SecureMemoryHandle.Create(totalCapacityNUint);
3132
unsafe
3233
{
3334
fixed (char* sourceptr = source)
3435
{
36+
sodium.sodium_mlock((IntPtr)sourceptr, totalCapacityNUint);
3537
var error = UTF8ArrayUnsafeUtility.Copy(
3638
handle.GetUnsafePtr(),
3739
out length,
3840
totalCapacity - 1,
3941
sourceptr,
4042
source.Length
4143
);
44+
sodium.sodium_munlock((IntPtr)sourceptr, totalCapacityNUint);
4245
if (error != CopyError.None)
4346
{
4447
if (handle.IsCreated) handle.Dispose();

0 commit comments

Comments
 (0)