1
package org.bouncycastle.jce.provider.test;
3
import org.bouncycastle.jce.provider.BouncyCastleProvider;
4
import org.bouncycastle.util.encoders.Hex;
5
import org.bouncycastle.util.test.SimpleTest;
7
import javax.crypto.Mac;
8
import javax.crypto.SecretKey;
9
import javax.crypto.spec.IvParameterSpec;
10
import javax.crypto.spec.SecretKeySpec;
11
import java.security.Security;
14
* MAC tester - vectors from
15
* <a href=http://www.itl.nist.gov/fipspubs/fip81.htm>FIP 81</a> and
16
* <a href=http://www.itl.nist.gov/fipspubs/fip113.htm>FIP 113</a>.
21
static byte[] keyBytes = Hex.decode("0123456789abcdef");
22
static byte[] ivBytes = Hex.decode("1234567890abcdef");
24
static byte[] input = Hex.decode("37363534333231204e6f77206973207468652074696d6520666f7220");
26
static byte[] output1 = Hex.decode("f1d30f68");
27
static byte[] output2 = Hex.decode("58d2e77e");
28
static byte[] output3 = Hex.decode("cd647403");
30
static byte[] keyBytesISO9797 = Hex.decode("7CA110454A1A6E570131D9619DC1376E");
32
static byte[] inputISO9797 = "Hello World !!!!".getBytes();
34
static byte[] outputISO9797 = Hex.decode("F09B856213BAB83B");
36
static byte[] inputDesEDE64 = "Hello World !!!!".getBytes();
38
static byte[] outputDesEDE64 = Hex.decode("862304d33af01096");
44
private void aliasTest(SecretKey key, String primary, String[] aliases)
47
Mac mac = Mac.getInstance(primary, "BC");
50
// standard DAC - zero IV
54
mac.update(input, 0, input.length);
56
byte[] ref = mac.doFinal();
58
for (int i = 0; i != aliases.length; i++)
60
mac = Mac.getInstance(aliases[i], "BC");
64
mac.update(input, 0, input.length);
66
byte[] out = mac.doFinal();
67
if (!areEqual(out, ref))
69
fail("Failed - expected " + new String(Hex.encode(ref)) + " got " + new String(Hex.encode(out)));
74
public void performTest()
77
SecretKey key = new SecretKeySpec(keyBytes, "DES");
81
mac = Mac.getInstance("DESMac", "BC");
84
// standard DAC - zero IV
88
mac.update(input, 0, input.length);
92
if (!areEqual(out, output1))
94
fail("Failed - expected " + new String(Hex.encode(output1)) + " got " + new String(Hex.encode(out)));
100
mac.init(key, new IvParameterSpec(ivBytes));
102
mac.update(input, 0, input.length);
106
if (!areEqual(out, output2))
108
fail("Failed - expected " + new String(Hex.encode(output2)) + " got " + new String(Hex.encode(out)));
112
// CFB mac with IV - 8 bit CFB mode
114
mac = Mac.getInstance("DESMac/CFB8", "BC");
116
mac.init(key, new IvParameterSpec(ivBytes));
118
mac.update(input, 0, input.length);
122
if (!areEqual(out, output3))
124
fail("Failed - expected " + new String(Hex.encode(output3)) + " got " + new String(Hex.encode(out)));
128
// ISO9797 algorithm 3 using DESEDE
130
key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
132
mac = Mac.getInstance("ISO9797ALG3", "BC");
136
mac.update(inputISO9797, 0, inputISO9797.length);
140
if (!areEqual(out, outputISO9797))
142
fail("Failed - expected " + new String(Hex.encode(outputISO9797)) + " got " + new String(Hex.encode(out)));
148
key = new SecretKeySpec(keyBytesISO9797, "DESEDE");
150
mac = Mac.getInstance("DESEDE64", "BC");
154
mac.update(inputDesEDE64, 0, inputDesEDE64.length);
158
if (!areEqual(out, outputDesEDE64))
160
fail("Failed - expected " + new String(Hex.encode(outputDesEDE64)) + " got " + new String(Hex.encode(out)));
163
aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"), "DESedeMac64withISO7816-4Padding",
164
new String[] { "DESEDE64WITHISO7816-4PADDING", "DESEDEISO9797ALG1MACWITHISO7816-4PADDING", "DESEDEISO9797ALG1WITHISO7816-4PADDING" });
166
aliasTest(new SecretKeySpec(keyBytesISO9797, "DESede"), "ISO9797ALG3WITHISO7816-4PADDING",
167
new String[] { "ISO9797ALG3MACWITHISO7816-4PADDING" });
170
public String getName()
175
public static void main(
178
Security.addProvider(new BouncyCastleProvider());
180
runTest(new MacTest());