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.crypto.engines.DESEngine;
07: import org.bouncycastle.crypto.engines.SkipjackEngine;
08: import org.bouncycastle.crypto.modes.CBCBlockCipher;
09: import org.bouncycastle.crypto.modes.CTSBlockCipher;
10: import org.bouncycastle.crypto.params.KeyParameter;
11: import org.bouncycastle.crypto.params.ParametersWithIV;
12: import org.bouncycastle.util.encoders.Hex;
13: import org.bouncycastle.util.test.SimpleTest;
14:
15: /**
16: * CTS tester
17: */
18: public class CTSTest extends SimpleTest {
19: static byte[] in1 = Hex.decode("4e6f7720697320746865207420");
20: static byte[] in2 = Hex
21: .decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f0aaa");
22: static byte[] out1 = Hex.decode("9952f131588465033fa40e8a98");
23: static byte[] out2 = Hex.decode("358f84d01eb42988dc34efb994");
24: static byte[] out3 = Hex
25: .decode("170171cfad3f04530c509b0c1f0be0aefbd45a8e3755a873bff5ea198504b71683c6");
26:
27: private void testCTS(int id, BlockCipher cipher,
28: CipherParameters params, byte[] input, byte[] output)
29: throws Exception {
30: byte[] out = new byte[input.length];
31: BufferedBlockCipher engine = new CTSBlockCipher(cipher);
32:
33: engine.init(true, params);
34:
35: int len = engine.processBytes(input, 0, input.length, out, 0);
36:
37: engine.doFinal(out, len);
38:
39: if (!areEqual(output, out)) {
40: fail("failed encryption expected "
41: + new String(Hex.encode(output)) + " got "
42: + new String(Hex.encode(out)));
43: }
44:
45: engine.init(false, params);
46:
47: len = engine.processBytes(output, 0, output.length, out, 0);
48:
49: engine.doFinal(out, len);
50:
51: if (!areEqual(input, out)) {
52: fail("failed encryption expected "
53: + new String(Hex.encode(input)) + " got "
54: + new String(Hex.encode(out)));
55: }
56: }
57:
58: public String getName() {
59: return "CTS";
60: }
61:
62: public void performTest() throws Exception {
63: byte[] key1 = { (byte) 0x01, (byte) 0x23, (byte) 0x45,
64: (byte) 0x67, (byte) 0x89, (byte) 0xAB, (byte) 0xCD,
65: (byte) 0xEF };
66: byte[] key2 = { (byte) 0x01, (byte) 0x23, (byte) 0x45,
67: (byte) 0x67, (byte) 0x89, (byte) 0xAB, (byte) 0xCD,
68: (byte) 0xEF, (byte) 0xee, (byte) 0xff };
69: byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
70:
71: testCTS(1, new DESEngine(), new KeyParameter(key1), in1, out1);
72: testCTS(2, new CBCBlockCipher(new DESEngine()),
73: new ParametersWithIV(new KeyParameter(key1), iv), in1,
74: out2);
75: testCTS(3, new CBCBlockCipher(new SkipjackEngine()),
76: new ParametersWithIV(new KeyParameter(key2), iv), in2,
77: out3);
78: }
79:
80: public static void main(String[] args) {
81: runTest(new CTSTest());
82: }
83: }
|