-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add FixedTimeEquals and other crypto helper routines as public API #27103
Conversation
For stats lovers, here's some of the data from perf runs (augmented by Excel-fu) comparing the subtract-OR-accumulator to the early aborting if:
|
{ | ||
GetBytes(ref MemoryMarshal.GetReference(data), data.Length); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't FillSpan identical in each implementation? Can it just be in the shared file, and just be part of the Fill implementation there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is currently. I think I was being defensive that at some point one of the platforms could end up needing some sort of state managed... but I guess that ambiguous future can deal with things then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
There are several places in corefx where we create a RandomNumberGenerator instance just to be able to use GetBytes. Now that this is adding a static Fill that avoids the RNG allocation, it'd be nice to use it as part of this PR. For example, this: corefx/src/Common/src/System/Marvin.cs Lines 109 to 117 in 53be85c
could now be: private static ulong GenerateSeed()
{
Span<byte> bytes = stackalloc byte[sizeof(long)];
RandomNumberGenerator.Fill(bytes);
return BitConverter.ToUInt64(bytes, 0);
} There's one in SocketsHttpHandler for digest auth: corefx/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.Digest.cs Line 221 in eb89247
That static can be removed and replaced with just a call to Fill. Similarly there's one in ManagedWebSocket here:
though that one might be more complicated and not worth it, as I believe we compile that file into an assembly that targets netstandard20 as well. |
// The chances of this failing are 1 in 1.2e24, unless the RNG is broken. | ||
for (int i = 0; i < 10 && !hasData; i++) | ||
{ | ||
rng.GetBytes(testSpan); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't want to just use Fill? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drat, missed one :). Fill was written last :).
I changed Marvin, which also builds as UAP/UAPAOT for System.Private.Xml. I'll look at some others in a bit, since I have to rebase/rebuild to pull in the sockets HTTP handler. |
Looks like Marvin builds under NetFx, so it can't be changed to Fill without some #defines:
|
Ok, thanks. |
@@ -10,11 +10,12 @@ | |||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Release|AnyCPU'" /> | |||
<ItemGroup> | |||
<Compile Include="System.Security.Cryptography.Primitives.cs" /> | |||
<Compile Include="System.Security.Cryptography.Primitives.netcoreapp.cs" Condition="'$(TargetGroup)' == 'netcoreapp'" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need it specialized like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the last library I made API changes to wasn't part of netstandard, apparently. Merged it into the other file.
Fixes #10749.