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 TestECDSA2.java |
32 * @compile -XDignore.symbol.file TestECDSA2.java |
33 * @run main TestECDSA2 |
33 * @run main/othervm TestECDSA2 |
|
34 * @run main/othervm TestECDSA2 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.PrivateKey; |
43 |
42 import java.security.Provider; |
44 import sun.security.util.ECUtil; |
43 import java.security.PublicKey; |
|
44 import java.security.Signature; |
|
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; |
45 |
50 |
46 public class TestECDSA2 extends PKCS11Test { |
51 public class TestECDSA2 extends PKCS11Test { |
47 |
52 |
48 // values of the keys we use for the tests |
53 // values of the keys we use for the tests |
49 |
54 |
76 System.out.println(p.getName() + ": " + alg + " Passed"); |
81 System.out.println(p.getName() + ": " + alg + " Passed"); |
77 } |
82 } |
78 |
83 |
79 private KeyPair genECKeyPair(String curvName, String privD, String pubX, |
84 private KeyPair genECKeyPair(String curvName, String privD, String pubX, |
80 String pubY, Provider p) throws Exception { |
85 String pubY, Provider p) throws Exception { |
81 ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName); |
86 AlgorithmParameters params = AlgorithmParameters.getInstance("EC", p); |
|
87 params.init(new ECGenParameterSpec(curvName)); |
|
88 ECParameterSpec ecParams = params.getParameterSpec(ECParameterSpec.class); |
82 ECPrivateKeySpec privKeySpec = |
89 ECPrivateKeySpec privKeySpec = |
83 new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams); |
90 new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams); |
84 ECPublicKeySpec pubKeySpec = |
91 ECPublicKeySpec pubKeySpec = |
85 new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)), |
92 new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)), |
86 ecParams); |
93 ecParams); |
88 PublicKey pubKey = kf.generatePublic(pubKeySpec); |
95 PublicKey pubKey = kf.generatePublic(pubKeySpec); |
89 return new KeyPair(pubKey, privKey); |
96 return new KeyPair(pubKey, privKey); |
90 } |
97 } |
91 |
98 |
92 public static void main(String[] args) throws Exception { |
99 public static void main(String[] args) throws Exception { |
93 main(new TestECDSA2()); |
100 main(new TestECDSA2(), args); |
94 } |
101 } |
95 |
102 |
|
103 @Override |
96 public void main(Provider provider) throws Exception { |
104 public void main(Provider provider) throws Exception { |
97 boolean testP256 = |
105 boolean testP256 = |
98 (provider.getService("Signature", "SHA256withECDSA") != null); |
106 (provider.getService("Signature", "SHA256withECDSA") != null); |
99 |
107 |
100 boolean testP384 = |
108 boolean testP384 = |
103 if (!testP256 && !testP384) { |
111 if (!testP256 && !testP384) { |
104 System.out.println("ECDSA not supported, skipping"); |
112 System.out.println("ECDSA not supported, skipping"); |
105 return; |
113 return; |
106 } |
114 } |
107 |
115 |
108 if (isNSS(provider) && getNSSVersion() >= 3.11 && |
116 if (isBadNSSVersion(provider)) { |
109 getNSSVersion() < 3.12) { |
|
110 System.out.println("NSS 3.11 has a DER issue that recent " + |
|
111 "version do not."); |
|
112 return; |
117 return; |
113 } |
118 } |
114 |
119 |
115 kf = KeyFactory.getInstance("EC", provider); |
120 kf = KeyFactory.getInstance("EC", provider); |
116 |
121 |