aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/prov/src/test/java/org/spongycastle/jce/provider/test/DoFinalTest.java
blob: fd0da3e50610ff0e0a0e150c030e82023e9c856b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.spongycastle.jce.provider.test;

import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.util.test.SimpleTestResult;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;

/**
 * check that doFinal is properly reseting the cipher.
 */
public class DoFinalTest
    implements Test
{
    public DoFinalTest()
    {
    }

    private boolean equalArray(
        byte[]  a,
        int        aOff,
        byte[]  b,
        int        length)
    {
        if (aOff + a.length < length)
        {
            return false;
        }
        
        if (b.length < length)
        {
            return false;
        }
        
        for (int i = 0; i != length; i++)
        {
            if (a[aOff + i] != b[i])
            {
                return false;
            }
        }

        return true;
    }
    
    public TestResult checkCipher(
        String    cipherName)
    {
        String lCode = "ABCDEFGHIJKLMNOPQRSTUVWXY0123456789";
        String  baseAlgorithm;
        int     index = cipherName.indexOf('/');

        if (index > 0)
        {
            baseAlgorithm = cipherName.substring(0, index);
        }
        else
        {
            baseAlgorithm = cipherName;
        }
        
        try
        {
            KeyGenerator    kGen = KeyGenerator.getInstance(baseAlgorithm, "SC");
            Cipher          cipher = Cipher.getInstance(cipherName, "SC");
            Key             key = kGen.generateKey();

            cipher.init(Cipher.ENCRYPT_MODE, key);

            byte[] encrypted = cipher.doFinal(lCode.getBytes());

            // 2nd try
            byte[]    encrypted2 = cipher.doFinal(lCode.getBytes());

            if (encrypted.length != encrypted2.length)
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + encrypted2.length);
            }

            if (!equalArray(encrypted, 0, encrypted2, encrypted.length))
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - first two arrays not equal");
            }
            
            // 3rd try
            byte[]  enc1 = cipher.update(lCode.getBytes());
            byte[]  enc2 = cipher.doFinal();

            if ((enc1.length + enc2.length) != encrypted.length)
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + (enc1.length + enc2.length));
            }

            if (!equalArray(encrypted, 0, enc1, enc1.length))
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - enc1 array not equal");
            }
            
            if (!equalArray(encrypted, enc1.length, enc2, enc2.length))
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - enc1 array not equal");
            }
            
            enc1 = cipher.update(lCode.getBytes());
            
            if (!equalArray(encrypted, 0, enc1, enc1.length))
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - 2nd enc1 array not equal");
            }
            
            int len = cipher.doFinal(enc1, 0);
            if ((enc1.length + len) != encrypted.length)
            {
                return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - expected length " + encrypted.length + " got " + (enc1.length + len));
            }
        }
        catch (Exception e)
        {
            return new SimpleTestResult(false, getName() + ": Failed " + cipherName + " - exception " + e.toString());
        }

        return new SimpleTestResult(true, getName() + ": Okay");
    }

    public TestResult perform()
    {
        TestResult    result = checkCipher("RC4");
        
        if (!result.isSuccessful())
        {
            return result;
        }
        
        result = checkCipher("DES/CBC/PKCS5Padding");
        
        if (!result.isSuccessful())
        {
            return result;
        }
        
        return checkCipher("Rijndael");
    }
    
    public String getName()
    {
        return "DoFinalTest";
    }

    public static void main(
        String[]    args)
    {
        Security.addProvider(new BouncyCastleProvider());

        Test            test = new DoFinalTest();
        TestResult      result = test.perform();

        System.out.println(result.toString());
    }
}