1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package org.xwiki.crypto.cipher.internal.symmetric.factory; |
22 |
|
|
23 |
|
import java.io.ByteArrayInputStream; |
24 |
|
import java.io.ByteArrayOutputStream; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.OutputStream; |
28 |
|
|
29 |
|
import org.junit.BeforeClass; |
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.junit.rules.ExpectedException; |
33 |
|
import org.xwiki.crypto.cipher.Cipher; |
34 |
|
import org.xwiki.crypto.cipher.CipherFactory; |
35 |
|
import org.xwiki.crypto.params.cipher.asymmetric.AsymmetricCipherParameters; |
36 |
|
import org.xwiki.crypto.params.cipher.symmetric.SymmetricCipherParameters; |
37 |
|
|
38 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
39 |
|
import static org.hamcrest.CoreMatchers.is; |
40 |
|
import static org.hamcrest.CoreMatchers.not; |
41 |
|
import static org.hamcrest.CoreMatchers.nullValue; |
42 |
|
import static org.junit.Assert.assertThat; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@version |
48 |
|
|
|
|
| 93.4% |
Uncovered Elements: 10 (152) |
Complexity: 28 |
Complexity Density: 0.24 |
|
49 |
|
public abstract class AbstractSymmetricCipherFactoryTest |
50 |
|
{ |
51 |
|
|
52 |
|
private static final String TEXT = "Congress shall make no law respecting an establishment of religion, or " |
53 |
|
+ "prohibiting the free exercise thereof; or abridging the freedom of speech, " |
54 |
|
+ "or of the press; or the right of the people peaceably to assemble, and to " |
55 |
|
+ "petition the Government for a redress of grievances."; |
56 |
|
|
57 |
|
protected static final byte[] BYTES = TEXT.getBytes(); |
58 |
|
|
59 |
|
|
60 |
|
private static final String ANOTHER_TEXT = "The length of this text is 113 byte. This is 1 byte more " |
61 |
|
+ "than a multiple of block size for 128 bit block ciphers."; |
62 |
|
|
63 |
|
protected static final byte[] ANOTHER_BYTES = ANOTHER_TEXT.getBytes(); |
64 |
|
|
65 |
|
|
66 |
|
protected static final byte[] KEY8 = { |
67 |
|
0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73 }; |
68 |
|
|
69 |
|
|
70 |
|
protected static final byte[] KEY16 = { |
71 |
|
0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73, |
72 |
|
0x20, 0x74, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79 }; |
73 |
|
|
74 |
|
|
75 |
|
protected static final byte[] KEY32 = { |
76 |
|
0x58, 0x57, 0x69, 0x6b, 0x69, 0x20, 0x69, 0x73, |
77 |
|
0x20, 0x74, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, |
78 |
|
0x79, 0x65, 0x6b, 0x20, 0x65, 0x68, 0x74, 0x20, |
79 |
|
0x73, 0x69, 0x20, 0x69, 0x6b, 0x69, 0x57, 0x58 }; |
80 |
|
|
81 |
|
|
82 |
|
protected static final byte[] IV8 = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 }; |
83 |
|
|
84 |
|
|
85 |
|
protected static final byte[] IV16 = { |
86 |
|
0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12, |
87 |
|
0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 }; |
88 |
|
|
89 |
|
protected CipherFactory factory; |
90 |
|
|
91 |
|
protected String CIPHER_ALGO; |
92 |
|
protected int BLOCK_SIZE; |
93 |
|
protected int KEY_SIZE; |
94 |
|
protected int[] SUPPORTED_KEY_SIZE; |
95 |
|
|
96 |
|
protected int BYTES_ENCRYPTED_SIZE; |
97 |
|
protected int ANOTHER_BYTES_ENCRYPTED_SIZE; |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
99 |
7 |
@Test... |
100 |
|
public void testGetCipherFactoryProperties() throws Exception |
101 |
|
{ |
102 |
7 |
assertThat(factory.getCipherAlgorithmName(),equalTo(CIPHER_ALGO)); |
103 |
7 |
assertThat(factory.getIVSize(),equalTo(BLOCK_SIZE)); |
104 |
7 |
assertThat(factory.getKeySize(),equalTo(KEY_SIZE)); |
105 |
7 |
assertThat(factory.getSupportedKeySizes(),equalTo(SUPPORTED_KEY_SIZE)); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
4-
|
|
108 |
7 |
@Test... |
109 |
|
public void testGetCipherProperties() throws Exception |
110 |
|
{ |
111 |
7 |
Cipher cipher = getCipher(true); |
112 |
|
|
113 |
7 |
assertThat(cipher.getAlgorithmName(),equalTo(CIPHER_ALGO)); |
114 |
7 |
assertThat(cipher.getOutputBlockSize(),equalTo(BLOCK_SIZE)); |
115 |
7 |
assertThat(cipher.isForEncryption(),is(true)); |
116 |
|
|
117 |
7 |
cipher = getCipher(false); |
118 |
7 |
assertThat(cipher.getAlgorithmName(),equalTo(CIPHER_ALGO)); |
119 |
7 |
assertThat(cipher.getOutputBlockSize(),equalTo(BLOCK_SIZE)); |
120 |
7 |
assertThat(cipher.isForEncryption(),is(false)); |
121 |
|
} |
122 |
|
|
123 |
|
private static Cipher encryptCipher; |
124 |
|
private static Cipher decryptCipher; |
125 |
|
|
126 |
|
protected static byte[] encrypted; |
127 |
|
protected static byte[] anotherEncrypted; |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
129 |
7 |
@BeforeClass... |
130 |
|
public static void cleanUpCaches() { |
131 |
7 |
encryptCipher = null; |
132 |
7 |
decryptCipher = null; |
133 |
7 |
encrypted = null; |
134 |
7 |
anotherEncrypted = null; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@return |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
122 |
protected Cipher getCipher(boolean forEncryption)... |
142 |
|
{ |
143 |
122 |
return getCipher(forEncryption, false); |
144 |
|
} |
145 |
|
|
146 |
|
abstract Cipher getCipherInstance(boolean forEncryption); |
147 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
148 |
122 |
private Cipher getCipher(boolean forEncryption, boolean reset)... |
149 |
|
{ |
150 |
122 |
Cipher cipher = (forEncryption) ? encryptCipher : decryptCipher; |
151 |
|
|
152 |
122 |
if (reset || cipher == null) { |
153 |
14 |
cipher = getCipherInstance(forEncryption); |
154 |
14 |
if (forEncryption) { |
155 |
7 |
encryptCipher = cipher; |
156 |
|
} else { |
157 |
7 |
decryptCipher = cipher; |
158 |
|
} |
159 |
|
} |
160 |
122 |
return cipher; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
@return |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
167 |
64 |
protected byte[] getEncrypted() throws Exception... |
168 |
|
{ |
169 |
64 |
if (encrypted == null) { |
170 |
5 |
encrypted = getCipher(true).doFinal(BYTES); |
171 |
|
} |
172 |
64 |
return encrypted; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@return |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
179 |
64 |
protected byte[] getAnotherEncrypted() throws Exception... |
180 |
|
{ |
181 |
64 |
if (anotherEncrypted == null) { |
182 |
5 |
anotherEncrypted = getCipher(true).doFinal(ANOTHER_BYTES); |
183 |
|
} |
184 |
64 |
return anotherEncrypted; |
185 |
|
} |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
187 |
7 |
@Test... |
188 |
|
public void testCipherOneShotEncryption() throws Exception |
189 |
|
{ |
190 |
7 |
Cipher cipher = getCipher(true); |
191 |
|
|
192 |
7 |
assertThat(getEncrypted().length,equalTo(BYTES_ENCRYPTED_SIZE)); |
193 |
7 |
assertThat(cipher.doFinal(BYTES),equalTo(getEncrypted())); |
194 |
|
|
195 |
7 |
assertThat(getAnotherEncrypted().length,equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE)); |
196 |
7 |
assertThat(cipher.doFinal(ANOTHER_BYTES),equalTo(getAnotherEncrypted())); |
197 |
|
} |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
199 |
7 |
@Test... |
200 |
|
public void testCipherOneShotDecryption() throws Exception |
201 |
|
{ |
202 |
7 |
Cipher cipher = getCipher(false); |
203 |
|
|
204 |
7 |
byte[] result = cipher.doFinal(getEncrypted()); |
205 |
7 |
assertThat(result.length, equalTo(BYTES.length)); |
206 |
7 |
assertThat(result,equalTo(BYTES)); |
207 |
|
|
208 |
7 |
result = cipher.doFinal(getAnotherEncrypted()); |
209 |
7 |
assertThat(result.length, equalTo(ANOTHER_BYTES.length)); |
210 |
7 |
assertThat(result, equalTo(ANOTHER_BYTES)); |
211 |
|
} |
212 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 3 |
Complexity Density: 0.14 |
|
213 |
28 |
private byte[] getProgressive(boolean forEncryption, byte[] bytes, int size) throws Exception... |
214 |
|
{ |
215 |
28 |
Cipher cipher = getCipher(forEncryption); |
216 |
28 |
byte[] result = new byte[size]; |
217 |
28 |
byte[] tmp; |
218 |
28 |
int len = 0; |
219 |
|
|
220 |
28 |
tmp = cipher.update(bytes, 0, BLOCK_SIZE + 1); |
221 |
28 |
assertThat(tmp, not(nullValue())); |
222 |
28 |
System.arraycopy(tmp, 0, result, 0, len = tmp.length); |
223 |
|
|
224 |
28 |
assertThat(cipher.update(bytes, BLOCK_SIZE + 1, BLOCK_SIZE - 1), nullValue()); |
225 |
|
|
226 |
28 |
tmp = cipher.update(bytes, BLOCK_SIZE * 2, 1); |
227 |
28 |
assertThat(tmp, not(nullValue())); |
228 |
28 |
System.arraycopy(tmp, 0, result, len, tmp.length); |
229 |
28 |
len += tmp.length; |
230 |
|
|
231 |
28 |
tmp = cipher.update(bytes, ((BLOCK_SIZE * 2) + 1), bytes.length - ((BLOCK_SIZE * 2) + 1)); |
232 |
28 |
assertThat(tmp, not(nullValue())); |
233 |
28 |
System.arraycopy(tmp, 0, result, len, tmp.length); |
234 |
28 |
len += tmp.length; |
235 |
|
|
236 |
28 |
tmp = cipher.doFinal(); |
237 |
28 |
if (forEncryption || tmp != null) { |
238 |
21 |
assertThat(tmp, not(nullValue())); |
239 |
21 |
System.arraycopy(tmp, 0, result, len, tmp.length); |
240 |
21 |
len += tmp.length; |
241 |
|
} |
242 |
|
|
243 |
28 |
return result; |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4-
|
|
246 |
7 |
@Test... |
247 |
|
public void testCipherProgressiveEncryption() throws Exception |
248 |
|
{ |
249 |
7 |
assertThat(getProgressive(true, BYTES, BYTES_ENCRYPTED_SIZE), equalTo(getEncrypted())); |
250 |
7 |
assertThat(getProgressive(true, ANOTHER_BYTES, ANOTHER_BYTES_ENCRYPTED_SIZE), equalTo(getAnotherEncrypted())); |
251 |
|
} |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4-
|
|
253 |
7 |
@Test... |
254 |
|
public void testCipherProgressiveDecryption() throws Exception |
255 |
|
{ |
256 |
7 |
assertThat(getProgressive(false, getEncrypted(), BYTES.length), equalTo(BYTES)); |
257 |
7 |
assertThat(getProgressive(false, getAnotherEncrypted(), ANOTHER_BYTES.length), equalTo(ANOTHER_BYTES)); |
258 |
|
} |
259 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
4-
|
|
260 |
7 |
@Test... |
261 |
|
public void testCipherOutputStreamEncryption() throws Exception |
262 |
|
{ |
263 |
7 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTES_ENCRYPTED_SIZE); |
264 |
7 |
OutputStream encos = getCipher(true).getOutputStream(baos); |
265 |
7 |
encos.write(BYTES); |
266 |
7 |
encos.close(); |
267 |
|
|
268 |
7 |
assertThat(baos.toByteArray(), equalTo(getEncrypted())); |
269 |
|
|
270 |
7 |
baos = new ByteArrayOutputStream(ANOTHER_BYTES_ENCRYPTED_SIZE); |
271 |
7 |
encos = getCipher(true).getOutputStream(baos); |
272 |
7 |
encos.write(ANOTHER_BYTES); |
273 |
7 |
encos.close(); |
274 |
|
|
275 |
7 |
assertThat(baos.toByteArray(), equalTo(getAnotherEncrypted())); |
276 |
|
} |
277 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
4-
|
|
278 |
7 |
@Test... |
279 |
|
public void testCipherOutputStreamDecryption() throws Exception |
280 |
|
{ |
281 |
7 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTES.length); |
282 |
7 |
OutputStream encos = getCipher(false).getOutputStream(baos); |
283 |
7 |
encos.write(getEncrypted()); |
284 |
7 |
encos.close(); |
285 |
|
|
286 |
7 |
assertThat(baos.toByteArray(), equalTo(BYTES)); |
287 |
|
|
288 |
7 |
baos = new ByteArrayOutputStream(ANOTHER_BYTES.length); |
289 |
7 |
encos = getCipher(false).getOutputStream(baos); |
290 |
7 |
encos.write(getAnotherEncrypted()); |
291 |
7 |
encos.close(); |
292 |
|
|
293 |
7 |
assertThat(baos.toByteArray(), equalTo(ANOTHER_BYTES)); |
294 |
|
} |
295 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
296 |
28 |
private int readAll(InputStream decis, byte[] out) throws IOException... |
297 |
|
{ |
298 |
28 |
int readLen = 0, len = 0; |
299 |
? |
while( (readLen = decis.read(out, len, BLOCK_SIZE + 1)) > 0 ) { |
300 |
555 |
len += readLen; |
301 |
|
} |
302 |
28 |
decis.close(); |
303 |
28 |
return len; |
304 |
|
} |
305 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
4-
|
|
306 |
7 |
@Test... |
307 |
|
public void testCipheInputStreamEncryption() throws Exception |
308 |
|
{ |
309 |
7 |
ByteArrayInputStream bais = new ByteArrayInputStream(BYTES); |
310 |
7 |
InputStream decis = getCipher(true).getInputStream(bais); |
311 |
7 |
byte[] buf = new byte[BYTES_ENCRYPTED_SIZE]; |
312 |
7 |
assertThat(readAll(decis, buf), equalTo(BYTES_ENCRYPTED_SIZE)); |
313 |
7 |
assertThat(buf, equalTo(getEncrypted())); |
314 |
|
|
315 |
7 |
bais = new ByteArrayInputStream(ANOTHER_BYTES); |
316 |
7 |
decis = getCipher(true).getInputStream(bais); |
317 |
7 |
buf = new byte[ANOTHER_BYTES_ENCRYPTED_SIZE]; |
318 |
7 |
assertThat(readAll(decis, buf), equalTo(ANOTHER_BYTES_ENCRYPTED_SIZE)); |
319 |
7 |
assertThat(buf, equalTo(getAnotherEncrypted())); |
320 |
|
} |
321 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
4-
|
|
322 |
7 |
@Test... |
323 |
|
public void testCipheInputStreamDecryption() throws Exception |
324 |
|
{ |
325 |
7 |
ByteArrayInputStream bais = new ByteArrayInputStream(getEncrypted()); |
326 |
7 |
InputStream decis = getCipher(false).getInputStream(bais); |
327 |
7 |
byte[] buf = new byte[BYTES.length]; |
328 |
7 |
assertThat(readAll(decis, buf), equalTo(BYTES.length)); |
329 |
7 |
assertThat(buf, equalTo(BYTES)); |
330 |
|
|
331 |
7 |
bais = new ByteArrayInputStream(getAnotherEncrypted()); |
332 |
7 |
decis = getCipher(false).getInputStream(bais); |
333 |
7 |
buf = new byte[ANOTHER_BYTES.length]; |
334 |
7 |
assertThat(readAll(decis, buf), equalTo(ANOTHER_BYTES.length)); |
335 |
7 |
assertThat(buf, equalTo(ANOTHER_BYTES)); |
336 |
|
} |
337 |
|
|
338 |
|
@Rule public ExpectedException thrown = ExpectedException.none(); |
339 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
340 |
|
class WrongParameters implements SymmetricCipherParameters |
341 |
|
{ } |
342 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4-
|
|
343 |
0 |
@Test... |
344 |
|
public void testCipherWithWrongParameters() throws Exception |
345 |
|
{ |
346 |
0 |
thrown.expect(IllegalArgumentException.class); |
347 |
0 |
thrown.expectMessage("Invalid parameters for cipher: " + WrongParameters.class.getName()); |
348 |
0 |
factory.getInstance(true, new WrongParameters()); |
349 |
|
} |
350 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
351 |
|
class AsymmetricParameters implements AsymmetricCipherParameters |
352 |
|
{ } |
353 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4-
|
|
354 |
0 |
@Test... |
355 |
|
public void testCipherWithAsymmetricParameters() throws Exception |
356 |
|
{ |
357 |
0 |
thrown.expect(IllegalArgumentException.class); |
358 |
0 |
thrown.expectMessage("Unexpected parameters received for a symmetric cipher: " |
359 |
|
+ AsymmetricParameters.class.getName()); |
360 |
0 |
factory.getInstance(true, new AsymmetricParameters()); |
361 |
|
} |
362 |
|
} |