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
23 /**
24 * GordianKnot interface for Message Authentication Codes.
25 */
26 public interface GordianMac
27 extends GordianConsumer {
28 /**
29 * Obtain MacSpec.
30 *
31 * @return the MacSpec
32 */
33 GordianMacSpec getMacSpec();
34
35 /**
36 * Obtain the key.
37 *
38 * @return the key
39 */
40 GordianKey<GordianMacSpec> getKey();
41
42 /**
43 * Obtain the keyType.
44 *
45 * @return the keyType
46 */
47 byte[] getInitVector();
48
49 /**
50 * Obtain the MAC size.
51 *
52 * @return the MAC size
53 */
54 int getMacSize();
55
56 /**
57 * Initialise the MAC with the given parameters.
58 *
59 * @param pParams the parameters
60 * @throws GordianException on error
61 */
62 void init(GordianMacParameters pParams) throws GordianException;
63
64 /**
65 * Init with bytes as key.
66 *
67 * @param pKeyBytes the bytes to use
68 * @throws GordianException on error
69 */
70 void initKeyBytes(byte[] pKeyBytes) throws GordianException;
71
72 /**
73 * Calculate the MAC.
74 *
75 * @return the MAC
76 */
77 byte[] finish();
78
79 /**
80 * Calculate the MAC, and return it in the buffer provided.
81 *
82 * @param pBuffer the buffer to return the digest in.
83 * @param pOffset the offset in the buffer to store the digest.
84 * @return the number of bytes placed into buffer
85 * @throws GordianException on error
86 */
87 int finish(byte[] pBuffer,
88 int pOffset) throws GordianException;
89
90 /**
91 * Update the MAC, calculate and reset it.
92 *
93 * @param pBytes the bytes to update with.
94 * @return the MAC
95 * @throws GordianException on error
96 */
97 default byte[] finish(final byte[] pBytes) throws GordianException {
98 update(pBytes);
99 return finish();
100 }
101 }