@@ -86,17 +86,10 @@ public static void setupParameters() throws Exception {
86
86
}
87
87
88
88
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 ));
100
93
List <Arguments > keys = new ArrayList <>();
101
94
if (algorithm .equals ("EC" )) {
102
95
// 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
236
229
final String algorithm = pubKey .getAlgorithm ();
237
230
238
231
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 ));
250
236
251
237
final X509EncodedKeySpec nativeSpec =
252
238
nativeFactory .getKeySpec (pubKey , X509EncodedKeySpec .class );
@@ -315,17 +301,10 @@ public void testPKCS8Encoding(final KeyPair keyPair, final String testName) thro
315
301
final String algorithm = privKey .getAlgorithm ();
316
302
317
303
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 ));
329
308
330
309
final PKCS8EncodedKeySpec nativeSpec =
331
310
nativeFactory .getKeySpec (privKey , PKCS8EncodedKeySpec .class );
@@ -740,6 +719,25 @@ private static class Samples<T> {
740
719
}
741
720
}
742
721
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
+
743
741
public static class NullDataKey implements Key {
744
742
private static final long serialVersionUID = 1 ;
745
743
private final Key delegate ;
0 commit comments