Path: blob/master/test/jdk/sun/security/pkcs11/ec/TestECGenSpec.java
41153 views
/*1* Copyright (c) 2006, 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 640553626* @summary Verify that we can use ECGenParameterSpec27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestECGenSpec31* @run main/othervm -Djava.security.manager=allow TestECGenSpec sm32*/3334import java.security.AlgorithmParameters;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;38import java.security.interfaces.ECPublicKey;39import java.security.spec.ECGenParameterSpec;40import java.security.spec.ECParameterSpec;4142public class TestECGenSpec extends PKCS11Test {4344public static void main(String[] args) throws Exception {45main(new TestECGenSpec(), args);46}4748@Override49protected boolean skipTest(Provider p) {50if (p.getService("Signature", "SHA1withECDSA") == null) {51System.out.println("Provider does not support ECDSA, skipping...");52return true;53}5455if (isBadNSSVersion(p)) {56return true;57}5859return false;60}6162@Override63public void main(Provider p) throws Exception {64String[] names = { "secp256r1", "NIST P-192", "sect163k1", "1.3.132.0.26",65"X9.62 c2tnb239v1"};66int curves = 1;67if (getNSSECC() == ECCState.Extended) {68curves = names.length;69}70int[] lengths = {256, 192, 163, 233, 239};71for (int i = 0; i < curves; i++) {72String name = names[i];73int len = lengths[i];74System.out.println("Testing " + name + "...");75ECGenParameterSpec spec = new ECGenParameterSpec(name);7677AlgorithmParameters algParams = AlgorithmParameters.getInstance("EC", p);78algParams.init(spec);79ECParameterSpec ecSpec = algParams.getParameterSpec(ECParameterSpec.class);80System.out.println(ecSpec);81// no public API to get the curve name, so rely on toString();82if (ecSpec.toString().contains(name) == false) {83throw new Exception("wrong curve");84}8586algParams = AlgorithmParameters.getInstance("EC", p);87algParams.init(ecSpec);88ECGenParameterSpec genSpec = algParams.getParameterSpec(ECGenParameterSpec.class);89System.out.println(genSpec.getName());9091KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);92kpg.initialize(spec);93KeyPair kp = kpg.generateKeyPair();94System.out.println(kp.getPrivate());95ECPublicKey publicKey = (ECPublicKey)kp.getPublic();96if (publicKey.getParams().getCurve().getField().getFieldSize() != len) {97throw new Exception("wrong curve");98}99System.out.println();100}101}102103}104105106