Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_sha3.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 - 2026, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_SHA3_H
21#define LC_SHA3_H
22
23#include "lc_hash.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30#define LC_SHA3_SIZE_RATE(bits) ((1600 - 2 * bits) >> 3)
31#define LC_SHA3_STATE_WORDS 25
32#define LC_SHA3_STATE_SIZE (LC_SHA3_STATE_WORDS * sizeof(uint64_t))
34
35/********************************** SHA3-224 **********************************/
36
41extern const struct lc_hash *lc_sha3_224;
42
44#define LC_SHA3_224_SIZE_DIGEST_BITS 224
45#define LC_SHA3_224_SIZE_DIGEST (LC_SHA3_224_SIZE_DIGEST_BITS >> 3)
46#define LC_SHA3_224_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_224_SIZE_DIGEST_BITS)
47
48struct lc_sha3_224_state {
49 uint64_t state[LC_SHA3_STATE_WORDS];
50 size_t msg_len;
51 size_t digestsize;
52 uint8_t offset;
53 uint8_t r;
54 uint8_t rword;
55 uint8_t padding;
56 uint8_t squeeze_more : 1;
57};
58
59#define LC_SHA3_224_STATE_SIZE \
60 LC_HASH_STATE_SIZE_ALIGN(sizeof(struct lc_sha3_224_state))
61#define LC_SHA3_224_CTX_SIZE \
62 (sizeof(struct lc_hash_ctx) + LC_SHA3_224_STATE_SIZE)
63
64#define LC_SHA3_224_CTX(name) \
65 LC_HASH_SET_CTX(name, lc_sha3_224); \
66 lc_hash_zero(name)
68
74#define LC_SHA3_224_CTX_ON_STACK(name) \
75 _Pragma("GCC diagnostic push") _Pragma( \
76 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
77 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_224_CTX_SIZE, \
78 LC_HASH_COMMON_ALIGNMENT); \
79 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
80 LC_SHA3_224_CTX(name); \
81 _Pragma("GCC diagnostic pop")
82
83/********************************** SHA3-256 **********************************/
84
89extern const struct lc_hash *lc_sha3_256;
90
92#define LC_SHA3_256_SIZE_DIGEST_BITS 256
93#define LC_SHA3_256_SIZE_DIGEST (LC_SHA3_256_SIZE_DIGEST_BITS >> 3)
94#define LC_SHA3_256_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_256_SIZE_DIGEST_BITS)
95
96struct lc_sha3_256_state {
97 uint64_t state[LC_SHA3_STATE_WORDS];
98 size_t msg_len;
99 size_t digestsize;
100 uint8_t offset;
101 uint8_t r;
102 uint8_t rword;
103 uint8_t padding;
104 uint8_t squeeze_more : 1;
105};
106
107#define LC_SHA3_256_STATE_SIZE \
108 LC_HASH_STATE_SIZE_ALIGN(sizeof(struct lc_sha3_256_state))
109#define LC_SHA3_256_CTX_SIZE \
110 (sizeof(struct lc_hash_ctx) + LC_SHA3_256_STATE_SIZE)
111
112#define LC_SHA3_256_CTX(name) \
113 LC_HASH_SET_CTX(name, lc_sha3_256); \
114 lc_hash_zero(name)
116
122#define LC_SHA3_256_CTX_ON_STACK(name) \
123 _Pragma("GCC diagnostic push") _Pragma( \
124 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
125 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_256_CTX_SIZE, \
126 LC_HASH_COMMON_ALIGNMENT); \
127 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
128 LC_SHA3_256_CTX(name); \
129 _Pragma("GCC diagnostic pop")
130
131/********************************** SHA3-384 **********************************/
132
137extern const struct lc_hash *lc_sha3_384;
138
140#define LC_SHA3_384_SIZE_DIGEST_BITS 384
141#define LC_SHA3_384_SIZE_DIGEST (LC_SHA3_384_SIZE_DIGEST_BITS >> 3)
142#define LC_SHA3_384_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_384_SIZE_DIGEST_BITS)
143
144struct lc_sha3_384_state {
145 uint64_t state[LC_SHA3_STATE_WORDS];
146 size_t msg_len;
147 size_t digestsize;
148 uint8_t offset;
149 uint8_t r;
150 uint8_t rword;
151 uint8_t padding;
152 uint8_t squeeze_more : 1;
153};
154
155#define LC_SHA3_384_STATE_SIZE \
156 LC_HASH_STATE_SIZE_ALIGN(sizeof(struct lc_sha3_384_state))
157#define LC_SHA3_384_CTX_SIZE \
158 (sizeof(struct lc_hash_ctx) + LC_SHA3_384_STATE_SIZE)
159
160#define LC_SHA3_384_CTX(name) \
161 LC_HASH_SET_CTX(name, lc_sha3_384); \
162 lc_hash_zero(name)
164
170#define LC_SHA3_384_CTX_ON_STACK(name) \
171 _Pragma("GCC diagnostic push") _Pragma( \
172 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
173 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_384_CTX_SIZE, \
174 LC_HASH_COMMON_ALIGNMENT); \
175 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
176 LC_SHA3_384_CTX(name); \
177 _Pragma("GCC diagnostic pop")
178
179/********************************** SHA3-512 **********************************/
180
185extern const struct lc_hash *lc_sha3_512;
186
188#define LC_SHA3_512_SIZE_DIGEST_BITS 512
189#define LC_SHA3_512_SIZE_DIGEST (LC_SHA3_512_SIZE_DIGEST_BITS >> 3)
190#define LC_SHA3_512_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_512_SIZE_DIGEST_BITS)
191
192struct lc_sha3_512_state {
193 uint64_t state[LC_SHA3_STATE_WORDS];
194 size_t msg_len;
195 size_t digestsize;
196 uint8_t offset;
197 uint8_t r;
198 uint8_t rword;
199 uint8_t padding;
200 uint8_t squeeze_more : 1;
201};
202
203#define LC_SHA3_512_STATE_SIZE \
204 LC_HASH_STATE_SIZE_ALIGN(sizeof(struct lc_sha3_512_state))
205#define LC_SHA3_512_CTX_SIZE \
206 (sizeof(struct lc_hash_ctx) + LC_SHA3_512_STATE_SIZE)
207
208#define LC_SHA3_512_CTX(name) \
209 LC_HASH_SET_CTX(name, lc_sha3_512); \
210 lc_hash_zero(name)
212
218#define LC_SHA3_512_CTX_ON_STACK(name) \
219 _Pragma("GCC diagnostic push") _Pragma( \
220 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
221 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHA3_512_CTX_SIZE, \
222 LC_HASH_COMMON_ALIGNMENT); \
223 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
224 LC_SHA3_512_CTX(name); \
225 _Pragma("GCC diagnostic pop")
226
227/********************************* SHAKE-128 **********************************/
228
233extern const struct lc_hash *lc_shake128;
234
236#define LC_SHAKE_128_SIZE_DIGEST_BITS 128
237#define LC_SHAKE_128_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHAKE_128_SIZE_DIGEST_BITS)
238
239struct lc_shake_128_state {
240 uint64_t state[LC_SHA3_STATE_WORDS];
241 size_t msg_len;
242 size_t digestsize;
243 uint8_t offset;
244 uint8_t r;
245 uint8_t rword;
246 uint8_t padding;
247 uint8_t squeeze_more : 1;
248};
249
250#define LC_SHAKE_128_STATE_SIZE \
251 LC_HASH_STATE_SIZE_ALIGN(sizeof(struct lc_shake_128_state))
252#define LC_SHAKE_128_CTX_SIZE \
253 (sizeof(struct lc_hash_ctx) + LC_SHAKE_128_STATE_SIZE)
254
255#define LC_SHAKE_128_CTX(name) \
256 LC_HASH_SET_CTX(name, lc_shake128); \
257 lc_hash_zero(name)
259
265#define LC_SHAKE_128_CTX_ON_STACK(name) \
266 _Pragma("GCC diagnostic push") _Pragma( \
267 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
268 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_128_CTX_SIZE, \
269 LC_HASH_COMMON_ALIGNMENT); \
270 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
271 LC_SHAKE_128_CTX(name); \
272 _Pragma("GCC diagnostic pop")
273
274/********************************* SHAKE-256 **********************************/
275
280extern const struct lc_hash *lc_shake256;
281
283#define LC_SHAKE_256_SIZE_DIGEST_BITS 256
284#define LC_SHAKE_256_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHAKE_256_SIZE_DIGEST_BITS)
285
286#define LC_SHAKE_256_CTX_SIZE LC_SHA3_256_CTX_SIZE
287
288#define LC_SHAKE_256_CTX(name) \
289 LC_HASH_SET_CTX(name, lc_shake256); \
290 lc_hash_zero(name)
292
298#define LC_SHAKE_256_CTX_ON_STACK(name) \
299 _Pragma("GCC diagnostic push") _Pragma( \
300 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
301 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_256_CTX_SIZE, \
302 LC_HASH_COMMON_ALIGNMENT); \
303 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
304 LC_SHAKE_256_CTX(name); \
305 _Pragma("GCC diagnostic pop")
306
307/********************************* SHAKE-512 **********************************/
308
313extern const struct lc_hash *lc_shake512;
314
316#define LC_SHAKE_512_SIZE_DIGEST_BITS 512
317#define LC_SHAKE_512_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHAKE_512_SIZE_DIGEST_BITS)
318
319#define LC_SHAKE_512_CTX_SIZE LC_SHA3_512_CTX_SIZE
320
321#define LC_SHAKE_512_CTX(name) \
322 LC_HASH_SET_CTX(name, lc_shake512); \
323 lc_hash_zero(name)
325
331#define LC_SHAKE_512_CTX_ON_STACK(name) \
332 _Pragma("GCC diagnostic push") _Pragma( \
333 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
334 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_SHAKE_512_CTX_SIZE, \
335 LC_HASH_COMMON_ALIGNMENT); \
336 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
337 LC_SHAKE_512_CTX(name); \
338 _Pragma("GCC diagnostic pop")
339
340/********************************* cSHAKE-256 *********************************/
341
346extern const struct lc_hash *lc_cshake256;
347
349
350#define LC_CSHAKE_256_CTX(name) \
351 LC_HASH_SET_CTX(name, lc_cshake256); \
352 lc_hash_zero(name)
354
360#define LC_CSHAKE_256_CTX_ON_STACK(name) \
361 _Pragma("GCC diagnostic push") _Pragma( \
362 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
363 LC_ALIGNED_BUFFER( \
364 name##_ctx_buf, \
365 LC_HASH_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE), \
366 LC_HASH_COMMON_ALIGNMENT); \
367 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
368 LC_CSHAKE_256_CTX(name); \
369 _Pragma("GCC diagnostic pop")
370
371/********************************* cSHAKE-128 *********************************/
372
377extern const struct lc_hash *lc_cshake128;
378
380#define LC_CSHAKE_128_CTX(name) \
381 LC_HASH_SET_CTX(name, lc_cshake128); \
382 lc_hash_zero(name)
384
390#define LC_CSHAKE_128_CTX_ON_STACK(name) \
391 _Pragma("GCC diagnostic push") _Pragma( \
392 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
393 LC_ALIGNED_BUFFER( \
394 name##_ctx_buf, \
395 LC_HASH_STATE_SIZE_ALIGN(LC_SHAKE_128_CTX_SIZE), \
396 LC_HASH_COMMON_ALIGNMENT); \
397 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
398 LC_CSHAKE_128_CTX(name); \
399 _Pragma("GCC diagnostic pop")
400
401/* Largest block size we support */
402#define LC_SHA3_MAX_SIZE_BLOCK LC_SHA3_224_SIZE_BLOCK
403
404#ifdef __cplusplus
405}
406#endif
407
408#endif /* LC_SHA3_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
const struct lc_hash * lc_sha3_384
SHA3-384 algorithm reference.
const struct lc_hash * lc_shake512
SHAKE512 algorithm reference.
const struct lc_hash * lc_sha3_224
SHA3-224 algorithm reference.
const struct lc_hash * lc_cshake256
cSHAKE256 algorithm reference
const struct lc_hash * lc_cshake128
cSHAKE128 algorithm reference
const struct lc_hash * lc_shake256
SHAKE256 algorithm reference.
const struct lc_hash * lc_shake128
SHAKE128 algorithm reference.
const struct lc_hash * lc_sha3_256
SHA3-256 algorithm reference.
const struct lc_hash * lc_sha3_512
SHA3-512 algorithm reference.