From d07ef6d12bc3e52a518e4b6b1473979b38d421ea Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Sat, 2 Jun 2018 23:57:59 +0200 Subject: [PATCH] Merge BCryptGenRandom.cs (#18233) --- .../Interop.BCryptGenRandom.GetRandomBytes.cs | 29 +++++++++++++++++++++ .../Windows/BCrypt/Interop.BCryptGenRandom.cs | 30 ++-------------------- .../Interop/Windows/BCrypt/Interop.NTSTATUS.cs | 19 ++++++++++++++ .../shared/System.Private.CoreLib.Shared.projitems | 2 ++ 4 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.GetRandomBytes.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.NTSTATUS.cs diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.GetRandomBytes.cs b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.GetRandomBytes.cs new file mode 100644 index 0000000..4d75163 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.GetRandomBytes.cs @@ -0,0 +1,29 @@ +// 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.Runtime.InteropServices; + +internal partial class Interop +{ + internal static unsafe void GetRandomBytes(byte* buffer, int length) + { + Debug.Assert(buffer != null); + Debug.Assert(length >= 0); + + BCrypt.NTSTATUS status = BCrypt.BCryptGenRandom(IntPtr.Zero, buffer, length, BCrypt.BCRYPT_USE_SYSTEM_PREFERRED_RNG); + if (status != BCrypt.NTSTATUS.STATUS_SUCCESS) + { + if (status == BCrypt.NTSTATUS.STATUS_NO_MEMORY) + { + throw new OutOfMemoryException(); + } + else + { + throw new InvalidOperationException(); + } + } + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs index 75288ac..9d072a3 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.BCryptGenRandom.cs @@ -10,35 +10,9 @@ internal partial class Interop { internal partial class BCrypt { - internal static unsafe int BCryptGenRandom(byte* pbBuffer, int count) - { - Debug.Assert(pbBuffer != null); - Debug.Assert(count >= 0); - - return BCryptGenRandom(IntPtr.Zero, pbBuffer, count, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - } - - private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002; - internal const int STATUS_SUCCESS = 0x0; - internal const int STATUS_NO_MEMORY = unchecked((int)0xC0000017); + internal const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002; [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] - private static extern unsafe int BCryptGenRandom(IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags); - } - - internal static unsafe void GetRandomBytes(byte* buffer, int length) - { - int status = BCrypt.BCryptGenRandom(buffer, length); - if (status != BCrypt.STATUS_SUCCESS) - { - if (status == BCrypt.STATUS_NO_MEMORY) - { - throw new OutOfMemoryException(); - } - else - { - throw new InvalidOperationException(); - } - } + internal static extern unsafe NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags); } } diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.NTSTATUS.cs b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.NTSTATUS.cs new file mode 100644 index 0000000..49d674f --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/BCrypt/Interop.NTSTATUS.cs @@ -0,0 +1,19 @@ +// 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.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 46b4d67..2021639 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -692,6 +692,8 @@ + + -- 2.7.4