Path: blob/master/test/jdk/java/security/KeyAgreement/KeyAgreementTest.java
41149 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4936763 8184359 8205476 822630726* @summary KeyAgreement Test with all supported algorithms from JCE.27* Arguments order <KeyExchangeAlgorithm> <KeyGenAlgorithm> <Provider>28* It removes com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java29* as the same functionality for DiffieHellman is covered along with30* this test file was covered before with JDK-4936763.31* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true KeyAgreementTest32* DiffieHellman DH SunJCE33* @run main KeyAgreementTest ECDH EC SunEC34* @run main KeyAgreementTest XDH XDH SunEC35*/36import java.security.KeyPair;37import java.security.KeyPairGenerator;38import java.security.spec.NamedParameterSpec;39import java.security.spec.AlgorithmParameterSpec;40import java.security.spec.ECGenParameterSpec;41import java.util.ArrayList;42import java.util.Arrays;43import java.util.List;44import javax.crypto.KeyAgreement;45import javax.crypto.spec.DHGenParameterSpec;4647public class KeyAgreementTest {4849public static void main(String[] args) throws Exception {5051String kaAlgo = args[0];52String kpgAlgo = args[1];53String provider = args[2];54System.out.println("Testing " + kaAlgo);55AlgoSpec aSpec = AlgoSpec.valueOf(AlgoSpec.class, kaAlgo);56List<AlgorithmParameterSpec> specs = aSpec.getAlgorithmParameterSpecs();57for (AlgorithmParameterSpec spec : specs) {58testKeyAgreement(provider, kaAlgo, kpgAlgo, spec);59}60}6162/**63* Generate AlgorithmParameterSpec using all possible supported curve for64* KeyExchangeAlgorithm.65*/66private enum AlgoSpec {67// EC curve supported for KeyGeneration can found between intersection68// of curves define in69// "java.base/share/classes/sun/security/util/CurveDB.java"7071ECDH("secp256r1", "secp384r1", "secp521r1"),72XDH("X25519", "X448", "x25519"),73// There is no curve for DiffieHellman74DiffieHellman(new String[]{});7576private final List<AlgorithmParameterSpec> specs = new ArrayList<>();7778private AlgoSpec(String... curves) {79// Generate AlgorithmParameterSpec for each KeyExchangeAlgorithm80for (String crv : curves) {81switch (this.name()) {82case "ECDH":83specs.add(new ECGenParameterSpec(crv));84break;85case "XDH":86specs.add(new NamedParameterSpec(crv));87break;88case "DiffieHellman":89specs.add(new DHGenParameterSpec(512, 64));90break;91default:92throw new RuntimeException("Invalid Algo name "93+ this.name());94}95}96}9798public List<AlgorithmParameterSpec> getAlgorithmParameterSpecs() {99return this.specs;100}101}102103/**104* Perform KeyAgreement operation105*/106private static void testKeyAgreement(String provider, String kaAlgo,107String kpgAlgo, AlgorithmParameterSpec spec) throws Exception {108109KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider);110kpg.initialize(spec);111if (spec instanceof ECGenParameterSpec) {112System.out.println("Testing curve: " +113((ECGenParameterSpec)spec).getName());114} else if (spec instanceof NamedParameterSpec) {115System.out.println("Testing curve: " +116((NamedParameterSpec)spec).getName());117}118KeyPair kp1 = kpg.generateKeyPair();119KeyPair kp2 = kpg.generateKeyPair();120121// Uses KeyAgreement based on Provider search order.122KeyAgreement ka1 = KeyAgreement.getInstance(kaAlgo);123ka1.init(kp1.getPrivate());124ka1.doPhase(kp2.getPublic(), true);125byte[] secret1 = ka1.generateSecret();126127// Uses SunJCE provider128KeyAgreement ka2 = KeyAgreement.getInstance(kaAlgo, provider);129ka2.init(kp2.getPrivate());130ka2.doPhase(kp1.getPublic(), true);131// Keeping the legacy generateSecret method for DiffieHellman as it was132// defined in removed Test file from JDK-4936763,133// com/sun/crypto/provider/KeyAgreement/DHGenSecretKey.java.134byte[] secret2 = "DiffieHellman".equals(kaAlgo)135? ka2.generateSecret("AES").getEncoded() : ka2.generateSecret();136137// With related keypairs, each provider should generate same138// KeyAgreement secret.139if (!Arrays.equals(secret1, secret2)) {140throw new Exception("KeyAgreement secret mismatch.");141}142143// ensure that a new secret cannot be produced before the next doPhase144try {145ka2.generateSecret();146throw new RuntimeException("state not reset");147} catch (IllegalStateException ex) {148// this is expected149}150151// calling doPhase and then generateSecret should succeed152ka2.doPhase(kp1.getPublic(), true);153ka2.generateSecret();154}155}156157158