xref: /freebsd/sys/contrib/openzfs/include/sys/blake3.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Based on BLAKE3 v1.3.1, https://github.com/BLAKE3-team/BLAKE3
25  * Copyright (c) 2019-2020 Samuel Neves and Jack O'Connor
26  * Copyright (c) 2021-2022 Tino Reichardt <milky-zfs@mcmilk.de>
27  */
28 
29 #ifndef	_SYS_BLAKE3_H
30 #define	_SYS_BLAKE3_H
31 
32 #ifdef  _KERNEL
33 #include <sys/types.h>
34 #else
35 #include <stdint.h>
36 #include <stdlib.h>
37 #endif
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #define	BLAKE3_KEY_LEN		32
44 #define	BLAKE3_OUT_LEN		32
45 #define	BLAKE3_MAX_DEPTH	54
46 #define	BLAKE3_BLOCK_LEN	64
47 #define	BLAKE3_CHUNK_LEN	1024
48 
49 /*
50  * This struct is a private implementation detail.
51  * It has to be here because it's part of BLAKE3_CTX below.
52  */
53 typedef struct {
54 	uint32_t cv[8];
55 	uint64_t chunk_counter;
56 	uint8_t buf[BLAKE3_BLOCK_LEN];
57 	uint8_t buf_len;
58 	uint8_t blocks_compressed;
59 	uint8_t flags;
60 } blake3_chunk_state_t;
61 
62 typedef struct {
63 	uint32_t key[8];
64 	blake3_chunk_state_t chunk;
65 	uint8_t cv_stack_len;
66 
67 	/*
68 	 * The stack size is MAX_DEPTH + 1 because we do lazy merging. For
69 	 * example, with 7 chunks, we have 3 entries in the stack. Adding an
70 	 * 8th chunk requires a 4th entry, rather than merging everything down
71 	 * to 1, because we don't know whether more input is coming. This is
72 	 * different from how the reference implementation does things.
73 	 */
74 	uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];
75 
76 	/* const blake3_ops_t *ops */
77 	const void *ops;
78 } BLAKE3_CTX;
79 
80 /* init the context for hash operation */
81 void Blake3_Init(BLAKE3_CTX *ctx);
82 
83 /* init the context for a MAC and/or tree hash operation */
84 void Blake3_InitKeyed(BLAKE3_CTX *ctx, const uint8_t key[BLAKE3_KEY_LEN]);
85 
86 /* process the input bytes */
87 void Blake3_Update(BLAKE3_CTX *ctx, const void *input, size_t input_len);
88 
89 /* finalize the hash computation and output the result */
90 void Blake3_Final(const BLAKE3_CTX *ctx, uint8_t *out);
91 
92 /* finalize the hash computation and output the result */
93 void Blake3_FinalSeek(const BLAKE3_CTX *ctx, uint64_t seek, uint8_t *out,
94     size_t out_len);
95 
96 /* these are pre-allocated contexts */
97 extern void **blake3_per_cpu_ctx;
98 extern void blake3_per_cpu_ctx_init(void);
99 extern void blake3_per_cpu_ctx_fini(void);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif	/* _SYS_BLAKE3_H */
106