Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/ec/TestECDSA2.java
38855 views
/*1* Copyright (c) 2012, 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 basic test of ECDSA signatures for P-256 and P-384 from the27* example data in "Suite B Implementer's Guide to FIPS 186-3".28* @library ..29* @library ../../../../java/security/testlibrary30* @compile -XDignore.symbol.file TestECDSA2.java31* @run main/othervm TestECDSA232* @run main/othervm TestECDSA2 sm33*/3435import java.math.BigInteger;36import java.security.AlgorithmParameters;37import java.security.KeyFactory;38import java.security.KeyPair;39import java.security.PrivateKey;40import java.security.Provider;41import java.security.PublicKey;42import java.security.Signature;43import java.security.spec.ECGenParameterSpec;44import java.security.spec.ECParameterSpec;45import java.security.spec.ECPoint;46import java.security.spec.ECPrivateKeySpec;47import java.security.spec.ECPublicKeySpec;4849public class TestECDSA2 extends PKCS11Test {5051// values of the keys we use for the tests5253// keypair using NIST P-25654private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";55private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";56private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";5758// keypair using NIST P-38459private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";60private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";61private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";6263// data to be signed64private final static byte[] data = "This is only a test message. It is 48 bytes long".getBytes();6566private KeyFactory kf = null;6768private static void testSignAndVerify(String alg, KeyPair kp, Provider p) throws Exception {69Signature s = Signature.getInstance(alg, p);70s.initSign(kp.getPrivate());71s.update(data);72byte[] result = s.sign();7374s.initVerify(kp.getPublic());75s.update(data);76if (!s.verify(result)) {77throw new Exception("Error: Signature verification failed");78}79System.out.println(p.getName() + ": " + alg + " Passed");80}8182private KeyPair genECKeyPair(String curvName, String privD, String pubX,83String pubY, Provider p) throws Exception {84AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p);85params.init(new ECGenParameterSpec(curvName));86ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class);87ECPrivateKeySpec privKeySpec =88new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);89ECPublicKeySpec pubKeySpec =90new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)),91ecParams);92PrivateKey privKey = kf.generatePrivate(privKeySpec);93PublicKey pubKey = kf.generatePublic(pubKeySpec);94return new KeyPair(pubKey, privKey);95}9697public static void main(String[] args) throws Exception {98main(new TestECDSA2(), args);99}100101@Override102public void main(Provider provider) throws Exception {103boolean testP256 =104(provider.getService("Signature", "SHA256withECDSA") != null);105106boolean testP384 =107(provider.getService("Signature", "SHA384withECDSA") != null);108109if (!testP256 && !testP384) {110System.out.println("ECDSA not supported, skipping");111return;112}113114if (isBadNSSVersion(provider)) {115return;116}117118kf = KeyFactory.getInstance("EC", provider);119120long start = System.currentTimeMillis();121if (testP256) {122// can use secp256r1, NIST P-256, X9.62 prime256v1, or 1.2.840.10045.3.1.7123KeyPair kp =124genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider);125testSignAndVerify("SHA256withECDSA", kp, provider);126}127if (testP384) {128// can use secp384r1, NIST P-384, 1.3.132.0.34129KeyPair kp =130genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider);131testSignAndVerify("SHA384withECDSA", kp, provider);132}133long stop = System.currentTimeMillis();134System.out.println("All tests passed (" + (stop - start) + " ms).");135}136}137138139