01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.MD5Digest;
04: import org.bouncycastle.crypto.macs.HMac;
05: import org.bouncycastle.crypto.params.KeyParameter;
06: import org.bouncycastle.util.encoders.Hex;
07: import org.bouncycastle.util.test.SimpleTest;
08:
09: /**
10: * MD5 HMac Test, test vectors from RFC 2202
11: */
12: public class MD5HMacTest extends SimpleTest {
13: final static String[] keys = {
14: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
15: "4a656665",
16: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17: "0102030405060708090a0b0c0d0e0f10111213141516171819",
18: "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
19: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
21:
22: final static String[] digests = {
23: "9294727a3638bb1c13f48ef8158bfc9d",
24: "750c783e6ab0b503eaa86e310a5db738",
25: "56be34521d144c88dbb8c733f0e8b3f6",
26: "697eaf0aca3a3aea3a75164746ffaa79",
27: "56461ef2342edc00f9bab995690efd4c",
28: "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
29: "6f630fad67cda0ee1fb1f562db3aa53e" };
30:
31: final static String[] messages = {
32: "Hi There",
33: "what do ya want for nothing?",
34: "0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
35: "0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
36: "Test With Truncation",
37: "Test Using Larger Than Block-Size Key - Hash Key First",
38: "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" };
39:
40: public String getName() {
41: return "MD5HMac";
42: }
43:
44: public void performTest() {
45: HMac hmac = new HMac(new MD5Digest());
46: byte[] resBuf = new byte[hmac.getMacSize()];
47:
48: for (int i = 0; i < messages.length; i++) {
49: byte[] m = messages[i].getBytes();
50: if (messages[i].startsWith("0x")) {
51: m = Hex.decode(messages[i].substring(2));
52: }
53: hmac.init(new KeyParameter(Hex.decode(keys[i])));
54: hmac.update(m, 0, m.length);
55: hmac.doFinal(resBuf, 0);
56:
57: if (!areEqual(resBuf, Hex.decode(digests[i]))) {
58: fail("Vector " + i + " failed");
59: }
60: }
61:
62: // test reset
63: int vector = 0; // vector used for test
64: byte[] m = messages[vector].getBytes();
65: if (messages[vector].startsWith("0x")) {
66: m = Hex.decode(messages[vector].substring(2));
67: }
68: hmac.init(new KeyParameter(Hex.decode(keys[vector])));
69: hmac.update(m, 0, m.length);
70: hmac.doFinal(resBuf, 0);
71: hmac.reset();
72: hmac.update(m, 0, m.length);
73: hmac.doFinal(resBuf, 0);
74:
75: if (!areEqual(resBuf, Hex.decode(digests[vector]))) {
76: fail("Reset with vector " + vector + " failed");
77: }
78: }
79:
80: public static void main(String[] args) {
81: runTest(new MD5HMacTest());
82: }
83: }
|