View Javadoc
1   package org.sentrysoftware.ipmi.core.coding.security;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, Sentry Software
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * HMAC-SHA1-96 integrity algorithm.
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       * Initiates HMAC-SHA1-96 integrity algorithm.
43       *
44       * @throws NoSuchAlgorithmException
45       *             - when initiation of the algorithm fails
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 /*there are no integrity pad bytes*/) {
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  }