From 9c5b86c2d7f2af7c5905a1cd63aacb8927d7ec4c Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 25 Apr 2024 14:55:50 -0400 Subject: latest noscryp integrations and testing --- lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs | 8 ++--- lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs | 10 +++--- lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs | 4 +++ lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs | 28 --------------- lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs | 31 ++++++++++++++++ lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs | 2 +- lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs | 8 +++-- lib/NVault.Crypto.Noscrypt/src/NCUtil.cs | 9 +++++ .../src/NVault.Crypto.Noscrypt.csproj | 4 +-- lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs | 2 +- lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs | 42 ++++++++++++---------- .../tests/LibNoscryptTests.cs | 13 ++++--- 12 files changed, 91 insertions(+), 70 deletions(-) delete mode 100644 lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs create mode 100644 lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs (limited to 'lib') diff --git a/lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs b/lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs index 6ca7dea..1628a93 100644 --- a/lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs +++ b/lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs @@ -93,16 +93,16 @@ namespace NVault.Crypto.Noscrypt internal delegate NCResult NCValidateSecretKeyDelegate(IntPtr ctx, NCSecretKey* secKey); [SafeMethodName("NCSignData")] - internal delegate NCResult NCSignDataDelegate(IntPtr ctx, NCSecretKey* sk, byte* random32, byte* data, nint dataSize, byte* sig64); + internal delegate NCResult NCSignDataDelegate(IntPtr ctx, NCSecretKey* sk, byte* random32, byte* data, uint dataSize, byte* sig64); [SafeMethodName("NCVerifyData")] - internal delegate NCResult NCVerifyDataDelegate(IntPtr ctx, NCPublicKey* sk, byte* data, nint dataSize, byte* sig64); + internal delegate NCResult NCVerifyDataDelegate(IntPtr ctx, NCPublicKey* sk, byte* data, uint dataSize, byte* sig64); [SafeMethodName("NCEncrypt")] - internal delegate NCResult NCEncryptDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, byte* hmacKeyOut32, NCCryptoData* data); + internal delegate NCResult NCEncryptDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, NCEncryptionArgs* data); [SafeMethodName("NCDecrypt")] - internal delegate NCResult NCDecryptDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, NCCryptoData* data); + internal delegate NCResult NCDecryptDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, NCEncryptionArgs* data); [SafeMethodName("NCVerifyMac")] internal delegate NCResult NCVerifyMacDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, NCMacVerifyArgs* args); diff --git a/lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs b/lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs index ad4eadb..07e3ee9 100644 --- a/lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs +++ b/lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs @@ -21,9 +21,9 @@ namespace NVault.Crypto.Noscrypt bool ValidateSecretKey(ref readonly NCSecretKey secretKey); - void SignData(ref readonly NCSecretKey secretKey, ref readonly byte random32, ref readonly byte data, nint dataSize, ref byte sig64); + void SignData(ref readonly NCSecretKey secretKey, ref readonly byte random32, ref readonly byte data, uint dataSize, ref byte sig64); - bool VerifyData(ref readonly NCPublicKey pubKey, ref readonly byte data, nint dataSize, ref byte sig64); + bool VerifyData(ref readonly NCPublicKey pubKey, ref readonly byte data, uint dataSize, ref byte sig64); bool VerifyMac( ref readonly NCSecretKey secretKey, @@ -31,7 +31,7 @@ namespace NVault.Crypto.Noscrypt ref readonly byte nonce32, ref readonly byte mac32, ref readonly byte payload, - nint payloadSize + uint payloadSize ); void Encrypt( @@ -39,7 +39,7 @@ namespace NVault.Crypto.Noscrypt ref readonly NCPublicKey publicKey, ref readonly byte nonce, ref readonly byte plainText, - ref byte cipherText, + ref byte cipherText, uint size, ref byte hmacKeyOut32 ); @@ -49,7 +49,7 @@ namespace NVault.Crypto.Noscrypt ref readonly NCPublicKey publicKey, ref readonly byte nonce, ref readonly byte cipherText, - ref byte plainText, + ref byte plainText, uint size ); } diff --git a/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs b/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs index 996681c..67f3ca7 100644 --- a/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs +++ b/lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs @@ -47,12 +47,16 @@ namespace NVault.Crypto.Noscrypt public const int NC_CONVERSATION_KEY_SIZE = 32; public const int CTX_ENTROPY_SIZE = 32; + public const uint NC_ENC_VERSION_NIP04 = 0x00000004u; + public const uint NC_ENC_VERSION_NIP44 = 0x00000002c; + public const NCResult NC_SUCCESS = 0; public const byte E_NULL_PTR = 0x01; public const byte E_INVALID_ARG = 0x02; public const byte E_INVALID_CTX = 0x03; public const byte E_ARGUMENT_OUT_OF_RANGE = 0x04; public const byte E_OPERATION_FAILED = 0x05; + public const byte E_VERSION_NOT_SUPPORTED = 0x06; private readonly FunctionTable _functions = FunctionTable.BuildFunctionTable(Library); diff --git a/lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs b/lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs deleted file mode 100644 index 72b43b2..0000000 --- a/lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (C) 2024 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -using System.Runtime.InteropServices; - -namespace NVault.Crypto.Noscrypt -{ - [StructLayout(LayoutKind.Sequential)] - internal unsafe struct NCCryptoData - { - public byte* nonce; - public void* inputData; - public void* outputData; - public uint dataSize; - } -} diff --git a/lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs b/lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs new file mode 100644 index 0000000..2d48696 --- /dev/null +++ b/lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs @@ -0,0 +1,31 @@ +// Copyright (C) 2024 Vaughn Nugent +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +using System; +using System.Runtime.InteropServices; + +namespace NVault.Crypto.Noscrypt +{ + [StructLayout(LayoutKind.Sequential)] + internal unsafe struct NCEncryptionArgs + { + public byte* nonce32; + public byte* hmacKeyOut32; + public byte* inputData; + public byte* outputData; + public UInt32 dataSize; + public UInt32 version; + } +} diff --git a/lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs b/lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs index 875721f..8b99ca8 100644 --- a/lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs +++ b/lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs @@ -38,7 +38,7 @@ namespace NVault.Crypto.Noscrypt in secKey, in MemoryMarshal.GetReference(random32), in MemoryMarshal.GetReference(data), - data.Length, + (uint)data.Length, ref MemoryMarshal.GetReference(signatureBuffer) ); } diff --git a/lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs b/lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs index d2867f6..6340f59 100644 --- a/lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs +++ b/lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs @@ -13,20 +13,22 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using System; + namespace NVault.Crypto.Noscrypt { internal unsafe struct NCMacVerifyArgs { /* The message authentication code certifying the Nip44 payload */ - public byte* mac; + public byte* mac32; /* The nonce used for the original message encryption */ - public byte* nonce; + public byte* nonce32; /* The message payload data */ public byte* payload; /* The size of the payload data */ - public nint payloadSize; + public UInt32 payloadSize; } } diff --git a/lib/NVault.Crypto.Noscrypt/src/NCUtil.cs b/lib/NVault.Crypto.Noscrypt/src/NCUtil.cs index 44c07aa..1e479de 100644 --- a/lib/NVault.Crypto.Noscrypt/src/NCUtil.cs +++ b/lib/NVault.Crypto.Noscrypt/src/NCUtil.cs @@ -169,6 +169,15 @@ namespace NVault.Crypto.Noscrypt case E_OPERATION_FAILED: RaiseOperationFailedException(raiseOnFailure); break; + case E_VERSION_NOT_SUPPORTED: + throw new NotSupportedException("The requested version is not supported"); + + default: + if(raiseOnFailure) + { + throw new InvalidOperationException($"The operation failed for an unknown reason, code: {errorCode:x}"); + } + break; } } diff --git a/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj b/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj index 00c2fec..e31c2f9 100644 --- a/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj +++ b/lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj @@ -20,8 +20,8 @@ - - + + diff --git a/lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs b/lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs index 1f3248b..669d5f0 100644 --- a/lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs +++ b/lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs @@ -263,7 +263,7 @@ namespace NVault.Crypto.Noscrypt in MemoryMarshal.GetReference(nonce32), in MemoryMarshal.GetReference(mac32), in MemoryMarshal.GetReference(payload), - payload.Length + (uint)payload.Length ); } diff --git a/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs b/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs index 30205a1..d33978c 100644 --- a/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs +++ b/lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs @@ -46,7 +46,7 @@ namespace NVault.Crypto.Noscrypt ref readonly NCPublicKey publicKey, ref readonly byte nonce32, ref readonly byte cipherText, - ref byte plainText, + ref byte plainText, uint size ) { @@ -58,13 +58,15 @@ namespace NVault.Crypto.Noscrypt fixed (NCPublicKey* pPubKey = &publicKey) fixed (byte* pCipherText = &cipherText, pTextPtr = &plainText, pNonce = &nonce32) { - NCCryptoData data = new() + NCEncryptionArgs data = new() { //Set input data to the cipher text to decrypt and the output data to the plaintext buffer - dataSize = size, + dataSize = size, + hmacKeyOut32 = null, inputData = pCipherText, outputData = pTextPtr, - nonce = pNonce + nonce32 = pNonce, + version = NC_ENC_VERSION_NIP44 }; NCResult result = Functions.NCDecrypt.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey, &data); @@ -89,18 +91,20 @@ namespace NVault.Crypto.Noscrypt fixed (NCSecretKey* pSecKey = &secretKey) fixed (NCPublicKey* pPubKey = &publicKey) - fixed (byte* pCipherText = &cipherText, pTextPtr = &plainText, pHmacKeyOut = &hmackKeyOut32, pNonce = &nonce32) + fixed (byte* pCipherText = &cipherText, pTextPtr = &plainText, pHmacKeyOut = &hmackKeyOut32, pNonce = &nonce32) { - NCCryptoData data = new() + NCEncryptionArgs data = new() { - dataSize = size, + 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, - nonce = pNonce + dataSize = size, + version = NC_ENC_VERSION_NIP44 //Force nip44 encryption }; - NCResult result = Functions.NCEncrypt.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey, pHmacKeyOut, &data); + NCResult result = Functions.NCEncrypt.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey, &data); NCUtil.CheckResult(result, true); } } @@ -110,8 +114,8 @@ namespace NVault.Crypto.Noscrypt { Check(); - fixed(NCSecretKey* pSecKey = &secretKey) - fixed(NCPublicKey* pPubKey = &publicKey) + fixed (NCSecretKey* pSecKey = &secretKey) + fixed (NCPublicKey* pPubKey = &publicKey) { NCResult result = Functions.NCGetPublicKey.Invoke(context.DangerousGetHandle(), pSecKey, pPubKey); NCUtil.CheckResult(result, true); @@ -122,15 +126,15 @@ namespace NVault.Crypto.Noscrypt public void SignData( ref readonly NCSecretKey secretKey, ref readonly byte random32, - ref readonly byte data, - nint dataSize, + ref readonly byte data, + uint dataSize, ref byte sig64 ) { Check(); fixed (NCSecretKey* pSecKey = &secretKey) - fixed(byte* pData = &data, pSig = &sig64, pRandom = &random32) + fixed (byte* pData = &data, pSig = &sig64, pRandom = &random32) { NCResult result = Functions.NCSignData.Invoke(context.DangerousGetHandle(), pSecKey, pRandom, pData, dataSize, pSig); NCUtil.CheckResult(result, true); @@ -161,8 +165,8 @@ namespace NVault.Crypto.Noscrypt /// public bool VerifyData( ref readonly NCPublicKey pubKey, - ref readonly byte data, - nint dataSize, + ref readonly byte data, + uint dataSize, ref byte sig64 ) { @@ -185,7 +189,7 @@ namespace NVault.Crypto.Noscrypt ref readonly byte nonce32, ref readonly byte mac32, ref readonly byte payload, - nint payloadSize + uint payloadSize ) { Check(); @@ -204,8 +208,8 @@ namespace NVault.Crypto.Noscrypt { payloadSize = payloadSize, payload = pPayload, - mac = pMac, - nonce = pNonce + mac32 = pMac, + nonce32 = pNonce }; //Exec and bypass failure diff --git a/lib/NVault.Crypto.Noscrypt/tests/LibNoscryptTests.cs b/lib/NVault.Crypto.Noscrypt/tests/LibNoscryptTests.cs index a575ab5..3721b5e 100644 --- a/lib/NVault.Crypto.Noscrypt/tests/LibNoscryptTests.cs +++ b/lib/NVault.Crypto.Noscrypt/tests/LibNoscryptTests.cs @@ -1,7 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; -using System.Buffers.Binary; using System.Text; using System.Text.Json; using System.Runtime.CompilerServices; @@ -228,12 +227,12 @@ namespace NVault.Crypto.Noscrypt.Tests nc.GetPublicKey(in NCUtil.AsSecretKey(secKey2), ref pub2); bool success = nc.VerifyMac( - in NCUtil.AsSecretKey(secKey1), - in pub2, - nip44Message.Nonce, - nip44Message.Mac, - nip44Message.NonceAndCiphertext - ); + in NCUtil.AsSecretKey(secKey1), + in pub2, + nip44Message.Nonce, + nip44Message.Mac, + nip44Message.NonceAndCiphertext + ); if (!success) { -- cgit