001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.DataLengthException;
004: import org.bouncycastle.crypto.InvalidCipherTextException;
005: import org.bouncycastle.crypto.Wrapper;
006: import org.bouncycastle.crypto.engines.AESWrapEngine;
007: import org.bouncycastle.crypto.params.KeyParameter;
008: import org.bouncycastle.util.Arrays;
009: import org.bouncycastle.util.encoders.Hex;
010: import org.bouncycastle.util.test.SimpleTestResult;
011: import org.bouncycastle.util.test.Test;
012: import org.bouncycastle.util.test.TestResult;
013:
014: /**
015: * Wrap Test
016: */
017: public class AESWrapTest implements Test {
018: public String getName() {
019: return "AESWrap";
020: }
021:
022: private TestResult wrapTest(int id, byte[] kek, byte[] in,
023: byte[] out) {
024: Wrapper wrapper = new AESWrapEngine();
025:
026: wrapper.init(true, new KeyParameter(kek));
027:
028: try {
029: byte[] cText = wrapper.wrap(in, 0, in.length);
030: if (!Arrays.areEqual(cText, out)) {
031: return new SimpleTestResult(false, getName()
032: + ": failed wrap test " + id + " expected "
033: + new String(Hex.encode(out)) + " got "
034: + new String(Hex.encode(cText)));
035: }
036: } catch (Exception e) {
037: return new SimpleTestResult(false, getName()
038: + ": failed wrap test exception " + e.toString());
039: }
040:
041: wrapper.init(false, new KeyParameter(kek));
042:
043: try {
044: byte[] pText = wrapper.unwrap(out, 0, out.length);
045: if (!Arrays.areEqual(pText, in)) {
046: return new SimpleTestResult(false, getName()
047: + ": failed unwrap test " + id + " expected "
048: + new String(Hex.encode(in)) + " got "
049: + new String(Hex.encode(pText)));
050: }
051: } catch (Exception e) {
052: return new SimpleTestResult(false, getName()
053: + ": failed unwrap test exception.", e);
054: }
055:
056: return new SimpleTestResult(true, getName() + ": Okay");
057: }
058:
059: public TestResult perform() {
060: byte[] kek1 = Hex.decode("000102030405060708090a0b0c0d0e0f");
061: byte[] in1 = Hex.decode("00112233445566778899aabbccddeeff");
062: byte[] out1 = Hex
063: .decode("1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5");
064: TestResult result = wrapTest(1, kek1, in1, out1);
065:
066: if (!result.isSuccessful()) {
067: return result;
068: }
069:
070: byte[] kek2 = Hex
071: .decode("000102030405060708090a0b0c0d0e0f1011121314151617");
072: byte[] in2 = Hex.decode("00112233445566778899aabbccddeeff");
073: byte[] out2 = Hex
074: .decode("96778b25ae6ca435f92b5b97c050aed2468ab8a17ad84e5d");
075: result = wrapTest(2, kek2, in2, out2);
076: if (!result.isSuccessful()) {
077: return result;
078: }
079:
080: byte[] kek3 = Hex
081: .decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
082: byte[] in3 = Hex.decode("00112233445566778899aabbccddeeff");
083: byte[] out3 = Hex
084: .decode("64e8c3f9ce0f5ba263e9777905818a2a93c8191e7d6e8ae7");
085: result = wrapTest(3, kek3, in3, out3);
086: if (!result.isSuccessful()) {
087: return result;
088: }
089:
090: byte[] kek4 = Hex
091: .decode("000102030405060708090a0b0c0d0e0f1011121314151617");
092: byte[] in4 = Hex
093: .decode("00112233445566778899aabbccddeeff0001020304050607");
094: byte[] out4 = Hex
095: .decode("031d33264e15d33268f24ec260743edce1c6c7ddee725a936ba814915c6762d2");
096: result = wrapTest(4, kek4, in4, out4);
097: if (!result.isSuccessful()) {
098: return result;
099: }
100:
101: byte[] kek5 = Hex
102: .decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
103: byte[] in5 = Hex
104: .decode("00112233445566778899aabbccddeeff0001020304050607");
105: byte[] out5 = Hex
106: .decode("a8f9bc1612c68b3ff6e6f4fbe30e71e4769c8b80a32cb8958cd5d17d6b254da1");
107: result = wrapTest(5, kek5, in5, out5);
108: if (!result.isSuccessful()) {
109: return result;
110: }
111:
112: byte[] kek6 = Hex
113: .decode("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
114: byte[] in6 = Hex
115: .decode("00112233445566778899aabbccddeeff000102030405060708090a0b0c0d0e0f");
116: byte[] out6 = Hex
117: .decode("28c9f404c4b810f4cbccb35cfb87f8263f5786e2d80ed326cbc7f0e71a99f43bfb988b9b7a02dd21");
118: result = wrapTest(6, kek6, in6, out6);
119: if (!result.isSuccessful()) {
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: wrapper.init(true, key);
129:
130: wrapper.unwrap(buf, 0, buf.length);
131:
132: return new SimpleTestResult(false, getName()
133: + ": failed unwrap state test.");
134: } catch (IllegalStateException e) {
135: // expected
136: } catch (InvalidCipherTextException e) {
137: return new SimpleTestResult(false, getName()
138: + ": unexpected exception: " + e, e);
139: }
140:
141: try {
142: wrapper.init(false, key);
143:
144: wrapper.wrap(buf, 0, buf.length);
145:
146: return new SimpleTestResult(false, getName()
147: + ": failed unwrap state test.");
148: } catch (IllegalStateException e) {
149: // expected
150: }
151:
152: //
153: // short test
154: //
155: try {
156: wrapper.init(false, key);
157:
158: wrapper.unwrap(buf, 0, buf.length / 2);
159:
160: return new SimpleTestResult(false, getName()
161: + ": failed unwrap short test.");
162: } catch (InvalidCipherTextException e) {
163: // expected
164: }
165:
166: try {
167: wrapper.init(true, key);
168:
169: wrapper.wrap(buf, 0, 15);
170:
171: return new SimpleTestResult(false, getName()
172: + ": failed wrap length test.");
173: } catch (DataLengthException e) {
174: // expected
175: }
176:
177: return new SimpleTestResult(true, getName() + ": Okay");
178: }
179:
180: public static void main(String[] args) {
181: AESWrapTest test = new AESWrapTest();
182: TestResult result = test.perform();
183:
184: System.out.println(result);
185: }
186: }
|