01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.digests.SHA384Digest;
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: * SHA384 HMac Test
14: */
15: public class SHA384HMacTest 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: "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6",
27: "af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649",
28: "88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27",
29: "3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffb",
30: "3abf34c3503b2a23a46efc619baef897f4c8e42c934ce55ccbae9740fcbc1af4ca62269e2a37cd88ba926341efe4aeea",
31: "4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952",
32: "6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523e" };
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 "SHA384HMac";
45: }
46:
47: public TestResult perform() {
48: HMac hmac = new HMac(new SHA384Digest());
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: SHA384HMacTest test = new SHA384HMacTest();
92: TestResult result = test.perform();
93:
94: System.out.println(result);
95: }
96: }
|