Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_sha3.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 - 2024, 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))
33
34#define LC_SHA3_STATE_SIZE_ALIGN(x) (x + LC_HASH_COMMON_ALIGNMENT)
36
37/********************************** SHA3-224 **********************************/
38
43extern const struct lc_hash *lc_sha3_224;
44
46#define LC_SHA3_224_SIZE_DIGEST_BITS 224
47#define LC_SHA3_224_SIZE_DIGEST (LC_SHA3_224_SIZE_DIGEST_BITS >> 3)
48#define LC_SHA3_224_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_224_SIZE_DIGEST_BITS)
49
50struct lc_sha3_224_state {
51 uint64_t state[LC_SHA3_STATE_WORDS];
52 size_t msg_len;
53 size_t digestsize;
54 uint8_t offset;
55 uint8_t r;
56 uint8_t rword;
57 uint8_t padding;
58 uint8_t squeeze_more : 1;
59};
60
61#define LC_SHA3_224_STATE_SIZE (sizeof(struct lc_sha3_224_state))
62#define LC_SHA3_224_CTX_SIZE \
63 (sizeof(struct lc_hash_ctx) + LC_SHA3_224_STATE_SIZE)
64
65#define LC_SHA3_224_CTX(name) \
66 LC_HASH_SET_CTX(name, lc_sha3_224); \
67 lc_hash_zero(name)
69
75#define LC_SHA3_224_CTX_ON_STACK(name) \
76 _Pragma("GCC diagnostic push") _Pragma( \
77 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
78 LC_ALIGNED_BUFFER( \
79 name##_ctx_buf, \
80 LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_224_CTX_SIZE), \
81 LC_HASH_COMMON_ALIGNMENT); \
82 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
83 LC_SHA3_224_CTX(name); \
84 _Pragma("GCC diagnostic pop")
85
86/********************************** SHA3-256 **********************************/
87
92extern const struct lc_hash *lc_sha3_256;
93
95#define LC_SHA3_256_SIZE_DIGEST_BITS 256
96#define LC_SHA3_256_SIZE_DIGEST (LC_SHA3_256_SIZE_DIGEST_BITS >> 3)
97#define LC_SHA3_256_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_256_SIZE_DIGEST_BITS)
98
99struct lc_sha3_256_state {
100 uint64_t state[LC_SHA3_STATE_WORDS];
101 size_t msg_len;
102 size_t digestsize;
103 uint8_t offset;
104 uint8_t r;
105 uint8_t rword;
106 uint8_t padding;
107 uint8_t squeeze_more : 1;
108};
109
110#define LC_SHA3_256_STATE_SIZE (sizeof(struct lc_sha3_256_state))
111#define LC_SHA3_256_CTX_SIZE (sizeof(struct lc_hash) + LC_SHA3_256_STATE_SIZE)
112
113#define LC_SHA3_256_CTX(name) \
114 LC_HASH_SET_CTX(name, lc_sha3_256); \
115 lc_hash_zero(name)
117
123#define LC_SHA3_256_CTX_ON_STACK(name) \
124 _Pragma("GCC diagnostic push") _Pragma( \
125 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
126 LC_ALIGNED_BUFFER( \
127 name##_ctx_buf, \
128 LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE), \
129 LC_HASH_COMMON_ALIGNMENT); \
130 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
131 LC_SHA3_256_CTX(name); \
132 _Pragma("GCC diagnostic pop")
133
134/********************************** SHA3-384 **********************************/
135
140extern const struct lc_hash *lc_sha3_384;
141
143#define LC_SHA3_384_SIZE_DIGEST_BITS 384
144#define LC_SHA3_384_SIZE_DIGEST (LC_SHA3_384_SIZE_DIGEST_BITS >> 3)
145#define LC_SHA3_384_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_384_SIZE_DIGEST_BITS)
146
147struct lc_sha3_384_state {
148 uint64_t state[LC_SHA3_STATE_WORDS];
149 size_t msg_len;
150 size_t digestsize;
151 uint8_t offset;
152 uint8_t r;
153 uint8_t rword;
154 uint8_t padding;
155 uint8_t squeeze_more : 1;
156};
157
158#define LC_SHA3_384_STATE_SIZE (sizeof(struct lc_sha3_384_state))
159#define LC_SHA3_384_CTX_SIZE (sizeof(struct lc_hash) + LC_SHA3_384_STATE_SIZE)
160
161#define LC_SHA3_384_CTX(name) \
162 LC_HASH_SET_CTX(name, lc_sha3_384); \
163 lc_hash_zero(name)
165
171#define LC_SHA3_384_CTX_ON_STACK(name) \
172 _Pragma("GCC diagnostic push") _Pragma( \
173 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
174 LC_ALIGNED_BUFFER( \
175 name##_ctx_buf, \
176 LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_384_CTX_SIZE), \
177 LC_HASH_COMMON_ALIGNMENT); \
178 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
179 LC_SHA3_384_CTX(name); \
180 _Pragma("GCC diagnostic pop")
181
182/********************************** SHA3-512 **********************************/
183
188extern const struct lc_hash *lc_sha3_512;
189
191#define LC_SHA3_512_SIZE_DIGEST_BITS 512
192#define LC_SHA3_512_SIZE_DIGEST (LC_SHA3_512_SIZE_DIGEST_BITS >> 3)
193#define LC_SHA3_512_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHA3_512_SIZE_DIGEST_BITS)
194
195struct lc_sha3_512_state {
196 uint64_t state[LC_SHA3_STATE_WORDS];
197 size_t msg_len;
198 size_t digestsize;
199 uint8_t offset;
200 uint8_t r;
201 uint8_t rword;
202 uint8_t padding;
203 uint8_t squeeze_more : 1;
204};
205
206#define LC_SHA3_512_STATE_SIZE (sizeof(struct lc_sha3_512_state))
207#define LC_SHA3_512_CTX_SIZE (sizeof(struct lc_hash) + LC_SHA3_512_STATE_SIZE)
208
209#define LC_SHA3_512_CTX(name) \
210 LC_HASH_SET_CTX(name, lc_sha3_512); \
211 lc_hash_zero(name)
213
219#define LC_SHA3_512_CTX_ON_STACK(name) \
220 _Pragma("GCC diagnostic push") _Pragma( \
221 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
222 LC_ALIGNED_BUFFER( \
223 name##_ctx_buf, \
224 LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_512_CTX_SIZE), \
225 LC_HASH_COMMON_ALIGNMENT); \
226 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
227 LC_SHA3_512_CTX(name); \
228 _Pragma("GCC diagnostic pop")
229
230/********************************* SHAKE-128 **********************************/
231
236extern const struct lc_hash *lc_shake128;
237
239#define LC_SHAKE_128_SIZE_DIGEST_BITS 128
240#define LC_SHAKE_128_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHAKE_128_SIZE_DIGEST_BITS)
241
242struct lc_shake_128_state {
243 uint64_t state[LC_SHA3_STATE_WORDS];
244 size_t msg_len;
245 size_t digestsize;
246 uint8_t offset;
247 uint8_t r;
248 uint8_t rword;
249 uint8_t padding;
250 uint8_t squeeze_more : 1;
251};
252
253#define LC_SHAKE_128_STATE_SIZE (sizeof(struct lc_shake_128_state))
254#define LC_SHAKE_128_CTX_SIZE (sizeof(struct lc_hash) + LC_SHAKE_128_STATE_SIZE)
255
256#define LC_SHAKE_128_CTX(name) \
257 LC_HASH_SET_CTX(name, lc_shake128); \
258 lc_hash_zero(name)
260
266#define LC_SHAKE_128_CTX_ON_STACK(name) \
267 _Pragma("GCC diagnostic push") _Pragma( \
268 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
269 LC_ALIGNED_BUFFER( \
270 name##_ctx_buf, \
271 LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_128_CTX_SIZE), \
272 LC_HASH_COMMON_ALIGNMENT); \
273 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
274 LC_SHAKE_128_CTX(name); \
275 _Pragma("GCC diagnostic pop")
276
277/********************************* SHAKE-256 **********************************/
278
283extern const struct lc_hash *lc_shake256;
284
286#define LC_SHAKE_256_SIZE_DIGEST_BITS 256
287#define LC_SHAKE_256_SIZE_BLOCK LC_SHA3_SIZE_RATE(LC_SHAKE_256_SIZE_DIGEST_BITS)
288
289#define LC_SHAKE_256_CTX_SIZE LC_SHA3_256_CTX_SIZE
290
291#define LC_SHAKE_256_CTX(name) \
292 LC_HASH_SET_CTX(name, lc_shake256); \
293 lc_hash_zero(name)
295
301#define LC_SHAKE_256_CTX_ON_STACK(name) \
302 _Pragma("GCC diagnostic push") _Pragma( \
303 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
304 LC_ALIGNED_BUFFER( \
305 name##_ctx_buf, \
306 LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_256_CTX_SIZE), \
307 LC_HASH_COMMON_ALIGNMENT); \
308 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
309 LC_SHAKE_256_CTX(name); \
310 _Pragma("GCC diagnostic pop")
311
312/********************************* cSHAKE-256 *********************************/
313
318extern const struct lc_hash *lc_cshake256;
319
321
322#define LC_CSHAKE_256_CTX(name) \
323 LC_HASH_SET_CTX(name, lc_cshake256); \
324 lc_hash_zero(name)
326
332#define LC_CSHAKE_256_CTX_ON_STACK(name) \
333 _Pragma("GCC diagnostic push") _Pragma( \
334 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
335 LC_ALIGNED_BUFFER( \
336 name##_ctx_buf, \
337 LC_SHA3_STATE_SIZE_ALIGN(LC_SHA3_256_CTX_SIZE), \
338 LC_HASH_COMMON_ALIGNMENT); \
339 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
340 LC_CSHAKE_256_CTX(name); \
341 _Pragma("GCC diagnostic pop")
342
343/********************************* cSHAKE-128 *********************************/
344
349extern const struct lc_hash *lc_cshake128;
350
352#define LC_CSHAKE_128_CTX(name) \
353 LC_HASH_SET_CTX(name, lc_cshake128); \
354 lc_hash_zero(name)
356
362#define LC_CSHAKE_128_CTX_ON_STACK(name) \
363 _Pragma("GCC diagnostic push") _Pragma( \
364 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
365 LC_ALIGNED_BUFFER( \
366 name##_ctx_buf, \
367 LC_SHA3_STATE_SIZE_ALIGN(LC_SHAKE_128_CTX_SIZE), \
368 LC_HASH_COMMON_ALIGNMENT); \
369 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
370 LC_CSHAKE_128_CTX(name); \
371 _Pragma("GCC diagnostic pop")
372
373/* Largest block size we support */
374#define LC_SHA3_MAX_SIZE_BLOCK LC_SHA3_224_SIZE_BLOCK
375
376#ifdef __cplusplus
377}
378#endif
379
380#endif /* LC_SHA3_H */
void 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_shake128
SHAKE128 algorithm reference.
const struct lc_hash * lc_sha3_384
SHA3-384 algorithm reference.
const struct lc_hash * lc_sha3_256
SHA3-256 algorithm reference.
const struct lc_hash * lc_sha3_512
SHA3-512 algorithm reference.
const struct lc_hash * lc_cshake256
cSHAKE256 algorithm reference
const struct lc_hash * lc_shake256
SHAKE256 algorithm reference.
const struct lc_hash * lc_cshake128
cSHAKE128 algorithm reference
const struct lc_hash * lc_sha3_224
SHA3-224 algorithm reference.