diff options
Diffstat (limited to 'lib/NVault.Crypto.Noscrypt')
-rw-r--r-- | lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs | 107 | ||||
-rw-r--r-- | lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj | 27 |
2 files changed, 134 insertions, 0 deletions
diff --git a/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs b/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs new file mode 100644 index 0000000..ec0ce2d --- /dev/null +++ b/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +using VNLib.Utils; +using VNLib.Utils.Extensions; +using VNLib.Utils.Native; + +using NCResult = System.Int64; + +namespace NVault.Crypto.Noscrypt +{ + public unsafe sealed class LibNoscrypt(SafeLibraryHandle Library, bool OwnsHandle) : VnDisposeable + { + //Values that match the noscrypt.h header + public const int NC_SEC_KEY_SIZE = 32; + public const int NC_SEC_PUBKEY_SIZE = 32; + public const int NC_ENCRYPTION_NONCE_SIZE = 32; + public const int NC_PUBKEY_SIZE = 32; + public const int NC_SIGNATURE_SIZE = 64; + public const int NC_CONV_KEY_SIZE = 32; + public const int NC_MESSAGE_KEY_SIZE = 32; + public const int CTX_ENTROPY_SIZE = 32; + + //STRUCTS MUST MATCH THE NOSCRYPT.H HEADER + + [StructLayout(LayoutKind.Sequential, Size = NC_SEC_KEY_SIZE)] + internal struct NCSecretKey + { + public fixed byte key[NC_SEC_KEY_SIZE]; + } + + [StructLayout(LayoutKind.Sequential, Size = NC_SEC_PUBKEY_SIZE)] + internal struct NCPublicKey + { + public fixed byte key[NC_SEC_PUBKEY_SIZE]; + } + + [StructLayout(LayoutKind.Sequential)] + internal struct NCCryptoData + { + public fixed byte nonce[NC_ENCRYPTION_NONCE_SIZE]; + public void* inputData; + public void* outputData; + public uint dataSize; + } + + //FUCNTIONS + [SafeMethodName("NCGetContextStructSize")] + internal delegate uint NCGetContextStructSizeDelegate(); + + [SafeMethodName("NCInitContext")] + internal delegate NCResult NCInitContextDelegate(void* ctx , byte* entropy32); + + [SafeMethodName("NCReInitContext")] + internal delegate NCResult NCReInitContextDelegate(void* ctx, byte* entropy32); + + [SafeMethodName("NCDestroyContext")] + internal delegate NCResult NCDestroyContextDelegate(void* ctx); + + [SafeMethodName("NCGetPublicKey")] + internal delegate NCResult NCGetPublicKeyDelegate(void* ctx, NCSecretKey* secKey, NCPublicKey* publicKey); + + [SafeMethodName("NCValidateSecretKey")] + internal delegate NCResult NCValidateSecretKeyDelegate(void* ctx, NCSecretKey* secKey); + + [SafeMethodName("NCSignData")] + internal delegate NCResult NCSignDataDelegate(void* ctx, NCSecretKey* secKey, byte* random32, byte* data, long dataSize, byte* sig64); + + [SafeMethodName("NCVerifyData")] + internal delegate NCResult NCVerifyDataDelegate(void* ctx, NCPublicKey* pubKey, byte* data, long dataSize, byte* sig64); + + [SafeMethodName("NCSignDigest")] + internal delegate NCResult NCSignDigestDelegate(void* ctx, NCSecretKey* secKey, byte* random32, byte* digest32, byte* sig64); + + [SafeMethodName("NCVerifyDigest")] + internal delegate NCResult NCVerifyDigestDelegate(void* ctx, NCPublicKey* pubKey, byte* digest32, byte* sig64); + + + + + ///<inheritdoc/> + protected override void Free() + { + if (OwnsHandle) + { + Library.Dispose(); + } + } + + public static LibNoscrypt Load(string path, DllImportSearchPath search) + { + //Load the native library + SafeLibraryHandle handle = SafeLibraryHandle.LoadLibrary(path, search); + + //Create the wrapper + return new LibNoscrypt(handle, true); + } + + public static LibNoscrypt Load(string path) => Load(path, DllImportSearchPath.SafeDirectories); + + + } +} diff --git a/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj b/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj new file mode 100644 index 0000000..2416535 --- /dev/null +++ b/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj @@ -0,0 +1,27 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net8.0</TargetFramework> + <Nullable>enable</Nullable> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + <PackageReadmeFile>README.md</PackageReadmeFile> + <RootNamespace>NVault.Crypto.Noscrypt</RootNamespace> + <AssemblyName>NVault.Crypto.Noscrypt</AssemblyName> + </PropertyGroup> + + <PropertyGroup> + <Authors>Vaughn Nugent</Authors> + <Company>Vaughn Nugent</Company> + <Product>NVault.Crypto.Noscrypt</Product> + <Description>Provides a managed library for the noscrypt native library, along with other helper types for NVault</Description> + <Copyright>Copyright © 2024 Vaughn Nugent</Copyright> + <PackageProjectUrl>https://www.vaughnnugent.com/resources/software/modules/NVault</PackageProjectUrl> + <RepositoryUrl>https://github.com/VnUgE/NVault/tree/master/</RepositoryUrl> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="VNLib.Hashing.Portable" Version="0.1.0-ci0114" /> + <PackageReference Include="VNLib.Utils" Version="0.1.0-ci0114" /> + </ItemGroup> + +</Project> |