author | ascarpino |
Mon, 12 Aug 2013 11:25:42 -0700 | |
changeset 19390 | 071b67eb2d0d |
child 19436 | cfc7d402795c |
permissions | -rw-r--r-- |
19390
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
1 |
/* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
4 |
* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
8 |
* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
13 |
* accompanied this code). |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
14 |
* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
18 |
* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
21 |
* questions. |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
22 |
*/ |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
23 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
24 |
/* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
25 |
* @test |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
26 |
* @bug 8020081 |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
27 |
* @summary encryption/decryption test for using OAEPPadding with |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
28 |
* OAEPParameterSpec specified and not specified during a Cipher.init(). |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
29 |
* @author Anthony Scarpino |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
30 |
*/ |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
31 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
32 |
import java.util.Arrays; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
33 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
34 |
import java.security.Security; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
35 |
import java.security.Provider; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
36 |
import java.security.KeyPair; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
37 |
import java.security.KeyPairGenerator; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
38 |
import java.security.interfaces.RSAPrivateKey; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
39 |
import java.security.interfaces.RSAPublicKey; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
40 |
import java.security.spec.MGF1ParameterSpec; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
41 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
42 |
import javax.crypto.Cipher; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
43 |
import javax.crypto.spec.OAEPParameterSpec; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
44 |
import javax.crypto.IllegalBlockSizeException; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
45 |
import javax.crypto.spec.PSource; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
46 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
47 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
48 |
public class TestOAEPPadding { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
49 |
private static RSAPrivateKey privateKey; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
50 |
private static RSAPublicKey publicKey; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
51 |
static Provider cp; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
52 |
static boolean failed = false; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
53 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
54 |
public static void main(String args[]) throws Exception { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
55 |
cp = Security.getProvider("SunJCE"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
56 |
System.out.println("Testing provider " + cp.getName() + "..."); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
57 |
Provider kfp = Security.getProvider("SunRsaSign"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
58 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", kfp); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
59 |
kpg.initialize(2048); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
60 |
KeyPair kp = kpg.generateKeyPair(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
61 |
privateKey = (RSAPrivateKey)kp.getPrivate(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
62 |
publicKey = (RSAPublicKey)kp.getPublic(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
63 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
64 |
// Test using a spec with each digest algorithm case |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
65 |
test(new OAEPParameterSpec("MD5", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
66 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
67 |
test(new OAEPParameterSpec("SHA1", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
68 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
69 |
// For default OAEPParameterSpec case (SHA1) |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
70 |
test(null); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
71 |
test(new OAEPParameterSpec("SHA-224", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
72 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
73 |
test(new OAEPParameterSpec("SHA-256", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
74 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
75 |
test(new OAEPParameterSpec("SHA-384", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
76 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
77 |
test(new OAEPParameterSpec("SHA-512", "MGF1", |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
78 |
MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
79 |
if (failed) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
80 |
throw new Exception("Test failed"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
81 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
82 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
83 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
84 |
/* |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
85 |
* Test with one byte, the max bytes, and the max + 1 bytes allowed by |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
86 |
* the RSA key size and the digest algorithm |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
87 |
*/ |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
88 |
static void test(OAEPParameterSpec spec) throws Exception { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
89 |
int dlen = 0; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
90 |
String algo; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
91 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
92 |
// For default OAEPParameterSpec case (SHA1) |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
93 |
if (spec == null) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
94 |
dlen = 20; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
95 |
algo = "Default"; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
96 |
} else { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
97 |
// Use the digest algorith provided in the spec |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
98 |
algo = spec.getDigestAlgorithm(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
99 |
if (algo.equals("MD5")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
100 |
dlen = 16; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
101 |
} else if (algo.equals("SHA1")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
102 |
dlen = 20; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
103 |
} else if (algo.equals("SHA-224")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
104 |
dlen = 28; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
105 |
} else if (algo.equals("SHA-256")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
106 |
dlen = 32; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
107 |
} else if (algo.equals("SHA-384")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
108 |
dlen = 48; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
109 |
} else if (algo.equals("SHA-512")) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
110 |
dlen = 64; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
111 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
112 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
113 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
114 |
// OAEP maximum length for a given digest algorith & RSA key length |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
115 |
int max = ((publicKey.getModulus().bitLength() / 8) - (2 * dlen) - 2); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
116 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
117 |
// Test with data length of 1 |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
118 |
try { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
119 |
testEncryptDecrypt(spec, 1); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
120 |
} catch (Exception e) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
121 |
System.out.println(algo + " failed with data length of 1"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
122 |
e.printStackTrace(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
123 |
failed = true; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
124 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
125 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
126 |
// Test with data length of maximum allowed |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
127 |
try { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
128 |
testEncryptDecrypt(spec, max); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
129 |
} catch (Exception e) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
130 |
System.out.println(algo + " failed with data length of " + max); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
131 |
e.printStackTrace(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
132 |
failed = true; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
133 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
134 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
135 |
// Test with data length of maximum allowed + 1 |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
136 |
try { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
137 |
testEncryptDecrypt(spec, max + 1); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
138 |
throw new Exception(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
139 |
} catch (IllegalBlockSizeException ie) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
140 |
// expected to fail |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
141 |
} catch (Exception e) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
142 |
System.err.println(algo + " failed with data length of " + |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
143 |
(max + 1)); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
144 |
e.printStackTrace(); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
145 |
failed = true; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
146 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
147 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
148 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
149 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
150 |
private static void testEncryptDecrypt(OAEPParameterSpec spec, |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
151 |
int dataLength) throws Exception { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
152 |
System.out.println("Testing OAEP with hash " + |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
153 |
((spec != null) ? spec.getDigestAlgorithm() : "Default") + |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
154 |
", " + dataLength + " bytes"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
155 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
156 |
Cipher c = Cipher.getInstance("RSA/ECB/OAEPPadding", cp); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
157 |
if (spec != null) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
158 |
c.init(Cipher.ENCRYPT_MODE, publicKey, spec); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
159 |
} else { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
160 |
c.init(Cipher.ENCRYPT_MODE, publicKey); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
161 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
162 |
|
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
163 |
byte[] data = new byte[dataLength]; |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
164 |
byte[] enc = c.doFinal(data); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
165 |
if (spec != null) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
166 |
c.init(Cipher.DECRYPT_MODE, privateKey, spec); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
167 |
} else { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
168 |
c.init(Cipher.DECRYPT_MODE, privateKey); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
169 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
170 |
byte[] dec = c.doFinal(enc); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
171 |
if (Arrays.equals(data, dec) == false) { |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
172 |
throw new Exception("Data does not match"); |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
173 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
174 |
} |
071b67eb2d0d
8020081: Cipher with OAEPPadding and OAEPParameterSpec can't be created
ascarpino
parents:
diff
changeset
|
175 |
} |