1/*===-- llvm-c/blake3.h - BLAKE3 C Interface ----------------------*- C -*-===*\
2|* *|
3|* Released into the public domain with CC0 1.0 *|
4|* See 'llvm/lib/Support/BLAKE3/LICENSE' for info. *|
5|* SPDX-License-Identifier: CC0-1.0 *|
6|* *|
7|*===----------------------------------------------------------------------===*|
8|* *|
9|* This header declares the C interface to LLVM's BLAKE3 implementation. *|
10|* Original BLAKE3 C API: https://github.com/BLAKE3-team/BLAKE3/tree/1.3.1/c *|
11|* *|
12|* Symbols are prefixed with 'llvm' to avoid a potential conflict with *|
13|* another BLAKE3 version within the same program. *|
14|* *|
15\*===----------------------------------------------------------------------===*/
16
17#ifndef LLVM_C_BLAKE3_H
18#define LLVM_C_BLAKE3_H
19
20#include "llvm-c/Visibility.h"
21#include <stddef.h>
22#include <stdint.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#define LLVM_BLAKE3_VERSION_STRING "1.8.2"
29#define LLVM_BLAKE3_KEY_LEN 32
30#define LLVM_BLAKE3_OUT_LEN 32
31#define LLVM_BLAKE3_BLOCK_LEN 64
32#define LLVM_BLAKE3_CHUNK_LEN 1024
33#define LLVM_BLAKE3_MAX_DEPTH 54
34
35// This struct is a private implementation detail. It has to be here because
36// it's part of llvm_blake3_hasher below.
37typedef struct {
38 uint32_t cv[8];
39 uint64_t chunk_counter;
40 uint8_t buf[LLVM_BLAKE3_BLOCK_LEN];
41 uint8_t buf_len;
42 uint8_t blocks_compressed;
43 uint8_t flags;
44} llvm_blake3_chunk_state;
45
46typedef struct {
47 uint32_t key[8];
48 llvm_blake3_chunk_state chunk;
49 uint8_t cv_stack_len;
50 // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example,
51 // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk
52 // requires a 4th entry, rather than merging everything down to 1, because we
53 // don't know whether more input is coming. This is different from how the
54 // reference implementation does things.
55 uint8_t cv_stack[(LLVM_BLAKE3_MAX_DEPTH + 1) * LLVM_BLAKE3_OUT_LEN];
56} llvm_blake3_hasher;
57
58LLVM_C_ABI const char *llvm_blake3_version(void);
59LLVM_C_ABI void llvm_blake3_hasher_init(llvm_blake3_hasher *self);
60LLVM_C_ABI void
61llvm_blake3_hasher_init_keyed(llvm_blake3_hasher *self,
62 const uint8_t key[LLVM_BLAKE3_KEY_LEN]);
63LLVM_C_ABI void llvm_blake3_hasher_init_derive_key(llvm_blake3_hasher *self,
64 const char *context);
65LLVM_C_ABI void llvm_blake3_hasher_init_derive_key_raw(llvm_blake3_hasher *self,
66 const void *context,
67 size_t context_len);
68LLVM_C_ABI void llvm_blake3_hasher_update(llvm_blake3_hasher *self,
69 const void *input, size_t input_len);
70LLVM_C_ABI void llvm_blake3_hasher_finalize(const llvm_blake3_hasher *self,
71 uint8_t *out, size_t out_len);
72LLVM_C_ABI void llvm_blake3_hasher_finalize_seek(const llvm_blake3_hasher *self,
73 uint64_t seek, uint8_t *out,
74 size_t out_len);
75LLVM_C_ABI void llvm_blake3_hasher_reset(llvm_blake3_hasher *self);
76
77#ifdef __cplusplus
78}
79#endif
80
81#endif /* LLVM_C_BLAKE3_H */
82