Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestCurves.java
38855 views
/*1* Copyright (c) 2006, 2016, 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 6405536 641498026* @summary Basic consistency test for all curves using ECDSA and ECDH27* @author Andreas Sterbenz28* @library ..29* @compile -XDignore.symbol.file TestCurves.java30* @run main/othervm TestCurves31* @run main/othervm TestCurves sm32* @key randomness33*/3435import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;38import java.security.ProviderException;39import java.security.Signature;40import java.security.spec.ECParameterSpec;41import java.util.Arrays;42import java.util.List;43import java.util.Random;44import javax.crypto.KeyAgreement;4546public class TestCurves extends PKCS11Test {4748public static void main(String[] args) throws Exception {49main(new TestCurves(), args);50}5152@Override53public void main(Provider p) throws Exception {54if (p.getService("KeyAgreement", "ECDH") == null) {55System.out.println("Not supported by provider, skipping");56return;57}5859if (isBadNSSVersion(p)) {60return;61}6263// Check if this is sparc for later failure avoidance.64boolean sparc = false;65if (props.getProperty("os.arch").equals("sparcv9")) {66sparc = true;67System.out.println("This is a sparcv9");68}6970Random random = new Random();71byte[] data = new byte[2048];72random.nextBytes(data);7374List<ECParameterSpec> curves = getKnownCurves(p);75for (ECParameterSpec params : curves) {76System.out.println("Testing " + params + "...");77KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);78kpg.initialize(params);79KeyPair kp1, kp2;8081kp1 = kpg.generateKeyPair();82kp2 = kpg.generateKeyPair();8384testSigning(p, "SHA1withECDSA", data, kp1, kp2);85// Check because Solaris ncp driver does not support these but86// Solaris metaslot causes them to be run.87try {88testSigning(p, "SHA224withECDSA", data, kp1, kp2);89testSigning(p, "SHA256withECDSA", data, kp1, kp2);90testSigning(p, "SHA384withECDSA", data, kp1, kp2);91testSigning(p, "SHA512withECDSA", data, kp1, kp2);92} catch (ProviderException e) {93if (sparc) {94Throwable t = e.getCause();95if (t instanceof sun.security.pkcs11.wrapper.PKCS11Exception &&96t.getMessage().equals("CKR_ATTRIBUTE_VALUE_INVALID")) {97System.out.print("-Failure not uncommon. Probably pre-T4.");98} else {99throw e;100}101} else {102throw e;103}104}105System.out.println();106107KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);108ka1.init(kp1.getPrivate());109ka1.doPhase(kp2.getPublic(), true);110byte[] secret1 = ka1.generateSecret();111112KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);113ka2.init(kp2.getPrivate());114ka2.doPhase(kp1.getPublic(), true);115byte[] secret2 = ka2.generateSecret();116117if (Arrays.equals(secret1, secret2) == false) {118throw new Exception("Secrets do not match");119}120}121122System.out.println("OK");123}124125private static void testSigning(Provider p, String algorithm,126byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {127System.out.print(" " + algorithm);128Signature s = Signature.getInstance(algorithm, p);129s.initSign(kp1.getPrivate());130s.update(data);131byte[] sig = s.sign();132133s = Signature.getInstance(algorithm, p);134s.initVerify(kp1.getPublic());135s.update(data);136boolean r = s.verify(sig);137if (r == false) {138throw new Exception("Signature did not verify");139}140141s.initVerify(kp2.getPublic());142s.update(data);143r = s.verify(sig);144if (r) {145throw new Exception("Signature should not verify");146}147}148}149150151