Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/RSA/TestOAEPParameterSpec.java
41161 views
1
/*
2
* Copyright (c) 2003, 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
/*
25
* @test
26
* @bug 4923484 8146293
27
* @summary test ASN.1 encoding generation/parsing for the OAEPParameters
28
* implementation in SunJCE provider.
29
* @author Valerie Peng
30
*/
31
import java.math.BigInteger;
32
import java.util.*;
33
import java.security.*;
34
import java.security.spec.MGF1ParameterSpec;
35
import javax.crypto.*;
36
import javax.crypto.spec.OAEPParameterSpec;
37
import javax.crypto.spec.PSource;
38
39
public class TestOAEPParameterSpec {
40
41
private static Provider cp;
42
43
private static boolean runTest(String mdName, MGF1ParameterSpec mgfSpec,
44
byte[] p) throws Exception {
45
OAEPParameterSpec spec = new OAEPParameterSpec(mdName, "MGF1",
46
mgfSpec, new PSource.PSpecified(p));
47
cp = Security.getProvider("SunJCE");
48
System.out.println("Testing provider " + cp.getName() + "...");
49
AlgorithmParameters ap = AlgorithmParameters.getInstance("OAEP", cp);
50
51
ap.init(spec);
52
byte[] encoding = ap.getEncoded();
53
54
AlgorithmParameters ap2 = AlgorithmParameters.getInstance("OAEP", cp);
55
ap2.init(encoding);
56
57
OAEPParameterSpec spec2 = (OAEPParameterSpec) ap2.getParameterSpec
58
(OAEPParameterSpec.class);
59
return compareSpec(spec, spec2);
60
}
61
62
private static boolean compareMD(OAEPParameterSpec s1,
63
OAEPParameterSpec s2) {
64
boolean result = false;
65
String alg1 = s1.getDigestAlgorithm().toUpperCase().trim();
66
String alg2 = s2.getDigestAlgorithm().toUpperCase().trim();
67
alg1 = alg1.replaceAll("\\-", "");
68
alg2 = alg2.replaceAll("\\-", "");
69
if (alg1.equals("SHA") || alg1.equals("SHA1")) {
70
result = (alg2.equals("SHA") || alg2.equals("SHA1"));
71
} else {
72
result = (alg1.equals(alg2));
73
}
74
return result;
75
}
76
77
78
private static boolean compareMGF(OAEPParameterSpec s1,
79
OAEPParameterSpec s2) {
80
String alg1 = s1.getMGFAlgorithm();
81
String alg2 = s2.getMGFAlgorithm();
82
if (alg1.equals(alg2)) {
83
MGF1ParameterSpec mp1 = (MGF1ParameterSpec)s1.getMGFParameters();
84
MGF1ParameterSpec mp2 = (MGF1ParameterSpec)s2.getMGFParameters();
85
alg1 = mp1.getDigestAlgorithm();
86
alg2 = mp2.getDigestAlgorithm();
87
if (alg1.equals(alg2)) {
88
return true;
89
} else {
90
System.out.println("MGF's MD algos: " + alg1 + " vs " + alg2);
91
return false;
92
}
93
} else {
94
System.out.println("MGF algos: " + alg1 + " vs " + alg2);
95
return false;
96
}
97
}
98
99
private static boolean comparePSource(OAEPParameterSpec s1,
100
OAEPParameterSpec s2) {
101
PSource src1 = s1.getPSource();
102
PSource src2 = s2.getPSource();
103
String alg1 = src1.getAlgorithm();
104
String alg2 = src2.getAlgorithm();
105
if (alg1.equals(alg2)) {
106
// assumes they are PSource.PSpecified
107
return Arrays.equals(((PSource.PSpecified) src1).getValue(),
108
((PSource.PSpecified) src2).getValue());
109
} else {
110
System.out.println("PSource algos: " + alg1 + " vs " + alg2);
111
return false;
112
}
113
}
114
115
private static boolean compareSpec(OAEPParameterSpec s1,
116
OAEPParameterSpec s2) {
117
return (compareMD(s1, s2) && compareMGF(s1, s2) &&
118
comparePSource(s1, s2));
119
}
120
121
public static void main(String[] argv) throws Exception {
122
boolean status = true;
123
byte[] p = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 };
124
status &= runTest("SHA-224", MGF1ParameterSpec.SHA224, p);
125
status &= runTest("SHA-256", MGF1ParameterSpec.SHA256, p);
126
status &= runTest("SHA-384", MGF1ParameterSpec.SHA384, p);
127
status &= runTest("SHA-512", MGF1ParameterSpec.SHA512, p);
128
status &= runTest("SHA-512/224", MGF1ParameterSpec.SHA512_224, p);
129
status &= runTest("SHA-512/256", MGF1ParameterSpec.SHA512_256, p);
130
status &= runTest("SHA", MGF1ParameterSpec.SHA1, new byte[0]);
131
status &= runTest("SHA-1", MGF1ParameterSpec.SHA1, new byte[0]);
132
status &= runTest("SHA1", MGF1ParameterSpec.SHA1, new byte[0]);
133
if (status) {
134
System.out.println("Test Passed");
135
} else {
136
throw new Exception("One or More Test Failed");
137
}
138
}
139
}
140
141