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.DESEngine;
006: import org.bouncycastle.crypto.macs.CBCBlockCipherMac;
007: import org.bouncycastle.crypto.macs.CFBBlockCipherMac;
008: import org.bouncycastle.crypto.params.KeyParameter;
009: import org.bouncycastle.crypto.params.ParametersWithIV;
010: import org.bouncycastle.crypto.paddings.PKCS7Padding;
011: import org.bouncycastle.util.encoders.Hex;
012: import org.bouncycastle.util.test.SimpleTest;
013:
014: /**
015: * MAC tester - vectors from
016: * <a href=http://www.itl.nist.gov/fipspubs/fip81.htm>FIP 81</a> and
017: * <a href=http://www.itl.nist.gov/fipspubs/fip113.htm>FIP 113</a>.
018: */
019: public class MacTest extends SimpleTest {
020: static byte[] keyBytes = Hex.decode("0123456789abcdef");
021: static byte[] ivBytes = Hex.decode("1234567890abcdef");
022:
023: static byte[] input1 = Hex
024: .decode("37363534333231204e6f77206973207468652074696d6520666f7220");
025:
026: static byte[] output1 = Hex.decode("f1d30f68");
027: static byte[] output2 = Hex.decode("58d2e77e");
028: static byte[] output3 = Hex.decode("cd647403");
029:
030: //
031: // these aren't NIST vectors, just for regression testing.
032: //
033: static byte[] input2 = Hex.decode("3736353433323120");
034:
035: static byte[] output4 = Hex.decode("3af549c9");
036: static byte[] output5 = Hex.decode("188fbdd5");
037: static byte[] output6 = Hex.decode("7045eecd");
038:
039: public MacTest() {
040: }
041:
042: public void performTest() {
043: KeyParameter key = new KeyParameter(keyBytes);
044: BlockCipher cipher = new DESEngine();
045: Mac mac = new CBCBlockCipherMac(cipher);
046:
047: //
048: // standard DAC - zero IV
049: //
050: mac.init(key);
051:
052: mac.update(input1, 0, input1.length);
053:
054: byte[] out = new byte[4];
055:
056: mac.doFinal(out, 0);
057:
058: if (!areEqual(out, output1)) {
059: fail("Failed - expected " + new String(Hex.encode(output1))
060: + " got " + new String(Hex.encode(out)));
061: }
062:
063: //
064: // mac with IV.
065: //
066: ParametersWithIV param = new ParametersWithIV(key, ivBytes);
067:
068: mac.init(param);
069:
070: mac.update(input1, 0, input1.length);
071:
072: out = new byte[4];
073:
074: mac.doFinal(out, 0);
075:
076: if (!areEqual(out, output2)) {
077: fail("Failed - expected " + new String(Hex.encode(output2))
078: + " got " + new String(Hex.encode(out)));
079: }
080:
081: //
082: // CFB mac with IV - 8 bit CFB mode
083: //
084: param = new ParametersWithIV(key, ivBytes);
085:
086: mac = new CFBBlockCipherMac(cipher);
087:
088: mac.init(param);
089:
090: mac.update(input1, 0, input1.length);
091:
092: out = new byte[4];
093:
094: mac.doFinal(out, 0);
095:
096: if (!areEqual(out, output3)) {
097: fail("Failed - expected " + new String(Hex.encode(output3))
098: + " got " + new String(Hex.encode(out)));
099: }
100:
101: //
102: // word aligned data - zero IV
103: //
104: mac.init(key);
105:
106: mac.update(input2, 0, input2.length);
107:
108: out = new byte[4];
109:
110: mac.doFinal(out, 0);
111:
112: if (!areEqual(out, output4)) {
113: fail("Failed - expected " + new String(Hex.encode(output4))
114: + " got " + new String(Hex.encode(out)));
115: }
116:
117: //
118: // word aligned data - zero IV - CBC padding
119: //
120: mac = new CBCBlockCipherMac(cipher, new PKCS7Padding());
121:
122: mac.init(key);
123:
124: mac.update(input2, 0, input2.length);
125:
126: out = new byte[4];
127:
128: mac.doFinal(out, 0);
129:
130: if (!areEqual(out, output5)) {
131: fail("Failed - expected " + new String(Hex.encode(output5))
132: + " got " + new String(Hex.encode(out)));
133: }
134:
135: //
136: // non-word aligned data - zero IV - CBC padding
137: //
138: mac.reset();
139:
140: mac.update(input1, 0, input1.length);
141:
142: out = new byte[4];
143:
144: mac.doFinal(out, 0);
145:
146: if (!areEqual(out, output6)) {
147: fail("Failed - expected " + new String(Hex.encode(output6))
148: + " got " + new String(Hex.encode(out)));
149: }
150:
151: //
152: // non-word aligned data - zero IV - CBC padding
153: //
154: mac.init(key);
155:
156: mac.update(input1, 0, input1.length);
157:
158: out = new byte[4];
159:
160: mac.doFinal(out, 0);
161:
162: if (!areEqual(out, output6)) {
163: fail("Failed - expected " + new String(Hex.encode(output6))
164: + " got " + new String(Hex.encode(out)));
165: }
166: }
167:
168: public String getName() {
169: return "Mac";
170: }
171:
172: public static void main(String[] args) {
173: runTest(new MacTest());
174: }
175: }
|