aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-04-25 14:55:50 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-04-25 14:55:50 -0400
commit9c5b86c2d7f2af7c5905a1cd63aacb8927d7ec4c (patch)
treef18598298f4344042452a63bf7b94f5d69e46b93 /lib
parent86fdda1fdece6e8dc1e655b81bcd21a852c5d3fc (diff)
latest noscryp integrations and testing
Diffstat (limited to 'lib')
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/FunctionTable.cs8
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/INostrCrypto.cs10
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/LibNoscrypt.cs4
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs (renamed from lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs)13
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NCExtensions.cs2
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NCMacVerifyArgs.cs8
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NCUtil.cs9
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NVault.Crypto.Noscrypt.csproj4
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/Nip44Util.cs2
-rw-r--r--lib/NVault.Crypto.Noscrypt/src/NostrCrypto.cs42
-rw-r--r--lib/NVault.Crypto.Noscrypt/tests/LibNoscryptTests.cs13
11 files changed, 68 insertions, 47 deletions
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/NCEncryptionArgs.cs
index 72b43b2..2d48696 100644
--- a/lib/NVault.Crypto.Noscrypt/src/NCCryptoData.cs
+++ b/lib/NVault.Crypto.Noscrypt/src/NCEncryptionArgs.cs
@@ -13,16 +13,19 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+using System;
using System.Runtime.InteropServices;
namespace NVault.Crypto.Noscrypt
{
[StructLayout(LayoutKind.Sequential)]
- internal unsafe struct NCCryptoData
+ internal unsafe struct NCEncryptionArgs
{
- public byte* nonce;
- public void* inputData;
- public void* outputData;
- public uint dataSize;
+ 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 <https://www.gnu.org/licenses/>.
+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 @@
</PropertyGroup>
<ItemGroup>
- <PackageReference Include="VNLib.Hashing.Portable" Version="0.1.0-ci0118" />
- <PackageReference Include="VNLib.Utils" Version="0.1.0-ci0118" />
+ <PackageReference Include="VNLib.Hashing.Portable" Version="0.1.0-ci0121" />
+ <PackageReference Include="VNLib.Utils" Version="0.1.0-ci0121" />
</ItemGroup>
</Project>
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<FunctionTable.NCEncryptDelegate>(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<FunctionTable.NCGetPublicKeyDelegate>(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<FunctionTable.NCSignDataDelegate>(result, true);
@@ -161,8 +165,8 @@ namespace NVault.Crypto.Noscrypt
///<inheritdoc/>
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)
{