diff options
author | Marco Rossignoli <marco.rossignoli@gmail.com> | 2018-06-04 15:40:33 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2018-06-21 09:34:30 +0300 |
commit | 7a066c88d129b416cf0962dc3b105aca805ee648 (patch) | |
tree | 31adfcf296a29fc90731eda60de8b250ca15551a | |
parent | c5b60e36dcd80db8ecbb107a13f4a45446fe0b39 (diff) |
Substitute BCryptGenRandom.cs with coreclr mirror version (#30115)
* cleanup BCrypt
* fix call sites
* Revert "fix call sites"
This reverts commit 9a2cf64935bb4237d62724033765af59dfd952e0.
* fix non win call sites
* add unsafe keyword
* add unsafe keyword on pinvoke
(cherry picked from commit 756e289686080ab8e18476aacb4a419a2ee1f483)
11 files changed, 24 insertions, 68 deletions
diff --git a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Random.cs b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Random.cs index 7291b23b91..e18157754a 100644 --- a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Random.cs +++ b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Random.cs @@ -11,12 +11,12 @@ internal static partial class Interop { internal static partial class AppleCrypto { - internal static void GetRandomBytes(ref byte pbBuffer, int count) + internal unsafe static void GetRandomBytes(byte* pbBuffer, int count) { Debug.Assert(count >= 0); int errorCode; - int ret = AppleCryptoNative_GetRandomBytes(ref pbBuffer, count, out errorCode); + int ret = AppleCryptoNative_GetRandomBytes(pbBuffer, count, out errorCode); if (ret == 0) { @@ -30,6 +30,6 @@ internal static partial class Interop } [DllImport(Libraries.AppleCryptoNative)] - private static extern int AppleCryptoNative_GetRandomBytes(ref byte buf, int num, out int errorCode); + private unsafe static extern int AppleCryptoNative_GetRandomBytes(byte* buf, int num, out int errorCode); } } diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.RAND.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.RAND.cs index ef5f09e542..2643abe6f6 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.RAND.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.RAND.cs @@ -9,15 +9,15 @@ internal static partial class Interop { internal static partial class Crypto { - internal static bool GetRandomBytes(ref byte pbBuffer, int count) + internal unsafe static bool GetRandomBytes(byte* pbBuffer, int count) { Debug.Assert(count >= 0); - return CryptoNative_GetRandomBytes(ref pbBuffer, count); + return CryptoNative_GetRandomBytes(pbBuffer, count); } [DllImport(Libraries.CryptoNative)] [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool CryptoNative_GetRandomBytes(ref byte buf, int num); + private unsafe static extern bool CryptoNative_GetRandomBytes(byte* buf, int num); } } diff --git a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs b/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs deleted file mode 100644 index db5506b1d4..0000000000 --- a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Diagnostics; -using System.Diagnostics.Private; -using System.Runtime.InteropServices; - -internal partial class Interop -{ - internal partial class BCrypt - { - internal static NTSTATUS BCryptGenRandom(ref byte pbBuffer, int count) - { - Debug.Assert(count >= 0); - return BCryptGenRandom(IntPtr.Zero, ref pbBuffer, count, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - } - - private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002; - - [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - private static extern NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, ref byte pbBuffer, int cbBuffer, int dwFlags); - } -} diff --git a/src/Common/src/Interop/Windows/BCrypt/Interop.NTSTATUS.cs b/src/Common/src/Interop/Windows/BCrypt/Interop.NTSTATUS.cs deleted file mode 100644 index 49d674f399..0000000000 --- a/src/Common/src/Interop/Windows/BCrypt/Interop.NTSTATUS.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; - -internal partial class Interop -{ - internal partial class BCrypt - { - internal enum NTSTATUS : uint - { - STATUS_SUCCESS = 0x0, - STATUS_NOT_FOUND = 0xc0000225, - STATUS_INVALID_PARAMETER = 0xc000000d, - STATUS_NO_MEMORY = 0xc0000017, - } - } -} 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 5a570133a1..f10e1aeeea 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 @@ -9,11 +9,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private static void GetBytes(ref byte pbBuffer, int count) + private static unsafe void GetBytes(byte* pbBuffer, int count) { Debug.Assert(count > 0); - Interop.AppleCrypto.GetRandomBytes(ref pbBuffer, count); + Interop.AppleCrypto.GetRandomBytes(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 2ea1f828fb..9a5f9661d7 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,11 +8,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private static void GetBytes(ref byte pbBuffer, int count) + private static unsafe void GetBytes(byte* pbBuffer, int count) { Debug.Assert(count > 0); - if (!Interop.Crypto.GetRandomBytes(ref pbBuffer, count)) + if (!Interop.Crypto.GetRandomBytes(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 02e7be34fc..f3f9de3ba1 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 @@ -9,11 +9,11 @@ namespace System.Security.Cryptography { partial class RandomNumberGeneratorImplementation { - private static void GetBytes(ref byte pbBuffer, int count) + private static unsafe void GetBytes(byte* pbBuffer, int count) { Debug.Assert(count > 0); - Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(ref pbBuffer, count); + Interop.BCrypt.NTSTATUS status = Interop.BCrypt.BCryptGenRandom(IntPtr.Zero, pbBuffer, count, Interop.BCrypt.BCRYPT_USE_SYSTEM_PREFERRED_RNG); 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 8badd131ea..4f0d2e4acb 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs @@ -10,11 +10,11 @@ namespace System.Security.Cryptography { // As long as each implementation can provide a static GetBytes(ref byte buf, int length) // they can share this one implementation of FillSpan. - internal static void FillSpan(Span<byte> data) + internal static unsafe void FillSpan(Span<byte> data) { if (data.Length > 0) { - GetBytes(ref MemoryMarshal.GetReference(data), data.Length); + fixed (byte* ptr = data) GetBytes(ptr, data.Length); } } @@ -30,11 +30,11 @@ namespace System.Security.Cryptography GetBytes(new Span<byte>(data, offset, count)); } - public override void GetBytes(Span<byte> data) + public override unsafe void GetBytes(Span<byte> data) { if (data.Length > 0) { - GetBytes(ref MemoryMarshal.GetReference(data), data.Length); + fixed (byte* ptr = data) GetBytes(ptr, data.Length); } } diff --git a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj index 9b1cb26508..97332e9203 100644 --- a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj +++ b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj @@ -141,11 +141,11 @@ <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Cng.cs"> <Link>Common\Interop\Windows\BCrypt\Cng.cs</Link> </Compile> - <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs"> - <Link>Common\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs</Link> + <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs"> + <Link>Common\CoreLib\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs</Link> </Compile> - <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> - <Link>Common\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> + <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> + <Link>Common\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> </Compile> <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.AsymmetricEncryption.Types.cs"> <Link>Common\Interop\Windows\BCrypt\Interop.AsymmetricEncryption.Types.cs</Link> diff --git a/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj b/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj index 52fd7dff42..b6f007a07a 100644 --- a/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj +++ b/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj @@ -97,8 +97,8 @@ <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Cng.cs"> <Link>Interop\Windows\BCrypt\Cng.cs</Link> </Compile> - <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> - <Link>Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> + <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> + <Link>Common\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> </Compile> <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.AsymmetricEncryption.Types.cs"> <Link>Interop\Windows\BCrypt\Interop.AsymmetricEncryption.Types.cs</Link> diff --git a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj index d850e2e780..510a792488 100644 --- a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj +++ b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj @@ -138,8 +138,8 @@ <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.Blobs.cs"> <Link>Common\Interop\Windows\BCrypt\Interop.Blobs.cs</Link> </Compile> - <Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> - <Link>Common\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> + <Compile Include="$(CommonPath)\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs"> + <Link>Common\CoreLib\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link> </Compile> <Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeBCryptHandle.cs"> <Link>Common\Microsoft\Win32\SafeHandles\SafeBCryptHandle.cs</Link> |