Skip to content

Commit

Permalink
updated native libs dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
m3sserschmitt committed Oct 1, 2024
1 parent b2d6037 commit d7e9341
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 39 deletions.
37 changes: 23 additions & 14 deletions Enigma5.Crypto/Enigma5.Crypto.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
<None Include="../Libaenigma7/build/libaenigma.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="../Libaenigma7/build/libaenigma-kernelkeys.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
68 changes: 43 additions & 25 deletions Enigma5.Crypto/Native.cs
Original file line number Diff line number Diff line change
@@ -1,100 +1,118 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

namespace Enigma5.Crypto;

internal static unsafe partial class Native
{
[LibraryImport("cryptography")]
private static readonly List<string> Libs = ["libaenigma", "libaenigma-kernelkeys"];

static Native()
{
NativeLibrary.SetDllImportResolver(typeof(Native).Assembly, ImportResolver);
}

private static IntPtr ImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
IntPtr libHandle = IntPtr.Zero;
if (Libs.Contains(libraryName))
{
NativeLibrary.TryLoad(libraryName, assembly, DllImportSearchPath.AssemblyDirectory, out libHandle);
}
return libHandle;
}

[LibraryImport("libaenigma")]
internal static partial uint GetDefaultAddressSize();

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial uint GetDefaultPKeySize();

[LibraryImport("kernelkeys")]
[LibraryImport("libaenigma-kernelkeys")]
internal static partial uint GetKernelKeyMaxSize();

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateAsymmetricEncryptionContext(
[MarshalAs(UnmanagedType.LPStr)] string key);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateAsymmetricDecryptionContext(
IntPtr key,
[MarshalAs(UnmanagedType.LPStr)] string passphrase);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateAsymmetricEncryptionContextFromFile(
[MarshalAs(UnmanagedType.LPStr)] string path);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateAsymmetricDecryptionContextFromFile(
[MarshalAs(UnmanagedType.LPStr)] string path,
[MarshalAs(UnmanagedType.LPStr)] string passphrase);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateSignatureContext(
IntPtr key,
[MarshalAs(UnmanagedType.LPStr)] string passphrase);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateVerificationContext(
[MarshalAs(UnmanagedType.LPStr)] string key);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateSignatureContextFromFile(
[MarshalAs(UnmanagedType.LPStr)] string path,
[MarshalAs(UnmanagedType.LPStr)] string passphrase);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr CreateVerificationContextFromFile(
[MarshalAs(UnmanagedType.LPStr)] string path);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial void FreeContext(IntPtr ctx);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr EncryptData(
IntPtr ctx,
[In] byte[] plaintext,
uint plaintextLen,
out int ciphertextLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr DecryptData(
IntPtr ctx,
[In] byte[]
ciphertext,
uint ciphertextLen,
out int plaintextLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr SignData(
IntPtr ctx,
[In] byte[]
plaintext,
uint plaintextLen,
out int signatureLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool VerifySignature(IntPtr ctx,
[In] byte[] ciphertext,
uint ciphertextLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial uint GetEnvelopeSize(uint plaintextLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial uint GetOpenEnvelopeSize(uint envelopeSize);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial uint GetSignedDataSize(uint dataLen);

[LibraryImport("cryptography")]
[LibraryImport("libaenigma")]
internal static partial IntPtr UnsealOnion(IntPtr ctx, [In] byte[] onion, out int outLen);

[LibraryImport("cryptography", StringMarshallingCustomType = typeof(Utf8StringMarshaller))]
[LibraryImport("libaenigma", StringMarshallingCustomType = typeof(Utf8StringMarshaller))]
internal static partial IntPtr SealOnion(
[In] byte[] plaintext,
uint plaintextLen,
Expand All @@ -103,28 +121,28 @@ internal static partial IntPtr SealOnion(
uint count,
out int outLen);

[LibraryImport("kernelkeys")]
[LibraryImport("libaenigma-kernelkeys")]
internal static partial int CreateKey(
[In] char[] keyName,
IntPtr keyMaterial,
uint keyMaterialSize,
[In] char[] description,
KernelKeyring ringId);

[LibraryImport("kernelkeys")]
[LibraryImport("libaenigma-kernelkeys")]
internal static partial int SearchKey(
[In] char[] keyName,
[In] char[] description,
KernelKeyring ringId
);

[LibraryImport("kernelkeys")]
[LibraryImport("libaenigma-kernelkeys")]
internal static partial int ReadKey(
int keyId,
IntPtr keyMaterial
);

[LibraryImport("kernelkeys")]
[LibraryImport("libaenigma-kernelkeys")]
internal static partial int RemoveKey(
int keyId
);
Expand Down

0 comments on commit d7e9341

Please sign in to comment.