xref: /aosp_15_r20/external/tink/java_src/src/main/java/com/google/crypto/tink/mac/ChunkedMacComputation.java (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.mac;
18 
19 import java.nio.ByteBuffer;
20 import java.security.GeneralSecurityException;
21 
22 /**
23  * An interface representing a computation of the Streaming MAC.
24  *
25  * <p>WARNING: Implementations of this interface are not thread-safe, so the caller must ensure
26  * correctness in case of concurrent use of an instance of this class.
27  */
28 public interface ChunkedMacComputation {
29   /**
30    * Processes the next chunk of input, represented by {@code ByteBuffer data}.
31    * In particular, reads the {@code data.remaining()} number of bytes from the provided buffer,
32    * starting at the byte with position {@code data.position()}.
33    *
34    * <p>Updates the inner state of the computation. Requires exclusive access.
35    *
36    * <p>NOTE: arbitrary slicing of data is permitted, i.e. a series of {@code update()}'s with
37    * inputs {@code "ab"}, {@code "cd"}, and {@code "ef"} produces the same result as a series of
38    * inputs {@code "abc"}, {@code "def"}.
39    *
40    * @throws IllegalStateException if called after computeMac()
41    * @throws GeneralSecurityException when something went wrong with the update
42    */
update(ByteBuffer data)43   void update(ByteBuffer data) throws GeneralSecurityException;
44 
45   /**
46    * Computes a tag for the provided data. After this method has been called, the object can no
47    * longer be used.
48    *
49    * <p>Requires exclusive access.
50    *
51    * @throws IllegalStateException when called more than once
52    * @throws GeneralSecurityException when something went wrong with the computation
53    */
computeMac()54   byte[] computeMac() throws GeneralSecurityException;
55 }
56