Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestECGenSpec.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 640553626* @summary Verify that we can use ECGenParameterSpec27* @author Andreas Sterbenz28* @library ..29* @run main/othervm TestECGenSpec30* @run main/othervm TestECGenSpec sm31*/3233import java.security.AlgorithmParameters;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.Provider;37import java.security.interfaces.ECPublicKey;38import java.security.spec.ECGenParameterSpec;39import java.security.spec.ECParameterSpec;4041public class TestECGenSpec extends PKCS11Test {4243public static void main(String[] args) throws Exception {44main(new TestECGenSpec(), args);45}4647@Override48public void main(Provider p) throws Exception {49if (p.getService("Signature", "SHA1withECDSA") == null) {50System.out.println("Provider does not support ECDSA, skipping...");51return;52}5354if (isBadNSSVersion(p)) {55return;56}5758String[] names = { "secp256r1", "NIST P-192", "sect163k1", "1.3.132.0.26",59"X9.62 c2tnb239v1"};60int curves = 1;61if (getNSSECC() == ECCState.Extended) {62curves = names.length;63}64int[] lengths = {256, 192, 163, 233, 239};65for (int i = 0; i < curves; i++) {66String name = names[i];67int len = lengths[i];68System.out.println("Testing " + name + "...");69ECGenParameterSpec spec = new ECGenParameterSpec(name);7071AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC", p);72algParams.init(spec);73ECParameterSpec ecSpec = algParams.getParameterSpec(ECParameterSpec.class);74System.out.println(ecSpec);75// no public API to get the curve name, so rely on toString();76if (ecSpec.toString().contains(name) == false) {77throw new Exception("wrong curve");78}7980algParams = AlgorithmParameters.getInstance("EC", p);81algParams.init(ecSpec);82ECGenParameterSpec genSpec = algParams.getParameterSpec(ECGenParameterSpec.class);83System.out.println(genSpec.getName());8485KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);86kpg.initialize(spec);87KeyPair kp = kpg.generateKeyPair();88System.out.println(kp.getPrivate());89ECPublicKey publicKey = (ECPublicKey)kp.getPublic();90if (publicKey.getParams().getCurve().getField().getFieldSize() != len) {91throw new Exception("wrong curve");92}93System.out.println();94}95}9697}9899100