aboutsummaryrefslogtreecommitdiff
path: root/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures
diff options
context:
space:
mode:
Diffstat (limited to 'wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures')
-rw-r--r--wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NCSignatureUtil.cs175
-rw-r--r--wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NoscryptSigner.cs167
2 files changed, 342 insertions, 0 deletions
diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NCSignatureUtil.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NCSignatureUtil.cs
new file mode 100644
index 0000000..2755ceb
--- /dev/null
+++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NCSignatureUtil.cs
@@ -0,0 +1,175 @@
+// 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 <https://www.gnu.org/licenses/>.
+
+using System;
+using System.Runtime.InteropServices;
+
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Cryptography.Noscrypt.@internal;
+using static VNLib.Utils.Cryptography.Noscrypt.NoscryptLibrary;
+
+using NCResult = System.Int64;
+
+namespace VNLib.Utils.Cryptography.Noscrypt.Singatures
+{
+
+ /// <summary>
+ /// Contains utility methods for signing and verifying data using the noscrypt library
+ /// </summary>
+ public unsafe static class NCSignatureUtil
+ {
+ /// <summary>
+ /// Signs the data using the supplied secret key and
+ /// entropy pointer
+ /// </summary>
+ /// <param name="context">The initialized context memory to pass to the library</param>
+ /// <param name="secretKey">A reference to a structure containing the private key data</param>
+ /// <param name="random32">A pointer to a 32 byte buffer containing high entropy random data</param>
+ /// <param name="data">A pointer to a buffer containing the data to sign</param>
+ /// <param name="dataSize">The size of the data buffer in bytes</param>
+ /// <param name="sig64">A pointer to a 64 byte buffer to write signature data to</param>
+ /// <exception cref="InvalidOperationException"></exception>
+ public static void SignData(
+ NCContext context,
+ ref readonly NCSecretKey secretKey,
+ ref readonly byte random32,
+ ref readonly byte data,
+ uint dataSize,
+ ref byte sig64
+ )
+ {
+ Check(context);
+
+ fixed (NCSecretKey* pSecKey = &secretKey)
+ fixed (byte* pData = &data, pSig = &sig64, pRandom = &random32)
+ {
+ NCResult result = GetTable(context).NCSignData(
+ ctx: context.DangerousGetHandle(),
+ sk: pSecKey,
+ random32: pRandom,
+ data: pData,
+ dataSize,
+ sig64: pSig
+ );
+
+ NCUtil.CheckResult<FunctionTable.NCSignDataDelegate>(result, raiseOnFailure: true);
+ }
+ }
+
+ /// <summary>
+ /// Verifies signed data against the supplied public key
+ /// </summary>
+ /// <param name="context">The initialized context memory to pass to the library</param>
+ /// <param name="publicKey">A reference to a structure containing the public key data</param>
+ /// <param name="data">A pointer to a buffer containing the data to verify</param>
+ /// <param name="dataSize">The size of the data buffer in bytes</param>
+ /// <param name="sig64">A pointer to a 64 byte buffer to read signature data from</param>
+ /// <returns>True if the signature was signed by the supplied public key, false otherwise</returns>
+ public static bool VerifyData(
+ NCContext context,
+ ref readonly NCPublicKey publicKey,
+ ref readonly byte data,
+ uint dataSize,
+ ref readonly byte sig64
+ )
+ {
+ Check(context);
+
+ fixed (NCPublicKey* pPubKey = &publicKey)
+ fixed (byte* pData = &data, pSig = &sig64)
+ {
+ NCResult result = GetTable(context).NCVerifyData(
+ context.DangerousGetHandle(),
+ pk: pPubKey,
+ data: pData,
+ dataSize,
+ sig64: pSig
+ );
+
+ NCUtil.CheckResult<FunctionTable.NCVerifyDataDelegate>(result, false);
+
+ return result == NC_SUCCESS;
+ }
+ }
+
+ /// <summary>
+ /// Signs the data using the supplied secret key and
+ /// entropy pointer
+ /// </summary>
+ /// <param name="context">The initialized context memory to pass to the library</param>
+ /// <param name="secretKey">A reference to a structure containing the private key data</param>
+ /// <param name="random32">A pointer to a 32 byte buffer containing high entropy random data</param>
+ /// <param name="data">A pointer to a buffer containing the data to sign</param>
+ /// <param name="signatureBuffer">A pointer to a 64 byte buffer to write signature data to</param>
+ /// <exception cref="InvalidOperationException"></exception>
+ public static void SignData(
+ NCContext context,
+ ref readonly NCSecretKey secretKey,
+ ReadOnlySpan<byte> random32,
+ ReadOnlySpan<byte> data,
+ Span<byte> signatureBuffer
+ )
+ {
+ ArgumentOutOfRangeException.ThrowIfLessThan(signatureBuffer.Length, NC_SIGNATURE_SIZE, nameof(signatureBuffer));
+ ArgumentOutOfRangeException.ThrowIfLessThan(random32.Length, 32, nameof(random32));
+ ArgumentOutOfRangeException.ThrowIfZero(data.Length, nameof(data));
+
+ SignData(
+ context,
+ secretKey: in secretKey,
+ random32: in MemoryMarshal.GetReference(random32),
+ data: in MemoryMarshal.GetReference(data),
+ dataSize: (uint)data.Length,
+ sig64: ref MemoryMarshal.GetReference(signatureBuffer)
+ );
+ }
+
+ /// <summary>
+ /// Verifies signed data against the supplied public key
+ /// </summary>
+ /// <param name="context">The initialized context memory to pass to the library</param>
+ /// <param name="publicKey">A reference to a structure containing the public key data</param>
+ /// <param name="data">A pointer to a buffer containing the data to verify</param>
+ /// <param name="signatureBuffer">A pointer to a 64 byte buffer to read signature data from</param>
+ /// <returns>True if the signature was signed by the supplied public key, false otherwise</returns>
+ public static bool VerifyData(
+ NCContext context,
+ ref readonly NCPublicKey publicKey,
+ ReadOnlySpan<byte> data,
+ ReadOnlySpan<byte> signatureBuffer
+ )
+ {
+ ArgumentOutOfRangeException.ThrowIfLessThan(signatureBuffer.Length, NC_SIGNATURE_SIZE, nameof(signatureBuffer));
+ ArgumentOutOfRangeException.ThrowIfZero(data.Length, nameof(data));
+
+ return VerifyData(
+ context,
+ publicKey: in publicKey,
+ data: in MemoryMarshal.GetReference(data),
+ dataSize: (uint)data.Length,
+ sig64: ref MemoryMarshal.GetReference(signatureBuffer)
+ );
+ }
+
+ private static void Check(NCContext? context)
+ {
+ ArgumentNullException.ThrowIfNull(context);
+ context.ThrowIfClosed();
+ }
+
+ private static ref readonly FunctionTable GetTable(NCContext ctx)
+ => ref ctx.Library.Functions;
+ }
+}
diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NoscryptSigner.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NoscryptSigner.cs
new file mode 100644
index 0000000..063d2c0
--- /dev/null
+++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/Singatures/NoscryptSigner.cs
@@ -0,0 +1,167 @@
+// 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 <https://www.gnu.org/licenses/>.
+
+using System;
+
+using VNLib.Utils.Memory;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Cryptography.Noscrypt;
+using VNLib.Utils.Cryptography.Noscrypt.Random;
+using static VNLib.Utils.Cryptography.Noscrypt.NoscryptLibrary;
+
+namespace VNLib.Utils.Cryptography.Noscrypt.Singatures
+{
+
+ /// <summary>
+ /// A simple wrapper class to sign nostr message data using
+ /// the noscrypt library
+ /// </summary>
+ /// <param name="noscrypt">The noscrypt library instance</param>
+ /// <param name="random">A random entropy pool used to source random data for signature entropy</param>
+ public class NoscryptSigner(NCContext context, IRandomSource random)
+ {
+ /// <summary>
+ /// Gets the size of the buffer required to hold the signature
+ /// </summary>
+ public static int SignatureBufferSize => NC_SIGNATURE_SIZE;
+
+ /// <summary>
+ /// Signs a message using the specified private key and message data
+ /// </summary>
+ /// <param name="hexPrivateKey">The hexadecimal private key used to sign the message</param>
+ /// <param name="message">The message data to sign</param>
+ /// <param name="format">A encoder used to convert the signature data to an encoded string</param>
+ /// <returns>The string encoded nostr signature</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ public string SignData(string hexPrivateKey, ReadOnlySpan<byte> message, INostrSignatureEncoder? format = null)
+ {
+ ArgumentException.ThrowIfNullOrWhiteSpace(hexPrivateKey);
+ ArgumentOutOfRangeException.ThrowIfNotEqual(hexPrivateKey.Length / 2, NC_SEC_KEY_SIZE, nameof(hexPrivateKey));
+
+ //Have to allocate array unfortunately
+ byte[] privKey = Convert.FromHexString(hexPrivateKey);
+ try
+ {
+ return SignData(privKey.AsSpan(), message, format);
+ }
+ finally
+ {
+ //Always zero key beofre leaving
+ MemoryUtil.InitializeBlock(privKey);
+ }
+ }
+
+ /// <summary>
+ /// Signs a message using the specified secret key and message data
+ /// </summary>
+ /// <param name="secretKey">The secret key data buffer</param>
+ /// <param name="message">The message data to sign</param>
+ /// <param name="format">A encoder used to convert the signature data to an encoded string</param>
+ /// <returns>The string encoded nostr signature</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ public string SignData(
+ ReadOnlySpan<byte> secretKey,
+ ReadOnlySpan<byte> message,
+ INostrSignatureEncoder? format = null
+ )
+ {
+ return SignData(
+ in NCKeyUtil.AsSecretKey(secretKey),
+ message,
+ format
+ );
+ }
+
+ /// <summary>
+ /// Signs a message using the specified secret key and message data
+ /// </summary>
+ /// <param name="secretkey">A reference to the secret key structurer</param>
+ /// <param name="message">The message data to sign</param>
+ /// <param name="format">A encoder used to convert the signature data to an encoded string</param>
+ /// <returns>The string encoded nostr signature</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ public string SignData(
+ ref readonly NCSecretKey secretkey,
+ ReadOnlySpan<byte> message,
+ INostrSignatureEncoder? format = null
+ )
+ {
+ //Default to hex encoding because that is the default NIP-01 format
+ format ??= HexSignatureEncoder.Instance;
+
+ Span<byte> sigBuffer = stackalloc byte[SignatureBufferSize];
+
+ SignData(message, sigBuffer);
+
+ return format.GetString(sigBuffer);
+ }
+
+
+ /// <summary>
+ /// Signs a message using the specified secret key and message data
+ /// </summary>
+ /// <param name="secretkey">A reference to the secret key structurer</param>
+ /// <param name="data">The message data to sign</param>
+ /// <param name="signature">A buffer to write signature data to</param>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ArgumentOutOfRangeException"></exception>
+ public void SignData(
+ ref readonly NCSecretKey secretkey,
+ ReadOnlySpan<byte> data,
+ Span<byte> signature
+ )
+ {
+ ArgumentOutOfRangeException.ThrowIfLessThan(signature.Length, NC_SIGNATURE_SIZE, nameof(signature));
+
+ //Signature generation required random entropy to be secure
+ Span<byte> entropy = stackalloc byte[NC_SIG_ENTROPY_SIZE];
+ random.GetRandomBytes(entropy);
+
+ NCSignatureUtil.SignData(
+ context,
+ in secretkey,
+ entropy,
+ data,
+ signature
+ );
+ }
+
+ public bool VerifyData(
+ ReadOnlySpan<byte> publicKey,
+ ReadOnlySpan<byte> data,
+ ReadOnlySpan<byte> sig
+ )
+ {
+ return VerifyData(
+ in NCKeyUtil.AsPublicKey(publicKey),
+ data,
+ sig
+ );
+ }
+
+ public bool VerifyData(
+ ref readonly NCPublicKey pk,
+ ReadOnlySpan<byte> data,
+ ReadOnlySpan<byte> sig
+ )
+ {
+ return NCSignatureUtil.VerifyData(context, in pk, data, sig);
+ }
+ }
+
+}