12
12
import com .amazon .corretto .crypto .provider .AmazonCorrettoCryptoProvider ;
13
13
import com .amazon .corretto .crypto .provider .FipsStatusException ;
14
14
import com .amazon .corretto .crypto .provider .RuntimeCryptoException ;
15
+ import com .amazon .corretto .crypto .provider .SelfTestStatus ;
15
16
import java .security .KeyPairGenerator ;
16
17
import javax .crypto .KeyGenerator ;
17
18
import org .junit .jupiter .api .Test ;
@@ -35,6 +36,7 @@ public class FipsStatusTest {
35
36
public void givenAccpBuiltWithFips_whenAWS_LC_fips_failure_callback_expectException ()
36
37
throws Exception {
37
38
if (provider .isFips () && provider .isFipsSelfTestFailureSkipAbort ()) {
39
+ blockUntilSelfTestsRun ();
38
40
assertTrue (provider .isFipsStatusOk ());
39
41
assertEquals (0 , provider .getFipsSelfTestFailures ().size ());
40
42
assertNotNull (KeyGenerator .getInstance ("AES" , provider ));
@@ -48,6 +50,7 @@ public void givenAccpBuiltWithFips_whenAWS_LC_fips_failure_callback_expectExcept
48
50
// we need to flip the status back to OK so the rest of tests would work. In practice, once
49
51
// the flag is set to false, it remains false.
50
52
NativeTestHooks .resetFipsStatus ();
53
+ assertTrue (provider .isFipsStatusOk ());
51
54
} else {
52
55
assertThrows (UnsupportedOperationException .class , () -> provider .isFipsStatusOk ());
53
56
assertThrows (UnsupportedOperationException .class , () -> provider .getFipsSelfTestFailures ());
@@ -72,21 +75,37 @@ private void testPwctBreakage(final String algo, String envVarValue) throws Exce
72
75
}
73
76
// Be sure to reset provider-global state!
74
77
TestUtil .setEnv (PWCT_BREAKAGE_ENV_VAR , null );
78
+ assertNotNull (kpg .generateKeyPair ());
75
79
NativeTestHooks .resetFipsStatus ();
76
80
assertTrue (provider .isFipsStatusOk ());
77
81
}
78
82
79
83
@ Test
80
84
public void testPwctBreakageSkipAbort () throws Exception {
85
+ blockUntilSelfTestsRun ();
81
86
assumeTrue (provider .isFips ());
82
87
assumeTrue (provider .isFipsSelfTestFailureSkipAbort ());
83
88
testPwctBreakage ("RSA" , "RSA_PWCT" );
84
89
testPwctBreakage ("EC" , "ECDSA_PWCT" );
85
- if ( TestUtil . getJavaVersion () >= 15 ) {
86
- testPwctBreakage ( "EdDSA" , "EDDSA_PWCT" );
87
- }
90
+ // TODO: Re-enable this test when AWS-LC's EdDSA can fail keygen
91
+ // https://github.com/aws/aws-lc/pull/2256
92
+ // testPwctBreakage("EdDSA", "EDDSA_PWCT");
88
93
if (provider .isExperimentalFips ()) { // can be removed when AWS-LC-FIPS supports ML-DSA
89
94
testPwctBreakage ("ML-DSA" , "MLDSA_PWCT" );
90
95
}
91
96
}
97
+
98
+ // FIPS status won't be OK until the power-on self tests have run and passed, so provide a method
99
+ // that blocks until the tests have completed. Set a deadline to make terminal hang impossible.
100
+ private void blockUntilSelfTestsRun () throws Exception {
101
+ assertTrue (provider .isFips ());
102
+ long timeout = 5 * 1000 ;
103
+ long deadline = System .currentTimeMillis () + timeout ;
104
+ while (provider .getSelfTestStatus () == SelfTestStatus .NOT_RUN ) {
105
+ Thread .sleep (100 );
106
+ if (System .currentTimeMillis () > deadline ) {
107
+ throw new RuntimeException ("FIPS self tests timed out" );
108
+ }
109
+ }
110
+ }
92
111
}
0 commit comments