01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.SHA224Digest;
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: * SHA224 HMac Test
14: */
15: public class SHA224HMacTest implements Test {
16: final static String[] keys = {
17: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
18: "4a656665",
19: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
20: "0102030405060708090a0b0c0d0e0f10111213141516171819",
21: "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
22: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
23: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" };
24:
25: final static String[] digests = {
26: "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
27: "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44",
28: "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea",
29: "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a",
30: "0e2aea68a90c8d37c988bcdb9fca6fa8099cd857c7ec4a1815cac54c",
31: "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e",
32: "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1" };
33:
34: final static String[] messages = {
35: "Hi There",
36: "what do ya want for nothing?",
37: "0xdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
38: "0xcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd",
39: "Test With Truncation",
40: "Test Using Larger Than Block-Size Key - Hash Key First",
41: "This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm." };
42:
43: public String getName() {
44: return "SHA224HMac";
45: }
46:
47: public TestResult perform() {
48: HMac hmac = new HMac(new SHA224Digest());
49: byte[] resBuf = new byte[hmac.getMacSize()];
50:
51: for (int i = 0; i < messages.length; i++) {
52: byte[] m = messages[i].getBytes();
53: if (messages[i].startsWith("0x")) {
54: m = Hex.decode(messages[i].substring(2));
55: }
56: hmac.init(new KeyParameter(Hex.decode(keys[i])));
57: hmac.update(m, 0, m.length);
58: hmac.doFinal(resBuf, 0);
59:
60: if (!Arrays.areEqual(resBuf, Hex.decode(digests[i]))) {
61: return new SimpleTestResult(false, getName()
62: + ": Vector " + i + " failed got -"
63: + new String(Hex.encode(resBuf)));
64: }
65: }
66:
67: //
68: // test reset
69: //
70: int vector = 0; // vector used for test
71: byte[] m = messages[vector].getBytes();
72: if (messages[vector].startsWith("0x")) {
73: m = Hex.decode(messages[vector].substring(2));
74: }
75: hmac.init(new KeyParameter(Hex.decode(keys[vector])));
76: hmac.update(m, 0, m.length);
77: hmac.doFinal(resBuf, 0);
78: hmac.reset();
79: hmac.update(m, 0, m.length);
80: hmac.doFinal(resBuf, 0);
81:
82: if (!Arrays.areEqual(resBuf, Hex.decode(digests[vector]))) {
83: return new SimpleTestResult(false, getName()
84: + "Reset with vector " + vector + " failed");
85: }
86:
87: return new SimpleTestResult(true, getName() + ": Okay");
88: }
89:
90: public static void main(String[] args) {
91: SHA224HMacTest test = new SHA224HMacTest();
92: TestResult result = test.perform();
93:
94: System.out.println(result);
95: }
96: }
|