Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initialization by KMAC #100419

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ internal static partial class Crypto
{
internal static partial class EvpMacAlgs
{
internal static SafeEvpMacHandle? Kmac128 { get; } = EvpMacFetch(HashAlgorithmNames.KMAC128);
internal static SafeEvpMacHandle? Kmac256 { get; } = EvpMacFetch(HashAlgorithmNames.KMAC256);
internal static SafeEvpMacHandle? Kmac128 { get; }
internal static SafeEvpMacHandle? Kmac256 { get; }

static EvpMacAlgs()
{
CryptoInitializer.Initialize();

// Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize
// is called first. Property initializers happen before cctors, so instead set the property after the
// initializer is run.
Kmac128 = EvpMacFetch(HashAlgorithmNames.KMAC128);
Kmac256 = EvpMacFetch(HashAlgorithmNames.KMAC256);
}

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMacFetch", StringMarshalling = StringMarshalling.Utf8)]
private static partial SafeEvpMacHandle CryptoNative_EvpMacFetch(string algorithm, out int haveFeature);
Expand Down
18 changes: 18 additions & 0 deletions src/libraries/System.Security.Cryptography/tests/KmacTestDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

Expand Down Expand Up @@ -1067,6 +1068,23 @@ public void IsSupported_AgreesWithPlatform()
Assert.Equal(TKmacTrait.IsSupported, PlatformSupportsKmac());
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void IsSupported_InitializesCrypto()
{
if (!IsSupported)
{
throw new SkipTestException("Algorithm is not supported on current platform.");
}

// This ensures that KMAC is the first cryptographic algorithm touched in the process, which kicks off
// the initialization of the crypto layer on some platforms. Running in a remote executor ensures no other
// test has pre-initialized anything.
RemoteExecutor.Invoke(static () =>
{
return TKmacTrait.IsSupported ? RemoteExecutor.SuccessExitCode : 0;
}).Dispose();
}

private static async Task AssertOneShotsThrowAnyAsync<TException>(
int? keySize = null,
int? customizationStringSize = null,
Expand Down
Loading