38 |
38 |
39 import java.security.*; |
39 import java.security.*; |
40 import java.security.spec.*; |
40 import java.security.spec.*; |
41 import java.security.interfaces.*; |
41 import java.security.interfaces.*; |
42 |
42 |
43 import sun.security.ec.NamedCurve; |
43 import sun.security.util.ECUtil; |
44 |
44 |
45 public class TestECDSA2 extends PKCS11Test { |
45 public class TestECDSA2 extends PKCS11Test { |
46 |
46 |
47 // values of the keys we use for the tests |
47 // values of the keys we use for the tests |
48 |
48 |
73 throw new Exception("Error: Signature verification failed"); |
73 throw new Exception("Error: Signature verification failed"); |
74 } |
74 } |
75 System.out.println(p.getName() + ": " + alg + " Passed"); |
75 System.out.println(p.getName() + ": " + alg + " Passed"); |
76 } |
76 } |
77 |
77 |
78 private KeyPair genECKeyPair(String curvName, String privD, String pubX, String pubY) throws Exception { |
78 private KeyPair genECKeyPair(String curvName, String privD, String pubX, |
79 ECParameterSpec ecParams = NamedCurve.getECParameterSpec(curvName); |
79 String pubY, Provider p) throws Exception { |
|
80 ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName); |
80 ECPrivateKeySpec privKeySpec = |
81 ECPrivateKeySpec privKeySpec = |
81 new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams); |
82 new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams); |
82 ECPublicKeySpec pubKeySpec = |
83 ECPublicKeySpec pubKeySpec = |
83 new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)), |
84 new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16), new BigInteger(pubY, 16)), |
84 ecParams); |
85 ecParams); |
106 kf = KeyFactory.getInstance("EC", provider); |
107 kf = KeyFactory.getInstance("EC", provider); |
107 |
108 |
108 long start = System.currentTimeMillis(); |
109 long start = System.currentTimeMillis(); |
109 if (testP256) { |
110 if (testP256) { |
110 // can use secp256r1, NIST P-256, X9.62 prime256v1, or 1.2.840.10045.3.1.7 |
111 // can use secp256r1, NIST P-256, X9.62 prime256v1, or 1.2.840.10045.3.1.7 |
111 KeyPair kp = genECKeyPair("secp256r1", privD256, pubX256, pubY256); |
112 KeyPair kp = |
|
113 genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider); |
112 testSignAndVerify("SHA256withECDSA", kp, provider); |
114 testSignAndVerify("SHA256withECDSA", kp, provider); |
113 } |
115 } |
114 if (testP384) { |
116 if (testP384) { |
115 // can use secp384r1, NIST P-384, 1.3.132.0.34 |
117 // can use secp384r1, NIST P-384, 1.3.132.0.34 |
116 KeyPair kp = genECKeyPair("secp384r1", privD384, pubX384, pubY384); |
118 KeyPair kp = |
|
119 genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider); |
117 testSignAndVerify("SHA384withECDSA", kp, provider); |
120 testSignAndVerify("SHA384withECDSA", kp, provider); |
118 } |
121 } |
119 long stop = System.currentTimeMillis(); |
122 long stop = System.currentTimeMillis(); |
120 System.out.println("All tests passed (" + (stop - start) + " ms)."); |
123 System.out.println("All tests passed (" + (stop - start) + " ms)."); |
121 } |
124 } |