~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/crypto/test/AESWrapTest.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.DataLengthException;
 
4
import org.bouncycastle.crypto.InvalidCipherTextException;
 
5
import org.bouncycastle.crypto.Wrapper;
 
6
import org.bouncycastle.crypto.engines.AESWrapEngine;
 
7
import org.bouncycastle.crypto.params.KeyParameter;
 
8
import org.bouncycastle.util.Arrays;
 
9
import org.bouncycastle.util.encoders.Hex;
 
10
import org.bouncycastle.util.test.SimpleTestResult;
 
11
import org.bouncycastle.util.test.Test;
 
12
import org.bouncycastle.util.test.TestResult;
 
13
 
 
14
/**
 
15
 * Wrap Test
 
16
 */
 
17
public class AESWrapTest
 
18
    implements Test
 
19
{
 
20
    public String getName()
 
21
    {
 
22
        return "AESWrap";
 
23
    }
 
24
 
 
25
    private TestResult wrapTest(
 
26
        int     id,
 
27
        byte[]  kek,
 
28
        byte[]  in,
 
29
        byte[]  out)
 
30
    {
 
31
        Wrapper wrapper = new AESWrapEngine();
 
32
 
 
33
        wrapper.init(true, new KeyParameter(kek));
 
34
 
 
35
        try
 
36
        {
 
37
            byte[]  cText = wrapper.wrap(in, 0, in.length);
 
38
            if (!Arrays.areEqual(cText, out))
 
39
            {
 
40
                return new SimpleTestResult(false, getName() + ": failed wrap test " + id  + " expected " + new String(Hex.encode(out)) + " got " + new String(Hex.encode(cText)));
 
41
            }
 
42
        }
 
43
        catch (Exception e)
 
44
        {
 
45
            return new SimpleTestResult(false, getName() + ": failed wrap test exception " + e.toString());
 
46
        }
 
47
 
 
48
        wrapper.init(false, new KeyParameter(kek));
 
49
 
 
50
        try
 
51
        {
 
52
            byte[]  pText = wrapper.unwrap(out, 0, out.length);
 
53
            if (!Arrays.areEqual(pText, in))
 
54
            {
 
55
                return new SimpleTestResult(false, getName() + ": failed unwrap test " + id  + " expected " + new String(Hex.encode(in)) + " got " + new String(Hex.encode(pText)));
 
56
            }
 
57
        }
 
58
        catch (Exception e)
 
59
        {
 
60
            return new SimpleTestResult(false, getName() + ": failed unwrap test exception.", e);
 
61
        }
 
62
 
 
63
        return new SimpleTestResult(true, getName() + ": Okay");
 
64
    }
 
65
 
 
66
    public TestResult perform()
 
67
    {
 
68
        byte[]  kek1 = Hex.decode("000102030405060708090a0b0c0d0e0f");
 
69
        byte[]  in1 = Hex.decode("00112233445566778899aabbccddeeff");
 
70
        byte[]  out1 = Hex.decode("1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5");
 
71
        TestResult result = wrapTest(1, kek1, in1, out1);
 
72
        
 
73
        if (!result.isSuccessful())
 
74
        {
 
75
            return result;
 
76
        }
 
77
 
 
78
        byte[]  kek2 = Hex.decode("000102030405060708090a0b0c0d0e0f1011121314151617");
 
79
        byte[]  in2 = Hex.decode("00112233445566778899aabbccddeeff");
 
80
        byte[]  out2 = Hex.decode("96778b25ae6ca435f92b5b97c050aed2468ab8a17ad84e5d");
 
81
        result = wrapTest(2, kek2, in2, out2);
 
82
        if (!result.isSuccessful())
 
83
        {
 
84
            return result;
 
85
        }
 
86
 
 
87
        byte[]  kek3 = Hex.decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
 
88
        byte[]  in3 = Hex.decode("00112233445566778899aabbccddeeff");
 
89
        byte[]  out3 = Hex.decode("64e8c3f9ce0f5ba263e9777905818a2a93c8191e7d6e8ae7");
 
90
        result = wrapTest(3, kek3, in3, out3);
 
91
        if (!result.isSuccessful())
 
92
        {
 
93
            return result;
 
94
        }
 
95
 
 
96
        byte[]  kek4 = Hex.decode("000102030405060708090a0b0c0d0e0f1011121314151617");
 
97
        byte[]  in4 = Hex.decode("00112233445566778899aabbccddeeff0001020304050607");
 
98
        byte[]  out4 = Hex.decode("031d33264e15d33268f24ec260743edce1c6c7ddee725a936ba814915c6762d2");
 
99
        result = wrapTest(4, kek4, in4, out4);
 
100
        if (!result.isSuccessful())
 
101
        {
 
102
            return result;
 
103
        }
 
104
 
 
105
        byte[]  kek5 = Hex.decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
 
106
        byte[]  in5 = Hex.decode("00112233445566778899aabbccddeeff0001020304050607");
 
107
        byte[]  out5 = Hex.decode("a8f9bc1612c68b3ff6e6f4fbe30e71e4769c8b80a32cb8958cd5d17d6b254da1");
 
108
        result = wrapTest(5, kek5, in5, out5);
 
109
        if (!result.isSuccessful())
 
110
        {
 
111
            return result;
 
112
        }
 
113
 
 
114
        byte[]  kek6 = Hex.decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
 
115
        byte[]  in6 = Hex.decode("00112233445566778899aabbccddeeff000102030405060708090a0b0c0d0e0f");
 
116
        byte[]  out6 = Hex.decode("28c9f404c4b810f4cbccb35cfb87f8263f5786e2d80ed326cbc7f0e71a99f43bfb988b9b7a02dd21");
 
117
        result = wrapTest(6, kek6, in6, out6);
 
118
        if (!result.isSuccessful())
 
119
        {
 
120
            return result;
 
121
        }
 
122
 
 
123
        Wrapper      wrapper = new AESWrapEngine();
 
124
        KeyParameter key = new KeyParameter(new byte[16]);
 
125
        byte[]       buf = new byte[16];
 
126
        
 
127
        try
 
128
        {
 
129
            wrapper.init(true, key);
 
130
            
 
131
            wrapper.unwrap(buf, 0, buf.length);
 
132
            
 
133
            return new SimpleTestResult(false, getName() + ": failed unwrap state test.");
 
134
        }
 
135
        catch (IllegalStateException e)
 
136
        {
 
137
            // expected
 
138
        }
 
139
        catch (InvalidCipherTextException e)
 
140
        {
 
141
            return new SimpleTestResult(false, getName() + ": unexpected exception: " + e, e);
 
142
        }
 
143
        
 
144
        try
 
145
        {
 
146
            wrapper.init(false, key);
 
147
            
 
148
            wrapper.wrap(buf, 0, buf.length);
 
149
            
 
150
            return new SimpleTestResult(false, getName() + ": failed unwrap state test.");
 
151
        }
 
152
        catch (IllegalStateException e)
 
153
        {
 
154
            // expected
 
155
        }
 
156
        
 
157
        //
 
158
        // short test
 
159
        //
 
160
        try
 
161
        {
 
162
            wrapper.init(false, key);
 
163
            
 
164
            wrapper.unwrap(buf, 0, buf.length / 2);
 
165
            
 
166
            return new SimpleTestResult(false, getName() + ": failed unwrap short test.");
 
167
        }
 
168
        catch (InvalidCipherTextException e)
 
169
        {
 
170
            // expected
 
171
        }
 
172
        
 
173
        try
 
174
        {
 
175
            wrapper.init(true, key);
 
176
            
 
177
            wrapper.wrap(buf, 0, 15);
 
178
            
 
179
            return new SimpleTestResult(false, getName() + ": failed wrap length test.");
 
180
        }
 
181
        catch (DataLengthException e)
 
182
        {
 
183
            // expected
 
184
        }
 
185
        
 
186
        return new SimpleTestResult(true, getName() + ": Okay");
 
187
    }
 
188
 
 
189
    public static void main(
 
190
        String[]    args)
 
191
    {
 
192
        AESWrapTest     test = new AESWrapTest();
 
193
        TestResult      result = test.perform();
 
194
 
 
195
        System.out.println(result);
 
196
    }
 
197
}