From dd50b014f7ea0dc80505f03292777f8531cf25c5 Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 18 Jun 2024 21:20:49 -0400 Subject: Push latest changes from develop merge --- noscrypt.build.sln | 3 + wrappers/dotnet/Taskfile.yaml | 60 ++++++++++++++++++ .../src/LibNoscrypt.cs | 8 +++ .../src/NostrCrypto.cs | 71 +++++++++++++++------- .../src/Taskfile.yaml | 70 --------------------- .../src/internal/FunctionTable.cs | 14 +++++ .../src/internal/NCEncryptionArgs.cs | 4 +- 7 files changed, 135 insertions(+), 95 deletions(-) create mode 100644 wrappers/dotnet/Taskfile.yaml delete mode 100644 wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Taskfile.yaml diff --git a/noscrypt.build.sln b/noscrypt.build.sln index 7efa33e..7863802 100644 --- a/noscrypt.build.sln +++ b/noscrypt.build.sln @@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "wrappers", "wrappers", "{C20879D7-93D5-4C78-A347-9BC3CF72A6F2}" + ProjectSection(SolutionItems) = preProject + wrappers\dotnet\Taskfile.yaml = wrappers\dotnet\Taskfile.yaml + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VNLib.Utils.Cryptography.Noscrypt", "wrappers\dotnet\VNLib.Utils.Cryptography.Noscrypt\src\VNLib.Utils.Cryptography.Noscrypt.csproj", "{7A41AFB2-7DE1-4E13-BABC-F96B4261D1E3}" EndProject diff --git a/wrappers/dotnet/Taskfile.yaml b/wrappers/dotnet/Taskfile.yaml new file mode 100644 index 0000000..9690e07 --- /dev/null +++ b/wrappers/dotnet/Taskfile.yaml @@ -0,0 +1,60 @@ +#Builds c# libraries for produc + +version: '3' + +vars: + INT_DIR: '{{.SCRATCH_DIR}}/obj/{{.MODULE_NAME}}/' + MS_ARGS: '/p:RunAnalyzersDuringBuild=false /p:IntermediateOutputPath="{{.INT_DIR}}" /p:UseCommonOutputDirectory=true /p:BuildInParallel=true /p:MultiProcessorCompilation=true /p:ErrorOnDuplicatePublishOutputFiles=false' + PACK_OUT: '{{.OUTPUT_DIR}}/{{.HEAD_SHA}}/pkg' + +tasks: + +#called by build pipeline to build module + build: + dir: '{{.USER_WORKING_DIR}}' + cmds: + - echo "building module {{.MODULE_NAME}}" + + #build debug mode first + - task: build_debug + - task: build_release + + publish: + dir: '{{.USER_WORKING_DIR}}' + cmds: + + #push packages to the sleet feed (feed path is vnbuild global) + - sleet push "{{.PACK_OUT}}/debug/" --source debug --config "{{.SLEET_CONFIG_PATH}}" --force + - sleet push "{{.PACK_OUT}}/release/" --source release --config "{{.SLEET_CONFIG_PATH}}" --force + +#called by build pipeline to clean module + clean: + dir: '{{.USER_WORKING_DIR}}' + cmds: + #clean solution + - dotnet clean /p:BuildInParallel=true /p:MultiProcessorCompilation=true + - for: [ obj/, bin/ ] + cmd: powershell rm -Recurse -Force "{{.ITEM}}" + +#Build tasks that use the solution file to build the module + build_debug: + dir: '{{.USER_WORKING_DIR}}' + internal: true + cmds: + - cd {{.MODULE_DIR}} && dotnet publish -c debug {{.MS_ARGS}} + - cd {{.MODULE_DIR}} && dotnet pack -c debug {{.MS_ARGS}} -o "{{.PACK_OUT}}/debug/" + + build_release: + dir: '{{.USER_WORKING_DIR}}' + internal: true + cmds: + - cd {{.MODULE_DIR}} && dotnet publish -c release {{.MS_ARGS}} + - cd {{.MODULE_DIR}} && dotnet pack -c release {{.MS_ARGS}} -o "{{.PACK_OUT}}/release/" + + + packsource: + dir: '{{.USER_WORKING_DIR}}' + internal: true + cmds: + #copy source code to target + - powershell -Command "Get-ChildItem -Include *.cs,*.csproj -Recurse | Where { \$_.FullName -notlike '*\obj\*' -and \$_.FullName -notlike '*\bin\*' } | Resolve-Path -Relative | tar --files-from - -czf '{{.TARGET}}/src.tgz'" \ No newline at end of file diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/LibNoscrypt.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/LibNoscrypt.cs index e2b3ebe..32a07f4 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/LibNoscrypt.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/LibNoscrypt.cs @@ -22,6 +22,8 @@ using VNLib.Utils.Extensions; using VNLib.Utils.Memory; using VNLib.Utils.Native; +using VNLib.Utils.Cryptography.Noscrypt.@internal; + using NCResult = System.Int64; namespace VNLib.Utils.Cryptography.Noscrypt @@ -50,6 +52,12 @@ namespace VNLib.Utils.Cryptography.Noscrypt public const uint NC_ENC_VERSION_NIP04 = 0x00000004u; public const uint NC_ENC_VERSION_NIP44 = 0x00000002c; + public const uint NC_ENC_SET_VERSION = 0x01u; + public const uint NC_ENC_SET_NIP44_NONCE = 0x02u; + public const uint NC_ENC_SET_NIP44_MAC_KEY = 0x03u; + public const uint NC_ENC_SET_NIP04_KEY = 0x04u; + public const uint NC_ENC_SET_NIP04_IV = 0x05u; + public const NCResult NC_SUCCESS = 0; public const byte E_NULL_PTR = 0x01; public const byte E_INVALID_ARG = 0x02; diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NostrCrypto.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NostrCrypto.cs index c4bef05..ec2cf66 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NostrCrypto.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/NostrCrypto.cs @@ -55,17 +55,14 @@ namespace VNLib.Utils.Cryptography.Noscrypt fixed (NCSecretKey* pSecKey = &secretKey) fixed (NCPublicKey* pPubKey = &publicKey) fixed (byte* pCipherText = &cipherText, pTextPtr = &plainText, pNonce = &nonce32) - { - NCEncryptionArgs data = new() - { - //Set input data to the cipher text to decrypt and the output data to the plaintext buffer - dataSize = size, - hmacKeyOut32 = null, - inputData = pCipherText, - outputData = pTextPtr, - nonce32 = pNonce, - version = NC_ENC_VERSION_NIP44 - }; + { + NCEncryptionArgs data = new(); + + //Version set first otherwise errors will occur + SetEncProperty(&data, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44); + //Only the nonce must be set, the hmac key is not needed for decryption + SetEncPropertyEx(&data, NC_ENC_SET_NIP44_NONCE, pNonce, NC_ENCRYPTION_NONCE_SIZE); + SetEncData(&data, pTextPtr, pCipherText, size); NCResult result = Functions.NCDecrypt.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey, &data); NCUtil.CheckResult(result, true); @@ -91,16 +88,20 @@ namespace VNLib.Utils.Cryptography.Noscrypt fixed (NCPublicKey* pPubKey = &publicKey) fixed (byte* pCipherText = &cipherText, pTextPtr = &plainText, pHmacKeyOut = &hmackKeyOut32, pNonce = &nonce32) { - NCEncryptionArgs data = new() - { - nonce32 = pNonce, - hmacKeyOut32 = pHmacKeyOut, - //Set input data to the plaintext to encrypt and the output data to the cipher text buffer - inputData = pTextPtr, - outputData = pCipherText, - dataSize = size, - version = NC_ENC_VERSION_NIP44 //Force nip44 encryption - }; + NCEncryptionArgs data = new(); + + /* + * Use the extended api to set properties correctly and validate them. + * + * The version MUST be set before continuing to set properties + * + * Since pointers are used, they must be only be set/accessed inside + * this fixed statement. + */ + SetEncProperty(&data, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44); + SetEncPropertyEx(&data, NC_ENC_SET_NIP44_MAC_KEY, pHmacKeyOut, NC_HMAC_KEY_SIZE); + SetEncPropertyEx(&data, NC_ENC_SET_NIP44_NONCE, pNonce, NC_ENCRYPTION_NONCE_SIZE); + SetEncData(&data, pTextPtr, pCipherText, size); NCResult result = Functions.NCEncrypt.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey, &data); NCUtil.CheckResult(result, true); @@ -265,6 +266,32 @@ namespace VNLib.Utils.Cryptography.Noscrypt #endif + + private void SetEncPropertyEx(NCEncryptionArgs* args, uint prop, byte* value, uint valueLen) + { + NCResult result = Functions.NCSetEncryptionPropertyEx(args, prop, value, valueLen); + NCUtil.CheckResult(result, true); + } + + private void SetEncProperty(NCEncryptionArgs* args, uint prop, uint value) + { + NCResult result = Functions.NCSetEncryptionProperty(args, prop, value); + NCUtil.CheckResult(result, true); + } + + private void SetEncData(NCEncryptionArgs* args, byte* input, byte* output, uint dataLen) + { + /* + * WARNING: + * For now this a short-cut for setting the input and output data pointers + * technically this still works and avoids the PInvoke call, but this may + * change in the future. + */ + args->dataSize = dataLen; + args->inputData = input; + args->outputData = output; + } + /// protected override void Free() { @@ -281,7 +308,5 @@ namespace VNLib.Utils.Cryptography.Noscrypt throw new ArgumentNullException(name); } } - - } } diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Taskfile.yaml b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Taskfile.yaml deleted file mode 100644 index 0b441a3..0000000 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Taskfile.yaml +++ /dev/null @@ -1,70 +0,0 @@ -# https://taskfile.dev - -#Called by the vnbuild system to produce builds for my website -#https://www.vaughnnugent.com/resources/software - -#This taskfile is called from the root of a project that is being built -#and the purpose of this taskfile is to package up the output of a build -#from the solution file, and package it up into a tgz files for distribution - -version: '3' - -vars: - TARGET: '{{.USER_WORKING_DIR}}/bin' - RELEASE_DIR: "./bin/release/{{.TARGET_FRAMEWORK}}/publish" - -tasks: - - #when build succeeds, archive the output into a tgz - postbuild_success: - dir: '{{.USER_WORKING_DIR}}' - cmds: - #pack up source code - - task: packsource - - #run post in debug mode - - task: postbuild - vars: { BUILD_MODE: debug } - - #remove uncessary files from the release dir - - powershell -Command "Get-ChildItem -Recurse '{{.RELEASE_DIR}}/' -Include *.pdb,*.xml | Remove-Item" - - #run post in release mode - - task: postbuild - vars: { BUILD_MODE: release } - - - postbuild_failed: - dir: '{{.USER_WORKING_DIR}}' - cmds: [] - - postbuild: - dir: '{{.USER_WORKING_DIR}}' - internal: true - vars: - #the build output directory - BUILD_OUT: "{{.USER_WORKING_DIR}}/bin/{{.BUILD_MODE}}/{{.TARGET_FRAMEWORK}}/publish" - - cmds: - #copy license and readme to target - - cd .. && powershell -Command "Copy-Item -Path ./build.readme.md -Destination '{{.BUILD_OUT}}/readme.md'" - - #tar outputs - - cd "{{.BUILD_OUT}}" && tar -czf "{{.TARGET}}/{{.BUILD_MODE}}.tgz" . - - packsource: - dir: '{{.USER_WORKING_DIR}}' - internal: true - cmds: - #copy source code to target - - powershell -Command "Get-ChildItem -Include *.cs,*.csproj -Recurse | Where { \$_.FullName -notlike '*\obj\*' -and \$_.FullName -notlike '*\bin\*' } | Resolve-Path -Relative | tar --files-from - -czf '{{.TARGET}}/src.tgz'" - - -#Remove the output dirs on clean - clean: - dir: '{{.USER_WORKING_DIR}}' - ignore_error: true - cmds: - - for: ['bin/', 'obj/'] - cmd: powershell Remove-Item -Recurse '{{.ITEM}}' - diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs index aa916eb..17b66b2 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs @@ -38,6 +38,9 @@ namespace VNLib.Utils.Cryptography.Noscrypt.@internal public readonly NCDecryptDelegate NCDecrypt; public readonly NCVerifyMacDelegate NCVerifyMac; public readonly NCComputeMacDelegate NCComputeMac; + public readonly NCSetEncryptionDataDelegate NCSetEncryptionData; + public readonly NCSetEncryptionPropertyDelegate NCSetEncryptionProperty; + public readonly NCSetEncryptionPropertyExDelegate NCSetEncryptionPropertyEx; #if DEBUG public readonly NCGetConversationKeyDelegate NCGetConversationKey; @@ -60,6 +63,9 @@ namespace VNLib.Utils.Cryptography.Noscrypt.@internal NCDecrypt = library.DangerousGetFunction(); NCVerifyMac = library.DangerousGetFunction(); NCComputeMac = library.DangerousGetFunction(); + NCSetEncryptionData = library.DangerousGetFunction(); + NCSetEncryptionProperty = library.DangerousGetFunction(); + NCSetEncryptionPropertyEx = library.DangerousGetFunction(); #if DEBUG NCGetConversationKey = library.DangerousGetFunction(); @@ -123,5 +129,13 @@ namespace VNLib.Utils.Cryptography.Noscrypt.@internal [SafeMethodName("NCGetConversationKey")] internal delegate NCResult NCGetConversationKeyDelegate(nint ctx, NCSecretKey* sk, NCPublicKey* pk, byte* keyOut32); + [SafeMethodName("NCSetEncryptionProperty")] + internal delegate NCResult NCSetEncryptionPropertyDelegate(NCEncryptionArgs* args, uint property, uint value); + + [SafeMethodName("NCSetEncryptionPropertyEx")] + internal delegate NCResult NCSetEncryptionPropertyExDelegate(NCEncryptionArgs* args, uint property, byte* value, uint valueLen); + + [SafeMethodName("NCSetEncryptionData")] + internal delegate NCResult NCSetEncryptionDataDelegate(NCEncryptionArgs* args, byte* input, byte* output, uint dataSize); } } diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs index a63d3b3..91f0ff5 100644 --- a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs +++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs @@ -21,8 +21,8 @@ namespace VNLib.Utils.Cryptography.Noscrypt.@internal [StructLayout(LayoutKind.Sequential)] internal unsafe struct NCEncryptionArgs { - public byte* nonce32; - public byte* hmacKeyOut32; + public byte* nonceData; + public byte* keyData; public byte* inputData; public byte* outputData; public uint dataSize; -- cgit