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/TestOAEP.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 4894151 8146293
27
* @summary encryption/decryption test for OAEP
28
* @author Andreas Sterbenz
29
* @key randomness
30
*/
31
32
import java.util.*;
33
34
import java.security.*;
35
36
import javax.crypto.*;
37
38
public class TestOAEP {
39
40
private static Provider cp;
41
42
private static PrivateKey privateKey;
43
44
private static PublicKey publicKey;
45
46
private static Random random = new Random();
47
48
public static void main(String[] args) throws Exception {
49
long start = System.currentTimeMillis();
50
cp = Security.getProvider("SunJCE");
51
System.out.println("Testing provider " + cp.getName() + "...");
52
Provider kfp = Security.getProvider("SunRsaSign");
53
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp);
54
kpg.initialize(768);
55
KeyPair kp = kpg.generateKeyPair();
56
privateKey = kp.getPrivate();
57
publicKey = kp.getPublic();
58
59
Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding");
60
Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding");
61
Cipher.getInstance("RSA/ECB/OAEPwithSHA-1andMGF1Padding");
62
Cipher.getInstance("RSA/ECB/OAEPwithSHA-224andMGF1Padding");
63
Cipher.getInstance("RSA/ECB/OAEPwithSHA-256andMGF1Padding");
64
Cipher.getInstance("RSA/ECB/OAEPwithSHA-384andMGF1Padding");
65
Cipher.getInstance("RSA/ECB/OAEPwithSHA-512andMGF1Padding");
66
Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/224andMGF1Padding");
67
Cipher.getInstance("RSA/ECB/OAEPwithSHA-512/256andMGF1Padding");
68
69
// basic test using MD5
70
testEncryptDecrypt("MD5", 0);
71
testEncryptDecrypt("MD5", 16);
72
testEncryptDecrypt("MD5", 62);
73
try {
74
testEncryptDecrypt("MD5", 63);
75
throw new Exception("Unexpectedly completed call");
76
} catch (IllegalBlockSizeException e) {
77
// ok
78
System.out.println(e);
79
}
80
81
// basic test using SHA-1
82
testEncryptDecrypt("SHA1", 0);
83
testEncryptDecrypt("SHA1", 16);
84
testEncryptDecrypt("SHA1", 54);
85
try {
86
testEncryptDecrypt("SHA1", 55);
87
throw new Exception("Unexpectedly completed call");
88
} catch (IllegalBlockSizeException e) {
89
// ok
90
System.out.println(e);
91
}
92
// tests alias works
93
testEncryptDecrypt("SHA-1", 16);
94
95
String[] HASH_ALG_224 = { "SHA-224", "SHA-512/224" };
96
for (String ha : HASH_ALG_224) {
97
testEncryptDecrypt(ha, 0);
98
testEncryptDecrypt(ha, 16);
99
testEncryptDecrypt(ha, 38);
100
try {
101
testEncryptDecrypt(ha, 39);
102
throw new Exception("Unexpectedly completed call");
103
} catch (IllegalBlockSizeException e) {
104
// ok
105
System.out.println(e);
106
}
107
}
108
109
String[] HASH_ALG_256 = { "SHA-256", "SHA-512/256" };
110
for (String ha : HASH_ALG_256) {
111
testEncryptDecrypt(ha, 0);
112
testEncryptDecrypt(ha, 16);
113
testEncryptDecrypt(ha, 30);
114
try {
115
testEncryptDecrypt(ha, 31);
116
throw new Exception("Unexpectedly completed call");
117
} catch (IllegalBlockSizeException e) {
118
// ok
119
System.out.println(e);
120
}
121
}
122
123
// 768 bit key too short for OAEP with 64 byte digest
124
try {
125
testEncryptDecrypt("SHA-512", 1);
126
throw new Exception("Unexpectedly completed call");
127
} catch (InvalidKeyException e) {
128
// ok
129
System.out.println(e);
130
}
131
132
Cipher c;
133
byte[] enc;
134
byte[] data = new byte[16];
135
random.nextBytes(data);
136
137
try {
138
c = Cipher.getInstance("RSA/ECB/OAEPwithFOOandMGF1Padding", cp);
139
throw new Exception("Unexpectedly completed call");
140
} catch (NoSuchPaddingException e) {
141
// ok
142
System.out.println(e);
143
}
144
145
c = Cipher.getInstance("RSA/ECB/OAEPwithMD5andMGF1Padding", cp);
146
// cannot "sign" using OAEP
147
try {
148
c.init(Cipher.ENCRYPT_MODE, privateKey);
149
throw new Exception("Unexpectedly completed call");
150
} catch (InvalidKeyException e) {
151
// ok
152
System.out.println(e);
153
}
154
155
// cannot "verify" using OAEP
156
try {
157
c.init(Cipher.DECRYPT_MODE, publicKey);
158
throw new Exception("Unexpectedly completed call");
159
} catch (InvalidKeyException e) {
160
// ok
161
System.out.println(e);
162
}
163
164
// decryption failure
165
c.init(Cipher.DECRYPT_MODE, privateKey);
166
try {
167
c.doFinal(data);
168
throw new Exception("Unexpectedly completed call");
169
} catch (BadPaddingException e) {
170
// ok
171
System.out.println(e);
172
}
173
174
// wrong hash length
175
c.init(Cipher.ENCRYPT_MODE, publicKey);
176
enc = c.doFinal(data);
177
c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);
178
c.init(Cipher.DECRYPT_MODE, privateKey);
179
try {
180
c.doFinal(enc);
181
throw new Exception("Unexpectedly completed call");
182
} catch (BadPaddingException e) {
183
// ok
184
System.out.println(e);
185
}
186
187
/* MD2 not supported with OAEP
188
// wrong hash value
189
c = Cipher.getInstance("RSA/ECB/OAEPwithMD2andMGF1Padding", cp);
190
c.init(Cipher.DECRYPT_MODE, privateKey);
191
try {
192
c.doFinal(enc);
193
throw new Exception("Unexpectedly completed call");
194
} catch (BadPaddingException e) {
195
// ok
196
System.out.println(e);
197
}
198
*/
199
200
// wrong padding type
201
c = Cipher.getInstance("RSA/ECB/PKCS1Padding", cp);
202
c.init(Cipher.ENCRYPT_MODE, publicKey);
203
enc = c.doFinal(data);
204
c = Cipher.getInstance("RSA/ECB/OAEPwithSHA1andMGF1Padding", cp);
205
c.init(Cipher.DECRYPT_MODE, privateKey);
206
try {
207
c.doFinal(enc);
208
throw new Exception("Unexpectedly completed call");
209
} catch (BadPaddingException e) {
210
// ok
211
System.out.println(e);
212
}
213
214
long stop = System.currentTimeMillis();
215
System.out.println("Done (" + (stop - start) + " ms).");
216
}
217
218
// NOTE: OAEP can process up to (modLen - 2*digestLen - 2) bytes of data
219
private static void testEncryptDecrypt(String hashAlg, int dataLength) throws Exception {
220
System.out.println("Testing OAEP with hash " + hashAlg + ", " + dataLength + " bytes");
221
Cipher c = Cipher.getInstance("RSA/ECB/OAEPwith" + hashAlg + "andMGF1Padding", cp);
222
c.init(Cipher.ENCRYPT_MODE, publicKey);
223
byte[] data = new byte[dataLength];
224
byte[] enc = c.doFinal(data);
225
c.init(Cipher.DECRYPT_MODE, privateKey);
226
byte[] dec = c.doFinal(enc);
227
if (Arrays.equals(data, dec) == false) {
228
throw new Exception("Data does not match");
229
}
230
}
231
}
232
233