Skip to content

Commit f60a6e0

Browse files
PR 438 follow ups (#440)
Follow-ups from PR #438: 1. Tighten self test timeout and sleeps (discussed offline) 2. Tighten and consolidate some test logic (see [here][1]) [1]: #438 (comment)
1 parent 0a05b47 commit f60a6e0

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

src/com/amazon/corretto/crypto/provider/AmazonCorrettoCryptoProvider.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,11 @@ public boolean isFipsStatusOk() {
663663
}
664664
if (getSelfTestStatus() == SelfTestStatus.NOT_RUN) {
665665
// If FIPS self tests haven't completed, give them a 5s timeout to complete.
666-
final long timeout = 5 * 1000;
666+
final long timeout = 3 * 1000;
667667
final long deadline = System.currentTimeMillis() + timeout;
668668
while (getSelfTestStatus() == SelfTestStatus.NOT_RUN) {
669669
try {
670-
Thread.sleep(10);
670+
Thread.sleep(1);
671671
} catch (Exception e) {
672672
throw new RuntimeCryptoException(e);
673673
}

tst/com/amazon/corretto/crypto/provider/test/EvpKeyFactoryTest.java

+31-33
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,10 @@ public static void setupParameters() throws Exception {
8686
}
8787

8888
for (String algorithm : ALGORITHMS) {
89-
KeyPairGenerator kpg;
90-
if (algorithm.startsWith("ML-DSA")
91-
|| (algorithm.startsWith("Ed") && TestUtil.getJavaVersion() < 15)) {
92-
// JCE doesn't support ML-DSA until JDK24, and BouncyCastle currently
93-
// serializes ML-DSA private keys via seeds.
94-
// TODO: switch to BouncyCastle once BC supports CHOICE-encoded private keys
95-
// Similarly, JDK doesn't support EdDSA/Ed25519 until JDK15
96-
kpg = KeyPairGenerator.getInstance(algorithm, NATIVE_PROVIDER);
97-
} else {
98-
kpg = KeyPairGenerator.getInstance(algorithm);
99-
}
89+
KeyPairGenerator kpg =
90+
getAlternateProvider(algorithm) == null
91+
? KeyPairGenerator.getInstance(algorithm)
92+
: KeyPairGenerator.getInstance(algorithm, getAlternateProvider(algorithm));
10093
List<Arguments> keys = new ArrayList<>();
10194
if (algorithm.equals("EC")) {
10295
// Different curves can excercise different areas of ASN.1/DER and so should all be tested.
@@ -236,17 +229,10 @@ public void testX509Encoding(final KeyPair keyPair, final String testName) throw
236229
final String algorithm = pubKey.getAlgorithm();
237230

238231
final KeyFactory nativeFactory = KeyFactory.getInstance(algorithm, NATIVE_PROVIDER);
239-
final KeyFactory jceFactory;
240-
if (algorithm.startsWith("ML-DSA")
241-
|| (algorithm.startsWith("Ed") && TestUtil.getJavaVersion() < 15)) {
242-
// JCE doesn't support ML-DSA until JDK24, and BouncyCastle currently
243-
// serializes ML-DSA private keys via seeds.
244-
// TODO: switch to BouncyCastle once BC supports CHOICE-encoded private keys
245-
// Similarly, JDK doesn't support EdDSA/Ed25519 until JDK15
246-
jceFactory = KeyFactory.getInstance(algorithm, NATIVE_PROVIDER);
247-
} else {
248-
jceFactory = KeyFactory.getInstance(algorithm);
249-
}
232+
final KeyFactory jceFactory =
233+
getAlternateProvider(algorithm) == null
234+
? KeyFactory.getInstance(algorithm)
235+
: KeyFactory.getInstance(algorithm, getAlternateProvider(algorithm));
250236

251237
final X509EncodedKeySpec nativeSpec =
252238
nativeFactory.getKeySpec(pubKey, X509EncodedKeySpec.class);
@@ -315,17 +301,10 @@ public void testPKCS8Encoding(final KeyPair keyPair, final String testName) thro
315301
final String algorithm = privKey.getAlgorithm();
316302

317303
final KeyFactory nativeFactory = KeyFactory.getInstance(algorithm, NATIVE_PROVIDER);
318-
final KeyFactory jceFactory;
319-
if (algorithm.startsWith("ML-DSA")
320-
|| (algorithm.startsWith("Ed") && TestUtil.getJavaVersion() < 15)) {
321-
// JCE doesn't support ML-DSA until JDK24, and BouncyCastle currently
322-
// serializes ML-DSA private keys via seeds.
323-
// TODO: switch to BouncyCastle once BC supports CHOICE-encoded private keys
324-
// Similarly, JDK doesn't support EdDSA/Ed25519 until JDK15
325-
jceFactory = KeyFactory.getInstance(algorithm, NATIVE_PROVIDER);
326-
} else {
327-
jceFactory = KeyFactory.getInstance(algorithm);
328-
}
304+
final KeyFactory jceFactory =
305+
getAlternateProvider(algorithm) == null
306+
? KeyFactory.getInstance(algorithm)
307+
: KeyFactory.getInstance(algorithm, getAlternateProvider(algorithm));
329308

330309
final PKCS8EncodedKeySpec nativeSpec =
331310
nativeFactory.getKeySpec(privKey, PKCS8EncodedKeySpec.class);
@@ -740,6 +719,25 @@ private static class Samples<T> {
740719
}
741720
}
742721

722+
// This method is used to determine whether tests should use an alternate provider for a given
723+
// algorithm. In cases where JCE doesn't support the requested algorithm, the alternate provider
724+
// will be returned. In cases where JCE does support the requested algorithm, null will be
725+
// returned.
726+
private static Provider getAlternateProvider(String algorithm) {
727+
// JCE doesn't support ML-DSA until JDK24, and BouncyCastle currently serializes ML-DSA private
728+
// keys via seeds.
729+
// TODO: switch to BouncyCastle once BC supports CHOICE-encoded private keys
730+
if ((algorithm.startsWith("ML-DSA") && TestUtil.getJavaVersion() < 24)
731+
// Similarly, JDK doesn't support EdDSA/Ed25519 until JDK15
732+
|| ((algorithm.equals("Ed25519")
733+
|| algorithm.equals("Ed25519ph")
734+
|| algorithm.equals("EdDSA"))
735+
&& TestUtil.getJavaVersion() < 15)) {
736+
return NATIVE_PROVIDER;
737+
}
738+
return null;
739+
}
740+
743741
public static class NullDataKey implements Key {
744742
private static final long serialVersionUID = 1;
745743
private final Key delegate;

0 commit comments

Comments
 (0)