Coverage Report

Created: 2026-10-27 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/fipsmodule/keccak/internal.h
Line
Count
Source
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
#include "../../internal.h"
21
22
23
BSSL_NAMESPACE_BEGIN
24
25
#define BITS_TO_BYTES(BITS) ((BITS) / 8)
26
#define SHA3_256_DIGEST_LENGTH BITS_TO_BYTES(256)
27
#define SHA3_384_DIGEST_LENGTH BITS_TO_BYTES(384)
28
#define SHA3_512_DIGEST_LENGTH BITS_TO_BYTES(512)
29
#define SHA3_BLOCK_SIZE(BITS) BITS_TO_BYTES(1600 - 2 * (BITS))
30
#define SHA3_256_CBLOCK SHA3_BLOCK_SIZE(256)
31
#define SHA3_384_CBLOCK SHA3_BLOCK_SIZE(384)
32
#define SHA3_512_CBLOCK SHA3_BLOCK_SIZE(512)
33
#define SHAKE256_CBLOCK SHA3_BLOCK_SIZE(256)
34
35
const size_t kShake256CblockSize = SHAKE256_CBLOCK;
36
37
enum boringssl_keccak_config_t : int32_t {
38
  boringssl_sha3_256,
39
  boringssl_sha3_384,
40
  boringssl_sha3_512,
41
  boringssl_shake128,
42
  boringssl_shake256,
43
  boringssl_turboshake128,
44
  boringssl_turboshake256,
45
  boringssl_cshake128,
46
  boringssl_cshake256,
47
};
48
49
enum boringssl_keccak_phase_t : int32_t {
50
  boringssl_keccak_phase_absorb,
51
  boringssl_keccak_phase_squeeze,
52
};
53
54
struct BORINGSSL_keccak_st {
55
  // Note: the state with 64-bit integers comes first so that the size of this
56
  // struct is easy to compute on all architectures without padding surprises
57
  // due to alignment.
58
  uint64_t state[25];
59
  enum boringssl_keccak_config_t config;
60
  enum boringssl_keccak_phase_t phase;
61
  size_t required_out_len;
62
  size_t rate_bytes;
63
  size_t absorb_offset;
64
  size_t squeeze_offset;
65
};
66
67
// BORINGSSL_keccak hashes `in_len` bytes from `in` and writes `out_len` bytes
68
// of output to `out`. If the `config` specifies a fixed-output function, like
69
// SHA3-256, then `out_len` must be the correct length for that function. The
70
// cSHAKE configurations are only valid with the BORINGSSL_cshake functions.
71
OPENSSL_EXPORT void BORINGSSL_keccak(uint8_t *out, size_t out_len,
72
                                     const uint8_t *in, size_t in_len,
73
                                     enum boringssl_keccak_config_t config);
74
75
// BORINGSSL_keccak_init prepares `ctx` for absorbing. If the `config` specifies
76
// a fixed-output function, like SHA3-256, then the output must be squeezed in a
77
// single call to `BORINGSSL_keccak_squeeze`. In that case, it is recommended to
78
// use `BORINGSSL_keccak` if the input can be absorbed in a single call.
79
OPENSSL_EXPORT void BORINGSSL_keccak_init(
80
    struct BORINGSSL_keccak_st *ctx, enum boringssl_keccak_config_t config);
81
82
// BORINGSSL_cshake_init prepares `ctx` for cSHAKE, including absorbing the
83
// function-name and customization strings. `config` must be
84
// `boringssl_cshake128` or `boringssl_cshake256`. The message may then be
85
// absorbed and output squeezed with the usual Keccak functions.
86
OPENSSL_EXPORT void BORINGSSL_cshake_init(struct BORINGSSL_keccak_st *ctx,
87
                                          enum boringssl_keccak_config_t config,
88
                                          const uint8_t *function_name,
89
                                          size_t function_name_len,
90
                                          const uint8_t *customization,
91
                                          size_t customization_len);
92
93
// BORINGSSL_cshake is the one-shot form of BORINGSSL_cshake_init.
94
OPENSSL_EXPORT void BORINGSSL_cshake(uint8_t *out, size_t out_len,
95
                                     const uint8_t *in, size_t in_len,
96
                                     enum boringssl_keccak_config_t config,
97
                                     const uint8_t *function_name,
98
                                     size_t function_name_len,
99
                                     const uint8_t *customization,
100
                                     size_t customization_len);
101
102
// BORINGSSL_keccak_absorb absorbs `in_len` bytes from `in`.
103
OPENSSL_EXPORT void BORINGSSL_keccak_absorb(struct BORINGSSL_keccak_st *ctx,
104
                                            const uint8_t *in, size_t in_len);
105
106
// BORINGSSL_keccak_squeeze writes `out_len` bytes to `out` from `ctx`. If the
107
// configuration previously passed in `BORINGSSL_keccak_init` specifies a
108
// fixed-output function, then a single call to `BORINGSSL_keccak_squeeze` is
109
// allowed, where `out_len` must be the correct length for that function.
110
OPENSSL_EXPORT void BORINGSSL_keccak_squeeze(struct BORINGSSL_keccak_st *ctx,
111
                                             uint8_t *out, size_t out_len);
112
113
#if defined(__has_attribute)
114
#if __has_attribute(vector_size)
115
116
#define HAVE_KECCAK_X2
117
118
#if defined(OPENSSL_AARCH64)
119
#define HAVE_KECCAK_X4
120
#define KECCAK_X4_TARGET
121
#endif
122
123
#if defined(OPENSSL_X86_64)
124
#define HAVE_KECCAK_X4
125
#define KECCAK_X4_TARGET __attribute__((target("avx2")))
126
#endif
127
128
#endif  // vector_size
129
#endif  // __has_attribute
130
131
#if defined(HAVE_KECCAK_X2)
132
// BORINGSSL_keccak_squeeze_x2 performs BORINGSSL_keccak_squeeze in parallel
133
// with two same-length outputs. The contexts must be in equivalent state (i.e.
134
// same config, same amount of bytes absorbed and squeezed).
135
// This function is not compatible with cSHAKE nor TurboSHAKE.
136
OPENSSL_EXPORT void BORINGSSL_keccak_squeeze_x2(
137
    struct BORINGSSL_keccak_st ctx[2], uint8_t *outs[2], size_t out_len);
138
139
// BORINGSSL_keccak_short_x2 performs BORINGSSL_keccak in parallel on two
140
// same-length strings with same-length outputs. `in_len` must be less than 72
141
// (or actually `rate_bytes`).
142
// This function is not compatible with cSHAKE nor TurboSHAKE.
143
OPENSSL_EXPORT void BORINGSSL_keccak_short_x2(
144
    uint8_t *outs[2], size_t out_len, const uint8_t *ins[2], size_t in_len,
145
    enum boringssl_keccak_config_t config);
146
#endif
147
148
#if defined(HAVE_KECCAK_X4)
149
// BORINGSSL_have_keccak_x4 needs to return true before using any of the _x4
150
// variants here, as they may require additional CPU features.
151
// This function is not compatible with cSHAKE nor TurboSHAKE.
152
163k
inline bool BORINGSSL_have_keccak_x4() {
153
163k
#if defined(OPENSSL_X86_64)
154
163k
  return CRYPTO_is_AVX2_capable();
155
#else
156
  return true;
157
#endif
158
163k
}
159
160
// BORINGSSL_keccak_squeeze_x4 performs BORINGSSL_keccak_squeeze in parallel
161
// with four same-length outputs. The contexts must be in equivalent state (i.e.
162
// same config, same amount of bytes absorbed and squeezed).
163
// This function is not compatible with cSHAKE nor TurboSHAKE.
164
OPENSSL_EXPORT void BORINGSSL_keccak_squeeze_x4(
165
    struct BORINGSSL_keccak_st ctx[4], uint8_t *outs[4], size_t out_len);
166
167
// BORINGSSL_keccak_short_x4 performs BORINGSSL_keccak in parallel on four
168
// same-length strings with same-length outputs. in_len must be less than 72
169
// (or actually `rate_bytes`).
170
// This function is not compatible with cSHAKE nor TurboSHAKE.
171
OPENSSL_EXPORT void BORINGSSL_keccak_short_x4(
172
    uint8_t *outs[4], size_t out_len, const uint8_t *ins[4], size_t in_len,
173
    enum boringssl_keccak_config_t config);
174
#endif
175
176
BSSL_NAMESPACE_END
177
178
#endif  // OPENSSL_HEADER_CRYPTO_FIPSMODULE_KECCAK_INTERNAL_H