25 import java.security.spec.*; |
25 import java.security.spec.*; |
26 import java.util.stream.IntStream; |
26 import java.util.stream.IntStream; |
27 |
27 |
28 /** |
28 /** |
29 * @test |
29 * @test |
30 * @bug 8080462 |
30 * @bug 8080462 8226651 |
31 * @summary Generate a RSASSA-PSS signature and verify it using PKCS11 provider |
31 * @summary Generate a RSASSA-PSS signature and verify it using PKCS11 provider |
32 * @library /test/lib .. |
32 * @library /test/lib .. |
33 * @modules jdk.crypto.cryptoki |
33 * @modules jdk.crypto.cryptoki |
34 * @run main SignatureTestPSS |
34 * @run main SignatureTestPSS |
35 */ |
35 */ |
84 // create a key pair |
84 // create a key pair |
85 KeyPair kpair = generateKeys(KEYALG, keySize); |
85 KeyPair kpair = generateKeys(KEYALG, keySize); |
86 test(DIGESTS, kpair.getPrivate(), kpair.getPublic(), data); |
86 test(DIGESTS, kpair.getPrivate(), kpair.getPublic(), data); |
87 } |
87 } |
88 |
88 |
89 private void test(String[] testAlgs, PrivateKey privKey, |
89 private void test(String[] digestAlgs, PrivateKey privKey, |
90 PublicKey pubKey, byte[] data) throws RuntimeException { |
90 PublicKey pubKey, byte[] data) throws RuntimeException { |
91 // For signature algorithm, create and verify a signature |
91 // For signature algorithm, create and verify a signature |
92 for (String testAlg : testAlgs) { |
92 for (String hash : digestAlgs) { |
93 try { |
93 for (String mgfHash : digestAlgs) { |
94 checkSignature(data, pubKey, privKey, testAlg); |
94 try { |
95 } catch (NoSuchAlgorithmException | InvalidKeyException | |
95 checkSignature(data, pubKey, privKey, hash, mgfHash); |
96 SignatureException | NoSuchProviderException ex) { |
96 } catch (NoSuchAlgorithmException | InvalidKeyException | |
97 throw new RuntimeException(ex); |
97 SignatureException | NoSuchProviderException ex) { |
98 } catch (InvalidAlgorithmParameterException ex2) { |
98 throw new RuntimeException(ex); |
99 System.out.println("Skip test due to " + ex2); |
99 } catch (InvalidAlgorithmParameterException ex2) { |
|
100 System.out.println("Skip test due to " + ex2); |
|
101 } |
100 } |
102 } |
101 }; |
103 }; |
102 } |
104 } |
103 |
105 |
104 private KeyPair generateKeys(String keyalg, int size) |
106 private KeyPair generateKeys(String keyalg, int size) |
107 kpg.initialize(size); |
109 kpg.initialize(size); |
108 return kpg.generateKeyPair(); |
110 return kpg.generateKeyPair(); |
109 } |
111 } |
110 |
112 |
111 private void checkSignature(byte[] data, PublicKey pub, |
113 private void checkSignature(byte[] data, PublicKey pub, |
112 PrivateKey priv, String mdAlg) throws NoSuchAlgorithmException, |
114 PrivateKey priv, String hash, String mgfHash) |
113 InvalidKeyException, SignatureException, NoSuchProviderException, |
115 throws NoSuchAlgorithmException, InvalidKeyException, |
|
116 SignatureException, NoSuchProviderException, |
114 InvalidAlgorithmParameterException { |
117 InvalidAlgorithmParameterException { |
115 System.out.println("Testing against " + mdAlg); |
118 System.out.println("Testing against " + hash + " and MGF1_" + mgfHash); |
116 Signature sig = Signature.getInstance(SIGALG, prov); |
119 Signature sig = Signature.getInstance(SIGALG, prov); |
117 AlgorithmParameterSpec params = new PSSParameterSpec( |
120 AlgorithmParameterSpec params = new PSSParameterSpec( |
118 mdAlg, "MGF1", new MGF1ParameterSpec(mdAlg), 0, 1); |
121 hash, "MGF1", new MGF1ParameterSpec(mgfHash), 0, 1); |
119 sig.setParameter(params); |
122 sig.setParameter(params); |
120 sig.initSign(priv); |
123 sig.initSign(priv); |
121 for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) { |
124 for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) { |
122 sig.update(data); |
125 sig.update(data); |
123 } |
126 } |