From 48824069f0ea614802307015ea0db898918430ca Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 19 Mar 2024 10:05:26 -0700 Subject: [PATCH 1/6] Change the CryptoKit interop entrypoints to use the Swift calling convention This does not change the APIs yet, as we need Swift struct lowering support in Mono before we can switch to using more structured Swift types. This PR switches the entry-points on the C side to use non-method symbols as we'll lose the ability to represent the Swift signatures well in C as we move to a more Swift-friendly API surface (until we are able to remove the native PAL API surface) --- .../Interop.Aead.cs | 4 ++ .../pal_swiftbindings.h | 59 ++----------------- .../pal_swiftbindings.swift | 8 +-- 3 files changed, 12 insertions(+), 59 deletions(-) diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs index edadae0ea60ecf..da2511a6c3ea26 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs @@ -164,6 +164,7 @@ internal static unsafe void AesGcmDecrypt( } [LibraryImport(Libraries.AppleCryptoNative)] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])] private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Encrypt( byte* keyPtr, int keyLength, @@ -179,6 +180,7 @@ private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Encrypt( int aadLength); [LibraryImport(Libraries.AppleCryptoNative)] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])] private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Decrypt( byte* keyPtr, int keyLength, @@ -194,6 +196,7 @@ private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Decrypt( int aadLength); [LibraryImport(Libraries.AppleCryptoNative)] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])] private static unsafe partial int AppleCryptoNative_AesGcmEncrypt( byte* keyPtr, int keyLength, @@ -209,6 +212,7 @@ private static unsafe partial int AppleCryptoNative_AesGcmEncrypt( int aadLength); [LibraryImport(Libraries.AppleCryptoNative)] + [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])] private static unsafe partial int AppleCryptoNative_AesGcmDecrypt( byte* keyPtr, int keyLength, diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.h b/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.h index 9fd0f1ea0ab318..fd90fd0ad82164 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.h +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.h @@ -6,58 +6,7 @@ #include "pal_types.h" #include "pal_compiler.h" -PALEXPORT int32_t AppleCryptoNative_ChaCha20Poly1305Encrypt( - uint8_t* keyPtr, - int32_t keyLength, - uint8_t* noncePtr, - int32_t nonceLength, - uint8_t* plaintextPtr, - int32_t plaintextLength, - uint8_t* ciphertextBuffer, - int32_t ciphertextBufferLength, - uint8_t* tagBuffer, - int32_t tagBufferLength, - uint8_t* aadPtr, - int32_t aadLength); - -PALEXPORT int32_t AppleCryptoNative_ChaCha20Poly1305Decrypt( - uint8_t* keyPtr, - int32_t keyLength, - uint8_t* noncePtr, - int32_t nonceLength, - uint8_t* ciphertextPtr, - int32_t ciphertextLength, - uint8_t* tagPtr, - int32_t tagLength, - uint8_t* plaintextBuffer, - int32_t plaintextBufferLength, - uint8_t* aadPtr, - int32_t aadLength); - -PALEXPORT int32_t AppleCryptoNative_AesGcmEncrypt( - uint8_t* keyPtr, - int32_t keyLength, - uint8_t* noncePtr, - int32_t nonceLength, - uint8_t* plaintextPtr, - int32_t plaintextLength, - uint8_t* ciphertextBuffer, - int32_t ciphertextBufferLength, - uint8_t* tagBuffer, - int32_t tagBufferLength, - uint8_t* aadPtr, - int32_t aadLength); - -PALEXPORT int32_t AppleCryptoNative_AesGcmDecrypt( - uint8_t* keyPtr, - int32_t keyLength, - uint8_t* noncePtr, - int32_t nonceLength, - uint8_t* ciphertextPtr, - int32_t ciphertextLength, - uint8_t* tagPtr, - int32_t tagLength, - uint8_t* plaintextBuffer, - int32_t plaintextBufferLength, - uint8_t* aadPtr, - int32_t aadLength); +EXTERN_C void* AppleCryptoNative_ChaCha20Poly1305Encrypt; +EXTERN_C void* AppleCryptoNative_ChaCha20Poly1305Decrypt; +EXTERN_C void* AppleCryptoNative_AesGcmEncrypt; +EXTERN_C void* AppleCryptoNative_AesGcmDecrypt; diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.swift b/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.swift index 7b04d52504fa24..2a50deefb52bc6 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.swift +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/pal_swiftbindings.swift @@ -4,7 +4,7 @@ import CryptoKit import Foundation -@_cdecl("AppleCryptoNative_ChaCha20Poly1305Encrypt") +@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Encrypt") public func AppleCryptoNative_ChaCha20Poly1305Encrypt( keyPtr: UnsafeMutableRawPointer, keyLength: Int32, @@ -41,7 +41,7 @@ public func AppleCryptoNative_ChaCha20Poly1305Encrypt( return 1 } -@_cdecl("AppleCryptoNative_ChaCha20Poly1305Decrypt") +@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Decrypt") public func AppleCryptoNative_ChaCha20Poly1305Decrypt( keyPtr: UnsafeMutableRawPointer, keyLength: Int32, @@ -86,7 +86,7 @@ public func AppleCryptoNative_ChaCha20Poly1305Decrypt( } } -@_cdecl("AppleCryptoNative_AesGcmEncrypt") +@_silgen_name("AppleCryptoNative_AesGcmEncrypt") public func AppleCryptoNative_AesGcmEncrypt( keyPtr: UnsafeMutableRawPointer, keyLength: Int32, @@ -123,7 +123,7 @@ public func AppleCryptoNative_AesGcmEncrypt( return 1 } -@_cdecl("AppleCryptoNative_AesGcmDecrypt") +@_silgen_name("AppleCryptoNative_AesGcmDecrypt") public func AppleCryptoNative_AesGcmDecrypt( keyPtr: UnsafeMutableRawPointer, keyLength: Int32, From ddfc75fdf50f1533f0e71d3f77ed5af00ea170f8 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 19 Mar 2024 10:30:48 -0700 Subject: [PATCH 2/6] Now that we have a non-function entry in DllImportEntry, make the address-of operation explicit in the DllImportEntry macro. --- src/native/minipal/entrypoints.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/minipal/entrypoints.h b/src/native/minipal/entrypoints.h index d7908764e05e69..d92b4546a29262 100644 --- a/src/native/minipal/entrypoints.h +++ b/src/native/minipal/entrypoints.h @@ -16,7 +16,7 @@ typedef struct // expands to: {"impl", (void*)impl}, #define DllImportEntry(impl) \ - {#impl, (void*)impl}, + {#impl, (void*)&impl}, static const void* minipal_resolve_dllimport(const Entry* resolutionTable, size_t tableLength, const char* name) { From a1d15e0b047c2b4ad784d99755edecb79b5aec90 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 19 Mar 2024 10:47:50 -0700 Subject: [PATCH 3/6] Explicitly specify the null qcall (that's just a sentinel until a QCall is added to Mono) instead of using DllImportEntry, which now takes an address --- src/mono/mono/metadata/native-library-qcall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/native-library-qcall.c b/src/mono/mono/metadata/native-library-qcall.c index 5b17173c666123..77ead5fbd48749 100644 --- a/src/mono/mono/metadata/native-library-qcall.c +++ b/src/mono/mono/metadata/native-library-qcall.c @@ -3,7 +3,7 @@ static Entry mono_qcalls[] = { - DllImportEntry(NULL) // This NULL entry can be removed when a QCall is added to Mono (and added to this array) + {"NULL", NULL}, // This NULL entry can be removed when a QCall is added to Mono (and added to this array) }; gpointer From 788b9e599c94daa1a80a06379e8f93a2ca31a6aa Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 19 Mar 2024 12:08:43 -0700 Subject: [PATCH 4/6] Add using --- .../System.Security.Cryptography.Native.Apple/Interop.Aead.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs index da2511a6c3ea26..07027d0aa2b2c0 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.Apple; From 628d6c1fd759edf1fb5e704f0e34dd41df0c355e Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 19 Mar 2024 17:53:22 -0700 Subject: [PATCH 5/6] Disable warning --- .../System.Security.Cryptography.Native.Apple/Interop.Aead.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs index 07027d0aa2b2c0..2f405e0bfd0da1 100644 --- a/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs +++ b/src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Aead.cs @@ -8,6 +8,8 @@ using System.Security.Cryptography; using System.Security.Cryptography.Apple; +#pragma warning disable CS3016 // Arrays as attribute arguments are not CLS Compliant + internal static partial class Interop { internal static partial class AppleCrypto From 555051514cb00c8ee63a1f37202fe8ce83e53e94 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 20 Mar 2024 08:48:17 -0700 Subject: [PATCH 6/6] Update entrypoints.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aleksey Kliger (λgeek) --- src/native/minipal/entrypoints.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/minipal/entrypoints.h b/src/native/minipal/entrypoints.h index d92b4546a29262..5ef45699cacdf1 100644 --- a/src/native/minipal/entrypoints.h +++ b/src/native/minipal/entrypoints.h @@ -14,7 +14,7 @@ typedef struct const void* method; } Entry; -// expands to: {"impl", (void*)impl}, +// expands to: {"impl", (void*)&impl}, #define DllImportEntry(impl) \ {#impl, (void*)&impl},