Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/SameDHKeyStressTest.java
41155 views
/*1* Copyright (c) 1999, 2017, 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 804881926* @summary This test stressful verifies the assertion of "The secret keys generated27* by all involved parties should be the same." for javax.crypto.KeyAgreement28* @run main/othervm -Djdk.crypto.KeyAgreement.legacyKDF=true SameDHKeyStressTest29*/30import java.security.AlgorithmParameterGenerator;31import java.security.InvalidAlgorithmParameterException;32import java.security.InvalidKeyException;33import java.security.Key;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.NoSuchAlgorithmException;37import java.security.NoSuchProviderException;38import java.security.spec.AlgorithmParameterSpec;39import java.util.Arrays;40import javax.crypto.KeyAgreement;41import javax.crypto.SecretKey;42import javax.crypto.spec.DHGenParameterSpec;43import javax.crypto.spec.DHParameterSpec;4445public class SameDHKeyStressTest {4647static final String[] ALGORITHMS = {"DH", "DiffieHellman", "dh", "diffieHELLMAN"};48static final String[] SECRET_ALOGRITHMS = {"DES", "DESede", "blowfish"};49static final int[] NUMBER_OF_PARTIES = {2, 3, 4};50static final String[] PA_NAMES = {"Alice", "Bob", "Carol", "David"};5152public static void main(String args[]) {53int failedCnt = 0;54StringBuilder failedList = new StringBuilder("Failed List:");5556for (String algorithm : ALGORITHMS) {57for (int numOfParties : NUMBER_OF_PARTIES) {58for (String secretAlgorithm : SECRET_ALOGRITHMS) {59if (!runTest(algorithm, numOfParties, secretAlgorithm)) {60failedCnt++;61failedList.append("\n Altorightm = ").append(algorithm).62append(" Number of Parties = ").append(numOfParties).63append(" Secret Algorithm = ").append(secretAlgorithm);64}65}66}67} //end of for loop6869if (failedCnt > 0) {70System.out.println(failedList);71throw new RuntimeException("SameDHKeyStressTest Failed");72}73}7475public static boolean runTest(String algo, int numParties, String secretAlgo) {76KAParticipant[] parties = new KAParticipant[numParties];77Key[] keyArchives = new Key[numParties];78try {79// generate AlogirhtmParameterSpec80AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH","SunJCE");81AlgorithmParameterSpec aps = new DHGenParameterSpec(512, 64);82apg.init(aps);83DHParameterSpec spec = apg.generateParameters().84getParameterSpec(DHParameterSpec.class);8586//initilize all KeyAgreement participants87for (int i = 0; i < numParties; i++) {88parties[i] = new KAParticipant(PA_NAMES[i], algo);89parties[i].initialize(spec);90keyArchives[i] = parties[i].getPublicKey();91}9293// Do all phases in the KeyAgreement for all participants94Key[] keyBuffer = new Key[numParties];95boolean lastPhase = false;96for (int j = 0; j < numParties - 1; j++) {97if (j == numParties - 2) {98lastPhase = true;99}100for (int k = 0; k < numParties; k++) {101if (k == numParties - 1) {102keyBuffer[k] = parties[k].doPhase(keyArchives[0], lastPhase);103} else {104keyBuffer[k] = parties[k].doPhase(keyArchives[k + 1], lastPhase);105}106}107System.arraycopy(keyBuffer, 0, keyArchives, 0, numParties);108}109110//Comparison: The secret keys generated by all involved parties should be the same111SecretKey[] sKeys = new SecretKey[numParties];112for (int n = 0; n < numParties; n++) {113sKeys[n] = parties[n].generateSecret(secretAlgo);114}115for (int q = 0; q < numParties - 1; q++) {116if (!Arrays.equals(sKeys[q].getEncoded(), sKeys[q + 1].getEncoded())) {117return false;118}119}120return true;121} catch (Exception ex) {122ex.printStackTrace();123return false;124}125126}127128}129130class KAParticipant {131132private String name = null;133private String algorithm = null;134private KeyPairGenerator keyGen = null;135private KeyPair keys = null;136private KeyAgreement ka = null;137138public KAParticipant(String pName, String algo) throws NoSuchAlgorithmException, NoSuchProviderException {139name = pName;140algorithm = algo;141keyGen = KeyPairGenerator.getInstance(algo,"SunJCE");142ka = KeyAgreement.getInstance(algo,"SunJCE");143}144145public void initialize(AlgorithmParameterSpec spec) throws InvalidAlgorithmParameterException, InvalidKeyException {146keyGen.initialize(spec);147keys = keyGen.generateKeyPair();148ka.init(keys.getPrivate());149}150151public Key doPhase(Key key, boolean lastPhase) throws InvalidKeyException {152return ka.doPhase(key, lastPhase);153}154155public Key getPublicKey() {156return keys.getPublic();157}158159public byte[] generateSecret() {160return ka.generateSecret();161}162163public SecretKey generateSecret(String algo) throws java.lang.IllegalStateException,164java.security.NoSuchAlgorithmException,165java.security.InvalidKeyException {166return ka.generateSecret(algo);167}168}169170171