01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.SHA512Digest;
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: * SHA512 HMac Test
14: */
15: public class SHA512HMacTest 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: "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
27: "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737",
28: "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb",
29: "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd",
30: "415fad6271580a531d4179bc891d87a650188707922a4fbb36663a1eb16da008711c5b50ddd0fc235084eb9d3364a1454fb2ef67cd1d29fe6773068ea266e96b",
31: "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598",
32: "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58" };
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 "SHA512HMac";
45: }
46:
47: public TestResult perform() {
48: HMac hmac = new HMac(new SHA512Digest());
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: SHA512HMacTest test = new SHA512HMacTest();
92: TestResult result = test.perform();
93:
94: System.out.println(result);
95: }
96: }
|