aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/embeddable-dll-service/csharp/Keypair.cs
diff options
context:
space:
mode:
Diffstat (limited to 'embeddable-dll-service/csharp/Keypair.cs')
-rw-r--r--embeddable-dll-service/csharp/Keypair.cs46
1 files changed, 7 insertions, 39 deletions
diff --git a/embeddable-dll-service/csharp/Keypair.cs b/embeddable-dll-service/csharp/Keypair.cs
index 98a00a30..e5764fbd 100644
--- a/embeddable-dll-service/csharp/Keypair.cs
+++ b/embeddable-dll-service/csharp/Keypair.cs
@@ -4,7 +4,6 @@
*/
using System;
-using System.ComponentModel;
using System.Runtime.InteropServices;
namespace Tunnel
@@ -20,46 +19,15 @@ namespace Tunnel
Private = priv;
}
+ [DllImport("tunnel.dll", EntryPoint = "WireGuardGenerateKeypair", CallingConvention = CallingConvention.Cdecl)]
+ private static extern bool WireGuardGenerateKeypair(byte[] publicKey, byte[] privateKey);
+
public static Keypair Generate()
{
- var algoHandle = new IntPtr();
- var statusCode = Win32.BCryptOpenAlgorithmProvider(ref algoHandle, Win32.BCRYPT_ECDH_ALGORITHM, null, 0);
- if (statusCode > 0)
- throw new Win32Exception((int)statusCode);
-
- try
- {
- var curveType = Win32.BCRYPT_ECC_CURVE_25519 + Char.MinValue;
- statusCode = Win32.BCryptSetProperty(algoHandle, Win32.BCRYPT_ECC_CURVE_NAME, curveType, curveType.Length * sizeof(char), 0);
- if (statusCode > 0)
- throw new Win32Exception((int)statusCode);
- var key = new IntPtr();
- statusCode = Win32.BCryptGenerateKeyPair(algoHandle, ref key, 255, 0);
- if (statusCode > 0)
- throw new Win32Exception((int)statusCode);
- try
- {
- statusCode = Win32.BCryptFinalizeKeyPair(key, 0);
- if (statusCode > 0)
- throw new Win32Exception((int)statusCode);
-
- var keyBlob = new Win32.KeyBlob();
- int exportedKeySize = 0;
- statusCode = Win32.BCryptExportKey(key, IntPtr.Zero, Win32.BCRYPT_ECCPRIVATE_BLOB, keyBlob, Marshal.SizeOf(typeof(Win32.KeyBlob)), out exportedKeySize);
- if (statusCode > 0)
- throw new Win32Exception((int)statusCode);
-
- return new Keypair(Convert.ToBase64String(keyBlob.Public), Convert.ToBase64String(keyBlob.Private));
- }
- finally
- {
- Win32.BCryptDestroyKey(key);
- }
- }
- finally
- {
- Win32.BCryptCloseAlgorithmProvider(algoHandle, 0);
- }
+ var publicKey = new byte[32];
+ var privateKey = new byte[32];
+ WireGuardGenerateKeypair(publicKey, privateKey);
+ return new Keypair(Convert.ToBase64String(publicKey), Convert.ToBase64String(privateKey));
}
}
}