1 package org.sentrysoftware.ipmi.core.coding.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import javax.crypto.Mac;
26 import javax.crypto.spec.SecretKeySpec;
27 import java.security.InvalidKeyException;
28 import java.security.NoSuchAlgorithmException;
29
30
31
32
33 public class IntegrityHmacSha1_96 extends IntegrityAlgorithm {
34
35 public static final String ALGORITHM_NAME = "HmacSHA1";
36 private Mac mac;
37
38 private static final byte[] CONST1 = new byte[] { 1, 1, 1, 1,
39 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
40
41
42
43
44
45
46
47 public IntegrityHmacSha1_96() throws NoSuchAlgorithmException {
48 mac = Mac.getInstance(ALGORITHM_NAME);
49 }
50
51 @Override
52 public void initialize(byte[] sik) throws InvalidKeyException {
53 super.initialize(sik);
54
55 SecretKeySpec k1 = new SecretKeySpec(sik, ALGORITHM_NAME);
56
57 mac.init(k1);
58
59 k1 = new SecretKeySpec(mac.doFinal(CONST1), ALGORITHM_NAME);
60
61 mac.init(k1);
62 }
63
64 @Override
65 public byte getCode() {
66 return SecurityConstants.IA_HMAC_SHA1_96;
67 }
68
69 @Override
70 public byte[] generateAuthCode(final byte[] base) {
71 if (sik == null) {
72 throw new NullPointerException("Algorithm not initialized.");
73 }
74
75 byte[] result = new byte[12];
76 byte[] updatedBase;
77
78 if(base[base.length - 2] == 0 ) {
79 updatedBase = injectIntegrityPad(base,12);
80 } else {
81 updatedBase = base;
82 }
83
84 System.arraycopy(mac.doFinal(updatedBase), 0, result, 0, 12);
85
86 return result;
87 }
88
89 }