aboutsummaryrefslogtreecommitdiff
path: root/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-06-10 22:08:52 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-06-10 22:08:52 -0400
commit5e32450ccf9186e86a7596a7d774621cf81c62ff (patch)
treef30f3671357f8c47fba448347d30fdf436c18227 /wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal
parenta74f96251bcc81fb2c94fe75dd6f8043fd35fe0b (diff)
feat: Begin migrating noscrypt c# library
Diffstat (limited to 'wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal')
-rw-r--r--wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs127
-rw-r--r--wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs31
-rw-r--r--wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCMacVerifyArgs.cs34
3 files changed, 192 insertions, 0 deletions
diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs
new file mode 100644
index 0000000..aa916eb
--- /dev/null
+++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/FunctionTable.cs
@@ -0,0 +1,127 @@
+// 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.Native;
+using VNLib.Utils.Extensions;
+
+using NCResult = System.Int64;
+
+namespace VNLib.Utils.Cryptography.Noscrypt.@internal
+{
+
+ internal unsafe readonly struct FunctionTable
+ {
+
+ public readonly NCGetContextStructSizeDelegate NCGetContextStructSize;
+ public readonly NCInitContextDelegate NCInitContext;
+ public readonly NCReInitContextDelegate NCReInitContext;
+ public readonly NCDestroyContextDelegate NCDestroyContext;
+ public readonly NCGetPublicKeyDelegate NCGetPublicKey;
+ public readonly NCValidateSecretKeyDelegate NCValidateSecretKey;
+ public readonly NCSignDataDelegate NCSignData;
+ public readonly NCVerifyDataDelegate NCVerifyData;
+ public readonly NCEncryptDelegate NCEncrypt;
+ public readonly NCDecryptDelegate NCDecrypt;
+ public readonly NCVerifyMacDelegate NCVerifyMac;
+ public readonly NCComputeMacDelegate NCComputeMac;
+
+#if DEBUG
+ public readonly NCGetConversationKeyDelegate NCGetConversationKey;
+#endif
+
+ private FunctionTable(SafeLibraryHandle library)
+ {
+ //Load the required high-level api functions
+ NCGetContextStructSize = library.DangerousGetFunction<NCGetContextStructSizeDelegate>();
+ NCInitContext = library.DangerousGetFunction<NCInitContextDelegate>();
+ NCReInitContext = library.DangerousGetFunction<NCReInitContextDelegate>();
+ NCDestroyContext = library.DangerousGetFunction<NCDestroyContextDelegate>();
+ NCGetPublicKey = library.DangerousGetFunction<NCGetPublicKeyDelegate>();
+ NCValidateSecretKey = library.DangerousGetFunction<NCValidateSecretKeyDelegate>();
+ NCSignData = library.DangerousGetFunction<NCSignDataDelegate>();
+ NCVerifyData = library.DangerousGetFunction<NCVerifyDataDelegate>();
+ NCSignData = library.DangerousGetFunction<NCSignDataDelegate>();
+ NCVerifyData = library.DangerousGetFunction<NCVerifyDataDelegate>();
+ NCEncrypt = library.DangerousGetFunction<NCEncryptDelegate>();
+ NCDecrypt = library.DangerousGetFunction<NCDecryptDelegate>();
+ NCVerifyMac = library.DangerousGetFunction<NCVerifyMacDelegate>();
+ NCComputeMac = library.DangerousGetFunction<NCComputeMacDelegate>();
+
+#if DEBUG
+ NCGetConversationKey = library.DangerousGetFunction<NCGetConversationKeyDelegate>();
+#endif
+ }
+
+ /// <summary>
+ /// Initialize a new function table from the specified library
+ /// </summary>
+ /// <param name="library"></param>
+ /// <returns>The function table structure</returns>
+ /// <exception cref="MissingMemberException"></exception>
+ /// <exception cref="EntryPointNotFoundException"></exception>
+ public static FunctionTable BuildFunctionTable(SafeLibraryHandle library) => new (library);
+
+ /*
+ * ################################################
+ *
+ * Functions match the noscrypt.h header file
+ *
+ * ################################################
+ */
+
+ //FUCNTIONS
+ [SafeMethodName("NCGetContextStructSize")]
+ internal delegate uint NCGetContextStructSizeDelegate();
+
+ [SafeMethodName("NCInitContext")]
+ internal delegate NCResult NCInitContextDelegate(IntPtr ctx, byte* entropy32);
+
+ [SafeMethodName("NCReInitContext")]
+ internal delegate NCResult NCReInitContextDelegate(IntPtr ctx, byte* entropy32);
+
+ [SafeMethodName("NCDestroyContext")]
+ internal delegate NCResult NCDestroyContextDelegate(IntPtr ctx);
+
+ [SafeMethodName("NCGetPublicKey")]
+ internal delegate NCResult NCGetPublicKeyDelegate(IntPtr ctx, NCSecretKey* secKey, NCPublicKey* publicKey);
+
+ [SafeMethodName("NCValidateSecretKey")]
+ internal delegate NCResult NCValidateSecretKeyDelegate(IntPtr ctx, NCSecretKey* secKey);
+
+ [SafeMethodName("NCSignData")]
+ 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, uint dataSize, byte* sig64);
+
+ [SafeMethodName("NCEncrypt")]
+ internal delegate NCResult NCEncryptDelegate(IntPtr ctx, NCSecretKey* sk, NCPublicKey* pk, NCEncryptionArgs* data);
+
+ [SafeMethodName("NCDecrypt")]
+ 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);
+
+ [SafeMethodName("NCComputeMac")]
+ internal delegate NCResult NCComputeMacDelegate(IntPtr ctx, byte* hmacKey32, byte* payload, uint payloadSize, byte* hmacOut32);
+
+ [SafeMethodName("NCGetConversationKey")]
+ internal delegate NCResult NCGetConversationKeyDelegate(nint ctx, NCSecretKey* sk, NCPublicKey* pk, byte* keyOut32);
+
+ }
+}
diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCEncryptionArgs.cs
new file mode 100644
index 0000000..a63d3b3
--- /dev/null
+++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/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 <https://www.gnu.org/licenses/>.
+
+using System;
+using System.Runtime.InteropServices;
+
+namespace VNLib.Utils.Cryptography.Noscrypt.@internal
+{
+ [StructLayout(LayoutKind.Sequential)]
+ internal unsafe struct NCEncryptionArgs
+ {
+ public byte* nonce32;
+ public byte* hmacKeyOut32;
+ public byte* inputData;
+ public byte* outputData;
+ public uint dataSize;
+ public uint version;
+ }
+}
diff --git a/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCMacVerifyArgs.cs b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCMacVerifyArgs.cs
new file mode 100644
index 0000000..8a9ba1f
--- /dev/null
+++ b/wrappers/dotnet/VNLib.Utils.Cryptography.Noscrypt/src/internal/NCMacVerifyArgs.cs
@@ -0,0 +1,34 @@
+// 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;
+
+namespace VNLib.Utils.Cryptography.Noscrypt.@internal
+{
+ internal unsafe struct NCMacVerifyArgs
+ {
+ /* The message authentication code certifying the Nip44 payload */
+ public byte* mac32;
+
+ /* The nonce used for the original message encryption */
+ public byte* nonce32;
+
+ /* The message payload data */
+ public byte* payload;
+
+ /* The size of the payload data */
+ public uint payloadSize;
+ }
+}