diff options
author | Stephen Toub <stoub@microsoft.com> | 2017-08-16 16:18:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-16 16:18:41 +0300 |
commit | 36afe5506def3fb49c84945faf0732ec0ec45b16 (patch) | |
tree | 2ef800c90fff5a94286569b2ac4db1ed923db1e9 /src/System.Security.Cryptography.Algorithms | |
parent | 6f22497b4da2343293145cf94a8d01d81e862271 (diff) |
Avoid manually pinning spans in crypto interop (#23259)
My recent PRs to add span-based overloads to crypto types employed a pattern where span-based interop signatures pinned the spans manually and then passed pointers to the actual P/Invoke declaration accepting a `byte*`. We can instead let the runtime handle the pinning by declaring the P/Invokes in terms of `ref byte`. In addition to reducing the code involved, there's also the tiny benefit in several cases of the data being pinned for less time.
Diffstat (limited to 'src/System.Security.Cryptography.Algorithms')
5 files changed, 20 insertions, 35 deletions
diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs index c838da7bfc..01f5b4da0a 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs @@ -86,7 +86,7 @@ namespace Internal.Cryptography return result; } - public override unsafe bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten) + public override bool TryFinalizeHashAndReset(Span<byte> destination, out int bytesWritten) { if (destination.Length < _hashSize) { @@ -94,13 +94,10 @@ namespace Internal.Cryptography return false; } - fixed (byte* ptrDest = &destination.DangerousGetPinnableReference()) - { - uint length = (uint)destination.Length; - Check(Interop.Crypto.EvpDigestFinalEx(_ctx, ptrDest, ref length)); - Debug.Assert(length == _hashSize); - bytesWritten = (int)length; - } + uint length = (uint)destination.Length; + Check(Interop.Crypto.EvpDigestFinalEx(_ctx, ref destination.DangerousGetPinnableReference(), ref length)); + Debug.Assert(length == _hashSize); + bytesWritten = (int)length; // Reset the algorithm provider. Check(Interop.Crypto.EvpDigestReset(_ctx, _algorithmEvp)); @@ -124,7 +121,7 @@ namespace Internal.Cryptography private readonly int _hashSize; private SafeHmacCtxHandle _hmacCtx; - public unsafe HmacHashProvider(IntPtr algorithmEvp, byte[] key) + public HmacHashProvider(IntPtr algorithmEvp, byte[] key) { Debug.Assert(algorithmEvp != IntPtr.Zero); Debug.Assert(key != null); @@ -135,11 +132,8 @@ namespace Internal.Cryptography throw new CryptographicException(); } - fixed (byte* keyPtr = key) - { - _hmacCtx = Interop.Crypto.HmacCreate(keyPtr, key.Length, algorithmEvp); - Interop.Crypto.CheckValidOpenSslHandle(_hmacCtx); - } + _hmacCtx = Interop.Crypto.HmacCreate(ref new Span<byte>(key).DangerousGetPinnableReference(), key.Length, algorithmEvp); + Interop.Crypto.CheckValidOpenSslHandle(_hmacCtx); } public override void AppendHashData(ReadOnlySpan<byte> data) => @@ -162,13 +156,10 @@ namespace Internal.Cryptography return false; } - fixed (byte* ptrDest = &destination.DangerousGetPinnableReference()) - { - int length = destination.Length; - Check(Interop.Crypto.HmacFinal(_hmacCtx, ptrDest, ref length)); - Debug.Assert(length == _hashSize); - bytesWritten = length; - } + int length = destination.Length; + Check(Interop.Crypto.HmacFinal(_hmacCtx, ref destination.DangerousGetPinnableReference(), ref length)); + Debug.Assert(length == _hashSize); + bytesWritten = length; Check(Interop.Crypto.HmacReset(_hmacCtx)); return true; diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.OSX.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.OSX.cs index cd8a00d1a8..012fba22de 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.OSX.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.OSX.cs @@ -8,12 +8,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private unsafe void GetBytes(byte* pbBuffer, int count) + private void GetBytes(ref byte pbBuffer, int count) { - Debug.Assert(pbBuffer != null); Debug.Assert(count > 0); - Interop.AppleCrypto.GetRandomBytes(pbBuffer, count); + Interop.AppleCrypto.GetRandomBytes(ref pbBuffer, count); } } } diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Unix.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Unix.cs index 445f8ec857..3065b37c5e 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Unix.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Unix.cs @@ -8,12 +8,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private unsafe void GetBytes(byte* pbBuffer, int count) + private void GetBytes(ref byte pbBuffer, int count) { - Debug.Assert(pbBuffer != null); Debug.Assert(count > 0); - if (!Interop.Crypto.GetRandomBytes(pbBuffer, count)) + if (!Interop.Crypto.GetRandomBytes(ref pbBuffer, count)) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Windows.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Windows.cs index 3d350b5914..a7a0253e88 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Windows.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Windows.cs @@ -8,12 +8,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private unsafe void GetBytes(byte* pbBuffer, int count) + private void GetBytes(ref byte pbBuffer, int count) { - Debug.Assert(pbBuffer != null); Debug.Assert(count > 0); - Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(pbBuffer, count); + Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(ref pbBuffer, count); if (status != Interop.BCrypt.NTSTATUS.STATUS_SUCCESS) throw Interop.BCrypt.CreateCryptographicException(status); } diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs index 2252b7dca2..57e0be7b79 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs @@ -18,14 +18,11 @@ namespace System.Security.Cryptography GetBytes(new Span<byte>(data, offset, count)); } - public override unsafe void GetBytes(Span<byte> data) + public override void GetBytes(Span<byte> data) { if (data.Length > 0) { - fixed (byte* ptr = &data.DangerousGetPinnableReference()) - { - GetBytes(ptr, data.Length); - } + GetBytes(ref data.DangerousGetPinnableReference(), data.Length); } } |