| | 1 | | #if CRYPTO_LIBSODIUM |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | | using System.Security.Cryptography; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Crypto.Providers.Libsodium; |
| | 6 | |
|
| | 7 | | internal static partial class LibsodiumWrapper |
| | 8 | | { |
| | 9 | | private const string NAME = "libsodium"; |
| | 10 | |
|
| | 11 | | static LibsodiumWrapper() |
| | 12 | | { |
| 8 | 13 | | if (sodium_init() == -1) |
| | 14 | | { |
| 0 | 15 | | throw new CryptographicException("Failed to initialize libsodium."); |
| | 16 | | } |
| 8 | 17 | | } |
| | 18 | |
|
| | 19 | | [LibraryImport(NAME)] |
| | 20 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 21 | | private static partial int sodium_init(); |
| | 22 | |
|
| | 23 | | #region SHA256 |
| | 24 | | [LibraryImport(NAME)] |
| | 25 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 26 | | internal static partial int crypto_hash_sha256_init(IntPtr state); |
| | 27 | |
|
| | 28 | | [LibraryImport(NAME)] |
| | 29 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 30 | | internal static partial int crypto_hash_sha256_update(IntPtr state, ref byte @in, ulong inLen); |
| | 31 | |
|
| | 32 | | [LibraryImport(NAME)] |
| | 33 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 34 | | internal static partial int crypto_hash_sha256_final(IntPtr state, ref byte @out); |
| | 35 | | #endregion |
| | 36 | |
|
| | 37 | | #region AEAD ChaCha20 Poly1305 |
| | 38 | | [LibraryImport(NAME)] |
| | 39 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 40 | | internal static partial int crypto_aead_chacha20poly1305_ietf_encrypt(ref byte c, out long clenP, ref byte m, |
| | 41 | | long mLen, ref byte ad, long adLen, IntPtr nSec, |
| | 42 | | ref byte nPub, ref byte k); |
| | 43 | |
|
| | 44 | | [LibraryImport(NAME)] |
| | 45 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 46 | | internal static partial int crypto_aead_chacha20poly1305_ietf_decrypt(ref byte m, out long mLenP, IntPtr nSec, |
| | 47 | | ref byte c, long clen, ref byte ad, long adLen, |
| | 48 | | ref byte nPub, ref byte k); |
| | 49 | | #endregion |
| | 50 | |
|
| | 51 | | #region Secure Memory |
| | 52 | | [LibraryImport(NAME)] |
| | 53 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 54 | | internal static partial IntPtr sodium_malloc(ulong size); |
| | 55 | |
|
| | 56 | | [LibraryImport(NAME)] |
| | 57 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 58 | | internal static partial int sodium_mlock(IntPtr addr, ulong len); |
| | 59 | |
|
| | 60 | | [LibraryImport(NAME)] |
| | 61 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 62 | | internal static partial void sodium_free(IntPtr ptr); |
| | 63 | |
|
| | 64 | | [LibraryImport(NAME)] |
| | 65 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 66 | | internal static partial void sodium_memzero(IntPtr ptr, ulong len); |
| | 67 | |
|
| | 68 | | [LibraryImport(NAME)] |
| | 69 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 70 | | internal static partial void sodium_munlock(IntPtr addr, ulong len); |
| | 71 | | #endregion |
| | 72 | |
|
| | 73 | | #region AEAD XChaCha20 Poly1305 |
| | 74 | | [LibraryImport(NAME)] |
| | 75 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 76 | | internal static partial int crypto_aead_xchacha20poly1305_ietf_encrypt(ref byte c, out long clenP, ref byte m, |
| | 77 | | long mLen, ref byte ad, long adLen, |
| | 78 | | IntPtr nSec, ref byte nPub, ref byte k); |
| | 79 | |
|
| | 80 | | [LibraryImport(NAME)] |
| | 81 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 82 | | internal static partial int crypto_aead_xchacha20poly1305_ietf_decrypt(ref byte m, out long mLenP, IntPtr nSec, |
| | 83 | | ref byte c, long clen, ref byte ad, |
| | 84 | | long adLen, ref byte nPub, ref byte k); |
| | 85 | | #endregion |
| | 86 | |
|
| | 87 | | #region Argon2 Key Derivation |
| | 88 | | [LibraryImport(NAME)] |
| | 89 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 90 | | internal static partial int crypto_pwhash(ref byte outBuf, ulong outLen, |
| | 91 | | [MarshalAs(UnmanagedType.LPStr)] string passwd, ulong passwdLen, |
| | 92 | | ref byte salt, ulong opslimit, ulong memlimit, int alg); |
| | 93 | | #endregion |
| | 94 | |
|
| | 95 | | #region Random Bytes |
| | 96 | | [LibraryImport(NAME)] |
| | 97 | | [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])] |
| | 98 | | internal static partial void randombytes_buf(ref byte buf, UIntPtr size); |
| | 99 | | #endregion |
| | 100 | | } |
| | 101 | | #endif |