01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.BlockCipher;
04: import org.bouncycastle.crypto.Mac;
05: import org.bouncycastle.crypto.engines.DESEngine;
06: import org.bouncycastle.crypto.macs.ISO9797Alg3Mac;
07: import org.bouncycastle.crypto.params.KeyParameter;
08: import org.bouncycastle.util.encoders.Hex;
09: import org.bouncycastle.util.test.SimpleTest;
10:
11: public class ISO9797Alg3MacTest extends SimpleTest {
12: static byte[] keyBytes = Hex
13: .decode("7CA110454A1A6E570131D9619DC1376E");
14: static byte[] ivBytes = Hex.decode("0000000000000000");
15:
16: static byte[] input1 = "Hello World !!!!".getBytes();
17:
18: static byte[] output1 = Hex.decode("F09B856213BAB83B");
19:
20: public ISO9797Alg3MacTest() {
21: }
22:
23: public void performTest() {
24: KeyParameter key = new KeyParameter(keyBytes);
25: BlockCipher cipher = new DESEngine();
26: Mac mac = new ISO9797Alg3Mac(cipher);
27:
28: //
29: // standard DAC - zero IV
30: //
31: mac.init(key);
32:
33: mac.update(input1, 0, input1.length);
34:
35: byte[] out = new byte[8];
36:
37: mac.doFinal(out, 0);
38:
39: if (!areEqual(out, output1)) {
40: fail("Failed - expected " + new String(Hex.encode(output1))
41: + " got " + new String(Hex.encode(out)));
42: }
43:
44: //
45: // reset
46: //
47: mac.reset();
48:
49: mac.init(key);
50:
51: for (int i = 0; i != input1.length / 2; i++) {
52: mac.update(input1[i]);
53: }
54:
55: mac.update(input1, input1.length / 2, input1.length
56: - (input1.length / 2));
57:
58: mac.doFinal(out, 0);
59:
60: if (!areEqual(out, output1)) {
61: fail("Reset failed - expected "
62: + new String(Hex.encode(output1)) + " got "
63: + new String(Hex.encode(out)));
64: }
65: }
66:
67: public String getName() {
68: return "ISO9797Alg3Mac";
69: }
70:
71: public static void main(String[] args) {
72: runTest(new ISO9797Alg3MacTest());
73: }
74: }
|