View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.gordianknot.api.mac;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianConsumer;
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.key.GordianKey;
22  import io.github.tonywasher.joceanus.gordianknot.api.mac.spec.GordianMacSpec;
23  
24  /**
25   * GordianKnot interface for Message Authentication Codes.
26   */
27  public interface GordianMac
28          extends GordianConsumer {
29      /**
30       * Obtain MacSpec.
31       *
32       * @return the MacSpec
33       */
34      GordianMacSpec getMacSpec();
35  
36      /**
37       * Obtain the key.
38       *
39       * @return the key
40       */
41      GordianKey<GordianMacSpec> getKey();
42  
43      /**
44       * Obtain the keyType.
45       *
46       * @return the keyType
47       */
48      byte[] getInitVector();
49  
50      /**
51       * Obtain the MAC size.
52       *
53       * @return the MAC size
54       */
55      int getMacSize();
56  
57      /**
58       * Initialise the MAC with the given parameters.
59       *
60       * @param pParams the parameters
61       * @throws GordianException on error
62       */
63      void init(GordianMacParams pParams) throws GordianException;
64  
65      /**
66       * Init with bytes as key.
67       *
68       * @param pKeyBytes the bytes to use
69       * @throws GordianException on error
70       */
71      void initKeyBytes(byte[] pKeyBytes) throws GordianException;
72  
73      /**
74       * Calculate the MAC.
75       *
76       * @return the MAC
77       */
78      byte[] finish();
79  
80      /**
81       * Calculate the MAC, and return it in the buffer provided.
82       *
83       * @param pBuffer the buffer to return the digest in.
84       * @param pOffset the offset in the buffer to store the digest.
85       * @return the number of bytes placed into buffer
86       * @throws GordianException on error
87       */
88      int finish(byte[] pBuffer,
89                 int pOffset) throws GordianException;
90  
91      /**
92       * Update the MAC, calculate and reset it.
93       *
94       * @param pBytes the bytes to update with.
95       * @return the MAC
96       * @throws GordianException on error
97       */
98      default byte[] finish(final byte[] pBytes) throws GordianException {
99          update(pBytes);
100         return finish();
101     }
102 }