jdk/test/sun/security/pkcs11/ec/TestECDH2.java
changeset 35379 1e8e336ef66b
parent 30820 0d4717a011d3
child 40975 680639c9b307
equal deleted inserted replaced
35378:7e19fa0e4e5b 35379:1e8e336ef66b
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    28  * example data in "Suite B Implementer's Guide to FIPS 186-3".
    28  * example data in "Suite B Implementer's Guide to FIPS 186-3".
    29  * @library ..
    29  * @library ..
    30  * @library ../../../../java/security/testlibrary
    30  * @library ../../../../java/security/testlibrary
    31  * @modules java.base/sun.security.util
    31  * @modules java.base/sun.security.util
    32  * @compile -XDignore.symbol.file TestECDH2.java
    32  * @compile -XDignore.symbol.file TestECDH2.java
    33  * @run main TestECDH2
    33  * @run main/othervm TestECDH2
       
    34  * @run main/othervm TestECDH2 sm
    34  */
    35  */
    35 
    36 
    36 import java.io.*;
       
    37 import java.util.*;
       
    38 import java.math.BigInteger;
    37 import java.math.BigInteger;
    39 
    38 import java.security.AlgorithmParameters;
    40 import java.security.*;
    39 import java.security.KeyFactory;
    41 import java.security.spec.*;
    40 import java.security.KeyPair;
    42 import java.security.interfaces.*;
    41 import java.security.KeyPairGenerator;
    43 import javax.crypto.*;
    42 import java.security.PrivateKey;
    44 
    43 import java.security.Provider;
    45 import sun.security.util.ECUtil;
    44 import java.security.PublicKey;
       
    45 import java.security.spec.ECGenParameterSpec;
       
    46 import java.security.spec.ECParameterSpec;
       
    47 import java.security.spec.ECPoint;
       
    48 import java.security.spec.ECPrivateKeySpec;
       
    49 import java.security.spec.ECPublicKeySpec;
       
    50 import java.util.Arrays;
       
    51 import javax.crypto.KeyAgreement;
    46 
    52 
    47 public class TestECDH2 extends PKCS11Test {
    53 public class TestECDH2 extends PKCS11Test {
    48 
    54 
    49     // values of the keys we use for the tests
    55     // values of the keys we use for the tests
    50 
    56 
    79         }
    85         }
    80     }
    86     }
    81 
    87 
    82     private KeyPair genECKeyPair(String curvName, String privD, String pubX,
    88     private KeyPair genECKeyPair(String curvName, String privD, String pubX,
    83                                  String pubY, Provider p) throws Exception {
    89                                  String pubY, Provider p) throws Exception {
    84         ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
    90         AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p);
       
    91         params.init(new ECGenParameterSpec(curvName));
       
    92         ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class);
    85         ECPrivateKeySpec privKeySpec =
    93         ECPrivateKeySpec privKeySpec =
    86             new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    94             new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
    87         ECPublicKeySpec pubKeySpec =
    95         ECPublicKeySpec pubKeySpec =
    88             new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
    96             new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
    89                                             new BigInteger(pubY, 16)),
    97                                             new BigInteger(pubY, 16)),
    96         ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);
   104         ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);
    97         kpg.initialize(genParams, null);
   105         kpg.initialize(genParams, null);
    98         return kpg.generateKeyPair();
   106         return kpg.generateKeyPair();
    99     }
   107     }
   100     public static void main(String[] args) throws Exception {
   108     public static void main(String[] args) throws Exception {
   101         main(new TestECDH2());
   109         main(new TestECDH2(), args);
   102     }
   110     }
   103 
   111 
       
   112     @Override
   104     public void main(Provider provider) throws Exception {
   113     public void main(Provider provider) throws Exception {
   105         if (provider.getService("KeyAgreement", "ECDH") == null) {
   114         if (provider.getService("KeyAgreement", "ECDH") == null) {
   106             System.out.println("ECDH not supported, skipping");
   115             System.out.println("ECDH not supported, skipping");
   107             return;
   116             return;
   108         }
   117         }
   109 
   118 
   110         if (isNSS(provider) && getNSSVersion() >= 3.11 &&
   119         if (isBadNSSVersion(provider)) {
   111                 getNSSVersion() < 3.12) {
       
   112             System.out.println("NSS 3.11 has a DER issue that recent " +
       
   113                     "version do not.");
       
   114             return;
   120             return;
   115         }
   121         }
   116 
   122 
   117         kf = KeyFactory.getInstance("EC", provider);
   123         kf = KeyFactory.getInstance("EC", provider);
   118         kpg = KeyPairGenerator.getInstance("EC", provider);
   124         kpg = KeyPairGenerator.getInstance("EC", provider);