summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/test/ResetTest.java
blob: efd0e06ce8c922e0d4c0f9c2a42262495bc157fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.bouncycastle.crypto.test;

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.DESEngine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

public class ResetTest
    extends SimpleTest
{
    private static final byte[]   input = Hex.decode("4e6f77206973207468652074696d6520666f7220616c6c20");
    private static final byte[]   output = Hex.decode("3fa40e8a984d48156a271787ab8883f9893d51ec4b563b53");
    public String getName()
    {
        return "Reset";
    }

    public void performTest()
        throws Exception
    {
        BufferedBlockCipher cipher = new BufferedBlockCipher(new DESEngine());

        KeyParameter param = new KeyParameter(Hex.decode("0123456789abcdef"));

        basicTrial(cipher, param);

        cipher.init(false, param);

        byte[] out = new byte[input.length];
        
        int len2 = cipher.processBytes(output, 0, output.length - 1, out, 0);

        try
        {
            cipher.doFinal(out, len2);
            fail("no DataLengthException - short input");
        }
        catch (DataLengthException e)
        {
            // ignore
        }

        len2 = cipher.processBytes(output, 0, output.length, out, 0);

        cipher.doFinal(out, len2);

        if (!areEqual(input, out))
        {
            fail("failed reversal one got " + new String(Hex.encode(out)));
        }

        len2 = cipher.processBytes(output, 0, output.length - 1, out, 0);

        try
        {
            cipher.doFinal(out, len2);
            fail("no DataLengthException - short output");
        }
        catch (DataLengthException e)
        {
            // ignore
        }

        len2 = cipher.processBytes(output, 0, output.length, out, 0);

        cipher.doFinal(out, len2);

        if (!areEqual(input, out))
        {
            fail("failed reversal two got " + new String(Hex.encode(out)));
        }
    }

    private void basicTrial(BufferedBlockCipher cipher, KeyParameter param)
        throws InvalidCipherTextException
    {
        cipher.init(true, param);

        byte[]  out = new byte[input.length];

        int len1 = cipher.processBytes(input, 0, input.length, out, 0);

        cipher.doFinal(out, len1);

        if (!areEqual(out, output))
        {
            fail("failed - " + "expected " + new String(Hex.encode(output)) + " got " + new String(Hex.encode(out)));
        }
    }

    public static void main(
        String[]    args)
    {
        runTest(new ResetTest());
    }
}