Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static partial class Sys

public static unsafe int Sysctl(Span<int> name, ref byte* value, ref int len)
{
fixed (int * ptr = &name.DangerousGetPinnableReference())
fixed (int * ptr = &MemoryMarshal.GetReference(name))
{
return Sysctl(ptr, name.Length, ref value, ref len);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static unsafe bool TryCFWriteData(SafeCFDataHandle cfData, Span<byte> d
}

byte* dataBytes = CFDataGetBytePtr(cfData);
fixed (byte* destinationPtr = &destination.DangerousGetPinnableReference())
fixed (byte* destinationPtr = &MemoryMarshal.GetReference(destination))
{
Buffer.MemoryCopy(dataBytes, destinationPtr, destination.Length, length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ internal static partial class AppleCrypto
internal static extern SafeDigestCtxHandle DigestCreate(PAL_HashAlgorithm algorithm, out int cbDigest);

internal static int DigestUpdate(SafeDigestCtxHandle ctx, ReadOnlySpan<byte> pbData, int cbData) =>
DigestUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData);
DigestUpdate(ctx, ref MemoryMarshal.GetReference(pbData), cbData);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestUpdate")]
private static extern int DigestUpdate(SafeDigestCtxHandle ctx, ref byte pbData, int cbData);

internal static int DigestFinal(SafeDigestCtxHandle ctx, Span<byte> pbOutput, int cbOutput) =>
DigestFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput);
DigestFinal(ctx, ref MemoryMarshal.GetReference(pbOutput), cbOutput);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestFinal")]
private static extern int DigestFinal(SafeDigestCtxHandle ctx, ref byte pbOutput, int cbOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ internal static partial class AppleCrypto
internal static extern unsafe int HmacInit(SafeHmacHandle ctx, [In] byte[] pbKey, int cbKey);

internal static int HmacUpdate(SafeHmacHandle ctx, ReadOnlySpan<byte> pbData, int cbData) =>
HmacUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData);
HmacUpdate(ctx, ref MemoryMarshal.GetReference(pbData), cbData);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacUpdate")]
private static extern int HmacUpdate(SafeHmacHandle ctx, ref byte pbData, int cbData);

internal static int HmacFinal(SafeHmacHandle ctx, ReadOnlySpan<byte> pbOutput, int cbOutput) =>
HmacFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput);
HmacFinal(ctx, ref MemoryMarshal.GetReference(pbOutput), cbOutput);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacFinal")]
private static extern unsafe int HmacFinal(SafeHmacHandle ctx, ref byte pbOutput, int cbOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static int RsaEncryptOaep(
PAL_HashAlgorithm mgfAlgorithm,
out SafeCFDataHandle pEncryptedOut,
out SafeCFErrorHandle pErrorOut) =>
RsaEncryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
RsaEncryptOaep(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptOaep")]
private static extern int RsaEncryptOaep(
Expand All @@ -45,7 +45,7 @@ private static int RsaEncryptPkcs(
int cbData,
out SafeCFDataHandle pEncryptedOut,
out SafeCFErrorHandle pErrorOut) =>
RsaEncryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut);
RsaEncryptPkcs(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, out pEncryptedOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptPkcs")]
private static extern int RsaEncryptPkcs(
Expand All @@ -62,7 +62,7 @@ private static int RsaDecryptOaep(
PAL_HashAlgorithm mgfAlgorithm,
out SafeCFDataHandle pEncryptedOut,
out SafeCFErrorHandle pErrorOut) =>
RsaDecryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);
RsaDecryptOaep(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptOaep")]
private static extern int RsaDecryptOaep(
Expand All @@ -79,7 +79,7 @@ private static int RsaDecryptPkcs(
int cbData,
out SafeCFDataHandle pEncryptedOut,
out SafeCFErrorHandle pErrorOut) =>
RsaDecryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut);
RsaDecryptPkcs(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, out pEncryptedOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptPkcs")]
private static extern int RsaDecryptPkcs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private static int AppleCryptoNative_GenerateSignature(
out SafeCFDataHandle pSignatureOut,
out SafeCFErrorHandle pErrorOut) =>
AppleCryptoNative_GenerateSignature(
privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, out pSignatureOut, out pErrorOut);
privateKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, out pSignatureOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_GenerateSignature(
Expand All @@ -46,7 +46,7 @@ private static int AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
out SafeCFDataHandle pSignatureOut,
out SafeCFErrorHandle pErrorOut) =>
AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, hashAlgorithm, out pSignatureOut, out pErrorOut);
privateKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, hashAlgorithm, out pSignatureOut, out pErrorOut);

[DllImport(Libraries.AppleCryptoNative)]
private static extern int AppleCryptoNative_GenerateSignatureWithHashAlgorithm(
Expand All @@ -66,9 +66,9 @@ private static int AppleCryptoNative_VerifySignature(
out SafeCFErrorHandle pErrorOut) =>
AppleCryptoNative_VerifySignature(
publicKey,
ref pbDataHash.DangerousGetPinnableReference(),
ref MemoryMarshal.GetReference(pbDataHash),
cbDataHash,
ref pbSignature.DangerousGetPinnableReference(),
ref MemoryMarshal.GetReference(pbSignature),
cbSignature,
out pErrorOut);

Expand All @@ -91,9 +91,9 @@ private static int AppleCryptoNative_VerifySignatureWithHashAlgorithm(
out SafeCFErrorHandle pErrorOut) =>
AppleCryptoNative_VerifySignatureWithHashAlgorithm(
publicKey,
ref pbDataHash.DangerousGetPinnableReference(),
ref MemoryMarshal.GetReference(pbDataHash),
cbDataHash,
ref pbSignature.DangerousGetPinnableReference(),
ref MemoryMarshal.GetReference(pbSignature),
cbSignature,
hashAlgorithm,
out pErrorOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ internal static int DsaKeySize(SafeDsaHandle dsa)
}

internal static bool DsaSign(SafeDsaHandle dsa, ReadOnlySpan<byte> hash, int hashLength, ReadOnlySpan<byte> refSignature, out int outSignatureLength) =>
DsaSign(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref refSignature.DangerousGetPinnableReference(), out outSignatureLength);
DsaSign(dsa, ref MemoryMarshal.GetReference(hash), hashLength, ref MemoryMarshal.GetReference(refSignature), out outSignatureLength);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaSign")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DsaSign(SafeDsaHandle dsa, ref byte hash, int hashLength, ref byte refSignature, out int outSignatureLength);

internal static bool DsaVerify(SafeDsaHandle dsa, ReadOnlySpan<byte> hash, int hashLength, ReadOnlySpan<byte> signature, int signatureLength) =>
DsaVerify(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref signature.DangerousGetPinnableReference(), signatureLength);
DsaVerify(dsa, ref MemoryMarshal.GetReference(hash), hashLength, ref MemoryMarshal.GetReference(signature), signatureLength);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaVerify")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static partial class Crypto
internal extern static int EvpDigestReset(SafeEvpMdCtxHandle ctx, IntPtr type);

internal static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ReadOnlySpan<byte> d, int cnt) =>
EvpDigestUpdate(ctx, ref d.DangerousGetPinnableReference(), cnt);
EvpDigestUpdate(ctx, ref MemoryMarshal.GetReference(d), cnt);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpDigestUpdate")]
private extern static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ref byte d, int cnt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ internal static partial class Interop
internal static partial class Crypto
{
internal static bool EcDsaSign(ReadOnlySpan<byte> dgst, int dlen, Span<byte> sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey) =>
EcDsaSign(ref dgst.DangerousGetPinnableReference(), dlen, ref sig.DangerousGetPinnableReference(), ref siglen, ecKey);
EcDsaSign(ref MemoryMarshal.GetReference(dgst), dlen, ref MemoryMarshal.GetReference(sig), ref siglen, ecKey);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcDsaSign")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EcDsaSign(ref byte dgst, int dlen, ref byte sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey);

internal static unsafe int EcDsaVerify(ReadOnlySpan<byte> dgst, int dgst_len, ReadOnlySpan<byte> sigbuf, int sig_len, SafeEcKeyHandle ecKey) =>
EcDsaVerify(ref dgst.DangerousGetPinnableReference(), dgst_len, ref sigbuf.DangerousGetPinnableReference(), sig_len, ecKey);
EcDsaVerify(ref MemoryMarshal.GetReference(dgst), dgst_len, ref MemoryMarshal.GetReference(sigbuf), sig_len, ecKey);

/*-
* returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static partial class Crypto
internal extern static int HmacReset(SafeHmacCtxHandle ctx);

internal static int HmacUpdate(SafeHmacCtxHandle ctx, ReadOnlySpan<byte> data, int len) =>
HmacUpdate(ctx, ref data.DangerousGetPinnableReference(), len);
HmacUpdate(ctx, ref MemoryMarshal.GetReference(data), len);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_HmacUpdate")]
private extern static int HmacUpdate(SafeHmacCtxHandle ctx, ref byte data, int len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ private static unsafe int AlpnServerSelectCallback(IntPtr ssl, out byte* outp, o
Span<byte> clientProto = clientList.Slice(1, length);
if (clientProto.SequenceEqual(protocolList[i].Protocol.Span))
{
fixed (byte* p = &clientProto.DangerousGetPinnableReference()) outp = p;
fixed (byte* p = &MemoryMarshal.GetReference(clientProto)) outp = p;
outlen = length;
return Ssl.SSL_TLSEXT_ERR_OK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static int RsaPublicEncrypt(
Span<byte> to,
SafeRsaHandle rsa,
RsaPadding padding) =>
RsaPublicEncrypt(flen, ref from.DangerousGetPinnableReference(), ref to.DangerousGetPinnableReference(), rsa, padding);
RsaPublicEncrypt(flen, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa, padding);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaPublicEncrypt")]
private extern static int RsaPublicEncrypt(
Expand All @@ -47,7 +47,7 @@ internal static int RsaPrivateDecrypt(
Span<byte> to,
SafeRsaHandle rsa,
RsaPadding padding) =>
RsaPrivateDecrypt(flen, ref from.DangerousGetPinnableReference(), ref to.DangerousGetPinnableReference(), rsa, padding);
RsaPrivateDecrypt(flen, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa, padding);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaPrivateDecrypt")]
private extern static int RsaPrivateDecrypt(
Expand All @@ -64,14 +64,14 @@ private extern static int RsaPrivateDecrypt(
internal static extern int RsaGenerateKeyEx(SafeRsaHandle rsa, int bits, SafeBignumHandle e);

internal static bool RsaSign(int type, ReadOnlySpan<byte> m, int m_len, Span<byte> sigret, out int siglen, SafeRsaHandle rsa) =>
RsaSign(type, ref m.DangerousGetPinnableReference(), m_len, ref sigret.DangerousGetPinnableReference(), out siglen, rsa);
RsaSign(type, ref MemoryMarshal.GetReference(m), m_len, ref MemoryMarshal.GetReference(sigret), out siglen, rsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaSign")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RsaSign(int type, ref byte m, int m_len, ref byte sigret, out int siglen, SafeRsaHandle rsa);

internal static bool RsaVerify(int type, ReadOnlySpan<byte> m, int m_len, ReadOnlySpan<byte> sigbuf, int siglen, SafeRsaHandle rsa) =>
RsaVerify(type, ref m.DangerousGetPinnableReference(), m_len, ref sigbuf.DangerousGetPinnableReference(), siglen, rsa);
RsaVerify(type, ref MemoryMarshal.GetReference(m), m_len, ref MemoryMarshal.GetReference(sigbuf), siglen, rsa);

[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaVerify")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal partial class Interop
internal partial class BCrypt
{
internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span<byte> pbOutput, int cbOutput, int dwFlags) =>
BCryptFinishHash(hHash, ref pbOutput.DangerousGetPinnableReference(), cbOutput, dwFlags);
BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags);

[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)]
private static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal partial class Interop
internal partial class BCrypt
{
internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan<byte> pbInput, int cbInput, int dwFlags) =>
BCryptHashData(hHash, ref pbInput.DangerousGetPinnableReference(), cbInput, dwFlags);
BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags);

[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)]
private static extern NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ internal static partial class Interop
internal static partial class NCrypt
{
internal static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) =>
NCryptEncrypt(hKey, ref pbInput.DangerousGetPinnableReference(), cbInput, pPaddingInfo, ref pbOutput.DangerousGetPinnableReference(), cbOutput, out pcbResult, dwFlags);
NCryptEncrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);

[DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)]
private static extern unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);

internal static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan<byte> pbInput, int cbInput, void* pPaddingInfo, Span<byte> pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) =>
NCryptDecrypt(hKey, ref pbInput.DangerousGetPinnableReference(), cbInput, pPaddingInfo, ref pbOutput.DangerousGetPinnableReference(), cbOutput, out pcbResult, dwFlags);
NCryptDecrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags);

[DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)]
private static extern unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags);
Expand Down
4 changes: 2 additions & 2 deletions src/Common/src/Interop/Windows/NCrypt/Interop.SignVerify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ internal static partial class Interop
internal static partial class NCrypt
{
internal static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, Span<byte> pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags) =>
NCryptSignHash(hKey, pPaddingInfo, ref pbHashValue.DangerousGetPinnableReference(), cbHashValue, ref pbSignature.DangerousGetPinnableReference(), cbSignature, out pcbResult, dwFlags);
NCryptSignHash(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, out pcbResult, dwFlags);

[DllImport(Libraries.NCrypt, CharSet = CharSet.Unicode)]
private static extern unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags);

internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan<byte> pbHashValue, int cbHashValue, ReadOnlySpan<byte> pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) =>
NCryptVerifySignature(hKey, pPaddingInfo, ref pbHashValue.DangerousGetPinnableReference(), cbHashValue, ref pbSignature.DangerousGetPinnableReference(), cbSignature, dwFlags);
NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags);

[DllImport(Libraries.NCrypt, CharSet = CharSet.Unicode)]
private static extern unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static partial class Kernel32

internal static unsafe int GetSystemDirectoryW(Span<char> buffer)
{
fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference())
fixed (char* bufferPtr = &MemoryMarshal.GetReference(buffer))
{
return GetSystemDirectoryW(bufferPtr, buffer.Length);
}
Expand Down
Loading