|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
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 |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import java.io.BufferedReader; |
|
25 import java.io.File; |
|
26 import java.io.FileInputStream; |
|
27 import java.io.IOException; |
|
28 import java.io.InputStreamReader; |
|
29 import java.security.*; |
|
30 import java.security.spec.*; |
|
31 import java.security.interfaces.*; |
|
32 import java.util.ArrayList; |
|
33 import java.util.List; |
|
34 |
|
35 /* |
|
36 * @test |
|
37 * @bug 8146293 |
|
38 * @summary Known Answer Tests based on NIST 186-3 at: |
|
39 * @compile SigRecord.java |
|
40 * @run main/othervm TestSigGenPSS |
|
41 */ |
|
42 public class TestSigGenPSS { |
|
43 |
|
44 private static final String[] testFiles = { |
|
45 "SigGenPSS_186-3.txt", "SigGenPSS_186-3_TruncatedSHAs.txt" |
|
46 }; |
|
47 |
|
48 static final class MyKnownRandomSrc extends SecureRandom { |
|
49 final byte[] srcBytes; |
|
50 int numBytes; |
|
51 |
|
52 MyKnownRandomSrc(String srcString) { |
|
53 this.srcBytes = SigRecord.toByteArray(srcString); |
|
54 this.numBytes = this.srcBytes.length; |
|
55 } |
|
56 @Override |
|
57 public void nextBytes(byte[] bytes) { |
|
58 if (bytes.length > numBytes) { |
|
59 throw new RuntimeException("Not enough bytes, need " |
|
60 + bytes.length + ", got " + numBytes); |
|
61 } |
|
62 System.arraycopy(this.srcBytes, this.srcBytes.length - numBytes, bytes, 0, bytes.length); |
|
63 numBytes -= bytes.length; |
|
64 } |
|
65 |
|
66 } |
|
67 |
|
68 public static void main(String[] args) throws Exception { |
|
69 //for (Provider provider : Security.getProviders()) { |
|
70 Provider p = Security.getProvider("SunRsaSign"); |
|
71 Signature sig; |
|
72 try { |
|
73 sig = Signature.getInstance("RSASSA-PSS", p); |
|
74 } catch (NoSuchAlgorithmException e) { |
|
75 System.out.println("Skip testing RSASSA-PSS" + |
|
76 " due to no support"); |
|
77 return; |
|
78 } |
|
79 |
|
80 boolean success = true; |
|
81 for (String f : testFiles) { |
|
82 System.out.println("[INPUT FILE " + f + "]"); |
|
83 try { |
|
84 success &= runTest(SigRecord.read(f), sig); |
|
85 } catch (IOException e) { |
|
86 System.out.println("Unexpected exception: " + e); |
|
87 e.printStackTrace(System.out); |
|
88 success = false; |
|
89 } |
|
90 } |
|
91 |
|
92 if (!success) { |
|
93 throw new RuntimeException("One or more test failed"); |
|
94 } |
|
95 System.out.println("Test passed"); |
|
96 } |
|
97 |
|
98 /* |
|
99 * Run all the tests in the data list with specified algorithm |
|
100 */ |
|
101 static boolean runTest(List<SigRecord> records, Signature sig) throws Exception { |
|
102 boolean success = true; |
|
103 KeyFactory kf = KeyFactory.getInstance("RSA", sig.getProvider()); |
|
104 for (SigRecord sr : records) { |
|
105 System.out.println("==Testing Record : " + sr + "=="); |
|
106 PrivateKey privKey = kf.generatePrivate(sr.privKeySpec); |
|
107 PublicKey pubKey = kf.generatePublic(sr.pubKeySpec); |
|
108 success &= check(sig, privKey, pubKey, sr.testVectors); |
|
109 System.out.println("==Done=="); |
|
110 } |
|
111 return success; |
|
112 } |
|
113 |
|
114 /* |
|
115 * Generate the signature, check against known values and verify. |
|
116 */ |
|
117 static boolean check(Signature sig, PrivateKey privKey, PublicKey pubKey, |
|
118 List<SigRecord.SigVector> vectors) throws Exception { |
|
119 |
|
120 boolean success = true; |
|
121 for (SigRecord.SigVector v : vectors) { |
|
122 System.out.println("\tAgainst " + v.mdAlg); |
|
123 byte[] msgBytes = SigRecord.toByteArray(v.msg); |
|
124 byte[] expSigBytes = SigRecord.toByteArray(v.sig); |
|
125 |
|
126 MyKnownRandomSrc saltSrc = new MyKnownRandomSrc(v.salt); |
|
127 sig.initSign(privKey, saltSrc); |
|
128 PSSParameterSpec params = new PSSParameterSpec(v.mdAlg, "MGF1", |
|
129 new MGF1ParameterSpec(v.mdAlg), saltSrc.numBytes, 1); |
|
130 sig.setParameter(params); |
|
131 sig.update(msgBytes); |
|
132 byte[] actualSigBytes = sig.sign(); |
|
133 |
|
134 // Check if the supplied salt bytes are used up |
|
135 if (saltSrc.numBytes != 0) { |
|
136 throw new RuntimeException("Error: salt length mismatch! " |
|
137 + saltSrc.numBytes + " bytes leftover"); |
|
138 } |
|
139 |
|
140 success &= MessageDigest.isEqual(actualSigBytes, expSigBytes); |
|
141 |
|
142 if (!success) { |
|
143 System.out.println("\tFailed:"); |
|
144 System.out.println("\tSHAALG = " + v.mdAlg); |
|
145 System.out.println("\tMsg = " + v.msg); |
|
146 System.out.println("\tSalt = " + v.salt); |
|
147 System.out.println("\tExpected Sig = " + v.sig); |
|
148 System.out.println("\tActual Sig = " + SigRecord.toHexString(actualSigBytes)); |
|
149 } else { |
|
150 System.out.println("\t" + v.mdAlg + " Test Vector Passed"); |
|
151 } |
|
152 } |
|
153 return success; |
|
154 } |
|
155 } |