1 /* blake3 license:
2  * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020.
3  * This work is released into the public domain with CC0 1.0.
4  * Alternatively, it is licensed under the Apache License 2.0.
5  * For full license see
6  * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE
7  */
8 
9 #ifndef BLAKE3_H
10 #define BLAKE3_H
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #define BLAKE3_NO_AVX512
16 #define BLAKE3_NO_SSE41
17 #define BLAKE3_NO_AVX2
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define BLAKE3_KEY_LEN 32
24 #define BLAKE3_OUT_LEN 32
25 #define BLAKE3_BLOCK_LEN 64
26 #define BLAKE3_CHUNK_LEN 1024
27 #define BLAKE3_MAX_DEPTH 54
28 #define BLAKE3_MAX_SIMD_DEGREE 16
29 
30 // This struct is a private implementation detail. It has to be here because
31 // it's part of blake3_hasher below.
32 typedef struct {
33   uint32_t cv[8];
34   uint64_t chunk_counter;
35   uint8_t buf[BLAKE3_BLOCK_LEN];
36   uint8_t buf_len;
37   uint8_t blocks_compressed;
38   uint8_t flags;
39 } blake3_chunk_state;
40 
41 typedef struct {
42   uint32_t key[8];
43   blake3_chunk_state chunk;
44   uint8_t cv_stack_len;
45   // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example,
46   // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk
47   // requires a 4th entry, rather than merging everything down to 1, because we
48   // don't know whether more input is coming. This is different from how the
49   // reference implementation does things.
50   uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN];
51 } blake3_hasher;
52 
53 void blake3_hasher_init(blake3_hasher *self);
54 void blake3_hasher_init_keyed(blake3_hasher *self,
55                               const uint8_t key[BLAKE3_KEY_LEN]);
56 void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context);
57 void blake3_hasher_update(blake3_hasher *self, const void *input,
58                           size_t input_len);
59 void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out,
60                             size_t out_len);
61 void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek,
62                                  uint8_t *out, size_t out_len);
63 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 #endif /* BLAKE3_H */
69