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