xref: /openbsd-src/lib/libcrypto/sha/sha3_internal.h (revision 8a4ba2fcb6aa72c943522f6d78af9c86ef1d240a)
1 /*	$OpenBSD: sha3_internal.h,v 1.3 2023/04/15 18:07:44 jsing Exp $	*/
2 /*
3  * The MIT License (MIT)
4  *
5  * Copyright (c) 2015 Markku-Juhani O. Saarinen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 // sha3.h
27 // 19-Nov-11  Markku-Juhani O. Saarinen <mjos@iki.fi>
28 
29 #ifndef SHA3_H
30 #define SHA3_H
31 
32 #include <stddef.h>
33 #include <stdint.h>
34 
35 #ifndef KECCAKF_ROUNDS
36 #define KECCAKF_ROUNDS 24
37 #endif
38 
39 #ifndef ROTL64
40 #define ROTL64(x, y) (((x) << (y)) | ((x) >> (64 - (y))))
41 #endif
42 
43 // state context
44 typedef struct {
45 	union {				// state:
46 		uint8_t b[200];		// 8-bit bytes
47 		uint64_t q[25];		// 64-bit words
48 	} st;
49 	int pt, rsiz, mdlen;		// these don't overflow
50 } sha3_ctx_t;
51 
52 // Compression function.
53 void sha3_keccakf(uint64_t st[25]);
54 
55 // OpenSSL - like interfece
56 int sha3_init(sha3_ctx_t *c, int mdlen);	// mdlen = hash output in bytes
57 int sha3_update(sha3_ctx_t *c, const void *data, size_t len);
58 int sha3_final(void *md, sha3_ctx_t *c);	// digest goes to md
59 
60 // compute a sha3 hash (md) of given byte length from "in"
61 void *sha3(const void *in, size_t inlen, void *md, int mdlen);
62 
63 // SHAKE128 and SHAKE256 extensible-output functions
64 #define shake128_init(c) sha3_init(c, 16)
65 #define shake256_init(c) sha3_init(c, 32)
66 #define shake_update sha3_update
67 
68 void shake_xof(sha3_ctx_t *c);
69 void shake_out(sha3_ctx_t *c, void *out, size_t len);
70 
71 #endif
72 
73