Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/TestSigGen15.java
41152 views
1
/*
2
* Copyright (c) 2018, 2021, 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.HexFormat;
34
import java.util.List;
35
36
/*
37
* @test
38
* @bug 8146293
39
* @summary Known Answer Tests based on NIST 186-3 at:
40
* @compile SigRecord.java
41
* @run main/othervm TestSigGen15
42
*/
43
public class TestSigGen15 {
44
45
private static final String[] testFiles = {
46
"SigGen15_186-3.txt", "SigGen15_186-3_TruncatedSHAs.txt"
47
};
48
49
public static void main(String[] args) throws Exception {
50
boolean success = true;
51
for (String f : testFiles) {
52
System.out.println("[INPUT FILE " + f + "]");
53
try {
54
success &= runTest(SigRecord.read(f));
55
} catch (IOException e) {
56
System.out.println("Unexpected exception: " + e);
57
e.printStackTrace(System.out);
58
success = false;
59
}
60
}
61
62
if (!success) {
63
throw new RuntimeException("One or more test failed");
64
}
65
System.out.println("Test passed");
66
}
67
68
/*
69
* Run all the tests in the data list with specified algorithm
70
*/
71
static boolean runTest(List<SigRecord> records) throws Exception {
72
boolean success = true;
73
//for (Provider provider : Security.getProviders()) {
74
Provider p = Security.getProvider("SunRsaSign");
75
KeyFactory kf = KeyFactory.getInstance("RSA", p);
76
for (SigRecord sr : records) {
77
System.out.println("==Testing Record : " + sr + "==");
78
PrivateKey privKey = kf.generatePrivate(sr.privKeySpec);
79
PublicKey pubKey = kf.generatePublic(sr.pubKeySpec);
80
success &= check(privKey, pubKey, sr.testVectors, p);
81
System.out.println("==Done==");
82
}
83
return success;
84
}
85
86
/*
87
* Generate the signature, check against known values and verify.
88
*/
89
static boolean check(PrivateKey privKey, PublicKey pubKey,
90
List<SigRecord.SigVector> vectors, Provider p) throws Exception {
91
92
boolean success = true;
93
for (SigRecord.SigVector v : vectors) {
94
System.out.println("\tAgainst " + v.mdAlg);
95
String sigAlgo = v.mdAlg + "withRSA";
96
Signature sig;
97
try {
98
sig = Signature.getInstance(sigAlgo, p);
99
} catch (NoSuchAlgorithmException e) {
100
System.out.println("\tSkip " + sigAlgo +
101
" due to no support");
102
continue;
103
}
104
byte[] msgBytes = HexFormat.of().parseHex(v.msg);
105
byte[] expSigBytes = HexFormat.of().parseHex(v.sig);
106
107
sig.initSign(privKey);
108
sig.update(msgBytes);
109
byte[] actualSigBytes = sig.sign();
110
111
success &= MessageDigest.isEqual(actualSigBytes, expSigBytes);
112
113
if (!success) {
114
System.out.println("\tFailed:");
115
System.out.println("\tSHAALG = " + v.mdAlg);
116
System.out.println("\tMsg = " + v.msg);
117
System.out.println("\tExpected Sig = " + v.sig);
118
System.out.println("\tActual Sig = " + HexFormat.of().formatHex(actualSigBytes));
119
} else {
120
System.out.println("\t" + v.mdAlg + " Test Vector Passed");
121
}
122
}
123
124
return success;
125
}
126
}
127
128