1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6405536
  27  * @summary basic test of ECDSA signatures for P-256 and P-384 from the
  28  * example data in "Suite B Implementer's Guide to FIPS 186-3".
  29  * @library ..
  30  * @library ../../../../java/security/testlibrary
  31  * @modules java.base/sun.security.util
  32  *          jdk.crypto.token
  33  * @compile -XDignore.symbol.file TestECDH2.java
  34  * @run main/othervm TestECDH2
  35  * @run main/othervm TestECDH2 sm
  36  */
  37 
  38 import java.math.BigInteger;
  39 import java.security.AlgorithmParameters;
  40 import java.security.KeyFactory;
  41 import java.security.KeyPair;
  42 import java.security.KeyPairGenerator;
  43 import java.security.PrivateKey;
  44 import java.security.Provider;
  45 import java.security.PublicKey;
  46 import java.security.spec.ECGenParameterSpec;
  47 import java.security.spec.ECParameterSpec;
  48 import java.security.spec.ECPoint;
  49 import java.security.spec.ECPrivateKeySpec;
  50 import java.security.spec.ECPublicKeySpec;
  51 import java.util.Arrays;
  52 import javax.crypto.KeyAgreement;
  53 
  54 public class TestECDH2 extends PKCS11Test {
  55 
  56     // values of the keys we use for the tests
  57 
  58     // keypair using NIST P-256
  59     private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";
  60     private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";
  61     private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";
  62 
  63     // keypair using NIST P-384
  64     private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";
  65     private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";
  66     private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";
  67 
  68     private KeyFactory kf = null;
  69     private KeyPairGenerator kpg = null;
  70 
  71     private static void testKeyAgreement(KeyPair kpA, KeyPair kpB, Provider p)
  72         throws Exception {
  73         KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
  74         ka1.init(kpA.getPrivate());
  75         ka1.doPhase(kpB.getPublic(), true);
  76         byte[] s1 = ka1.generateSecret();
  77 
  78         KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
  79         ka2.init(kpB.getPrivate());
  80         ka2.doPhase(kpA.getPublic(), true);
  81         byte[] s2 = ka2.generateSecret();
  82         if (Arrays.equals(s1, s2) == false) {
  83             System.out.println("expected: " + toString(s1));
  84             System.out.println("actual:   " + toString(s2));
  85             throw new Exception("Generated secrets do not match");
  86         }
  87     }
  88 
  89     private KeyPair genECKeyPair(String curvName, String privD, String pubX,
  90                                  String pubY, Provider p) throws Exception {
  91         AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p);
  92         params.init(new ECGenParameterSpec(curvName));
  93         ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class);
  94         ECPrivateKeySpec privKeySpec =
  95             new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
  96         ECPublicKeySpec pubKeySpec =
  97             new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
  98                                             new BigInteger(pubY, 16)),
  99                                 ecParams);
 100         PrivateKey privKey = kf.generatePrivate(privKeySpec);
 101         PublicKey pubKey = kf.generatePublic(pubKeySpec);
 102         return new KeyPair(pubKey, privKey);
 103     }
 104     private KeyPair genECKeyPair(String curvName) throws Exception {
 105         ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);
 106         kpg.initialize(genParams, null);
 107         return kpg.generateKeyPair();
 108     }
 109     public static void main(String[] args) throws Exception {
 110         main(new TestECDH2(), args);
 111     }
 112 
 113     @Override
 114     public void main(Provider provider) throws Exception {
 115         if (provider.getService("KeyAgreement", "ECDH") == null) {
 116             System.out.println("ECDH not supported, skipping");
 117             return;
 118         }
 119 
 120         if (isBadNSSVersion(provider)) {
 121             return;
 122         }
 123 
 124         kf = KeyFactory.getInstance("EC", provider);
 125         kpg = KeyPairGenerator.getInstance("EC", provider);
 126 
 127         System.out.println("Testing against NIST P-256");
 128 
 129         long start = System.currentTimeMillis();
 130         KeyPair kp256A =
 131             genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider);
 132         KeyPair kp256B = genECKeyPair("secp256r1");
 133         testKeyAgreement(kp256A, kp256B, provider);
 134 
 135         System.out.println("Testing against NIST P-384");
 136         KeyPair kp384A =
 137             genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider);
 138         KeyPair kp384B = genECKeyPair("secp384r1");
 139         testKeyAgreement(kp384A, kp384B, provider);
 140 
 141         long stop = System.currentTimeMillis();
 142         System.out.println("All tests passed (" + (stop - start) + " ms).");
 143     }
 144 }
--- EOF ---