private static int RsaEncryptPkcs( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => RsaEncryptPkcs(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, out pEncryptedOut, out pErrorOut);
private static unsafe extern int AppleCryptoNative_SecKeyCreateWithData( byte *pKey, int cbKey, PAL_KeyAlgorithm keyAlgorithm, int isPublic, out SafeSecKeyRefHandle pDataKey, out SafeCFErrorHandle pErrorOut);
private static extern int RsaDecryptOaep( SafeSecKeyRefHandle publicKey, ref byte pbData, int cbData, PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut);
internal static int GetErrorCode(SafeCFErrorHandle cfError) { unchecked { return((int)(CFErrorGetCode(cfError).ToInt64())); } }
private static int RsaDecryptPkcs( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => RsaDecryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut);
private static extern int AppleCryptoNative_GenerateSignatureWithHashAlgorithm( SafeSecKeyRefHandle privateKey, ref byte pbDataHash, int cbDataHash, PAL_HashAlgorithm hashAlgorithm, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut);
private static extern int AppleCryptoNative_VerifySignature( SafeSecKeyRefHandle publicKey, ref byte pbDataHash, int cbDataHash, ref byte pbSignature, int cbSignature, out SafeCFErrorHandle pErrorOut);
private static partial int RsaDecryptOaep( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut);
private static int AppleCryptoNative_GenerateSignature( SafeSecKeyRefHandle privateKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_GenerateSignature( privateKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, out pSignatureOut, out pErrorOut);
private static int AppleCryptoNative_GenerateSignature( SafeSecKeyRefHandle privateKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_GenerateSignature( privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, out pSignatureOut, out pErrorOut);
private static extern unsafe int AppleCryptoNative_VerifySignatureWithHashAlgorithm( SafeSecKeyRefHandle publicKey, byte *pbDataHash, int cbDataHash, byte *pbSignature, int cbSignature, PAL_HashAlgorithm hashAlgorithm, out SafeCFErrorHandle pErrorOut);
private static int RsaDecryptOaep( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => RsaDecryptOaep(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
private static int RsaDecryptOaep( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => RsaDecryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
private static unsafe extern int AppleCryptoNative_SecKeyCreateSignature( SafeSecKeyRefHandle privateKey, byte *pbDataHash, int cbDataHash, PAL_HashAlgorithm hashAlgorithm, PAL_SignatureAlgorithm signatureAlgorithm, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut);
private static unsafe partial int AppleCryptoNative_SecKeyVerifySignature( SafeSecKeyRefHandle publicKey, byte *pbDataHash, int cbDataHash, byte *pbSignature, int cbSignature, PAL_HashAlgorithm hashAlgorithm, PAL_SignatureAlgorithm signatureAlgorithm, out SafeCFErrorHandle pErrorOut);
internal static Exception CreateExceptionForCFError(SafeCFErrorHandle cfError) { Debug.Assert(cfError != null); if (cfError.IsInvalid) { return(new CryptographicException()); } return(new AppleCFErrorCryptographicException(cfError)); }
private static unsafe int RsaDecryptPkcs( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) { fixed(byte *pbDataPtr = &pbData.DangerousGetPinnableReference()) { return(RsaDecryptPkcs(publicKey, pbDataPtr, cbData, out pEncryptedOut, out pErrorOut)); } }
private static int AppleCryptoNative_VerifySignature( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbDataHash, ReadOnlySpan <byte> pbSignature, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_VerifySignature( publicKey, ref MemoryMarshal.GetReference(pbDataHash), pbDataHash.Length, ref MemoryMarshal.GetReference(pbSignature), pbSignature.Length, out pErrorOut);
private static unsafe int RsaEncryptOaep( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbData, int cbData, PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) { fixed(byte *pbDataPtr = &pbData.DangerousGetPinnableReference()) { return(RsaEncryptOaep(publicKey, pbDataPtr, cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut)); } }
private static int AppleCryptoNative_GenerateSignatureWithHashAlgorithm( SafeSecKeyRefHandle privateKey, ReadOnlySpan <byte> pbDataHash, PAL_HashAlgorithm hashAlgorithm, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_GenerateSignatureWithHashAlgorithm( privateKey, ref MemoryMarshal.GetReference(pbDataHash), pbDataHash.Length, hashAlgorithm, out pSignatureOut, out pErrorOut);
private static unsafe int AppleCryptoNative_GenerateSignature( SafeSecKeyRefHandle privateKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) { fixed(byte *pbDataHashPtr = &pbDataHash.DangerousGetPinnableReference()) { return(AppleCryptoNative_GenerateSignature( privateKey, pbDataHashPtr, cbDataHash, out pSignatureOut, out pErrorOut)); } }
private static unsafe int AppleCryptoNative_VerifySignature( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, ReadOnlySpan <byte> pbSignature, int cbSignature, out SafeCFErrorHandle pErrorOut) { fixed(byte *pbDataHashPtr = &pbDataHash.DangerousGetPinnableReference()) fixed(byte *pbSignaturePtr = &pbSignature.DangerousGetPinnableReference()) { return(AppleCryptoNative_VerifySignature(publicKey, pbDataHashPtr, cbDataHash, pbSignaturePtr, cbSignature, out pErrorOut)); } }
private static int AppleCryptoNative_VerifySignature( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, ReadOnlySpan <byte> pbSignature, int cbSignature, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_VerifySignature( publicKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, ref pbSignature.DangerousGetPinnableReference(), cbSignature, out pErrorOut);
internal static string?GetErrorDescription(SafeCFErrorHandle cfError) { Debug.Assert(cfError != null); if (cfError.IsInvalid) { return(null); } Debug.Assert(!cfError.IsClosed); using (SafeCFStringHandle cfString = CFErrorCopyDescription(cfError)) { return(CFStringToString(cfString)); } }
private static int AppleCryptoNative_VerifySignatureWithHashAlgorithm( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> pbDataHash, int cbDataHash, ReadOnlySpan <byte> pbSignature, int cbSignature, PAL_HashAlgorithm hashAlgorithm, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_VerifySignatureWithHashAlgorithm( publicKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, ref MemoryMarshal.GetReference(pbSignature), cbSignature, hashAlgorithm, out pErrorOut);
private static unsafe int AppleCryptoNative_SecKeyCreateSignature( SafeSecKeyRefHandle privateKey, ReadOnlySpan <byte> dataHash, PAL_HashAlgorithm hashAlgorithm, PAL_SignatureAlgorithm signatureAlgorithm, out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) { fixed(byte *pDataHash = dataHash) { return(AppleCryptoNative_SecKeyCreateSignature( privateKey, pDataHash, dataHash.Length, hashAlgorithm, signatureAlgorithm, out pSignatureOut, out pErrorOut)); } }
private static bool ProcessPrimitiveResponse( int returnValue, SafeCFDataHandle cfData, SafeCFErrorHandle cfError, Span <byte> destination, out int bytesWritten) { if (returnValue == kErrorSeeError) { throw CreateExceptionForCFError(cfError); } if (returnValue == kSuccess && !cfData.IsInvalid) { return(CoreFoundation.TryCFWriteData(cfData, destination, out bytesWritten)); } Debug.Fail($"Unknown return value ({returnValue}) or no data object returned"); throw new CryptographicException(); }
private static unsafe int AppleCryptoNative_SecKeyVerifySignature( SafeSecKeyRefHandle publicKey, ReadOnlySpan <byte> dataHash, ReadOnlySpan <byte> signature, PAL_HashAlgorithm hashAlgorithm, PAL_SignatureAlgorithm signatureAlgorithm, out SafeCFErrorHandle pErrorOut) { fixed(byte *pDataHash = dataHash) fixed(byte *pSignature = signature) { return(AppleCryptoNative_SecKeyVerifySignature( publicKey, pDataHash, dataHash.Length, pSignature, signature.Length, hashAlgorithm, signatureAlgorithm, out pErrorOut)); } }
internal AppleCFErrorCryptographicException(SafeCFErrorHandle cfError) : base(Interop.CoreFoundation.GetErrorDescription(cfError)) { HResult = Interop.CoreFoundation.GetErrorCode(cfError); }
private static partial SafeCFStringHandle CFErrorCopyDescription(SafeCFErrorHandle cfError);