Path: blob/master/test/jdk/sun/security/pkcs11/ec/TestECDH2.java
41153 views
/*1* Copyright (c) 2012, 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 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 /test/lib ..29* @library ../../../../java/security/testlibrary30* @modules java.base/sun.security.util31* jdk.crypto.cryptoki32* @compile -XDignore.symbol.file TestECDH2.java33* @run main/othervm TestECDH234* @run main/othervm -Djava.security.manager=allow TestECDH2 sm35*/3637import java.math.BigInteger;38import java.security.AlgorithmParameters;39import java.security.KeyFactory;40import java.security.KeyPair;41import java.security.KeyPairGenerator;42import java.security.PrivateKey;43import java.security.Provider;44import java.security.PublicKey;45import java.security.spec.ECGenParameterSpec;46import java.security.spec.ECParameterSpec;47import java.security.spec.ECPoint;48import java.security.spec.ECPrivateKeySpec;49import java.security.spec.ECPublicKeySpec;50import java.util.Arrays;51import javax.crypto.KeyAgreement;5253public class TestECDH2 extends PKCS11Test {5455// values of the keys we use for the tests5657// keypair using NIST P-25658private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";59private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";60private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";6162// keypair using NIST P-38463private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";64private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";65private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";6667private KeyFactory kf = null;68private KeyPairGenerator kpg = null;6970private static void testKeyAgreement(KeyPair kpA, KeyPair kpB, Provider p)71throws Exception {72KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);73ka1.init(kpA.getPrivate());74ka1.doPhase(kpB.getPublic(), true);75byte[] s1 = ka1.generateSecret();7677KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);78ka2.init(kpB.getPrivate());79ka2.doPhase(kpA.getPublic(), true);80byte[] s2 = ka2.generateSecret();81if (Arrays.equals(s1, s2) == false) {82System.out.println("expected: " + toString(s1));83System.out.println("actual: " + toString(s2));84throw new Exception("Generated secrets do not match");85}86}8788private KeyPair genECKeyPair(String curvName, String privD, String pubX,89String pubY, Provider p) throws Exception {90AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p);91params.init(new ECGenParameterSpec(curvName));92ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class);93ECPrivateKeySpec privKeySpec =94new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);95ECPublicKeySpec pubKeySpec =96new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),97new BigInteger(pubY, 16)),98ecParams);99PrivateKey privKey = kf.generatePrivate(privKeySpec);100PublicKey pubKey = kf.generatePublic(pubKeySpec);101return new KeyPair(pubKey, privKey);102}103private KeyPair genECKeyPair(String curvName) throws Exception {104ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);105kpg.initialize(genParams, null);106return kpg.generateKeyPair();107}108public static void main(String[] args) throws Exception {109main(new TestECDH2(), args);110}111112@Override113protected boolean skipTest(Provider provider) {114if (provider.getService("KeyAgreement", "ECDH") == null) {115System.out.println("ECDH not supported, skipping");116return true;117}118119if (isBadNSSVersion(provider)) {120return true;121}122123return false;124}125126@Override127public void main(Provider provider) throws Exception {128kf = KeyFactory.getInstance("EC", provider);129kpg = KeyPairGenerator.getInstance("EC", provider);130131System.out.println("Testing against NIST P-256");132133long start = System.currentTimeMillis();134KeyPair kp256A =135genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider);136KeyPair kp256B = genECKeyPair("secp256r1");137testKeyAgreement(kp256A, kp256B, provider);138139System.out.println("Testing against NIST P-384");140KeyPair kp384A =141genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider);142KeyPair kp384B = genECKeyPair("secp384r1");143testKeyAgreement(kp384A, kp384B, provider);144145long stop = System.currentTimeMillis();146System.out.println("All tests passed (" + (stop - start) + " ms).");147}148}149150151