001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.util.encoders.Hex;
005: import org.bouncycastle.util.test.SimpleTest;
006:
007: import javax.crypto.Mac;
008: import javax.crypto.SecretKey;
009: import javax.crypto.spec.IvParameterSpec;
010: import javax.crypto.spec.SecretKeySpec;
011: import java.security.Security;
012:
013: /**
014: * MAC tester - vectors from
015: * <a href=http://www.itl.nist.gov/fipspubs/fip81.htm>FIP 81</a> and
016: * <a href=http://www.itl.nist.gov/fipspubs/fip113.htm>FIP 113</a>.
017: */
018: public class MacTest extends SimpleTest {
019: static byte[] keyBytes = Hex.decode("0123456789abcdef");
020: static byte[] ivBytes = Hex.decode("1234567890abcdef");
021:
022: static byte[] input = Hex
023: .decode("37363534333231204e6f77206973207468652074696d6520666f7220");
024:
025: static byte[] output1 = Hex.decode("f1d30f68");
026: static byte[] output2 = Hex.decode("58d2e77e");
027: static byte[] output3 = Hex.decode("cd647403");
028:
029: static byte[] keyBytesISO9797 = Hex
030: .decode("7CA110454A1A6E570131D9619DC1376E");
031:
032: static byte[] inputISO9797 = "Hello World !!!!".getBytes();
033:
034: static byte[] outputISO9797 = Hex.decode("F09B856213BAB83B");
035:
036: static byte[] inputDesEDE64 = "Hello World !!!!".getBytes();
037:
038: static byte[] outputDesEDE64 = Hex.decode("862304d33af01096");
039:
040: public MacTest() {
041: }
042:
043: private void aliasTest(SecretKey key, String primary,
044: String[] aliases) throws Exception {
045: Mac mac = Mac.getInstance(primary, "BC");
046:
047: //
048: // standard DAC - zero IV
049: //
050: mac.init(key);
051:
052: mac.update(input, 0, input.length);
053:
054: byte[] ref = mac.doFinal();
055:
056: for (int i = 0; i != aliases.length; i++) {
057: mac = Mac.getInstance(aliases[i], "BC");
058:
059: mac.init(key);
060:
061: mac.update(input, 0, input.length);
062:
063: byte[] out = mac.doFinal();
064: if (!areEqual(out, ref)) {
065: fail("Failed - expected " + new String(Hex.encode(ref))
066: + " got " + new String(Hex.encode(out)));
067: }
068: }
069: }
070:
071: public void performTest() throws Exception {
072: SecretKey key = new SecretKeySpec(keyBytes, "DES");
073: byte[] out;
074: Mac mac;
075:
076: mac = Mac.getInstance("DESMac", "BC");
077:
078: //
079: // standard DAC - zero IV
080: //
081: mac.init(key);
082:
083: mac.update(input, 0, input.length);
084:
085: out = mac.doFinal();
086:
087: if (!areEqual(out, output1)) {
088: fail("Failed - expected " + new String(Hex.encode(output1))
089: + " got " + new String(Hex.encode(out)));
090: }
091:
092: //
093: // mac with IV.
094: //
095: mac.init(key, new IvParameterSpec(ivBytes));
096:
097: mac.update(input, 0, input.length);
098:
099: out = mac.doFinal();
100:
101: if (!areEqual(out, output2)) {
102: fail("Failed - expected " + new String(Hex.encode(output2))
103: + " got " + new String(Hex.encode(out)));
104: }
105:
106: //
107: // CFB mac with IV - 8 bit CFB mode
108: //
109: mac = Mac.getInstance("DESMac/CFB8", "BC");
110:
111: mac.init(key, new IvParameterSpec(ivBytes));
112:
113: mac.update(input, 0, input.length);
114:
115: out = mac.doFinal();
116:
117: if (!areEqual(out, output3)) {
118: fail("Failed - expected " + new String(Hex.encode(output3))
119: + " got " + new String(Hex.encode(out)));
120: }
121:
122: //
123: // ISO9797 algorithm 3 using DESEDE
124: //
125: key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
126:
127: mac = Mac.getInstance("ISO9797ALG3", "BC");
128:
129: mac.init(key);
130:
131: mac.update(inputISO9797, 0, inputISO9797.length);
132:
133: out = mac.doFinal();
134:
135: if (!areEqual(out, outputISO9797)) {
136: fail("Failed - expected "
137: + new String(Hex.encode(outputISO9797)) + " got "
138: + new String(Hex.encode(out)));
139: }
140:
141: //
142: // 64bit DESede Mac
143: //
144: key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
145:
146: mac = Mac.getInstance("DESEDE64", "BC");
147:
148: mac.init(key);
149:
150: mac.update(inputDesEDE64, 0, inputDesEDE64.length);
151:
152: out = mac.doFinal();
153:
154: if (!areEqual(out, outputDesEDE64)) {
155: fail("Failed - expected "
156: + new String(Hex.encode(outputDesEDE64)) + " got "
157: + new String(Hex.encode(out)));
158: }
159:
160: aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"),
161: "DESedeMac64withISO7816-4Padding", new String[] {
162: "DESEDE64WITHISO7816-4PADDING",
163: "DESEDEISO9797ALG1MACWITHISO7816-4PADDING",
164: "DESEDEISO9797ALG1WITHISO7816-4PADDING" });
165:
166: aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"),
167: "ISO9797ALG3WITHISO7816-4PADDING",
168: new String[] { "ISO9797ALG3MACWITHISO7816-4PADDING" });
169: }
170:
171: public String getName() {
172: return "Mac";
173: }
174:
175: public static void main(String[] args) {
176: Security.addProvider(new BouncyCastleProvider());
177:
178: runTest(new MacTest());
179: }
180: }
|