File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
Common/src/Interop/Unix/System.Security.Cryptography.Native
System.Security.Cryptography/tests Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -12,8 +12,19 @@ internal static partial class Crypto
12
12
{
13
13
internal static partial class EvpMacAlgs
14
14
{
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
+ }
17
28
18
29
[ LibraryImport ( Libraries . CryptoNative , EntryPoint = "CryptoNative_EvpMacFetch" , StringMarshalling = StringMarshalling . Utf8 ) ]
19
30
private static partial SafeEvpMacHandle CryptoNative_EvpMacFetch ( string algorithm , out int haveFeature ) ;
Original file line number Diff line number Diff line change 7
7
using System . Text ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
+ using Microsoft . DotNet . RemoteExecutor ;
10
11
using Microsoft . DotNet . XUnitExtensions ;
11
12
using Xunit ;
12
13
@@ -1067,6 +1068,23 @@ public void IsSupported_AgreesWithPlatform()
1067
1068
Assert . Equal ( TKmacTrait . IsSupported , PlatformSupportsKmac ( ) ) ;
1068
1069
}
1069
1070
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
+
1070
1088
private static async Task AssertOneShotsThrowAnyAsync < TException > (
1071
1089
int ? keySize = null ,
1072
1090
int ? customizationStringSize = null ,
You can’t perform that action at this time.
0 commit comments