11/*
2- * Copyright IBM Corp. 2024
2+ * Copyright IBM Corp. 2024, 2025
33 *
44 * This code is free software; you can redistribute it and/or modify it
55 * under the terms provided by IBM in the LICENSE file that accompanied
88package ibm .jceplus .junit .openjceplusfips ;
99
1010import ibm .jceplus .junit .base .BaseTestECDHKeyAgreementParamValidation ;
11+ import java .security .KeyPair ;
12+ import java .security .KeyPairGenerator ;
13+ import java .security .PrivateKey ;
14+ import java .security .PublicKey ;
15+ import java .security .spec .ECGenParameterSpec ;
16+ import javax .crypto .KeyAgreement ;
1117import org .junit .jupiter .api .BeforeAll ;
1218import org .junit .jupiter .api .TestInstance ;
1319import org .junit .jupiter .api .TestInstance .Lifecycle ;
20+ import org .junit .jupiter .params .ParameterizedTest ;
21+ import org .junit .jupiter .params .provider .ValueSource ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23+ import static org .junit .jupiter .api .Assertions .fail ;
24+ import static org .junit .jupiter .api .Assumptions .assumeTrue ;
1425
1526@ TestInstance (Lifecycle .PER_CLASS )
1627public class TestECDHKeyAgreementParamValidation extends BaseTestECDHKeyAgreementParamValidation {
@@ -21,4 +32,39 @@ public void beforeAll() throws Exception {
2132 setProviderName (Utils .TEST_SUITE_PROVIDER_NAME );
2233 }
2334
35+ @ ParameterizedTest
36+ @ ValueSource (strings = {"secp112r1" , "secp112r2" , "secp128r1" , "secp128r2" , "secp160r1" , "secp160r2" , "secp160k1" , "secp192k1" , "secp192r1" })
37+ public void testECDHKeyAgreementSharedSecretComputation (String curveName ) {
38+ assumeTrue (
39+ Boolean .getBoolean ("openjceplus.disableSmallerECKeySizeForSharedKeyComputing" ),
40+ "Property not true; skipping"
41+ );
42+
43+ try {
44+ KeyPair alice = genECKeyPair (curveName );
45+ KeyPair bob = genECKeyPair (curveName );
46+
47+ ecdhSharedSecretComputation (alice .getPrivate (), bob .getPublic ());
48+ ecdhSharedSecretComputation (bob .getPrivate (), alice .getPublic ());
49+
50+ fail ("Curve " + curveName + " worked unexpectedly" );
51+ } catch (Exception e ) {
52+ assertTrue (e .getMessage ().equals (curveName + " curve is not supported in FIPS for calculating the shared secret" ));
53+ }
54+ }
55+
56+ private KeyPair genECKeyPair (String curveName ) throws Exception {
57+ KeyPairGenerator kpg = KeyPairGenerator .getInstance ("EC" , getProviderName ());
58+ kpg .initialize (new ECGenParameterSpec (curveName ));
59+ KeyPair kp = kpg .generateKeyPair ();
60+ return kp ;
61+ }
62+
63+ private byte [] ecdhSharedSecretComputation (PrivateKey priv , PublicKey peerPub ) throws Exception {
64+ KeyAgreement ka = KeyAgreement .getInstance ("ECDH" , getProviderName ());
65+ ka .init (priv );
66+ ka .doPhase (peerPub , true );
67+ return ka .generateSecret ();
68+ }
69+
2470}
0 commit comments