1 // Copyright 2023 The BoringSSL Authors
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 //     https://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 #ifndef OPENSSL_HEADER_CRYPTO_FIPSMODULE_KECCAK_INTERNAL_H
16 #define OPENSSL_HEADER_CRYPTO_FIPSMODULE_KECCAK_INTERNAL_H
17 
18 #include <openssl/base.h>
19 
20 #if defined(__cplusplus)
21 extern "C" {
22 #endif
23 
24 
25 enum boringssl_keccak_config_t : int32_t {
26   boringssl_sha3_256,
27   boringssl_sha3_512,
28   boringssl_shake128,
29   boringssl_shake256,
30 };
31 
32 enum boringssl_keccak_phase_t : int32_t {
33   boringssl_keccak_phase_absorb,
34   boringssl_keccak_phase_squeeze,
35 };
36 
37 struct BORINGSSL_keccak_st {
38   // Note: the state with 64-bit integers comes first so that the size of this
39   // struct is easy to compute on all architectures without padding surprises
40   // due to alignment.
41   uint64_t state[25];
42   enum boringssl_keccak_config_t config;
43   enum boringssl_keccak_phase_t phase;
44   size_t required_out_len;
45   size_t rate_bytes;
46   size_t absorb_offset;
47   size_t squeeze_offset;
48 };
49 
50 // BORINGSSL_keccak hashes |in_len| bytes from |in| and writes |out_len| bytes
51 // of output to |out|. If the |config| specifies a fixed-output function, like
52 // SHA3-256, then |out_len| must be the correct length for that function.
53 OPENSSL_EXPORT void BORINGSSL_keccak(uint8_t *out, size_t out_len,
54                                      const uint8_t *in, size_t in_len,
55                                      enum boringssl_keccak_config_t config);
56 
57 // BORINGSSL_keccak_init prepares |ctx| for absorbing. If the |config| specifies
58 // a fixed-output function, like SHA3-256, then the output must be squeezed in a
59 // single call to |BORINGSSL_keccak_squeeze|. In that case, it is recommended to
60 // use |BORINGSSL_keccak| if the input can be absorbed in a single call.
61 OPENSSL_EXPORT void BORINGSSL_keccak_init(
62     struct BORINGSSL_keccak_st *ctx, enum boringssl_keccak_config_t config);
63 
64 // BORINGSSL_keccak_absorb absorbs |in_len| bytes from |in|.
65 OPENSSL_EXPORT void BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx,
66                                             const uint8_t *in, size_t in_len);
67 
68 // BORINGSSL_keccak_squeeze writes |out_len| bytes to |out| from |ctx|. If the
69 // configuration previously passed in |BORINGSSL_keccak_init| specifies a
70 // fixed-output function, then a single call to |BORINGSSL_keccak_squeeze| is
71 // allowed, where |out_len| must be the correct length for that function.
72 OPENSSL_EXPORT void BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx,
73                                              uint8_t *out, size_t out_len);
74 
75 #if defined(__cplusplus)
76 }
77 #endif
78 
79 #endif  // OPENSSL_HEADER_CRYPTO_FIPSMODULE_KECCAK_INTERNAL_H
80