32411
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 static java.lang.System.out;
|
|
25 |
|
|
26 |
import java.security.InvalidAlgorithmParameterException;
|
|
27 |
import java.security.InvalidKeyException;
|
|
28 |
import java.security.NoSuchAlgorithmException;
|
|
29 |
import java.security.NoSuchProviderException;
|
|
30 |
import java.security.spec.AlgorithmParameterSpec;
|
|
31 |
|
|
32 |
import javax.crypto.BadPaddingException;
|
|
33 |
import javax.crypto.Cipher;
|
|
34 |
import javax.crypto.IllegalBlockSizeException;
|
|
35 |
import javax.crypto.KeyGenerator;
|
|
36 |
import javax.crypto.NoSuchPaddingException;
|
|
37 |
import javax.crypto.SecretKey;
|
|
38 |
import javax.crypto.ShortBufferException;
|
|
39 |
import javax.crypto.spec.IvParameterSpec;
|
|
40 |
import javax.crypto.spec.SecretKeySpec;
|
|
41 |
|
|
42 |
/**
|
|
43 |
* This is a abstract class used to test various ciphers
|
|
44 |
*/
|
|
45 |
public abstract class TestCipher {
|
|
46 |
|
|
47 |
private final String SUNJCE = "SunJCE";
|
|
48 |
private final String ALGORITHM;
|
|
49 |
private final String[] MODES;
|
|
50 |
private final String[] PADDINGS;
|
|
51 |
|
|
52 |
/* Used to test cipher with different key strengths
|
|
53 |
Key size tested is increment of KEYCUTTER from MINIMUM_KEY_SIZE to
|
|
54 |
maximum allowed keysize.
|
|
55 |
DES/DESede/Blowfish work with currently selected key sizes.
|
|
56 |
*/
|
|
57 |
private final int variousKeySize;
|
|
58 |
private final int KEYCUTTER = 8;
|
|
59 |
private final int MINIMUM_KEY_SIZE = 32;
|
|
60 |
|
|
61 |
// Used to assert that Encryption/Decryption works with same buffer
|
|
62 |
// TEXT_LEN is multiple of blocks in order to work against ciphers w/ NoPadding
|
|
63 |
private final int TEXT_LEN = 800;
|
|
64 |
private final int ENC_OFFSET = 6;
|
|
65 |
private final int STORAGE_OFFSET = 3;
|
|
66 |
private final int PAD_BYTES = 16;
|
|
67 |
|
|
68 |
private final byte[] IV;
|
|
69 |
private final byte[] INPUT_TEXT;
|
|
70 |
|
|
71 |
TestCipher(String algo, String[] modes, String[] paddings,
|
|
72 |
boolean keyStrength) throws NoSuchAlgorithmException {
|
|
73 |
ALGORITHM = algo;
|
|
74 |
MODES = modes;
|
|
75 |
PADDINGS = paddings;
|
|
76 |
this.variousKeySize
|
|
77 |
= keyStrength ? Cipher.getMaxAllowedKeyLength(ALGORITHM) : 0;
|
|
78 |
|
|
79 |
IV = generateBytes(8);
|
|
80 |
INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
|
|
81 |
}
|
|
82 |
|
|
83 |
TestCipher(String algo, String[] modes, String[] paddings) {
|
|
84 |
ALGORITHM = algo;
|
|
85 |
MODES = modes;
|
|
86 |
PADDINGS = paddings;
|
|
87 |
variousKeySize = 0;
|
|
88 |
|
|
89 |
IV = generateBytes(8);
|
|
90 |
INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
|
|
91 |
}
|
|
92 |
|
|
93 |
private static byte[] generateBytes(int length) {
|
|
94 |
byte[] bytes = new byte[length];
|
|
95 |
for (int i = 0; i < length; i++) {
|
|
96 |
bytes[i] = (byte) (i & 0xff);
|
|
97 |
}
|
|
98 |
return bytes;
|
|
99 |
}
|
|
100 |
|
|
101 |
private boolean isKeyStrenthSupported() {
|
|
102 |
return (variousKeySize != 0);
|
|
103 |
}
|
|
104 |
|
|
105 |
public void runAll() throws InvalidKeyException,
|
|
106 |
NoSuchPaddingException, InvalidAlgorithmParameterException,
|
|
107 |
ShortBufferException, IllegalBlockSizeException,
|
|
108 |
BadPaddingException, NoSuchAlgorithmException,
|
|
109 |
NoSuchProviderException {
|
|
110 |
|
|
111 |
for (String mode : MODES) {
|
|
112 |
for (String padding : PADDINGS) {
|
|
113 |
if (!isKeyStrenthSupported()) {
|
|
114 |
runTest(mode, padding, 0);
|
|
115 |
} else {
|
|
116 |
int keySize = variousKeySize;
|
|
117 |
while (keySize >= MINIMUM_KEY_SIZE) {
|
|
118 |
out.println("With Key Strength: " + keySize);
|
|
119 |
runTest(mode, padding, keySize);
|
|
120 |
keySize -= KEYCUTTER;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
private void runTest(String mo, String pad, int keySize)
|
|
128 |
throws NoSuchPaddingException, BadPaddingException,
|
|
129 |
ShortBufferException, IllegalBlockSizeException,
|
|
130 |
InvalidAlgorithmParameterException, InvalidKeyException,
|
|
131 |
NoSuchAlgorithmException, NoSuchProviderException {
|
|
132 |
|
|
133 |
String TRANSFORMATION = ALGORITHM + "/" + mo + "/" + pad;
|
|
134 |
out.println("Testing: " + TRANSFORMATION);
|
|
135 |
|
|
136 |
// Initialization
|
|
137 |
Cipher ci = Cipher.getInstance(TRANSFORMATION, SUNJCE);
|
|
138 |
KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM, SUNJCE);
|
|
139 |
if (keySize != 0) {
|
|
140 |
kg.init(keySize);
|
|
141 |
}
|
|
142 |
SecretKey key = kg.generateKey();
|
|
143 |
SecretKeySpec skeySpec = new SecretKeySpec(key.getEncoded(), ALGORITHM);
|
|
144 |
|
|
145 |
AlgorithmParameterSpec aps = new IvParameterSpec(IV);
|
|
146 |
if (mo.equalsIgnoreCase("ECB")) {
|
|
147 |
ci.init(Cipher.ENCRYPT_MODE, key);
|
|
148 |
} else {
|
|
149 |
ci.init(Cipher.ENCRYPT_MODE, key, aps);
|
|
150 |
}
|
|
151 |
|
|
152 |
// Encryption
|
|
153 |
int PAD_LEN = 0;
|
|
154 |
if (pad.equalsIgnoreCase("PKCS5Padding")) {
|
|
155 |
// Need to consider pad bytes
|
|
156 |
PAD_LEN = 8;
|
|
157 |
}
|
|
158 |
|
|
159 |
byte[] plainText = INPUT_TEXT.clone();
|
|
160 |
|
|
161 |
// Generate cipher and save to separate buffer
|
|
162 |
byte[] cipherText = ci.doFinal(INPUT_TEXT, ENC_OFFSET, TEXT_LEN);
|
|
163 |
|
|
164 |
// Generate cipher and save to same buffer
|
|
165 |
int offset = ci.update(
|
|
166 |
INPUT_TEXT, ENC_OFFSET, TEXT_LEN, INPUT_TEXT, STORAGE_OFFSET);
|
|
167 |
ci.doFinal(INPUT_TEXT, offset + STORAGE_OFFSET);
|
|
168 |
|
|
169 |
if (!equalsBlock(
|
|
170 |
INPUT_TEXT, STORAGE_OFFSET, cipherText, 0, cipherText.length)) {
|
|
171 |
throw new RuntimeException(
|
|
172 |
"Different ciphers generated with same buffer");
|
|
173 |
}
|
|
174 |
|
|
175 |
// Decryption
|
|
176 |
if (mo.equalsIgnoreCase("ECB")) {
|
|
177 |
ci.init(Cipher.DECRYPT_MODE, skeySpec);
|
|
178 |
} else {
|
|
179 |
ci.init(Cipher.DECRYPT_MODE, skeySpec, aps);
|
|
180 |
}
|
|
181 |
|
|
182 |
// Recover text from cipher and save to separate buffer
|
|
183 |
byte[] recoveredText = ci.doFinal(cipherText, 0, cipherText.length);
|
|
184 |
|
|
185 |
if (!equalsBlock(
|
|
186 |
plainText, ENC_OFFSET, recoveredText, 0,
|
|
187 |
recoveredText.length)) {
|
|
188 |
throw new RuntimeException(
|
|
189 |
"Recovered text not same as plain text");
|
|
190 |
} else {
|
|
191 |
out.println("Recovered and plain text are same");
|
|
192 |
}
|
|
193 |
|
|
194 |
// Recover text from cipher and save to same buffer
|
|
195 |
ci.update(INPUT_TEXT, STORAGE_OFFSET, TEXT_LEN + PAD_LEN, INPUT_TEXT,
|
|
196 |
ENC_OFFSET);
|
|
197 |
ci.doFinal(INPUT_TEXT, ENC_OFFSET);
|
|
198 |
|
|
199 |
if (!equalsBlock(
|
|
200 |
plainText, ENC_OFFSET, recoveredText, 0,
|
|
201 |
recoveredText.length)) {
|
|
202 |
throw new RuntimeException(
|
|
203 |
"Recovered text not same as plain text with same buffer");
|
|
204 |
} else {
|
|
205 |
out.println("Recovered and plain text are same with same buffer");
|
|
206 |
}
|
|
207 |
|
|
208 |
out.println("Test Passed.");
|
|
209 |
}
|
|
210 |
|
|
211 |
private static boolean equalsBlock(byte[] b1, int off1, byte[] b2, int off2,
|
|
212 |
int len) {
|
|
213 |
for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) {
|
|
214 |
if (b1[i] != b2[j]) {
|
|
215 |
return false;
|
|
216 |
}
|
|
217 |
}
|
|
218 |
return true;
|
|
219 |
}
|
|
220 |
}
|