Path: blob/master/test/jdk/java/security/KeyAgreement/MultiThreadTest.java
41149 views
/*1* Copyright (c) 2018, 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 818435926* @summary KeyPairGenerator Test with multiple threads.27* Arguments order <KeyExchangeAlgorithm> <Provider> <KeyGenAlgorithm> <Curve*>28* @run main MultiThreadTest DiffieHellman SunJCE DiffieHellman29* @run main MultiThreadTest ECDH SunEC EC30* @run main MultiThreadTest XDH SunEC XDH X2551931* @run main MultiThreadTest XDH SunEC XDH X44832*/33import java.security.KeyPair;34import java.security.KeyPairGenerator;35import java.util.Arrays;36import java.util.concurrent.CountDownLatch;37import java.util.concurrent.ExecutorService;38import java.util.concurrent.Executors;39import java.util.concurrent.ThreadFactory;40import javax.crypto.KeyAgreement;4142/**43* This test targets KeyPairGenerator API related issue in a multi threaded44* context.45*/46public class MultiThreadTest {4748// Tested a shared KeyPairGenerator with 100 number of threads.49private static final int THREAD_COUNT = 100;5051public static void main(String[] args) throws Exception {5253String kaAlgo = args[0];54String provider = args[1];55String kpgAlgo = args[2];56KeyPairGenerator kpg = genKeyGenerator(provider, kpgAlgo,57(args.length > 3) ? args[3] : kpgAlgo);58new MultiThreadTest().runTest(provider, kaAlgo, kpg);59}6061/**62* Initialize KeyPairGenerator based on different algorithm names.63*/64private static KeyPairGenerator genKeyGenerator(String provider,65String kpgAlgo, String kpgInit) throws Exception {6667KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider);68switch (kpgInit) {69case "DiffieHellman":70kpg.initialize(512);71break;72case "EC":73kpg.initialize(256);74break;75case "X25519":76kpg.initialize(255);77break;78case "X448":79kpg.initialize(448);80break;81default:82throw new RuntimeException("Invalid Algo name " + kpgInit);83}84return kpg;85}8687private void runTest(String provider, String kaAlgo, KeyPairGenerator kpg)88throws Exception {8990ExecutorService executor = null;91try {92executor = Executors.newCachedThreadPool(new ThreadFactory() {93@Override94public Thread newThread(Runnable r) {95Thread t = Executors.defaultThreadFactory().newThread(r);96t.setDaemon(true);97return t;98}99});100CountDownLatch latch = new CountDownLatch(THREAD_COUNT);101102for (int i = 0; i < THREAD_COUNT; i++) {103executor.execute(new Runnable() {104@Override105public void run() {106try {107testKeyAgreement(provider, kaAlgo, kpg);108} catch (Exception e) {109throw new RuntimeException(e);110} finally {111// Indicate a task completed.112latch.countDown();113}114}115});116}117// Wait till all tasks get complete.118latch.await();119} finally {120if (executor != null) {121executor.shutdown();122}123}124}125126/**127* Perform KeyAgreement operation with a shared KeyPairGenerator instance.128*/129private static void testKeyAgreement(String provider, String kaAlgo,130KeyPairGenerator kpg) throws Exception {131132KeyPair kp1 = kpg.generateKeyPair();133KeyPair kp2 = kpg.generateKeyPair();134135KeyAgreement ka1 = KeyAgreement.getInstance(kaAlgo, provider);136ka1.init(kp1.getPrivate());137ka1.doPhase(kp2.getPublic(), true);138byte[] secret1 = ka1.generateSecret();139KeyAgreement ka2 = KeyAgreement.getInstance(kaAlgo, provider);140ka2.init(kp2.getPrivate());141ka2.doPhase(kp1.getPublic(), true);142byte[] secret2 = ka2.generateSecret();143144// With related keypairs, generated KeyAgreement secret should be same.145if (!Arrays.equals(secret1, secret2)) {146throw new Exception("KeyAgreement secret mismatch.");147}148}149}150151152