| | 1 | | namespace NLightning.Infrastructure.Crypto.Primitives; |
| | 2 | |
|
| | 3 | | using Factories; |
| | 4 | | using Interfaces; |
| | 5 | |
|
| | 6 | | public sealed class SecureMemory : IDisposable |
| | 7 | | { |
| | 8 | | private readonly ICryptoProvider _cryptoProvider; |
| | 9 | | private readonly IntPtr _handle; |
| | 10 | |
|
| 19164 | 11 | | public int Length { get; } |
| | 12 | |
|
| 476 | 13 | | public SecureMemory(int size) |
| | 14 | | { |
| 476 | 15 | | if (size <= 0) |
| 0 | 16 | | throw new ArgumentOutOfRangeException(nameof(size), "Size must be positive."); |
| | 17 | |
|
| 476 | 18 | | _cryptoProvider = CryptoFactory.GetCryptoProvider(); |
| 476 | 19 | | Length = size; |
| 476 | 20 | | _handle = _cryptoProvider.MemoryAlloc((ulong)size); |
| | 21 | |
|
| 476 | 22 | | if (_handle == IntPtr.Zero) |
| 0 | 23 | | throw new OutOfMemoryException("Failed to allocate secure memory."); |
| | 24 | |
|
| | 25 | | try |
| | 26 | | { |
| 476 | 27 | | _cryptoProvider.MemoryLock(new IntPtr(_handle), (ulong)Length); |
| 476 | 28 | | } |
| 0 | 29 | | catch |
| | 30 | | { |
| 0 | 31 | | _cryptoProvider.MemoryFree(new IntPtr(_handle)); |
| 0 | 32 | | throw; |
| | 33 | | } |
| 476 | 34 | | } |
| | 35 | |
|
| | 36 | | #region Implicit Conversions |
| | 37 | |
|
| | 38 | | public static unsafe implicit operator Span<byte>(SecureMemory secureMemory) |
| | 39 | | { |
| 932 | 40 | | ArgumentNullException.ThrowIfNull(secureMemory); |
| 932 | 41 | | return secureMemory._disposed |
| 932 | 42 | | ? throw new ObjectDisposedException(nameof(SecureMemory)) |
| 932 | 43 | | : new Span<byte>(secureMemory._handle.ToPointer(), secureMemory.Length); |
| | 44 | | } |
| | 45 | |
|
| | 46 | | public static unsafe implicit operator ReadOnlySpan<byte>(SecureMemory secureMemory) |
| | 47 | | { |
| 16804 | 48 | | ArgumentNullException.ThrowIfNull(secureMemory); |
| 16804 | 49 | | return secureMemory._disposed |
| 16804 | 50 | | ? throw new ObjectDisposedException(nameof(SecureMemory)) |
| 16804 | 51 | | : new ReadOnlySpan<byte>(secureMemory._handle.ToPointer(), secureMemory.Length); |
| | 52 | | } |
| | 53 | | #endregion |
| | 54 | |
|
| | 55 | | public override bool Equals(object? obj) |
| | 56 | | { |
| 0 | 57 | | if (obj is not SecureMemory castObj) return false; |
| | 58 | |
|
| 0 | 59 | | return castObj.Length == Length && castObj._handle == _handle; |
| | 60 | | } |
| | 61 | | public override int GetHashCode() |
| | 62 | | { |
| 0 | 63 | | return HashCode.Combine(Length, _handle); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | #region Dispose Pattern |
| | 67 | | private bool _disposed; |
| | 68 | | private void ReleaseUnmanagedResources() |
| | 69 | | { |
| 474 | 70 | | if (_handle == IntPtr.Zero) |
| 0 | 71 | | return; |
| | 72 | |
|
| | 73 | | try |
| | 74 | | { |
| 474 | 75 | | _cryptoProvider.MemoryZero(_handle, (ulong)Length); |
| 474 | 76 | | } |
| | 77 | | finally |
| | 78 | | { |
| | 79 | | try |
| | 80 | | { |
| 474 | 81 | | _cryptoProvider.MemoryUnlock(_handle, (ulong)Length); |
| 474 | 82 | | } |
| | 83 | | finally |
| | 84 | | { |
| 474 | 85 | | _cryptoProvider.MemoryFree(_handle); |
| 474 | 86 | | } |
| 474 | 87 | | } |
| 474 | 88 | | } |
| | 89 | |
|
| | 90 | | private void Dispose(bool disposing) |
| | 91 | | { |
| 474 | 92 | | if (_disposed) |
| 0 | 93 | | return; |
| | 94 | |
|
| 474 | 95 | | ReleaseUnmanagedResources(); |
| 474 | 96 | | if (disposing) |
| | 97 | | { |
| 376 | 98 | | _cryptoProvider.Dispose(); |
| | 99 | | } |
| | 100 | |
|
| 474 | 101 | | _disposed = true; |
| 474 | 102 | | } |
| | 103 | |
|
| | 104 | | public void Dispose() |
| | 105 | | { |
| 376 | 106 | | Dispose(true); |
| 376 | 107 | | GC.SuppressFinalize(this); |
| 376 | 108 | | } |
| | 109 | |
|
| | 110 | | ~SecureMemory() |
| | 111 | | { |
| 98 | 112 | | Dispose(false); |
| 196 | 113 | | } |
| | 114 | | #endregion |
| | 115 | | } |