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 BlockCipherMonteCarloTest extends SimpleTest {
15: int id;
16: int iterations;
17: BlockCipher engine;
18: CipherParameters param;
19: byte[] input;
20: byte[] output;
21:
22: public BlockCipherMonteCarloTest(int id, int iterations,
23: BlockCipher engine, CipherParameters param, String input,
24: String output) {
25: this .id = id;
26: this .iterations = iterations;
27: this .engine = engine;
28: this .param = param;
29: this .input = Hex.decode(input);
30: this .output = Hex.decode(output);
31: }
32:
33: public String getName() {
34: return engine.getAlgorithmName() + " Monte Carlo Test " + id;
35: }
36:
37: public void performTest() throws Exception {
38: BufferedBlockCipher cipher = new BufferedBlockCipher(engine);
39:
40: cipher.init(true, param);
41:
42: byte[] out = new byte[input.length];
43:
44: System.arraycopy(input, 0, out, 0, out.length);
45:
46: for (int i = 0; i != iterations; i++) {
47: int len1 = cipher.processBytes(out, 0, out.length, out, 0);
48:
49: cipher.doFinal(out, len1);
50: }
51:
52: if (!areEqual(out, output)) {
53: fail("failed - " + "expected "
54: + new String(Hex.encode(output)) + " got "
55: + new String(Hex.encode(out)));
56: }
57:
58: cipher.init(false, param);
59:
60: for (int i = 0; i != iterations; i++) {
61: int len1 = cipher.processBytes(out, 0, out.length, out, 0);
62:
63: cipher.doFinal(out, len1);
64: }
65:
66: if (!areEqual(input, out)) {
67: fail("failed reversal");
68: }
69: }
70: }
|