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

Commit 3872baf

Browse files
authored
Add FixedTimeEquals and other crypto helper routines as public API
1 parent 1c9cd81 commit 3872baf

19 files changed

+494
-9
lines changed

src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.Digest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ internal partial class AuthenticationHelper
4242
// 48='0', 65='A', 97='a'
4343
private static int[] s_alphaNumChooser = new int[] { 48, 65, 97 };
4444

45-
// Define a random number generator for cnonce
46-
private static RandomNumberGenerator s_rng = RandomNumberGenerator.Create();
47-
4845
public async static Task<bool> TrySetDigestAuthToken(HttpRequestMessage request, ICredentials credentials, DigestResponse digestResponse, string authHeader)
4946
{
5047
NetworkCredential credential = credentials.GetCredential(request.RequestUri, Digest);
@@ -218,7 +215,7 @@ private static string GetRandomAlphaNumericString()
218215
{
219216
const int Length = 16;
220217
Span<byte> randomNumbers = stackalloc byte[Length * 2];
221-
s_rng.GetBytes(randomNumbers);
218+
RandomNumberGenerator.Fill(randomNumbers);
222219

223220
StringBuilder sb = StringBuilderCache.Acquire(Length);
224221
for (int i = 0; i < randomNumbers.Length;)

src/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.netcoreapp.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public void AppendData(System.ReadOnlySpan<byte> data) { }
3131
}
3232
public abstract partial class RandomNumberGenerator : System.IDisposable
3333
{
34+
public static void Fill(Span<byte> data) => throw null;
3435
public virtual void GetBytes(System.Span<byte> data) { }
3536
public virtual void GetNonZeroBytes(System.Span<byte> data) { }
3637
}

src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.OSX.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Security.Cryptography
88
{
99
partial class RandomNumberGeneratorImplementation
1010
{
11-
private void GetBytes(ref byte pbBuffer, int count)
11+
private static void GetBytes(ref byte pbBuffer, int count)
1212
{
1313
Debug.Assert(count > 0);
1414

src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Security.Cryptography
88
{
99
partial class RandomNumberGeneratorImplementation
1010
{
11-
private void GetBytes(ref byte pbBuffer, int count)
11+
private static void GetBytes(ref byte pbBuffer, int count)
1212
{
1313
Debug.Assert(count > 0);
1414

src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.Security.Cryptography
88
{
99
partial class RandomNumberGeneratorImplementation
1010
{
11-
private void GetBytes(ref byte pbBuffer, int count)
11+
private static void GetBytes(ref byte pbBuffer, int count)
1212
{
1313
Debug.Assert(count > 0);
1414

src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ namespace System.Security.Cryptography
88
{
99
internal sealed partial class RandomNumberGeneratorImplementation : RandomNumberGenerator
1010
{
11+
// As long as each implementation can provide a static GetBytes(ref byte buf, int length)
12+
// they can share this one implementation of FillSpan.
13+
internal static void FillSpan(Span<byte> data)
14+
{
15+
if (data.Length > 0)
16+
{
17+
GetBytes(ref MemoryMarshal.GetReference(data), data.Length);
18+
}
19+
}
20+
1121
public override void GetBytes(byte[] data)
1222
{
1323
if (data == null) throw new ArgumentNullException(nameof(data));

src/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/RandomNumberGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public virtual void GetNonZeroBytes(Span<byte> data)
9191
}
9292
}
9393

94+
public static void Fill(Span<byte> data)
95+
{
96+
RandomNumberGeneratorImplementation.FillSpan(data);
97+
}
98+
9499
internal void VerifyGetBytes(byte[] data, int offset, int count)
95100
{
96101
if (data == null) throw new ArgumentNullException(nameof(data));

src/System.Security.Cryptography.Algorithms/tests/RandomNumberGeneratorTests.netcoreapp.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,42 @@ public static void GetNonZeroBytes_Span()
5555
Assert.Equal(-1, Array.IndexOf<byte>(rand, 0));
5656
}
5757
}
58+
59+
[Fact]
60+
public static void Fill_ZeroLengthSpan()
61+
{
62+
byte[] rand = { 1 };
63+
RandomNumberGenerator.Fill(new Span<byte>(rand, 0, 0));
64+
Assert.Equal(1, rand[0]);
65+
}
66+
67+
[Fact]
68+
public static void Fill_SpanLength1()
69+
{
70+
byte[] rand = { 1 };
71+
bool replacedValue = false;
72+
73+
for (int i = 0; i < 10; i++)
74+
{
75+
RandomNumberGenerator.Fill(rand);
76+
77+
if (rand[0] != 1)
78+
{
79+
replacedValue = true;
80+
break;
81+
}
82+
}
83+
84+
Assert.True(replacedValue, "Fill eventually wrote a different byte");
85+
}
86+
87+
[Fact]
88+
public static void Fill_RandomDistribution()
89+
{
90+
byte[] random = new byte[2048];
91+
RandomNumberGenerator.Fill(random);
92+
93+
RandomDataGenerator.VerifyRandomDistribution(random);
94+
}
5895
}
5996
}

src/System.Security.Cryptography.Primitives/System.Security.Cryptography.Primitives.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,48 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E89
2020
EndProject
2121
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}"
2222
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Security.Cryptography.Primitives.Performance.Tests", "tests\Performance\System.Security.Cryptography.Primitives.Performance.Tests.csproj", "{FB3EA273-567D-414F-B36D-3698BE8D198B}"
24+
EndProject
2325
Global
2426
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2527
Debug|Any CPU = Debug|Any CPU
28+
netcoreapp-Debug|Any CPU = netcoreapp-Debug|Any CPU
29+
netcoreapp-Release|Any CPU = netcoreapp-Release|Any CPU
2630
Release|Any CPU = Release|Any CPU
2731
EndGlobalSection
2832
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2933
{101EB757-55A4-4F48-841C-C088640B8F57}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
3034
{101EB757-55A4-4F48-841C-C088640B8F57}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
35+
{101EB757-55A4-4F48-841C-C088640B8F57}.netcoreapp-Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
36+
{101EB757-55A4-4F48-841C-C088640B8F57}.netcoreapp-Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
37+
{101EB757-55A4-4F48-841C-C088640B8F57}.netcoreapp-Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
38+
{101EB757-55A4-4F48-841C-C088640B8F57}.netcoreapp-Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3139
{101EB757-55A4-4F48-841C-C088640B8F57}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
3240
{101EB757-55A4-4F48-841C-C088640B8F57}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3341
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
3442
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
43+
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.netcoreapp-Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
44+
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.netcoreapp-Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
45+
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.netcoreapp-Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
46+
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.netcoreapp-Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3547
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
3648
{DF73E985-E143-4BF5-9FA4-E199E7D36235}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3749
{F050C895-297F-41C6-98C3-406D791AD515}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
3850
{F050C895-297F-41C6-98C3-406D791AD515}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
51+
{F050C895-297F-41C6-98C3-406D791AD515}.netcoreapp-Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
52+
{F050C895-297F-41C6-98C3-406D791AD515}.netcoreapp-Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
53+
{F050C895-297F-41C6-98C3-406D791AD515}.netcoreapp-Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
54+
{F050C895-297F-41C6-98C3-406D791AD515}.netcoreapp-Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
3955
{F050C895-297F-41C6-98C3-406D791AD515}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
4056
{F050C895-297F-41C6-98C3-406D791AD515}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
57+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
58+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
59+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.netcoreapp-Debug|Any CPU.ActiveCfg = netcoreapp-Debug|Any CPU
60+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.netcoreapp-Debug|Any CPU.Build.0 = netcoreapp-Debug|Any CPU
61+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.netcoreapp-Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
62+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.netcoreapp-Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
63+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.Release|Any CPU.ActiveCfg = netcoreapp-Release|Any CPU
64+
{FB3EA273-567D-414F-B36D-3698BE8D198B}.Release|Any CPU.Build.0 = netcoreapp-Release|Any CPU
4165
EndGlobalSection
4266
GlobalSection(SolutionProperties) = preSolution
4367
HideSolutionNode = FALSE
@@ -46,5 +70,6 @@ Global
4670
{101EB757-55A4-4F48-841C-C088640B8F57} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
4771
{DF73E985-E143-4BF5-9FA4-E199E7D36235} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
4872
{F050C895-297F-41C6-98C3-406D791AD515} = {2E666815-2EDB-464B-9DF6-380BF4789AD4}
73+
{FB3EA273-567D-414F-B36D-3698BE8D198B} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
4974
EndGlobalSection
5075
EndGlobal

src/System.Security.Cryptography.Primitives/ref/System.Security.Cryptography.Primitives.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public enum CipherMode
3636
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
3737
OFB = 3,
3838
}
39+
public static partial class CryptographicOperations
40+
{
41+
public static bool FixedTimeEquals(System.ReadOnlySpan<byte> left, System.ReadOnlySpan<byte> right) => throw null;
42+
public static void ZeroMemory(System.Span<byte> buffer) => throw null;
43+
}
3944
public partial class CryptographicUnexpectedOperationException : System.Security.Cryptography.CryptographicException
4045
{
4146
public CryptographicUnexpectedOperationException() { }

0 commit comments

Comments
 (0)