01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.SHA1Digest;
04: import org.bouncycastle.crypto.macs.HMac;
05: import org.bouncycastle.crypto.params.KeyParameter;
06: import org.bouncycastle.util.Arrays;
07: import org.bouncycastle.util.encoders.Hex;
08: import org.bouncycastle.util.test.SimpleTestResult;
09: import org.bouncycastle.util.test.Test;
10: import org.bouncycastle.util.test.TestResult;
11:
12: /**
13: * SHA1 HMac Test, test vectors from RFC 2202
14: */
15: public class SHA1HMacTest implements Test {
16: final static String[] keys = {
17: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
18: "4a656665",
19: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20: "0102030405060708090a0b0c0d0e0f10111213141516171819",
21: "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
22: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
23: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
24:
25: final static String[] digests = {
26: "b617318655057264e28bc0b6fb378c8ef146be00",
27: "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
28: "125d7342b9ac11cd91a39af48aa17b4f63f175d3",
29: "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
30: "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
31: "aa4ae5e15272d00e95705637ce8a3b55ed402112",
32: "e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
33: "4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
34: "aa4ae5e15272d00e95705637ce8a3b55ed402112",
35: "e8e99d0f45237d786d6bbaa7965c7808bbff1a91" };
36:
37: final static String[] messages = {
38: "Hi There",
39: "what do ya want for nothing?",
40: "0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
41: "0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
42: "Test With Truncation",
43: "Test Using Larger Than Block-Size Key - Hash Key First",
44: "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" };
45:
46: public String getName() {
47: return "SHA1HMac";
48: }
49:
50: public TestResult perform() {
51: HMac hmac = new HMac(new SHA1Digest());
52: byte[] resBuf = new byte[hmac.getMacSize()];
53:
54: for (int i = 0; i < messages.length; i++) {
55: byte[] m = messages[i].getBytes();
56: if (messages[i].startsWith("0x")) {
57: m = Hex.decode(messages[i].substring(2));
58: }
59: hmac.init(new KeyParameter(Hex.decode(keys[i])));
60: hmac.update(m, 0, m.length);
61: hmac.doFinal(resBuf, 0);
62:
63: if (!Arrays.areEqual(resBuf, Hex.decode(digests[i]))) {
64: return new SimpleTestResult(false, getName()
65: + ": Vector " + i + " failed");
66: }
67: }
68:
69: //
70: // test reset
71: //
72: int vector = 0; // vector used for test
73: byte[] m = messages[vector].getBytes();
74: if (messages[vector].startsWith("0x")) {
75: m = Hex.decode(messages[vector].substring(2));
76: }
77: hmac.init(new KeyParameter(Hex.decode(keys[vector])));
78: hmac.update(m, 0, m.length);
79: hmac.doFinal(resBuf, 0);
80: hmac.reset();
81: hmac.update(m, 0, m.length);
82: hmac.doFinal(resBuf, 0);
83:
84: if (!Arrays.areEqual(resBuf, Hex.decode(digests[vector]))) {
85: return new SimpleTestResult(false, getName()
86: + "Reset with vector " + vector + " failed");
87: }
88:
89: return new SimpleTestResult(true, getName() + ": Okay");
90: }
91:
92: public static void main(String[] args) {
93: SHA1HMacTest test = new SHA1HMacTest();
94: TestResult result = test.perform();
95:
96: System.out.println(result);
97: }
98: }
|