From 8434d8b8ecf54c6038a7e9ad7d57084a0865500c Mon Sep 17 00:00:00 2001 From: vnugent Date: Thu, 23 Nov 2023 12:44:53 -0500 Subject: fixed signing and working note encryption! --- .../src/ContextExtensions.cs | 23 ++++++++++++++++------ .../NVault.Crypto.Secp256k1/src/LibSecp256k1.cs | 5 +++++ 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'back-end/libs/NVault.Crypto.Secp256k1') diff --git a/back-end/libs/NVault.Crypto.Secp256k1/src/ContextExtensions.cs b/back-end/libs/NVault.Crypto.Secp256k1/src/ContextExtensions.cs index f5327df..9931698 100644 --- a/back-end/libs/NVault.Crypto.Secp256k1/src/ContextExtensions.cs +++ b/back-end/libs/NVault.Crypto.Secp256k1/src/ContextExtensions.cs @@ -254,7 +254,7 @@ namespace NVault.Crypto.Secp256k1 { if (secretKey.Length != SecretKeySize) { - throw new ArgumentException($"Your public key buffer must be exactly {SecretKeySize} bytes long"); + throw new ArgumentException($"Your secret key buffer must be exactly {SecretKeySize} bytes long"); } //Init callback state struct @@ -265,17 +265,28 @@ namespace NVault.Crypto.Secp256k1 OutLen = data.Length }; + context.Lib.SafeLibHandle.ThrowIfClosed(); + //Stack allocated keypair and x-only public key - Secp256k1PublicKey pubKeyStruct = new(); - //Recover the x-only public key structure - MemoryUtil.CopyStruct(xOnlyPubKey, &pubKeyStruct); + Secp256k1PublicKey peerPubKey = new(); - context.Lib.SafeLibHandle.ThrowIfClosed(); + //Parse the public key from the buffer + fixed (byte* pubkeyPtr = &MemoryMarshal.GetReference(xOnlyPubKey)) + { + context.Lib._xOnlyPubkeyParse(context.Context, &peerPubKey, pubkeyPtr); + } fixed (byte* dataPtr = &MemoryMarshal.GetReference(data), secKeyPtr = &MemoryMarshal.GetReference(secretKey)) { - return context.Lib._ecdh.Invoke(context.Context, dataPtr, &pubKeyStruct, secKeyPtr, UmanagedEcdhHashFuncCallback, &state) == 1; + return context.Lib._ecdh.Invoke( + context.Context, + dataPtr, + &peerPubKey, + secKeyPtr, + UmanagedEcdhHashFuncCallback, + &state + ) == 1; } /* diff --git a/back-end/libs/NVault.Crypto.Secp256k1/src/LibSecp256k1.cs b/back-end/libs/NVault.Crypto.Secp256k1/src/LibSecp256k1.cs index 5aeed00..f3afc33 100644 --- a/back-end/libs/NVault.Crypto.Secp256k1/src/LibSecp256k1.cs +++ b/back-end/libs/NVault.Crypto.Secp256k1/src/LibSecp256k1.cs @@ -82,6 +82,9 @@ namespace NVault.Crypto.Secp256k1 [SafeMethodName("secp256k1_ec_pubkey_serialize")] internal delegate int PubKeySerialize(IntPtr ctx, byte* outPubKey, ulong* outLen, Secp256k1PublicKey* pubKey, uint flags); + [SafeMethodName("secp256k1_xonly_pubkey_parse")] + internal delegate int XOnlyPubkeyParse(IntPtr ctx, Secp256k1PublicKey* pubkey, byte* input32); + [SafeMethodName("secp256k1_ecdh")] internal delegate int Ecdh( IntPtr ctx, @@ -143,6 +146,7 @@ namespace NVault.Crypto.Secp256k1 internal readonly SecKeyVerify _secKeyVerify; internal readonly PubKeySerialize _pubKeySerialize; internal readonly Ecdh _ecdh; + internal readonly XOnlyPubkeyParse _xOnlyPubkeyParse; private readonly IRandomSource _randomSource; /// @@ -171,6 +175,7 @@ namespace NVault.Crypto.Secp256k1 _secKeyVerify = handle.DangerousGetMethod(); _pubKeySerialize = handle.DangerousGetMethod(); _ecdh = handle.DangerousGetMethod(); + _xOnlyPubkeyParse = handle.DangerousGetMethod(); //Store random source _randomSource = randomSource; -- cgit