Skip to content

Commit 2567a94

Browse files
authored
Fix initialization OpenSSL by KMAC
1 parent e753712 commit 2567a94

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.MacAlgs.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ internal static partial class Crypto
1212
{
1313
internal static partial class EvpMacAlgs
1414
{
15-
internal static SafeEvpMacHandle? Kmac128 { get; } = EvpMacFetch(HashAlgorithmNames.KMAC128);
16-
internal static SafeEvpMacHandle? Kmac256 { get; } = EvpMacFetch(HashAlgorithmNames.KMAC256);
15+
internal static SafeEvpMacHandle? Kmac128 { get; }
16+
internal static SafeEvpMacHandle? Kmac256 { get; }
17+
18+
static EvpMacAlgs()
19+
{
20+
CryptoInitializer.Initialize();
21+
22+
// Do not use property initializers for these because we need to ensure CryptoInitializer.Initialize
23+
// is called first. Property initializers happen before cctors, so instead set the property after the
24+
// initializer is run.
25+
Kmac128 = EvpMacFetch(HashAlgorithmNames.KMAC128);
26+
Kmac256 = EvpMacFetch(HashAlgorithmNames.KMAC256);
27+
}
1728

1829
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpMacFetch", StringMarshalling = StringMarshalling.Utf8)]
1930
private static partial SafeEvpMacHandle CryptoNative_EvpMacFetch(string algorithm, out int haveFeature);

src/libraries/System.Security.Cryptography/tests/KmacTestDriver.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Microsoft.DotNet.RemoteExecutor;
1011
using Microsoft.DotNet.XUnitExtensions;
1112
using Xunit;
1213

@@ -1067,6 +1068,23 @@ public void IsSupported_AgreesWithPlatform()
10671068
Assert.Equal(TKmacTrait.IsSupported, PlatformSupportsKmac());
10681069
}
10691070

1071+
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
1072+
public void IsSupported_InitializesCrypto()
1073+
{
1074+
if (!IsSupported)
1075+
{
1076+
throw new SkipTestException("Algorithm is not supported on current platform.");
1077+
}
1078+
1079+
// This ensures that KMAC is the first cryptographic algorithm touched in the process, which kicks off
1080+
// the initialization of the crypto layer on some platforms. Running in a remote executor ensures no other
1081+
// test has pre-initialized anything.
1082+
RemoteExecutor.Invoke(static () =>
1083+
{
1084+
return TKmacTrait.IsSupported ? RemoteExecutor.SuccessExitCode : 0;
1085+
}).Dispose();
1086+
}
1087+
10701088
private static async Task AssertOneShotsThrowAnyAsync<TException>(
10711089
int? keySize = null,
10721090
int? customizationStringSize = null,

0 commit comments

Comments
 (0)