1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4936763 8184359 27 * @summary KeyAgreement Test with all supported algorithms from JCE. 28 * Arguments order <KeyExchangeAlgorithm> <KeyGenAlgorithm> <Provider> 29 * It removes com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java 30 * as the same functionality for DiffieHellman is covered along with 31 * this test file was covered before with JDK-4936763. 32 */ 33 package test.java.security.KeyAgreement; 34 35 import java.security.KeyPair; 36 import java.security.KeyPairGenerator; 37 import java.security.spec.NamedParameterSpec; 38 import java.security.spec.AlgorithmParameterSpec; 39 import java.security.spec.ECGenParameterSpec; 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.List; 43 import javax.crypto.KeyAgreement; 44 import javax.crypto.spec.DHGenParameterSpec; 45 46 import org.testng.annotations.Test; 47 import static org.junit.Assert.fail; 48 49 public class KeyAgreementTest { 50 51 List<String> kaAlgos = Arrays.asList("DiffieHellman", "ECDH"); 52 List<String> kpgAlgos = Arrays.asList("DH", "EC"); 53 List<String> providers = Arrays.asList("BC", "AndroidOpenSSL"); 54 55 @Test testKeyAgreement()56 public void testKeyAgreement() throws Exception { 57 for (int i = 0; i < kaAlgos.size(); i++) { 58 AlgoSpec aSpec = AlgoSpec.valueOf(AlgoSpec.class, kaAlgos.get(i)); 59 List<AlgorithmParameterSpec> specs = aSpec.getAlgorithmParameterSpecs(); 60 for (AlgorithmParameterSpec spec : specs) { 61 testKeyAgreement(providers.get(i), kaAlgos.get(i), kpgAlgos.get(i), spec); 62 } 63 } 64 } 65 66 /** 67 * Generate AlgorithmParameterSpec using all possible supported curve for 68 * KeyExchangeAlgorithm. 69 */ 70 private enum AlgoSpec { 71 // EC curve supported for KeyGeneration can found between intersection 72 // of curves define in 73 // "java.base/share/classes/sun/security/util/CurveDB.java" 74 // and 75 // "jdk.crypto.ec/share/native/libsunec/impl/ecdecode.c" 76 ECDH( "secp224r1", "secp256r1", "secp384r1", "secp521r1" 77 ), 78 XDH("X25519", "X448"), 79 // There is no curve for DiffieHellman 80 DiffieHellman(new String[]{}); 81 82 private final List<AlgorithmParameterSpec> specs = new ArrayList<>(); 83 AlgoSpec(String... curves)84 private AlgoSpec(String... curves) { 85 // Generate AlgorithmParameterSpec for each KeyExchangeAlgorithm 86 for (String crv : curves) { 87 switch (this.name()) { 88 case "ECDH": 89 specs.add(new ECGenParameterSpec(crv)); 90 break; 91 case "XDH": 92 specs.add(new NamedParameterSpec(crv)); 93 break; 94 case "DiffieHellman": 95 specs.add(new DHGenParameterSpec(512, 64)); 96 break; 97 default: 98 fail("Invalid Algo name " 99 + this.name()); 100 } 101 } 102 } 103 getAlgorithmParameterSpecs()104 public List<AlgorithmParameterSpec> getAlgorithmParameterSpecs() { 105 return this.specs; 106 } 107 } 108 109 /** 110 * Perform KeyAgreement operation using native as well as JCE provider. 111 */ testKeyAgreement(String provider, String kaAlgo, String kpgAlgo, AlgorithmParameterSpec spec)112 private static void testKeyAgreement(String provider, String kaAlgo, 113 String kpgAlgo, AlgorithmParameterSpec spec) throws Exception { 114 115 KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider); 116 kpg.initialize(spec); 117 KeyPair kp1 = kpg.generateKeyPair(); 118 KeyPair kp2 = kpg.generateKeyPair(); 119 120 // Uses KeyAgreement based on Provider search order. 121 KeyAgreement ka1 = KeyAgreement.getInstance(kaAlgo); 122 ka1.init(kp1.getPrivate()); 123 ka1.doPhase(kp2.getPublic(), true); 124 byte[] secret1 = ka1.generateSecret(); 125 126 // Uses SunJCE provider 127 KeyAgreement ka2 = KeyAgreement.getInstance(kaAlgo, provider); 128 ka2.init(kp2.getPrivate()); 129 ka2.doPhase(kp1.getPublic(), true); 130 // Keeping the legacy generateSecret method for DiffieHellman as it was 131 // defined in removed Test file from JDK-4936763, 132 // com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java. 133 byte[] secret2 = "DiffieHellman".equals(kaAlgo) 134 ? ka2.generateSecret("AES").getEncoded() : ka2.generateSecret(); 135 136 // With related keypairs, each provider should generate same 137 // KeyAgreement secret. 138 if (!Arrays.equals(secret1, secret2)) { 139 fail("KeyAgreement secret mismatch."); 140 } 141 } 142 }