001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.BlockCipher;
004: import org.bouncycastle.crypto.Mac;
005: import org.bouncycastle.crypto.engines.AESFastEngine;
006: import org.bouncycastle.crypto.macs.CMac;
007: import org.bouncycastle.crypto.params.KeyParameter;
008: import org.bouncycastle.util.encoders.Hex;
009: import org.bouncycastle.util.test.SimpleTest;
010:
011: /**
012: * CMAC tester - <a href="http://www.nuee.nagoya-u.ac.jp/labs/tiwata/omac/tv/omac1-tv.txt">Official Test Vectors</a>.
013: */
014: public class CMacTest extends SimpleTest {
015: private static final byte[] keyBytes128 = Hex
016: .decode("2b7e151628aed2a6abf7158809cf4f3c");
017: private static final byte[] keyBytes192 = Hex
018: .decode("8e73b0f7da0e6452c810f32b809079e5"
019: + "62f8ead2522c6b7b");
020: private static final byte[] keyBytes256 = Hex
021: .decode("603deb1015ca71be2b73aef0857d7781"
022: + "1f352c073b6108d72d9810a30914dff4");
023:
024: private static final byte[] input0 = Hex.decode("");
025: private static final byte[] input16 = Hex
026: .decode("6bc1bee22e409f96e93d7e117393172a");
027: private static final byte[] input40 = Hex
028: .decode("6bc1bee22e409f96e93d7e117393172a"
029: + "ae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411");
030: private static final byte[] input64 = Hex
031: .decode("6bc1bee22e409f96e93d7e117393172a"
032: + "ae2d8a571e03ac9c9eb76fac45af8e51"
033: + "30c81c46a35ce411e5fbc1191a0a52ef"
034: + "f69f2445df4f9b17ad2b417be66c3710");
035:
036: private static final byte[] output_k128_m0 = Hex
037: .decode("bb1d6929e95937287fa37d129b756746");
038: private static final byte[] output_k128_m16 = Hex
039: .decode("070a16b46b4d4144f79bdd9dd04a287c");
040: private static final byte[] output_k128_m40 = Hex
041: .decode("dfa66747de9ae63030ca32611497c827");
042: private static final byte[] output_k128_m64 = Hex
043: .decode("51f0bebf7e3b9d92fc49741779363cfe");
044:
045: private static final byte[] output_k192_m0 = Hex
046: .decode("d17ddf46adaacde531cac483de7a9367");
047: private static final byte[] output_k192_m16 = Hex
048: .decode("9e99a7bf31e710900662f65e617c5184");
049: private static final byte[] output_k192_m40 = Hex
050: .decode("8a1de5be2eb31aad089a82e6ee908b0e");
051: private static final byte[] output_k192_m64 = Hex
052: .decode("a1d5df0eed790f794d77589659f39a11");
053:
054: private static final byte[] output_k256_m0 = Hex
055: .decode("028962f61b7bf89efc6b551f4667d983");
056: private static final byte[] output_k256_m16 = Hex
057: .decode("28a7023f452e8f82bd4bf28d8c37c35c");
058: private static final byte[] output_k256_m40 = Hex
059: .decode("aaf3d8f1de5640c232f5b169b9c911e6");
060: private static final byte[] output_k256_m64 = Hex
061: .decode("e1992190549f6ed5696a2c056c315410");
062:
063: public CMacTest() {
064: }
065:
066: public void performTest() {
067: BlockCipher cipher = new AESFastEngine();
068: Mac mac = new CMac(cipher, 128);
069:
070: //128 bytes key
071:
072: KeyParameter key = new KeyParameter(keyBytes128);
073:
074: // 0 bytes message - 128 bytes key
075: mac.init(key);
076:
077: mac.update(input0, 0, input0.length);
078:
079: byte[] out = new byte[16];
080:
081: mac.doFinal(out, 0);
082:
083: if (!areEqual(out, output_k128_m0)) {
084: fail("Failed - expected "
085: + new String(Hex.encode(output_k128_m0)) + " got "
086: + new String(Hex.encode(out)));
087: }
088:
089: // 16 bytes message - 128 bytes key
090: mac.init(key);
091:
092: mac.update(input16, 0, input16.length);
093:
094: out = new byte[16];
095:
096: mac.doFinal(out, 0);
097:
098: if (!areEqual(out, output_k128_m16)) {
099: fail("Failed - expected "
100: + new String(Hex.encode(output_k128_m16)) + " got "
101: + new String(Hex.encode(out)));
102: }
103:
104: // 40 bytes message - 128 bytes key
105: mac.init(key);
106:
107: mac.update(input40, 0, input40.length);
108:
109: out = new byte[16];
110:
111: mac.doFinal(out, 0);
112:
113: if (!areEqual(out, output_k128_m40)) {
114: fail("Failed - expected "
115: + new String(Hex.encode(output_k128_m40)) + " got "
116: + new String(Hex.encode(out)));
117: }
118:
119: // 64 bytes message - 128 bytes key
120: mac.init(key);
121:
122: mac.update(input64, 0, input64.length);
123:
124: out = new byte[16];
125:
126: mac.doFinal(out, 0);
127:
128: if (!areEqual(out, output_k128_m64)) {
129: fail("Failed - expected "
130: + new String(Hex.encode(output_k128_m64)) + " got "
131: + new String(Hex.encode(out)));
132: }
133:
134: //192 bytes key
135:
136: key = new KeyParameter(keyBytes192);
137:
138: // 0 bytes message - 192 bytes key
139: mac.init(key);
140:
141: mac.update(input0, 0, input0.length);
142:
143: out = new byte[16];
144:
145: mac.doFinal(out, 0);
146:
147: if (!areEqual(out, output_k192_m0)) {
148: fail("Failed - expected "
149: + new String(Hex.encode(output_k192_m0)) + " got "
150: + new String(Hex.encode(out)));
151: }
152:
153: // 16 bytes message - 192 bytes key
154: mac.init(key);
155:
156: mac.update(input16, 0, input16.length);
157:
158: out = new byte[16];
159:
160: mac.doFinal(out, 0);
161:
162: if (!areEqual(out, output_k192_m16)) {
163: fail("Failed - expected "
164: + new String(Hex.encode(output_k192_m16)) + " got "
165: + new String(Hex.encode(out)));
166: }
167:
168: // 40 bytes message - 192 bytes key
169: mac.init(key);
170:
171: mac.update(input40, 0, input40.length);
172:
173: out = new byte[16];
174:
175: mac.doFinal(out, 0);
176:
177: if (!areEqual(out, output_k192_m40)) {
178: fail("Failed - expected "
179: + new String(Hex.encode(output_k192_m40)) + " got "
180: + new String(Hex.encode(out)));
181: }
182:
183: // 64 bytes message - 192 bytes key
184: mac.init(key);
185:
186: mac.update(input64, 0, input64.length);
187:
188: out = new byte[16];
189:
190: mac.doFinal(out, 0);
191:
192: if (!areEqual(out, output_k192_m64)) {
193: fail("Failed - expected "
194: + new String(Hex.encode(output_k192_m64)) + " got "
195: + new String(Hex.encode(out)));
196: }
197:
198: //256 bytes key
199:
200: key = new KeyParameter(keyBytes256);
201:
202: // 0 bytes message - 256 bytes key
203: mac.init(key);
204:
205: mac.update(input0, 0, input0.length);
206:
207: out = new byte[16];
208:
209: mac.doFinal(out, 0);
210:
211: if (!areEqual(out, output_k256_m0)) {
212: fail("Failed - expected "
213: + new String(Hex.encode(output_k256_m0)) + " got "
214: + new String(Hex.encode(out)));
215: }
216:
217: // 16 bytes message - 256 bytes key
218: mac.init(key);
219:
220: mac.update(input16, 0, input16.length);
221:
222: out = new byte[16];
223:
224: mac.doFinal(out, 0);
225:
226: if (!areEqual(out, output_k256_m16)) {
227: fail("Failed - expected "
228: + new String(Hex.encode(output_k256_m16)) + " got "
229: + new String(Hex.encode(out)));
230: }
231:
232: // 40 bytes message - 256 bytes key
233: mac.init(key);
234:
235: mac.update(input40, 0, input40.length);
236:
237: out = new byte[16];
238:
239: mac.doFinal(out, 0);
240:
241: if (!areEqual(out, output_k256_m40)) {
242: fail("Failed - expected "
243: + new String(Hex.encode(output_k256_m40)) + " got "
244: + new String(Hex.encode(out)));
245: }
246:
247: // 64 bytes message - 256 bytes key
248: mac.init(key);
249:
250: mac.update(input64, 0, input64.length);
251:
252: out = new byte[16];
253:
254: mac.doFinal(out, 0);
255:
256: if (!areEqual(out, output_k256_m64)) {
257: fail("Failed - expected "
258: + new String(Hex.encode(output_k256_m64)) + " got "
259: + new String(Hex.encode(out)));
260: }
261: }
262:
263: public String getName() {
264: return "CMac";
265: }
266:
267: public static void main(String[] args) {
268: runTest(new CMacTest());
269: }
270: }
|