Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the CryptoKit interop entrypoints to use the Swift calling convention #99970

Merged
merged 7 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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
Expand Down Expand Up @@ -164,6 +167,7 @@ internal static partial class AppleCrypto
}

[LibraryImport(Libraries.AppleCryptoNative)]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Encrypt(
byte* keyPtr,
int keyLength,
Expand All @@ -179,6 +183,7 @@ internal static partial class AppleCrypto
int aadLength);

[LibraryImport(Libraries.AppleCryptoNative)]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
private static unsafe partial int AppleCryptoNative_ChaCha20Poly1305Decrypt(
byte* keyPtr,
int keyLength,
Expand All @@ -194,6 +199,7 @@ internal static partial class AppleCrypto
int aadLength);

[LibraryImport(Libraries.AppleCryptoNative)]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
private static unsafe partial int AppleCryptoNative_AesGcmEncrypt(
byte* keyPtr,
int keyLength,
Expand All @@ -209,6 +215,7 @@ internal static partial class AppleCrypto
int aadLength);

[LibraryImport(Libraries.AppleCryptoNative)]
[UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
private static unsafe partial int AppleCryptoNative_AesGcmDecrypt(
byte* keyPtr,
int keyLength,
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/native-library-qcall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import CryptoKit
import Foundation

@_cdecl("AppleCryptoNative_ChaCha20Poly1305Encrypt")
@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Encrypt")
public func AppleCryptoNative_ChaCha20Poly1305Encrypt(
keyPtr: UnsafeMutableRawPointer,
keyLength: Int32,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -86,7 +86,7 @@ public func AppleCryptoNative_ChaCha20Poly1305Decrypt(
}
}

@_cdecl("AppleCryptoNative_AesGcmEncrypt")
@_silgen_name("AppleCryptoNative_AesGcmEncrypt")
public func AppleCryptoNative_AesGcmEncrypt(
keyPtr: UnsafeMutableRawPointer,
keyLength: Int32,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/native/minipal/entrypoints.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ typedef struct

// expands to: {"impl", (void*)impl},
jkoritzinsky marked this conversation as resolved.
Show resolved Hide resolved
#define DllImportEntry(impl) \
{#impl, (void*)impl},
{#impl, (void*)&impl},

static const void* minipal_resolve_dllimport(const Entry* resolutionTable, size_t tableLength, const char* name)
{
Expand Down