~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/crypto/test/CTSTest.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

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