56542
|
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.math.BigInteger;
|
|
30 |
import java.security.*;
|
|
31 |
import java.security.spec.*;
|
|
32 |
import java.util.ArrayList;
|
|
33 |
import java.util.List;
|
|
34 |
|
|
35 |
public final class SigRecord {
|
|
36 |
|
|
37 |
static final String TEST_SRC = System.getProperty("test.src", ".");
|
|
38 |
|
|
39 |
// utility method for converting byte array to hex string
|
|
40 |
static String toHexString(byte[] array) {
|
|
41 |
StringBuilder sb = new StringBuilder(array.length * 2);
|
|
42 |
for (byte b : array) {
|
|
43 |
// The single digits 0123456789abcdef get a leading 0
|
|
44 |
if ((b >= 0x00) && (b < 0x10)) {
|
|
45 |
sb.append('0');
|
|
46 |
}
|
|
47 |
sb.append(Integer.toHexString(b & 0xff));
|
|
48 |
}
|
|
49 |
return sb.toString();
|
|
50 |
}
|
|
51 |
|
|
52 |
// utility method for converting hex string to byte array
|
|
53 |
static byte[] toByteArray(String s) {
|
|
54 |
byte[] bytes = new byte[s.length() / 2];
|
|
55 |
for (int i = 0; i < bytes.length; i++) {
|
|
56 |
int index = i * 2;
|
|
57 |
int v = Integer.parseInt(s.substring(index, index + 2), 16);
|
|
58 |
bytes[i] = (byte) v;
|
|
59 |
}
|
|
60 |
return bytes;
|
|
61 |
}
|
|
62 |
|
|
63 |
public static final class SigVector {
|
|
64 |
// digest algorithm to use
|
|
65 |
final String mdAlg;
|
|
66 |
|
|
67 |
// message to test
|
|
68 |
final String msg;
|
|
69 |
|
|
70 |
// expected signature
|
|
71 |
final String sig;
|
|
72 |
|
|
73 |
// optional PSS-only salt value, maybe null
|
|
74 |
final String salt;
|
|
75 |
|
|
76 |
public SigVector(String mdAlg, String msg, String sig, String salt) {
|
|
77 |
if (mdAlg == null || mdAlg.isEmpty()) {
|
|
78 |
throw new IllegalArgumentException("Digest algo must be specified");
|
|
79 |
}
|
|
80 |
if (msg == null || mdAlg.isEmpty()) {
|
|
81 |
throw new IllegalArgumentException("Message must be specified");
|
|
82 |
}
|
|
83 |
if (sig == null || mdAlg.isEmpty()) {
|
|
84 |
throw new IllegalArgumentException("Signature must be specified");
|
|
85 |
}
|
|
86 |
this.mdAlg = mdAlg;
|
|
87 |
this.msg = msg;
|
|
88 |
this.sig = sig;
|
|
89 |
this.salt = salt;
|
|
90 |
}
|
|
91 |
|
|
92 |
@Override
|
|
93 |
public String toString() {
|
|
94 |
return mdAlg + ": msg=" + msg + ": sig=" + sig +
|
|
95 |
(salt != null? (": salt=" + salt) : "");
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
final String id;
|
|
100 |
// RSA private key value associated with the corresponding test vectors
|
|
101 |
final RSAPrivateKeySpec privKeySpec;
|
|
102 |
|
|
103 |
// RSA public key value associated with the corresponding test vectors
|
|
104 |
final RSAPublicKeySpec pubKeySpec;
|
|
105 |
|
|
106 |
// set of test vectors
|
|
107 |
final List<SigVector> testVectors;
|
|
108 |
|
|
109 |
SigRecord(String mod, String pubExp, String privExp, List<SigVector> testVectors) {
|
|
110 |
if (mod == null || mod.isEmpty()) {
|
|
111 |
throw new IllegalArgumentException("Modulus n must be specified");
|
|
112 |
}
|
|
113 |
if (pubExp == null || pubExp.isEmpty()) {
|
|
114 |
throw new IllegalArgumentException("Public Exponent e must be specified");
|
|
115 |
}
|
|
116 |
if (privExp == null || privExp.isEmpty()) {
|
|
117 |
throw new IllegalArgumentException("Private Exponent d must be specified");
|
|
118 |
}
|
|
119 |
if (testVectors == null || (testVectors.size() == 0)) {
|
|
120 |
throw new IllegalArgumentException("One or more test vectors must be specified");
|
|
121 |
}
|
|
122 |
|
|
123 |
BigInteger n = new BigInteger(1, toByteArray(mod));
|
|
124 |
BigInteger e = new BigInteger(1, toByteArray(pubExp));
|
|
125 |
BigInteger d = new BigInteger(1, toByteArray(privExp));
|
|
126 |
this.id = ("n=" + mod + ", e=" + pubExp);
|
|
127 |
this.pubKeySpec = new RSAPublicKeySpec(n, e);
|
|
128 |
this.privKeySpec = new RSAPrivateKeySpec(n, d);
|
|
129 |
this.testVectors = testVectors;
|
|
130 |
}
|
|
131 |
|
|
132 |
/*
|
|
133 |
* Read a data file into an ArrayList.
|
|
134 |
* This function will exit the program if reading the file fails
|
|
135 |
* or if the file is not in the expected format.
|
|
136 |
*/
|
|
137 |
public static List<SigRecord> read(String filename)
|
|
138 |
throws IOException {
|
|
139 |
|
|
140 |
List<SigRecord> data = new ArrayList<>();
|
|
141 |
try (BufferedReader br = new BufferedReader(
|
|
142 |
new InputStreamReader(new FileInputStream(
|
|
143 |
TEST_SRC + File.separator + filename)))) {
|
|
144 |
String line;
|
|
145 |
String mod = null;
|
|
146 |
String pubExp = null;
|
|
147 |
String privExp = null;
|
|
148 |
List<SigVector> testVectors = new ArrayList<>();
|
|
149 |
while ((line = br.readLine()) != null) {
|
|
150 |
if (line.startsWith("n =")) {
|
|
151 |
mod = line.split("=")[1].trim();
|
|
152 |
} else if (line.startsWith("e =")) {
|
|
153 |
pubExp = line.split("=")[1].trim();
|
|
154 |
} else if (line.startsWith("d =")) {
|
|
155 |
privExp = line.split("=")[1].trim();
|
|
156 |
|
|
157 |
// now should start parsing for test vectors
|
|
158 |
String mdAlg = null;
|
|
159 |
String msg = null;
|
|
160 |
String sig = null;
|
|
161 |
String salt = null;
|
|
162 |
boolean sigVectorDone = false;
|
|
163 |
while ((line = br.readLine()) != null) {
|
|
164 |
// we only care for lines starting with
|
|
165 |
// SHAALG, Msg, S, and Salt
|
|
166 |
if (line.startsWith("SHAAlg =")) {
|
|
167 |
mdAlg = line.split(" = ")[1].trim();
|
|
168 |
} else if (line.startsWith("Msg =")) {
|
|
169 |
msg = line.split(" = ")[1].trim();
|
|
170 |
} else if (line.startsWith("S =")) {
|
|
171 |
sig = line.split(" = ")[1].trim();
|
|
172 |
} else if (line.startsWith("SaltVal =")) {
|
|
173 |
salt = line.split(" = ")[1].trim();
|
|
174 |
if (salt.equals("00")) {
|
|
175 |
salt = "";
|
|
176 |
}
|
|
177 |
} else if (line.startsWith("[mod")) {
|
|
178 |
sigVectorDone = true;
|
|
179 |
}
|
|
180 |
|
|
181 |
if ((mdAlg != null) && (msg != null) && (sig != null) &&
|
|
182 |
(salt != null)) {
|
|
183 |
// finish off current SigVector
|
|
184 |
testVectors.add(new SigVector(mdAlg, msg, sig, salt));
|
|
185 |
mdAlg = msg = sig = salt = null;
|
|
186 |
}
|
|
187 |
if (sigVectorDone) {
|
|
188 |
break;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
// finish off current SigRecord and clear data for next SigRecord
|
|
192 |
data.add(new SigRecord(mod, pubExp, privExp, testVectors));
|
|
193 |
mod = pubExp = privExp = null;
|
|
194 |
testVectors = new ArrayList<>();
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
if (data.isEmpty()) {
|
|
199 |
throw new RuntimeException("Nothing read from file "
|
|
200 |
+ filename);
|
|
201 |
}
|
|
202 |
}
|
|
203 |
return data;
|
|
204 |
}
|
|
205 |
|
|
206 |
@Override
|
|
207 |
public String toString() {
|
|
208 |
return (id + ", " + testVectors.size() + " test vectors");
|
|
209 |
}
|
|
210 |
}
|