blake3.h 3.6 KB

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