01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.BlockCipher;
04: import org.bouncycastle.crypto.BufferedBlockCipher;
05: import org.bouncycastle.crypto.CipherParameters;
06: import org.bouncycastle.util.encoders.Hex;
07: import org.bouncycastle.util.test.SimpleTest;
08:
09: /**
10: * a basic test that takes a cipher, key parameter, and an input
11: * and output string. This test wraps the engine in a buffered block
12: * cipher with padding disabled.
13: */
14: public class BlockCipherVectorTest extends SimpleTest {
15: int id;
16: BlockCipher engine;
17: CipherParameters param;
18: byte[] input;
19: byte[] output;
20:
21: public BlockCipherVectorTest(int id, BlockCipher engine,
22: CipherParameters param, String input, String output) {
23: this .id = id;
24: this .engine = engine;
25: this .param = param;
26: this .input = Hex.decode(input);
27: this .output = Hex.decode(output);
28: }
29:
30: public String getName() {
31: return engine.getAlgorithmName() + " Vector Test " + id;
32: }
33:
34: public void performTest() throws Exception {
35: BufferedBlockCipher cipher = new BufferedBlockCipher(engine);
36:
37: cipher.init(true, param);
38:
39: byte[] out = new byte[input.length];
40:
41: int len1 = cipher.processBytes(input, 0, input.length, out, 0);
42:
43: cipher.doFinal(out, len1);
44:
45: if (!areEqual(out, output)) {
46: fail("failed - " + "expected "
47: + new String(Hex.encode(output)) + " got "
48: + new String(Hex.encode(out)));
49: }
50:
51: cipher.init(false, param);
52:
53: int len2 = cipher
54: .processBytes(output, 0, output.length, out, 0);
55:
56: cipher.doFinal(out, len2);
57:
58: if (!areEqual(input, out)) {
59: fail("failed reversal got " + new String(Hex.encode(out)));
60: }
61: }
62: }
|