Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 879182b

Browse files
committed
Separate BCryptGenRandom out of Cng.cs
As part of #1739, this commit begins to refactor Cng.cs, starting by pulling out the functionality needed for RandomNumberGenerator. The assembly was being filled with all of the Cng code, when in reality it only needed a very small amount of it. And since the assembly doesn't have any string resources, I also modified the .csproj to suppress the buildtools inclusion of the common resources support. As a result of the removals, this change boosts the code coverage number of System.Security.Cryptography.RandomNumberGenerator.dll from 13% to 96%. (Note that there are still copies of the code in Interop.NTSTATUS.cs in Cng.cs, due to other dependencies in Cng.cs that I didn't want to change. As we refactor Cng.cs further, we can remove those copies.)
1 parent 9aa6349 commit 879182b

File tree

6 files changed

+59
-19
lines changed

6 files changed

+59
-19
lines changed

src/Common/src/Interop/Windows/BCrypt/Cng.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,6 @@ public static int GetHashSizeInBytes(this SafeHashHandle hHash)
129129
}
130130
}
131131

132-
public static void BCryptGenRandom(byte[] buffer)
133-
{
134-
const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002;
135-
NTSTATUS ntStatus = Interop.BCryptGenRandom(IntPtr.Zero, buffer, buffer.Length, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
136-
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
137-
throw CreateCryptographicException(ntStatus);
138-
}
139-
140132
public static SafeKeyHandle BCryptImportKey(this SafeAlgorithmHandle hAlg, byte[] key)
141133
{
142134
unsafe
@@ -310,9 +302,6 @@ private static class Interop
310302
[DllImport(CngDll, CharSet = CharSet.Unicode)]
311303
public static extern unsafe NTSTATUS BCryptSetProperty(SafeBCryptHandle hObject, String pszProperty, String pbInput, int cbInput, int dwFlags);
312304

313-
[DllImport(CngDll, CharSet = CharSet.Unicode)]
314-
public static extern NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, [In, Out] byte[] pbBuffer, int cbBuffer, int dwFlags);
315-
316305
[DllImport(CngDll, CharSet = CharSet.Unicode)]
317306
public static extern NTSTATUS BCryptImportKey(SafeAlgorithmHandle hAlgorithm, IntPtr hImportKey, String pszBlobType, out SafeKeyHandle hKey, IntPtr pbKeyObject, int cbKeyObject, byte[] pbInput, int cbInput, int dwFlags);
318307

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
internal partial class Interop
8+
{
9+
internal partial class BCrypt
10+
{
11+
internal static void BCryptGenRandom(byte[] buffer)
12+
{
13+
NTSTATUS ntStatus = BCryptGenRandom(IntPtr.Zero, buffer, buffer.Length, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
14+
if (ntStatus != NTSTATUS.STATUS_SUCCESS)
15+
throw CreateCryptographicException(ntStatus);
16+
}
17+
18+
private const int BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002;
19+
20+
[DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)]
21+
private static extern NTSTATUS BCryptGenRandom(IntPtr hAlgorithm, [In, Out] byte[] pbBuffer, int cbBuffer, int dwFlags);
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Security.Cryptography;
6+
7+
internal partial class Interop
8+
{
9+
internal partial class BCrypt
10+
{
11+
private enum NTSTATUS : uint
12+
{
13+
STATUS_SUCCESS = 0x0,
14+
STATUS_NOT_FOUND = 0xc0000225,
15+
STATUS_INVALID_PARAMETER = 0xc000000d,
16+
STATUS_NO_MEMORY = 0xc0000017,
17+
}
18+
19+
private static Exception CreateCryptographicException(NTSTATUS ntStatus)
20+
{
21+
int hr = ((int)ntStatus) | 0x01000000;
22+
return new CryptographicException(hr);
23+
}
24+
}
25+
}

src/Common/src/Interop/Windows/Interop.Libraries.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ internal static partial class Interop
55
{
66
private static class Libraries
77
{
8+
internal const string BCrypt = "BCrypt.dll";
89
internal const string Console_L1 = "api-ms-win-core-console-l1-1-0.dll";
910
internal const string Console_L2 = "api-ms-win-core-console-l2-1-0.dll";
1011
internal const string CoreFile_L1 = "api-ms-win-core-file-l1-1-0.dll";

src/System.Security.Cryptography.RandomNumberGenerator/src/System.Security.Cryptography.RandomNumberGenerator.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<AssemblyVersion>4.0.0.0</AssemblyVersion>
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1212
<CLSCompliant>false</CLSCompliant>
13+
<SkipCommonResourcesIncludes>true</SkipCommonResourcesIncludes>
1314
</PropertyGroup>
1415
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Linux_Debug|AnyCPU' " />
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Linux_Release|AnyCPU' " />
@@ -22,8 +23,14 @@
2223
</ItemGroup>
2324
<ItemGroup Condition=" '$(TargetsWindows)' == 'true' ">
2425
<Compile Include="System\Security\Cryptography\RNGCryptoServiceProvider.Windows.cs" />
25-
<Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Cng.cs">
26-
<Link>Common\Interop\Windows\BCrypt\Cng.cs</Link>
26+
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
27+
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
28+
</Compile>
29+
<Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs">
30+
<Link>Common\Interop\Windows\BCrypt\Interop.BCryptGenRandom.cs</Link>
31+
</Compile>
32+
<Compile Include="$(CommonPath)\Interop\Windows\BCrypt\Interop.NTSTATUS.cs">
33+
<Link>Common\Interop\Windows\BCrypt\Interop.NTSTATUS.cs</Link>
2734
</Compile>
2835
</ItemGroup>
2936
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System.Diagnostics;
5-
6-
using Internal.NativeCrypto;
7-
84
namespace System.Security.Cryptography
95
{
106
internal sealed class RNGCryptoServiceProvider : RandomNumberGenerator
@@ -14,9 +10,8 @@ public sealed override void GetBytes(byte[] data)
1410
ValidateGetBytesArgs(data);
1511
if (data.Length > 0)
1612
{
17-
Cng.BCryptGenRandom(data);
13+
Interop.BCrypt.BCryptGenRandom(data);
1814
}
1915
}
2016
}
2117
}
22-

0 commit comments

Comments
 (0)