diff --git a/src/libraries/Common/src/Interop/OSX/Swift.Runtime/CryptoKit.cs b/src/libraries/Common/src/Interop/OSX/Swift.Runtime/CryptoKit.cs
new file mode 100644
index 00000000000000..80e4672acdd0e8
--- /dev/null
+++ b/src/libraries/Common/src/Interop/OSX/Swift.Runtime/CryptoKit.cs
@@ -0,0 +1,494 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.InteropServices.Swift;
+using System.Security.Cryptography;
+
+#pragma warning disable CS3016 // Arrays as attribute arguments are not CLS Compliant
+#pragma warning disable SYSLIB1051
+#pragma warning disable CA1805
+
+namespace Swift
+{
+ ///
+ /// Represents ChaChaPoly in C#.
+ ///
+ internal unsafe partial struct ChaChaPoly
+ {
+ ///
+ /// Represents Nonce in C#.
+ ///
+ internal sealed unsafe partial class Nonce : IDisposable, ISwiftObject
+ {
+ private static nuint PayloadSize = (nuint)((Runtime.ValueWitnessTable*)Swift.Runtime.GetValueWitnessTable(Metadata))->Size;
+
+ private readonly void* _payload;
+
+ private bool _disposed = false;
+
+ internal Nonce()
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+ CryptoKit.PInvoke_ChaChaPoly_Nonce_Init(swiftIndirectResult);
+ }
+
+ internal Nonce(Data data)
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+
+ void* metadata = Swift.Runtime.GetMetadata(data);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* witnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, metadata, null);
+
+ CryptoKit.PInvoke_ChaChaPoly_Nonce_Init2(swiftIndirectResult, &data, metadata, witnessTable, out SwiftError error);
+
+ if (error.Value != null)
+ {
+ NativeMemory.Free(_payload);
+ throw new CryptographicException();
+ }
+ }
+
+ internal void* Payload => _payload;
+
+ public static void* Metadata => CryptoKit.PInvoke_ChaChaPoly_Nonce_GetMetadata();
+
+ public void Dispose()
+ {
+ if (!_disposed)
+ {
+ NativeMemory.Free(_payload);
+ _disposed = true;
+ GC.SuppressFinalize(this);
+ }
+ }
+
+ ~Nonce()
+ {
+ NativeMemory.Free(_payload);
+ }
+ }
+
+ ///
+ /// Represents SealedBox in C#.
+ ///
+ [StructLayout(LayoutKind.Sequential, Size = 16)]
+ internal unsafe partial struct SealedBox
+ {
+ private readonly Data _combined;
+
+ internal SealedBox(ChaChaPoly.Nonce nonce, Data ciphertext, Data tag)
+ {
+ void* ciphertextMetadata = Swift.Runtime.GetMetadata(ciphertext);
+ void* tagMetadata = Swift.Runtime.GetMetadata(tag);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* ciphertextWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, ciphertextMetadata, null);
+ void* tagWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, tagMetadata, null);
+
+ this = CryptoKit.PInvoke_ChaChaPoly_SealedBox_Init(
+ nonce.Payload,
+ &ciphertext,
+ &tag,
+ ciphertextMetadata,
+ tagMetadata,
+ ciphertextWitnessTable,
+ tagWitnessTable,
+ out SwiftError error);
+
+ if (error.Value != null)
+ {
+ throw new CryptographicException();
+ }
+ }
+
+ internal Data Ciphertext => CryptoKit.PInvoke_ChaChaPoly_SealedBox_GetCiphertext(this);
+
+ internal Data Tag => CryptoKit.PInvoke_ChaChaPoly_SealedBox_GetTag(this);
+ }
+
+ ///
+ /// Encrypts the plaintext using the key, nonce, and authenticated data.
+ ///
+ internal static unsafe SealedBox seal(Plaintext plaintext, SymmetricKey key, Nonce nonce, AuthenticateData aad, out SwiftError error) where Plaintext : unmanaged, ISwiftObject where AuthenticateData : unmanaged, ISwiftObject {
+ void* plaintextMetadata = Swift.Runtime.GetMetadata(plaintext);
+ void* aadMetadata = Swift.Runtime.GetMetadata(aad);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* plaintextWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, plaintextMetadata, null);
+ void* aadWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, aadMetadata, null);
+
+ SealedBox sealedBox = CryptoKit.PInvoke_ChaChaPoly_Seal(
+ &plaintext,
+ key.Payload,
+ nonce.Payload,
+ &aad,
+ plaintextMetadata,
+ aadMetadata,
+ plaintextWitnessTable,
+ aadWitnessTable,
+ out error);
+
+ return sealedBox;
+ }
+
+ ///
+ /// Decrypts the sealed box using the key and authenticated data.
+ ///
+ internal static unsafe Data open(SealedBox sealedBox, SymmetricKey key, AuthenticateData aad, out SwiftError error) where AuthenticateData : unmanaged, ISwiftObject {
+ void* metadata = Swift.Runtime.GetMetadata(aad);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* witnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, metadata, null);
+
+ Data data = CryptoKit.PInvoke_ChaChaPoly_Open(
+ sealedBox,
+ key.Payload,
+ &aad,
+ metadata,
+ witnessTable,
+ out error);
+
+ return data;
+ }
+ }
+
+ ///
+ /// Represents AesGcm in C#.
+ ///
+ internal unsafe partial struct AesGcm
+ {
+ ///
+ /// Represents Nonce in C#.
+ ///
+ internal sealed unsafe partial class Nonce : IDisposable, ISwiftObject
+ {
+ private static nuint PayloadSize = (nuint)((Runtime.ValueWitnessTable*)Swift.Runtime.GetValueWitnessTable(Metadata))->Size;
+
+ private readonly void* _payload;
+
+ private bool _disposed = false;
+
+ internal Nonce()
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+ CryptoKit.PInvoke_AesGcm_Nonce_Init(swiftIndirectResult);
+ }
+
+ internal Nonce(Data data)
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+
+ void* metadata = Swift.Runtime.GetMetadata(data);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* witnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, metadata, null);
+
+ CryptoKit.PInvoke_AesGcm_Nonce_Init2(swiftIndirectResult, &data, metadata, witnessTable, out SwiftError error);
+
+ if (error.Value != null)
+ {
+ NativeMemory.Free(_payload);
+ throw new CryptographicException();
+ }
+ }
+
+ internal void* Payload => _payload;
+
+ public static void* Metadata => CryptoKit.PInvoke_AesGcm_Nonce_GetMetadata();
+
+ public void Dispose()
+ {
+ if (!_disposed)
+ {
+ NativeMemory.Free(_payload);
+ _disposed = true;
+ GC.SuppressFinalize(this);
+ }
+ }
+
+ ~Nonce()
+ {
+ NativeMemory.Free(_payload);
+ }
+ }
+
+ ///
+ /// Represents SealedBox in C#.
+ ///
+ internal sealed unsafe partial class SealedBox : IDisposable, ISwiftObject
+ {
+ private static nuint PayloadSize = (nuint)((Runtime.ValueWitnessTable*)Swift.Runtime.GetValueWitnessTable(Metadata))->Size;
+
+ private readonly void* _payload;
+
+ private bool _disposed = false;
+
+ internal SealedBox()
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ }
+
+ internal SealedBox(AesGcm.Nonce nonce, Data ciphertext, Data tag)
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+
+ void* ciphertextMetadata = Swift.Runtime.GetMetadata(ciphertext);
+ void* tagMetadata = Swift.Runtime.GetMetadata(tag);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* ciphertextWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, ciphertextMetadata, null);
+ void* tagWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, tagMetadata, null);
+
+ CryptoKit.PInvoke_AesGcm_SealedBox_Init(
+ swiftIndirectResult,
+ nonce.Payload,
+ &ciphertext,
+ &tag,
+ ciphertextMetadata,
+ tagMetadata,
+ ciphertextWitnessTable,
+ tagWitnessTable,
+ out SwiftError error);
+
+ if (error.Value != null)
+ {
+ NativeMemory.Free(_payload);
+ throw new CryptographicException();
+ }
+ }
+
+ internal void* Payload => _payload;
+
+ public static void* Metadata => CryptoKit.PInvoke_AesGcm_SealedBox_GetMetadata();
+
+ internal Data Ciphertext => CryptoKit.PInvoke_AesGcm_SealedBox_GetCiphertext(new SwiftSelf(_payload));
+
+ internal Data Tag => CryptoKit.PInvoke_AesGcm_SealedBox_GetTag(new SwiftSelf(_payload));
+
+ public void Dispose()
+ {
+ if (!_disposed)
+ {
+ NativeMemory.Free(_payload);
+ _disposed = true;
+ GC.SuppressFinalize(this);
+ }
+ }
+
+ ~SealedBox()
+ {
+ NativeMemory.Free(_payload);
+ }
+ }
+
+ ///
+ /// Encrypts the plaintext using the key, nonce, and authenticated data.
+ ///
+ internal static unsafe SealedBox seal(Plaintext plaintext, SymmetricKey key, Nonce nonce, AuthenticateData aad, out SwiftError error) where Plaintext : unmanaged, ISwiftObject where AuthenticateData : unmanaged, ISwiftObject {
+ AesGcm.SealedBox sealedBox = new AesGcm.SealedBox();
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(sealedBox.Payload);
+
+ void* plaintextMetadata = Swift.Runtime.GetMetadata(plaintext);
+ void* aadMetadata = Swift.Runtime.GetMetadata(aad);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* plaintextWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, plaintextMetadata, null);
+ void* aadWitnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, aadMetadata, null);
+
+ CryptoKit.PInvoke_AesGcm_Seal(
+ swiftIndirectResult,
+ &plaintext,
+ key.Payload,
+ nonce.Payload,
+ &aad,
+ plaintextMetadata,
+ aadMetadata,
+ plaintextWitnessTable,
+ aadWitnessTable,
+ out error);
+
+ return sealedBox;
+ }
+
+ ///
+ /// Decrypts the sealed box using the key and authenticated data.
+ ///
+ internal static unsafe Data open(SealedBox sealedBox, SymmetricKey key, AuthenticateData aad, out SwiftError error) where AuthenticateData : unmanaged, ISwiftObject {
+ void* metadata = Swift.Runtime.GetMetadata(aad);
+ void* conformanceDescriptor = IDataProtocol.GetConformanceDescriptor;
+ void* witnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, metadata, null);
+
+ Data data = CryptoKit.PInvoke_AesGcm_Open(
+ sealedBox.Payload,
+ key.Payload,
+ &aad,
+ metadata,
+ witnessTable,
+ out error);
+
+ return data;
+ }
+ }
+
+ ///
+ /// Represents SymmetricKey in C#.
+ ///
+ internal sealed unsafe partial class SymmetricKey : IDisposable, ISwiftObject
+ {
+ private static nuint PayloadSize = (nuint)((Runtime.ValueWitnessTable*)Swift.Runtime.GetValueWitnessTable(Metadata))->Size;
+
+ internal readonly void* _payload;
+
+ private bool _disposed = false;
+
+ internal SymmetricKey(SymmetricKeySize symmetricKeySize)
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+ CryptoKit.PInvoke_SymmetricKey_Init(swiftIndirectResult, &symmetricKeySize);
+ }
+
+ internal SymmetricKey(Data data)
+ {
+ _payload = NativeMemory.Alloc(PayloadSize);
+ SwiftIndirectResult swiftIndirectResult = new SwiftIndirectResult(_payload);
+
+ void* metadata = Swift.Runtime.GetMetadata(data);
+ void* conformanceDescriptor = IContiguousBytes.GetConformanceDescriptor;
+ void* witnessTable = Foundation.PInvoke_Swift_GetWitnessTable(conformanceDescriptor, metadata, null);
+
+ CryptoKit.PInvoke_SymmetricKey_Init2(swiftIndirectResult, &data, metadata, witnessTable);
+ }
+
+ internal void* Payload => _payload;
+
+ public static void* Metadata => CryptoKit.PInvoke_SymmetricKey_GetMetadata();
+
+ public void Dispose()
+ {
+ if (!_disposed)
+ {
+ NativeMemory.Free(_payload);
+ _disposed = true;
+ GC.SuppressFinalize(this);
+ }
+ }
+
+ ~SymmetricKey()
+ {
+ NativeMemory.Free(_payload);
+ }
+ }
+
+ ///
+ /// Represents SymmetricKeySize in C#.
+ ///
+ [StructLayout(LayoutKind.Sequential, Size = 8)]
+ internal unsafe partial struct SymmetricKeySize
+ {
+ private readonly nint _bitCount;
+
+ internal SymmetricKeySize(nint bitCount)
+ {
+ SymmetricKeySize instance;
+ CryptoKit.PInvoke_init(new SwiftIndirectResult(&instance), bitCount);
+ this = instance;
+ }
+ }
+
+ ///
+ /// Swift CryptoKit PInvoke methods in C#.
+ ///
+ internal static partial class CryptoKit
+ {
+ internal const string Path = "/System/Library/Frameworks/CryptoKit.framework/CryptoKit";
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO5NonceVAEycfC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_ChaChaPoly_Nonce_Init(SwiftIndirectResult result);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO5NonceV4dataAEx_tKc10Foundation12DataProtocolRzlufC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_ChaChaPoly_Nonce_Init2(SwiftIndirectResult result, void* data, void* metadata, void* witnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO5NonceVMa")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_ChaChaPoly_Nonce_GetMetadata();
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO9SealedBoxV10ciphertext10Foundation4DataVvg")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_ChaChaPoly_SealedBox_GetCiphertext(ChaChaPoly.SealedBox sealedBox);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO9SealedBoxV3tag10Foundation4DataVvg")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_ChaChaPoly_SealedBox_GetTag(ChaChaPoly.SealedBox sealedBox);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO9SealedBoxV5nonce10ciphertext3tagAeC5NonceV_xq_tKc10Foundation12DataProtocolRzAkLR_r0_lufC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial ChaChaPoly.SealedBox PInvoke_ChaChaPoly_SealedBox_Init(void* nonce, void* ciphertext, void* tag, void* ciphertextMetadata, void* tagMetadata, void* ciphertextWitnessTable, void* tagWitnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO5NonceVAGycfC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_AesGcm_Nonce_Init(SwiftIndirectResult result);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO5NonceV4dataAGx_tKc10Foundation12DataProtocolRzlufC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_AesGcm_Nonce_Init2(SwiftIndirectResult result, void* data, void* metadata, void* witnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO5NonceVMa")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_AesGcm_Nonce_GetMetadata();
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO9SealedBoxV10ciphertext10Foundation4DataVvg")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_AesGcm_SealedBox_GetCiphertext(SwiftSelf sealedBox);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO9SealedBoxV3tag10Foundation4DataVvg")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_AesGcm_SealedBox_GetTag(SwiftSelf sealedBox);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO9SealedBoxV5nonce10ciphertext3tagAgE5NonceV_xq_tKc10Foundation12DataProtocolRzAmNR_r0_lufC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_AesGcm_SealedBox_Init(SwiftIndirectResult result, void* nonce, void* ciphertext, void* tag, void* ciphertextMetadata, void* tagMetadata, void* ciphertextWitnessTable, void* tagWitnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO9SealedBoxVMa")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_AesGcm_SealedBox_GetMetadata();
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit12SymmetricKeyV4sizeAcA0cD4SizeV_tcfC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_SymmetricKey_Init(SwiftIndirectResult result, SymmetricKeySize* symmetricKeySize);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit12SymmetricKeyV4dataACx_tc10Foundation15ContiguousBytesRzlufC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_SymmetricKey_Init2(SwiftIndirectResult result, void* data, void* metadata, void* witnessTable);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit12SymmetricKeyVMa")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_SymmetricKey_GetMetadata();
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit16SymmetricKeySizeV8bitCountACSi_tcfC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_init(SwiftIndirectResult result, nint bitCount);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO4seal_5using5nonce14authenticatingAC9SealedBoxVx_AA12SymmetricKeyVAC5NonceVSgq_tK10Foundation12DataProtocolRzAoPR_r0_lFZ")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial ChaChaPoly.SealedBox PInvoke_ChaChaPoly_Seal(void* plaintext, void* key, void* nonce, void* aad, void* plaintextMetadata, void* aadMetadata, void* plaintextWitnessTable, void* aadWitnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit03ChaC4PolyO4open_5using14authenticating10Foundation4DataVAC9SealedBoxV_AA12SymmetricKeyVxtKAG0I8ProtocolRzlFZ")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_ChaChaPoly_Open(ChaChaPoly.SealedBox sealedBox, void* key, void* aad, void* metadata, void* witnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO4seal_5using5nonce14authenticatingAE9SealedBoxVx_AA12SymmetricKeyVAE5NonceVSgq_tK10Foundation12DataProtocolRzAqRR_r0_lFZ")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_AesGcm_Seal(SwiftIndirectResult result, void* plaintext, void* key, void* nonce, void* aad, void* plaintextMetadata, void* aadMetadata, void* plaintextWitnessTable, void* aadWitnessTable, out SwiftError error);
+
+ [LibraryImport(Path, EntryPoint = "$s9CryptoKit3AESO3GCMO4open_5using14authenticating10Foundation4DataVAE9SealedBoxV_AA12SymmetricKeyVxtKAI0I8ProtocolRzlFZ")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_AesGcm_Open(void* sealedBox, void* key, void* aad, void* metadata, void* witnessTable, out SwiftError error);
+ }
+}
diff --git a/src/libraries/Common/src/Interop/OSX/Swift.Runtime/Foundation.cs b/src/libraries/Common/src/Interop/OSX/Swift.Runtime/Foundation.cs
new file mode 100644
index 00000000000000..1c5ecec46f3652
--- /dev/null
+++ b/src/libraries/Common/src/Interop/OSX/Swift.Runtime/Foundation.cs
@@ -0,0 +1,278 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Reflection;
+
+#pragma warning disable CS3016 // Arrays as attribute arguments are not CLS Compliant
+#pragma warning disable SYSLIB1051
+#pragma warning disable IDE0060
+
+namespace Swift
+{
+ ///
+ /// Represents a Swift type in C#.
+ ///
+ internal unsafe interface ISwiftObject
+ {
+ public static abstract void* Metadata { get; }
+ }
+
+ //
+ // Represents Swift UnsafePointer in C#.
+ //
+ internal readonly unsafe struct UnsafePointer where T : unmanaged
+ {
+ private readonly T* _pointee;
+ public UnsafePointer(T* pointee)
+ {
+ this._pointee = pointee;
+ }
+
+ public T* Pointee => _pointee;
+
+ public static implicit operator T*(UnsafePointer pointer) => pointer.Pointee;
+
+ public static implicit operator UnsafePointer(T* pointee) => new(pointee);
+ }
+
+ //
+ // Represents Swift UnsafeMutablePointer in C#.
+ //
+ internal readonly unsafe struct UnsafeMutablePointer where T : unmanaged
+ {
+ private readonly T* _pointee;
+ public UnsafeMutablePointer(T* pointee)
+ {
+ _pointee = pointee;
+ }
+
+ public T* Pointee => _pointee;
+
+ public static implicit operator T*(UnsafeMutablePointer pointer) => pointer.Pointee;
+
+ public static implicit operator UnsafeMutablePointer(T* pointee) => new(pointee);
+ }
+
+ //
+ // Represents Swift UnsafeRawPointer in C#.
+ //
+ internal readonly unsafe struct UnsafeRawPointer
+ {
+ private readonly void* _pointee;
+ public UnsafeRawPointer(void* pointee)
+ {
+ _pointee = pointee;
+ }
+
+ public void* Pointee => _pointee;
+
+ public static implicit operator void*(UnsafeRawPointer pointer) => pointer.Pointee;
+
+ public static implicit operator UnsafeRawPointer(void* pointee) => new(pointee);
+ }
+
+ //
+ // Represents Swift UnsafeMutableRawPointer in C#.
+ //
+ internal readonly unsafe struct UnsafeMutableRawPointer
+ {
+ private readonly void* _pointee;
+ public UnsafeMutableRawPointer(void* pointee)
+ {
+ _pointee = pointee;
+ }
+
+ public void* Pointee => _pointee;
+
+ public static implicit operator void*(UnsafeMutableRawPointer pointer) => pointer.Pointee;
+
+ public static implicit operator UnsafeMutableRawPointer(void* pointee) => new(pointee);
+ }
+
+ //
+ // Represents Swift UnsafeBufferPointer in C#.
+ //
+ internal readonly unsafe struct UnsafeBufferPointer where T : unmanaged
+ {
+ private readonly T* _baseAddress;
+ private readonly nint _count;
+ public UnsafeBufferPointer(T* baseAddress, nint count)
+ {
+ _baseAddress = baseAddress;
+ _count = count;
+ }
+
+ public T* BaseAddress => _baseAddress;
+ public nint Count => _count;
+ }
+
+ //
+ // Represents Swift UnsafeMutableBufferPointer in C#.
+ //
+ internal readonly unsafe struct UnsafeMutableBufferPointer where T : unmanaged
+ {
+ private readonly T* _baseAddress;
+ private readonly nint _count;
+ public UnsafeMutableBufferPointer(T* baseAddress, nint count)
+ {
+ _baseAddress = baseAddress;
+ _count = count;
+ }
+
+ public T* BaseAddress => _baseAddress;
+ public nint Count => _count;
+ }
+
+ //
+ // Represents Swift Foundation.Data in C#.
+ //
+ [StructLayout(LayoutKind.Sequential, Size = 16)]
+ [InlineArray(16)]
+ internal unsafe partial struct Data : ISwiftObject
+ {
+ private byte _payload;
+
+ internal unsafe Data(UnsafeRawPointer pointer, nint count)
+ {
+ this = Foundation.PInvoke_Data_InitWithBytes(pointer, count);
+ }
+
+ internal byte Payload => _payload;
+
+ internal readonly nint Count => Foundation.PInvoke_Data_GetCount(this);
+
+ internal unsafe void CopyBytes(UnsafeMutablePointer buffer, nint count)
+ {
+ Foundation.PInvoke_Data_CopyBytes(buffer, count, this);
+ }
+
+ public static void* Metadata => Foundation.PInvoke_Data_GetMetadata();
+ }
+
+ ///
+ /// Represents Swift Foundation.DataProtocol in C#.
+ ///
+ internal unsafe interface IDataProtocol
+ {
+ public static void* GetConformanceDescriptor => Runtime.GetConformanceDescriptor("$s10Foundation4DataVAA0B8ProtocolAAMc");
+ }
+
+ ///
+ /// Represents Swift Foundation.ContiguousBytes in C#.
+ ///
+ internal unsafe interface IContiguousBytes
+ {
+ public static void* GetConformanceDescriptor => Runtime.GetConformanceDescriptor("$s10Foundation4DataVAA15ContiguousBytesAAMc");
+ }
+
+ ///
+ /// Swift Foundation PInvoke methods in C#.
+ ///
+ internal static partial class Foundation
+ {
+ internal const string Path = "/System/Library/Frameworks/Foundation.framework/Foundation";
+
+ [LibraryImport(Path, EntryPoint = "$s10Foundation4DataV5bytes5countACSV_SitcfC")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial Data PInvoke_Data_InitWithBytes(UnsafeRawPointer pointer, nint count);
+
+ [LibraryImport(Path, EntryPoint = "$s10Foundation4DataV5countSivg")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial nint PInvoke_Data_GetCount(Data data);
+
+ [LibraryImport(Path, EntryPoint = "$s10Foundation4DataV9copyBytes2to5countySpys5UInt8VG_SitF")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void PInvoke_Data_CopyBytes(UnsafeMutablePointer buffer, nint count, Data data);
+
+ [LibraryImport(Path, EntryPoint = "swift_getWitnessTable")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_Swift_GetWitnessTable(void* conformanceDescriptor, void* typeMetadata, void* instantiationArgs);
+
+ [LibraryImport(Path, EntryPoint = "$s10Foundation4DataVMa")]
+ [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
+ internal static unsafe partial void* PInvoke_Data_GetMetadata();
+ }
+
+ ///
+ /// Swift runtime helper methods in C#.
+ ///
+ internal static partial class Runtime
+ {
+ ///
+ /// https://github.com/apple/swift/blob/main/include/swift/ABI/MetadataValues.h#L117
+ ///
+ [Flags]
+ public enum ValueWitnessFlags
+ {
+ AlignmentMask = 0x0000FFFF,
+ IsNonPOD = 0x00010000,
+ IsNonInline = 0x00020000,
+ HasSpareBits = 0x00080000,
+ IsNonBitwiseTakable = 0x00100000,
+ HasEnumWitnesses = 0x00200000,
+ Incomplete = 0x00400000,
+ }
+
+ ///
+ /// See https://github.com/apple/swift/blob/main/include/swift/ABI/ValueWitness.def
+ ///
+ [StructLayout (LayoutKind.Sequential)]
+ public ref struct ValueWitnessTable
+ {
+ public IntPtr InitializeBufferWithCopyOfBuffer;
+ public IntPtr Destroy;
+ public IntPtr InitWithCopy;
+ public IntPtr AssignWithCopy;
+ public IntPtr InitWithTake;
+ public IntPtr AssignWithTake;
+ public IntPtr GetEnumTagSinglePayload;
+ public IntPtr StoreEnumTagSinglePayload;
+ private IntPtr _Size;
+ private IntPtr _Stride;
+ public ValueWitnessFlags Flags;
+ public uint ExtraInhabitantCount;
+ public int Size => _Size.ToInt32();
+ public int Stride => _Stride.ToInt32();
+ public int Alignment => (int)((Flags & ValueWitnessFlags.AlignmentMask) + 1);
+ public bool IsNonPOD => Flags.HasFlag (ValueWitnessFlags.IsNonPOD);
+ public bool IsNonBitwiseTakable => Flags.HasFlag (ValueWitnessFlags.IsNonBitwiseTakable);
+ public bool HasExtraInhabitants => ExtraInhabitantCount != 0;
+ }
+
+ internal static unsafe void* GetMetadata(T type) where T: ISwiftObject
+ {
+ return T.Metadata;
+ }
+
+ internal static unsafe void* GetValueWitnessTable(void* metadata)
+ {
+ void* valueWitnessTable = (void*)Marshal.ReadIntPtr((IntPtr)metadata, -IntPtr.Size);
+ return valueWitnessTable;
+ }
+
+ internal static unsafe void* GetConformanceDescriptor(string symbol)
+ {
+ IntPtr handle = IntPtr.Zero;
+ try
+ {
+ handle = NativeLibrary.Load(Foundation.Path);
+ void* conformanceDescriptor = NativeLibrary.GetExport(handle, symbol).ToPointer();
+ return conformanceDescriptor;
+ }
+ catch (Exception ex)
+ {
+ throw new InvalidOperationException($"Failed to get conformance descriptor for symbol: {symbol}", ex);
+ }
+ finally
+ {
+ if (handle != IntPtr.Zero)
+ {
+ NativeLibrary.Free(handle);
+ }
+ }
+ }
+ }
+}
diff --git a/src/libraries/Common/src/Interop/OSX/Swift.Runtime/UnsafeBufferPointer.cs b/src/libraries/Common/src/Interop/OSX/Swift.Runtime/UnsafeBufferPointer.cs
deleted file mode 100644
index ec54fb705a836c..00000000000000
--- a/src/libraries/Common/src/Interop/OSX/Swift.Runtime/UnsafeBufferPointer.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-
-namespace Swift.Runtime
-{
- //
- // Represents Swift UnsafeBufferPointer in C#.
- //
- internal readonly unsafe struct UnsafeBufferPointer where T : unmanaged
- {
- private readonly T* _baseAddress;
- private readonly nint _count;
- public UnsafeBufferPointer(T* baseAddress, nint count)
- {
- _baseAddress = baseAddress;
- _count = count;
- }
-
- public T* BaseAddress => _baseAddress;
- public nint Count => _count;
- }
-
- //
- // Represents Swift UnsafeMutableBufferPointer in C#.
- //
- internal readonly unsafe struct UnsafeMutableBufferPointer where T : unmanaged
- {
- private readonly T* _baseAddress;
- private readonly nint _count;
- public UnsafeMutableBufferPointer(T* baseAddress, nint count)
- {
- _baseAddress = baseAddress;
- _count = count;
- }
-
- public T* BaseAddress => _baseAddress;
- public nint Count => _count;
- }
-}
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 8d340797f444af..f8fa1397f666d9 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
@@ -9,7 +9,9 @@
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Apple;
-using Swift.Runtime;
+using Swift;
+
+using AesGcm = Swift.AesGcm;
#pragma warning disable CS3016 // Arrays as attribute arguments are not CLS Compliant
@@ -47,21 +49,37 @@ internal static unsafe void ChaCha20Poly1305Encrypt(
fixed (byte* tagPtr = tag)
fixed (byte* aadPtr = &GetSwiftRef(aad))
{
- AppleCryptoNative_ChaCha20Poly1305Encrypt(
- new UnsafeBufferPointer(keyPtr, key.Length),
- new UnsafeBufferPointer(noncePtr, nonce.Length),
- new UnsafeBufferPointer(plaintextPtr, plaintext.Length),
- new UnsafeMutableBufferPointer(ciphertextPtr, ciphertext.Length),
- new UnsafeMutableBufferPointer(tagPtr, tag.Length),
- new UnsafeBufferPointer(aadPtr, aad.Length),
+ Data symmetricKeyData = new Data(keyPtr, key.Length);
+ SymmetricKey symmetricKey = new SymmetricKey(symmetricKeyData);
+
+ Data nonceData = new Data(noncePtr, nonce.Length);
+ ChaChaPoly.Nonce chaChaPolyNonce = new ChaChaPoly.Nonce(nonceData);
+
+ Data plaintextData = new Data(plaintextPtr, plaintext.Length);
+ Data aadData = new Data(aadPtr, aad.Length);
+
+ ChaChaPoly.SealedBox sealedBox = ChaChaPoly.seal(
+ plaintextData,
+ symmetricKey,
+ chaChaPolyNonce,
+ aadData,
out SwiftError error);
if (error.Value != null)
{
+ chaChaPolyNonce.Dispose();
+ symmetricKey.Dispose();
+
CryptographicOperations.ZeroMemory(ciphertext);
CryptographicOperations.ZeroMemory(tag);
throw new CryptographicException();
}
+
+ Data resultCiphertext = sealedBox.Ciphertext;
+ Data resultTag = sealedBox.Tag;
+
+ resultCiphertext.CopyBytes(ciphertextPtr, resultCiphertext.Count);
+ resultTag.CopyBytes(tagPtr, resultTag.Count);
}
}
@@ -84,17 +102,29 @@ internal static unsafe void ChaCha20Poly1305Decrypt(
fixed (byte* plaintextPtr = &GetSwiftRef(plaintext))
fixed (byte* aadPtr = &GetSwiftRef(aad))
{
- AppleCryptoNative_ChaCha20Poly1305Decrypt(
- new UnsafeBufferPointer(keyPtr, key.Length),
- new UnsafeBufferPointer(noncePtr, nonce.Length),
- new UnsafeBufferPointer(ciphertextPtr, ciphertext.Length),
- new UnsafeBufferPointer(tagPtr, tag.Length),
- new UnsafeMutableBufferPointer(plaintextPtr, plaintext.Length),
- new UnsafeBufferPointer(aadPtr, aad.Length),
+ Data symmetricKeyData = new Data(keyPtr, key.Length);
+ SymmetricKey symmetricKey = new SymmetricKey(symmetricKeyData);
+
+ Data nonceData = new Data(noncePtr, nonce.Length);
+ ChaChaPoly.Nonce chaChaPolyNonce = new ChaChaPoly.Nonce(nonceData);
+
+ Data ciphertextData = new Data(ciphertextPtr, ciphertext.Length);
+ Data tagData = new Data(tagPtr, tag.Length);
+ Data aadData = new Data(aadPtr, aad.Length);
+
+ ChaChaPoly.SealedBox sealedBox = new ChaChaPoly.SealedBox(chaChaPolyNonce, ciphertextData, tagData);
+
+ Data data = ChaChaPoly.open(
+ sealedBox,
+ symmetricKey,
+ aadData,
out SwiftError error);
if (error.Value != null)
{
+ chaChaPolyNonce.Dispose();
+ symmetricKey.Dispose();
+
CryptographicOperations.ZeroMemory(plaintext);
if (AppleCryptoNative_IsAuthenticationFailure(error.Value))
@@ -106,6 +136,8 @@ internal static unsafe void ChaCha20Poly1305Decrypt(
throw new CryptographicException();
}
}
+
+ data.CopyBytes(plaintextPtr, data.Count);
}
}
@@ -128,21 +160,38 @@ internal static unsafe void AesGcmEncrypt(
fixed (byte* tagPtr = tag)
fixed (byte* aadPtr = &GetSwiftRef(aad))
{
- AppleCryptoNative_AesGcmEncrypt(
- new UnsafeBufferPointer(keyPtr, key.Length),
- new UnsafeBufferPointer(noncePtr, nonce.Length),
- new UnsafeBufferPointer(plaintextPtr, plaintext.Length),
- new UnsafeMutableBufferPointer(ciphertextPtr, ciphertext.Length),
- new UnsafeMutableBufferPointer(tagPtr, tag.Length),
- new UnsafeBufferPointer(aadPtr, aad.Length),
+ Data symmetricKeyData = new Data(keyPtr, key.Length);
+ SymmetricKey symmetricKey = new SymmetricKey(symmetricKeyData);
+
+ Data nonceData = new Data(noncePtr, nonce.Length);
+ AesGcm.Nonce aesGcmNonce = new AesGcm.Nonce(nonceData);
+
+ Data plaintextData = new Data(plaintextPtr, plaintext.Length);
+ Data aadData = new Data(aadPtr, aad.Length);
+
+ AesGcm.SealedBox sealedBox = AesGcm.seal(
+ plaintextData,
+ symmetricKey,
+ aesGcmNonce,
+ aadData,
out SwiftError error);
if (error.Value != null)
{
+ sealedBox.Dispose();
+ aesGcmNonce.Dispose();
+ symmetricKey.Dispose();
+
CryptographicOperations.ZeroMemory(ciphertext);
CryptographicOperations.ZeroMemory(tag);
throw new CryptographicException();
}
+
+ Data resultCiphertext = sealedBox.Ciphertext;
+ Data resultTag = sealedBox.Tag;
+
+ resultCiphertext.CopyBytes(ciphertextPtr, resultCiphertext.Count);
+ resultTag.CopyBytes(tagPtr, resultTag.Count);
}
}
@@ -165,17 +214,30 @@ internal static unsafe void AesGcmDecrypt(
fixed (byte* plaintextPtr = &GetSwiftRef(plaintext))
fixed (byte* aadPtr = &GetSwiftRef(aad))
{
- AppleCryptoNative_AesGcmDecrypt(
- new UnsafeBufferPointer(keyPtr, key.Length),
- new UnsafeBufferPointer(noncePtr, nonce.Length),
- new UnsafeBufferPointer(ciphertextPtr, ciphertext.Length),
- new UnsafeBufferPointer(tagPtr, tag.Length),
- new UnsafeMutableBufferPointer(plaintextPtr, plaintext.Length),
- new UnsafeBufferPointer(aadPtr, aad.Length),
+ Data symmetricKeyData = new Data(keyPtr, key.Length);
+ SymmetricKey symmetricKey = new SymmetricKey(symmetricKeyData);
+
+ Data nonceData = new Data(noncePtr, nonce.Length);
+ AesGcm.Nonce aesGcmNonce = new AesGcm.Nonce(nonceData);
+
+ Data ciphertextData = new Data(ciphertextPtr, ciphertext.Length);
+ Data tagData = new Data(tagPtr, tag.Length);
+ Data aadData = new Data(aadPtr, aad.Length);
+
+ AesGcm.SealedBox sealedBox = new AesGcm.SealedBox(aesGcmNonce, ciphertextData, tagData);
+
+ Data data = AesGcm.open(
+ sealedBox,
+ symmetricKey,
+ aadData,
out SwiftError error);
if (error.Value != null)
{
+ sealedBox.Dispose();
+ aesGcmNonce.Dispose();
+ symmetricKey.Dispose();
+
CryptographicOperations.ZeroMemory(plaintext);
if (AppleCryptoNative_IsAuthenticationFailure(error.Value))
@@ -187,53 +249,11 @@ internal static unsafe void AesGcmDecrypt(
throw new CryptographicException();
}
}
+
+ data.CopyBytes(plaintextPtr, data.Count);
}
}
- [LibraryImport(Libraries.AppleCryptoNative)]
- [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
- private static unsafe partial void AppleCryptoNative_ChaCha20Poly1305Encrypt(
- UnsafeBufferPointer key,
- UnsafeBufferPointer nonce,
- UnsafeBufferPointer plaintext,
- UnsafeMutableBufferPointer ciphertext,
- UnsafeMutableBufferPointer tag,
- UnsafeBufferPointer aad,
- out SwiftError error);
-
- [LibraryImport(Libraries.AppleCryptoNative)]
- [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
- private static unsafe partial void AppleCryptoNative_ChaCha20Poly1305Decrypt(
- UnsafeBufferPointer key,
- UnsafeBufferPointer nonce,
- UnsafeBufferPointer ciphertext,
- UnsafeBufferPointer tag,
- UnsafeMutableBufferPointer plaintext,
- UnsafeBufferPointer aad,
- out SwiftError error);
-
- [LibraryImport(Libraries.AppleCryptoNative)]
- [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
- private static unsafe partial void AppleCryptoNative_AesGcmEncrypt(
- UnsafeBufferPointer key,
- UnsafeBufferPointer nonce,
- UnsafeBufferPointer plaintext,
- UnsafeMutableBufferPointer ciphertext,
- UnsafeMutableBufferPointer tag,
- UnsafeBufferPointer aad,
- out SwiftError error);
-
- [LibraryImport(Libraries.AppleCryptoNative)]
- [UnmanagedCallConv(CallConvs = [ typeof(CallConvSwift) ])]
- private static unsafe partial void AppleCryptoNative_AesGcmDecrypt(
- UnsafeBufferPointer key,
- UnsafeBufferPointer nonce,
- UnsafeBufferPointer ciphertext,
- UnsafeBufferPointer tag,
- UnsafeMutableBufferPointer plaintext,
- UnsafeBufferPointer aad,
- out SwiftError error);
-
[LibraryImport(Libraries.AppleCryptoNative)]
[UnmanagedCallConv(CallConvs = new[] { typeof(CallConvSwift) })]
[return: MarshalAs(UnmanagedType.U1)]
diff --git a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj
index c609292d4059f6..2df45c6e3b0935 100644
--- a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj
+++ b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj
@@ -1086,8 +1086,10 @@
Link="Common\Interop\OSX\Interop.CoreFoundation.CFString.cs" />
-
+
+
(data: D) throws where D : DataProtocol
-}
-
-protocol SealedBoxProtocol {
- associatedtype Nonce : NonceProtocol
-
- var ciphertext: Data { get }
- var tag: Data { get }
-
- init(
- nonce: Nonce,
- ciphertext: C,
- tag: T
- ) throws where C : DataProtocol, T : DataProtocol
-}
-
-@available(iOS 13, tvOS 13, *)
-protocol AEADSymmetricAlgorithm {
- associatedtype SealedBox : SealedBoxProtocol
-
- static func seal(_ plaintext: Plaintext, using key: SymmetricKey, nonce: SealedBox.Nonce?) throws -> SealedBox where Plaintext: DataProtocol
- static func seal(_ plaintext: Plaintext, using key: SymmetricKey, nonce: SealedBox.Nonce?, authenticating additionalData: AuthenticatedData) throws -> SealedBox where Plaintext: DataProtocol, AuthenticatedData: DataProtocol
- static func open(_ sealedBox: SealedBox, using key: SymmetricKey, authenticating additionalData: AuthenticatedData) throws -> Data where AuthenticatedData: DataProtocol
- static func open(_ sealedBox: SealedBox, using key: SymmetricKey) throws -> Data
-}
-
-@available(iOS 13, tvOS 13, *)
-extension AES.GCM.Nonce: NonceProtocol {}
-
-@available(iOS 13, tvOS 13, *)
-extension AES.GCM.SealedBox: SealedBoxProtocol {
- typealias Nonce = AES.GCM.Nonce
-}
-
-@available(iOS 13, tvOS 13, *)
-extension AES.GCM: AEADSymmetricAlgorithm {}
-
-@available(iOS 13, tvOS 13, *)
-extension ChaChaPoly.Nonce: NonceProtocol {}
-
-@available(iOS 13, tvOS 13, *)
-extension ChaChaPoly.SealedBox: SealedBoxProtocol {
- typealias Nonce = ChaChaPoly.Nonce
-}
-
-@available(iOS 13, tvOS 13, *)
-extension ChaChaPoly: AEADSymmetricAlgorithm {}
-
-@available(iOS 13, tvOS 13, *)
-func encrypt(
- _ algorithm: Algorithm.Type,
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- plaintext: UnsafeBufferPointer,
- cipherText: UnsafeMutableBufferPointer,
- tag: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer) throws where Algorithm: AEADSymmetricAlgorithm {
-
- let symmetricKey = SymmetricKey(data: key)
-
- let nonce = try Algorithm.SealedBox.Nonce(data: nonceData)
-
- let result = try Algorithm.seal(plaintext, using: symmetricKey, nonce: nonce, authenticating: aad)
-
- // Copy results out of the SealedBox as the Data objects returned here are sometimes slices,
- // which don't have a correct implementation of copyBytes.
- // See https://github.com/apple/swift-foundation/issues/638 for more information.
- let resultCiphertext = Data(result.ciphertext)
- let resultTag = Data(result.tag)
-
- _ = resultCiphertext.copyBytes(to: cipherText)
- _ = resultTag.copyBytes(to: tag)
-}
-
-@available(iOS 13, tvOS 13, *)
-func decrypt(
- _ algorithm: Algorithm.Type,
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- cipherText: UnsafeBufferPointer,
- tag: UnsafeBufferPointer,
- plaintext: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer) throws where Algorithm: AEADSymmetricAlgorithm {
-
- let symmetricKey = SymmetricKey(data: key)
-
- let nonce = try Algorithm.SealedBox.Nonce(data: nonceData)
-
- let sealedBox = try Algorithm.SealedBox(nonce: nonce, ciphertext: cipherText, tag: tag)
-
- let result = try Algorithm.open(sealedBox, using: symmetricKey, authenticating: aad)
-
- _ = result.copyBytes(to: plaintext)
-}
-
-@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Encrypt")
-@available(iOS 13, tvOS 13, *)
-public func AppleCryptoNative_ChaCha20Poly1305Encrypt(
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- plaintext: UnsafeBufferPointer,
- cipherText: UnsafeMutableBufferPointer,
- tag: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer
-) throws {
- return try encrypt(
- ChaChaPoly.self,
- key: key,
- nonceData: nonceData,
- plaintext: plaintext,
- cipherText: cipherText,
- tag: tag,
- aad: aad)
- }
-
-@_silgen_name("AppleCryptoNative_ChaCha20Poly1305Decrypt")
-@available(iOS 13, tvOS 13, *)
-public func AppleCryptoNative_ChaCha20Poly1305Decrypt(
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- cipherText: UnsafeBufferPointer,
- tag: UnsafeBufferPointer,
- plaintext: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer
-) throws {
- return try decrypt(
- ChaChaPoly.self,
- key: key,
- nonceData: nonceData,
- cipherText: cipherText,
- tag: tag,
- plaintext: plaintext,
- aad: aad);
-}
-
-@_silgen_name("AppleCryptoNative_AesGcmEncrypt")
-@available(iOS 13, tvOS 13, *)
-public func AppleCryptoNative_AesGcmEncrypt(
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- plaintext: UnsafeBufferPointer,
- cipherText: UnsafeMutableBufferPointer,
- tag: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer
-) throws {
- return try encrypt(
- AES.GCM.self,
- key: key,
- nonceData: nonceData,
- plaintext: plaintext,
- cipherText: cipherText,
- tag: tag,
- aad: aad)
- }
-
-@_silgen_name("AppleCryptoNative_AesGcmDecrypt")
-@available(iOS 13, tvOS 13, *)
-public func AppleCryptoNative_AesGcmDecrypt(
- key: UnsafeBufferPointer,
- nonceData: UnsafeBufferPointer,
- cipherText: UnsafeBufferPointer,
- tag: UnsafeBufferPointer,
- plaintext: UnsafeMutableBufferPointer,
- aad: UnsafeBufferPointer
-) throws {
- return try decrypt(
- AES.GCM.self,
- key: key,
- nonceData: nonceData,
- cipherText: cipherText,
- tag: tag,
- plaintext: plaintext,
- aad: aad);
-}
-
@_silgen_name("AppleCryptoNative_IsAuthenticationFailure")
@available(iOS 13, tvOS 13, *)
public func AppleCryptoNative_IsAuthenticationFailure(error: Error) -> Bool {