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.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 TestSigGen15
|
|
41 |
*/
|
|
42 |
public class TestSigGen15 {
|
|
43 |
|
|
44 |
private static final String[] testFiles = {
|
|
45 |
"SigGen15_186-3.txt", "SigGen15_186-3_TruncatedSHAs.txt"
|
|
46 |
};
|
|
47 |
|
|
48 |
public static void main(String[] args) throws Exception {
|
|
49 |
boolean success = true;
|
|
50 |
for (String f : testFiles) {
|
|
51 |
System.out.println("[INPUT FILE " + f + "]");
|
|
52 |
try {
|
|
53 |
success &= runTest(SigRecord.read(f));
|
|
54 |
} catch (IOException e) {
|
|
55 |
System.out.println("Unexpected exception: " + e);
|
|
56 |
e.printStackTrace(System.out);
|
|
57 |
success = false;
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
if (!success) {
|
|
62 |
throw new RuntimeException("One or more test failed");
|
|
63 |
}
|
|
64 |
System.out.println("Test passed");
|
|
65 |
}
|
|
66 |
|
|
67 |
/*
|
|
68 |
* Run all the tests in the data list with specified algorithm
|
|
69 |
*/
|
|
70 |
static boolean runTest(List<SigRecord> records) throws Exception {
|
|
71 |
boolean success = true;
|
|
72 |
//for (Provider provider : Security.getProviders()) {
|
|
73 |
Provider p = Security.getProvider("SunRsaSign");
|
|
74 |
KeyFactory kf = KeyFactory.getInstance("RSA", p);
|
|
75 |
for (SigRecord sr : records) {
|
|
76 |
System.out.println("==Testing Record : " + sr + "==");
|
|
77 |
PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);
|
|
78 |
PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);
|
|
79 |
success &= check(privKey, pubKey, sr.testVectors, p);
|
|
80 |
System.out.println("==Done==");
|
|
81 |
}
|
|
82 |
return success;
|
|
83 |
}
|
|
84 |
|
|
85 |
/*
|
|
86 |
* Generate the signature, check against known values and verify.
|
|
87 |
*/
|
|
88 |
static boolean check(PrivateKey privKey, PublicKey pubKey,
|
|
89 |
List<SigRecord.SigVector> vectors, Provider p) throws Exception {
|
|
90 |
|
|
91 |
boolean success = true;
|
|
92 |
for (SigRecord.SigVector v : vectors) {
|
|
93 |
System.out.println("\tAgainst " + v.mdAlg);
|
|
94 |
String sigAlgo = v.mdAlg + "withRSA";
|
|
95 |
Signature sig;
|
|
96 |
try {
|
|
97 |
sig = Signature.getInstance(sigAlgo, p);
|
|
98 |
} catch (NoSuchAlgorithmException e) {
|
|
99 |
System.out.println("\tSkip " + sigAlgo +
|
|
100 |
" due to no support");
|
|
101 |
continue;
|
|
102 |
}
|
|
103 |
byte[] msgBytes = SigRecord.toByteArray(v.msg);
|
|
104 |
byte[] expSigBytes = SigRecord.toByteArray(v.sig);
|
|
105 |
|
|
106 |
sig.initSign(privKey);
|
|
107 |
sig.update(msgBytes);
|
|
108 |
byte[] actualSigBytes = sig.sign();
|
|
109 |
|
|
110 |
success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);
|
|
111 |
|
|
112 |
if (!success) {
|
|
113 |
System.out.println("\tFailed:");
|
|
114 |
System.out.println("\tSHAALG = " + v.mdAlg);
|
|
115 |
System.out.println("\tMsg = " + v.msg);
|
|
116 |
System.out.println("\tExpected Sig = " + v.sig);
|
|
117 |
System.out.println("\tActual Sig = " + SigRecord.toHexString(actualSigBytes));
|
|
118 |
} else {
|
|
119 |
System.out.println("\t" + v.mdAlg + " Test Vector Passed");
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
return success;
|
|
124 |
}
|
|
125 |
}
|