01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.BlockCipher;
04: import org.bouncycastle.crypto.DataLengthException;
05: import org.bouncycastle.crypto.params.KeyParameter;
06: import org.bouncycastle.util.test.SimpleTest;
07:
08: public abstract class CipherTest extends SimpleTest {
09: private SimpleTest[] _tests;
10: private BlockCipher _engine;
11: private KeyParameter _validKey;
12:
13: // protected CipherTest(
14: // SimpleTest[] tests)
15: // {
16: // _tests = tests;
17: // }
18:
19: protected CipherTest(SimpleTest[] tests, BlockCipher engine,
20: KeyParameter validKey) {
21: _tests = tests;
22: _engine = engine;
23: _validKey = validKey;
24: }
25:
26: public abstract String getName();
27:
28: public void performTest() throws Exception {
29: for (int i = 0; i != _tests.length; i++) {
30: _tests[i].performTest();
31: }
32:
33: if (_engine != null) {
34: //
35: // state tests
36: //
37: byte[] buf = new byte[16];
38:
39: try {
40: _engine.processBlock(buf, 0, buf, 0);
41:
42: fail("failed initialisation check");
43: } catch (IllegalStateException e) {
44: // expected
45: }
46:
47: bufferSizeCheck((_engine));
48: }
49: }
50:
51: private void bufferSizeCheck(BlockCipher engine) {
52: byte[] correctBuf = new byte[engine.getBlockSize()];
53: byte[] shortBuf = new byte[correctBuf.length / 2];
54:
55: engine.init(true, _validKey);
56:
57: try {
58: engine.processBlock(shortBuf, 0, correctBuf, 0);
59:
60: fail("failed short input check");
61: } catch (DataLengthException e) {
62: // expected
63: }
64:
65: try {
66: engine.processBlock(correctBuf, 0, shortBuf, 0);
67:
68: fail("failed short output check");
69: } catch (DataLengthException e) {
70: // expected
71: }
72:
73: engine.init(false, _validKey);
74:
75: try {
76: engine.processBlock(shortBuf, 0, correctBuf, 0);
77:
78: fail("failed short input check");
79: } catch (DataLengthException e) {
80: // expected
81: }
82:
83: try {
84: engine.processBlock(correctBuf, 0, shortBuf, 0);
85:
86: fail("failed short output check");
87: } catch (DataLengthException e) {
88: // expected
89: }
90: }
91: }
|