aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/prov/src/test/java/org/spongycastle/jce/provider/test/BaseBlockCipherTest.java
blob: f80ec5f324d4dc88939ca1b78b382e642845be52 (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
package org.spongycastle.jce.provider.test;

import org.spongycastle.util.encoders.Hex;
import org.spongycastle.util.test.SimpleTest;
import org.spongycastle.util.test.TestFailedException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

public abstract class BaseBlockCipherTest
    extends SimpleTest
{
    String algorithm;

    BaseBlockCipherTest(
        String algorithm)
    {
        this.algorithm = algorithm;
    }

    public String getName()
    {
        return algorithm;
    }

    protected void oidTest(String[] oids, String[] names, int groupSize)
        throws Exception
    {
        byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
        IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);

        for (int i = 0; i != oids.length; i++)
        {
            Cipher c1 = Cipher.getInstance(oids[i], "SC");
            Cipher c2 = Cipher.getInstance(names[i], "SC");
            KeyGenerator kg = KeyGenerator.getInstance(oids[i], "SC");

            SecretKey k = kg.generateKey();

            if (names[i].indexOf("/ECB/") > 0)
            {
                c1.init(Cipher.ENCRYPT_MODE, k);
                c2.init(Cipher.DECRYPT_MODE, k);
            }
            else
            {
                c1.init(Cipher.ENCRYPT_MODE, k, ivSpec);
                c2.init(Cipher.DECRYPT_MODE, k, ivSpec);
            }

            byte[] result = c2.doFinal(c1.doFinal(data));

            if (!areEqual(data, result))
            {
                fail("failed OID test");
            }

            if (k.getEncoded().length != (16 + ((i / groupSize) * 8)))
            {
                fail("failed key length test");
            }
        }
    }

    protected void wrapOidTest(String[] oids, String name)
        throws Exception
    {
        byte[] data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

        for (int i = 0; i != oids.length; i++)
        {
            Cipher c1 = Cipher.getInstance(oids[i], "SC");
            Cipher c2 = Cipher.getInstance(name, "SC");
            KeyGenerator kg = KeyGenerator.getInstance(oids[i], "SC");

            SecretKey k = kg.generateKey();

            c1.init(Cipher.WRAP_MODE, k);
            c2.init(Cipher.UNWRAP_MODE, k);

            Key wKey = c2.unwrap(c1.wrap(new SecretKeySpec(data, algorithm)), algorithm, Cipher.SECRET_KEY);

            if (!areEqual(data, wKey.getEncoded()))
            {
                fail("failed wrap OID test");
            }

            if (k.getEncoded().length != (16 + (i * 8)))
            {
                fail("failed key length test");
            }
        }
    }

    protected void wrapTest(
        int     id,
        String  wrappingAlgorithm,
        byte[]  kek,
        byte[]  in,
        byte[]  out)
        throws Exception
    {
        Cipher wrapper = Cipher.getInstance(wrappingAlgorithm, "SC");

        wrapper.init(Cipher.WRAP_MODE, new SecretKeySpec(kek, algorithm));

        try
        {
            byte[]  cText = wrapper.wrap(new SecretKeySpec(in, algorithm));
            if (!areEqual(cText, out))
            {
                fail("failed wrap test " + id  + " expected " + new String(Hex.encode(out)) + " got " + new String(Hex.encode(cText)));
            }
        }
        catch (TestFailedException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            fail("failed wrap test exception " + e.toString(), e);
        }

        wrapper.init(Cipher.UNWRAP_MODE, new SecretKeySpec(kek, algorithm));

        try
        {
            Key  pText = wrapper.unwrap(out, algorithm, Cipher.SECRET_KEY);
            if (!areEqual(pText.getEncoded(), in))
            {
                fail("failed unwrap test " + id  + " expected " + new String(Hex.encode(in)) + " got " + new String(Hex.encode(pText.getEncoded())));
            }
        }
        catch (Exception e)
        {
            fail("failed unwrap test exception " + e.toString(), e);
        }
    }
}