21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /** |
24 /** |
25 * @test |
25 * @test |
26 * @bug 4853305 4963723 |
26 * @bug 4853305 4963723 8146293 |
27 * @summary Test signing/verifying using all the signature algorithms |
27 * @summary Test signing/verifying using all the signature algorithms |
|
28 * @library /test/lib |
|
29 * @build jdk.test.lib.SigTestUtil |
|
30 * @run main TestSignatures |
28 * @author Andreas Sterbenz |
31 * @author Andreas Sterbenz |
29 * @key randomness |
32 * @key randomness |
30 */ |
33 */ |
31 |
34 |
32 import java.io.*; |
35 import java.io.*; |
33 import java.util.*; |
36 import java.util.*; |
34 |
37 |
35 import java.security.*; |
38 import java.security.*; |
36 import java.security.interfaces.*; |
39 import java.security.interfaces.*; |
|
40 |
|
41 import jdk.test.lib.SigTestUtil; |
|
42 import static jdk.test.lib.SigTestUtil.SignatureType; |
37 |
43 |
38 public class TestSignatures { |
44 public class TestSignatures { |
39 |
45 |
40 private final static String BASE = System.getProperty("test.src", "."); |
46 private final static String BASE = System.getProperty("test.src", "."); |
41 |
47 |
51 ks.load(in, password); |
57 ks.load(in, password); |
52 in.close(); |
58 in.close(); |
53 return ks; |
59 return ks; |
54 } |
60 } |
55 |
61 |
56 private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception { |
62 private static void testSignature(String mdAlg, PrivateKey privateKey, |
57 System.out.println("Testing " + algorithm + "..."); |
63 PublicKey publicKey) throws NoSuchAlgorithmException, |
58 Signature s = Signature.getInstance(algorithm, provider); |
64 InvalidKeyException, SignatureException { |
|
65 System.out.println("Testing against " + mdAlg + "..."); |
|
66 String sigAlg = SigTestUtil.generateSigAlg(SignatureType.RSA, mdAlg); |
|
67 Signature s = Signature.getInstance(sigAlg, provider); |
59 s.initSign(privateKey); |
68 s.initSign(privateKey); |
60 s.update(data); |
69 s.update(data); |
61 byte[] sig = s.sign(); |
70 byte[] sig = s.sign(); |
62 s.initVerify(publicKey); |
71 s.initVerify(publicKey); |
63 s.update(data); |
72 s.update(data); |
64 boolean result; |
73 boolean result; |
65 result = s.verify(sig); |
74 result = s.verify(sig); |
66 if (result == false) { |
75 if (result == false) { |
67 throw new Exception("Verification 1 failed"); |
76 throw new RuntimeException("Verification 1 failed"); |
68 } |
77 } |
69 s.update(data); |
78 s.update(data); |
70 result = s.verify(sig); |
79 result = s.verify(sig); |
71 if (result == false) { |
80 if (result == false) { |
72 throw new Exception("Verification 2 failed"); |
81 throw new RuntimeException("Verification 2 failed"); |
73 } |
82 } |
74 result = s.verify(sig); |
83 result = s.verify(sig); |
75 if (result == true) { |
84 if (result == true) { |
76 throw new Exception("Verification 3 succeeded"); |
85 throw new RuntimeException("Verification 3 succeeded"); |
77 } |
86 } |
78 } |
87 } |
79 |
88 |
80 private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception { |
89 private static void test(PrivateKey privateKey, PublicKey publicKey) |
81 testSignature("MD2withRSA", privateKey, publicKey); |
90 throws Exception { |
82 testSignature("MD5withRSA", privateKey, publicKey); |
91 |
83 testSignature("SHA1withRSA", privateKey, publicKey); |
92 int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength(); |
84 testSignature("SHA224withRSA", privateKey, publicKey); |
93 System.out.println("modulus size = " + testSize); |
85 testSignature("SHA256withRSA", privateKey, publicKey); |
94 // work around a corner case where the key size is one bit short |
86 RSAPublicKey rsaKey = (RSAPublicKey)publicKey; |
95 if ((testSize & 0x07) != 0) { |
87 if (rsaKey.getModulus().bitLength() > 512) { |
96 testSize += (8 - (testSize & 0x07)); |
88 // for SHA384 and SHA512 the data is too long for 512 bit keys |
97 System.out.println("adjusted modulus size = " + testSize); |
89 testSignature("SHA384withRSA", privateKey, publicKey); |
|
90 testSignature("SHA512withRSA", privateKey, publicKey); |
|
91 } |
98 } |
|
99 Iterable<String> sign_alg_pkcs15 = |
|
100 SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize); |
|
101 sign_alg_pkcs15.forEach(testAlg -> { |
|
102 try { |
|
103 testSignature(testAlg, privateKey, publicKey); |
|
104 } catch (NoSuchAlgorithmException | InvalidKeyException | |
|
105 SignatureException ex) { |
|
106 throw new RuntimeException(ex); |
|
107 } |
|
108 } |
|
109 ); |
92 } |
110 } |
93 |
111 |
94 public static void main(String[] args) throws Exception { |
112 public static void main(String[] args) throws Exception { |
95 long start = System.currentTimeMillis(); |
113 long start = System.currentTimeMillis(); |
96 provider = Security.getProvider("SunRsaSign"); |
114 provider = Security.getProvider("SunRsaSign"); |