| 1 |
The official C implementation of BLAKE3. |
| 2 |
|
| 3 |
# Example |
| 4 |
|
| 5 |
An example program that hashes bytes from standard input and prints the |
| 6 |
result: |
| 7 |
|
| 8 |
```c |
| 9 |
#include "blake3.h" |
| 10 |
#include <errno.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <unistd.h> |
| 14 |
|
| 15 |
int main(void) { |
| 16 |
// Initialize the hasher. |
| 17 |
blake3_hasher hasher; |
| 18 |
blake3_hasher_init(&hasher); |
| 19 |
|
| 20 |
// Read input bytes from stdin. |
| 21 |
unsigned char buf[65536]; |
| 22 |
while (1) { |
| 23 |
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf)); |
| 24 |
if (n > 0) { |
| 25 |
blake3_hasher_update(&hasher, buf, n); |
| 26 |
} else if (n == 0) { |
| 27 |
break; // end of file |
| 28 |
} else { |
| 29 |
fprintf(stderr, "read failed: %s\n", strerror(errno)); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Finalize the hash. BLAKE3_OUT_LEN is the default output length, 32 bytes. |
| 35 |
uint8_t output[BLAKE3_OUT_LEN]; |
| 36 |
blake3_hasher_finalize(&hasher, output, BLAKE3_OUT_LEN); |
| 37 |
|
| 38 |
// Print the hash as hexadecimal. |
| 39 |
for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) { |
| 40 |
printf("%02x", output[i]); |
| 41 |
} |
| 42 |
printf("\n"); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
``` |
| 46 |
|
| 47 |
The code above is included in this directory as `example.c`. If you're |
| 48 |
on x86\_64 with a Unix-like OS, you can compile a working binary like |
| 49 |
this: |
| 50 |
|
| 51 |
```bash |
| 52 |
gcc -O3 -o example example.c blake3.c blake3_dispatch.c blake3_portable.c \ |
| 53 |
blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \ |
| 54 |
blake3_avx512_x86-64_unix.S |
| 55 |
``` |
| 56 |
|
| 57 |
# API |
| 58 |
|
| 59 |
## The Struct |
| 60 |
|
| 61 |
```c |
| 62 |
typedef struct { |
| 63 |
// private fields |
| 64 |
} blake3_hasher; |
| 65 |
``` |
| 66 |
|
| 67 |
An incremental BLAKE3 hashing state, which can accept any number of |
| 68 |
updates. This implementation doesn't allocate any heap memory, but |
| 69 |
`sizeof(blake3_hasher)` itself is relatively large, currently 1912 bytes |
| 70 |
on x86-64. This size can be reduced by restricting the maximum input |
| 71 |
length, as described in Section 5.4 of [the BLAKE3 |
| 72 |
spec](https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf), |
| 73 |
but this implementation doesn't currently support that strategy. |
| 74 |
|
| 75 |
## Common API Functions |
| 76 |
|
| 77 |
```c |
| 78 |
void blake3_hasher_init( |
| 79 |
blake3_hasher *self); |
| 80 |
``` |
| 81 |
|
| 82 |
Initialize a `blake3_hasher` in the default hashing mode. |
| 83 |
|
| 84 |
--- |
| 85 |
|
| 86 |
```c |
| 87 |
void blake3_hasher_update( |
| 88 |
blake3_hasher *self, |
| 89 |
const void *input, |
| 90 |
size_t input_len); |
| 91 |
``` |
| 92 |
|
| 93 |
Add input to the hasher. This can be called any number of times. This function |
| 94 |
is always single-threaded; for multithreading see `blake3_hasher_update_tbb` |
| 95 |
below. |
| 96 |
|
| 97 |
|
| 98 |
--- |
| 99 |
|
| 100 |
```c |
| 101 |
void blake3_hasher_finalize( |
| 102 |
const blake3_hasher *self, |
| 103 |
uint8_t *out, |
| 104 |
size_t out_len); |
| 105 |
``` |
| 106 |
|
| 107 |
Finalize the hasher and return an output of any length, given in bytes. |
| 108 |
This doesn't modify the hasher itself, and it's possible to finalize |
| 109 |
again after adding more input. The constant `BLAKE3_OUT_LEN` provides |
| 110 |
the default output length, 32 bytes, which is recommended for most |
| 111 |
callers. See the [Security Notes](#security-notes) below. |
| 112 |
|
| 113 |
## Less Common API Functions |
| 114 |
|
| 115 |
```c |
| 116 |
void blake3_hasher_init_keyed( |
| 117 |
blake3_hasher *self, |
| 118 |
const uint8_t key[BLAKE3_KEY_LEN]); |
| 119 |
``` |
| 120 |
|
| 121 |
Initialize a `blake3_hasher` in the keyed hashing mode. The key must be |
| 122 |
exactly 32 bytes. |
| 123 |
|
| 124 |
--- |
| 125 |
|
| 126 |
```c |
| 127 |
void blake3_hasher_init_derive_key( |
| 128 |
blake3_hasher *self, |
| 129 |
const char *context); |
| 130 |
``` |
| 131 |
|
| 132 |
Initialize a `blake3_hasher` in the key derivation mode. The context |
| 133 |
string is given as an initialization parameter, and afterwards input key |
| 134 |
material should be given with `blake3_hasher_update`. The context string |
| 135 |
is a null-terminated C string which should be **hardcoded, globally |
| 136 |
unique, and application-specific**. The context string should not |
| 137 |
include any dynamic input like salts, nonces, or identifiers read from a |
| 138 |
database at runtime. A good default format for the context string is |
| 139 |
`"[application] [commit timestamp] [purpose]"`, e.g., `"example.com |
| 140 |
2019-12-25 16:18:03 session tokens v1"`. |
| 141 |
|
| 142 |
This function is intended for application code written in C. For |
| 143 |
language bindings, see `blake3_hasher_init_derive_key_raw` below. |
| 144 |
|
| 145 |
--- |
| 146 |
|
| 147 |
```c |
| 148 |
void blake3_hasher_init_derive_key_raw( |
| 149 |
blake3_hasher *self, |
| 150 |
const void *context, |
| 151 |
size_t context_len); |
| 152 |
``` |
| 153 |
|
| 154 |
As `blake3_hasher_init_derive_key` above, except that the context string |
| 155 |
is given as a pointer to an array of arbitrary bytes with a provided |
| 156 |
length. This is intended for writing language bindings, where C string |
| 157 |
conversion would add unnecessary overhead and new error cases. Unicode |
| 158 |
strings should be encoded as UTF-8. |
| 159 |
|
| 160 |
Application code in C should prefer `blake3_hasher_init_derive_key`, |
| 161 |
which takes the context as a C string. If you need to use arbitrary |
| 162 |
bytes as a context string in application code, consider whether you're |
| 163 |
violating the requirement that context strings should be hardcoded. |
| 164 |
|
| 165 |
--- |
| 166 |
|
| 167 |
```c |
| 168 |
void blake3_hasher_update_tbb( |
| 169 |
blake3_hasher *self, |
| 170 |
const void *input, |
| 171 |
size_t input_len); |
| 172 |
``` |
| 173 |
|
| 174 |
Add input to the hasher, using [oneTBB] to process large inputs using multiple |
| 175 |
threads. This can be called any number of times. This gives the same result as |
| 176 |
`blake3_hasher_update` above. |
| 177 |
|
| 178 |
[oneTBB]: https://uxlfoundation.github.io/oneTBB/ |
| 179 |
|
| 180 |
NOTE: This function is only enabled when the library is compiled with CMake option `BLAKE3_USE_TBB` |
| 181 |
and when the oneTBB library is detected on the host system. See the building instructions for |
| 182 |
further details. |
| 183 |
|
| 184 |
To get any performance benefit from multithreading, the input buffer needs to |
| 185 |
be large. As a rule of thumb on x86_64, `blake3_hasher_update_tbb` is _slower_ |
| 186 |
than `blake3_hasher_update` for inputs under 128 KiB. That threshold varies |
| 187 |
quite a lot across different processors, and it's important to benchmark your |
| 188 |
specific use case. |
| 189 |
|
| 190 |
Hashing large files with this function usually requires |
| 191 |
[memory-mapping](https://en.wikipedia.org/wiki/Memory-mapped_file), since |
| 192 |
reading a file into memory in a single-threaded loop takes longer than hashing |
| 193 |
the resulting buffer. Note that hashing a memory-mapped file with this function |
| 194 |
produces a "random" pattern of disk reads, which can be slow on spinning disks. |
| 195 |
Again it's important to benchmark your specific use case. |
| 196 |
|
| 197 |
This implementation doesn't require configuration of thread resources and will |
| 198 |
use as many cores as possible by default. More fine-grained control of |
| 199 |
resources is possible using the [oneTBB] API. |
| 200 |
|
| 201 |
--- |
| 202 |
|
| 203 |
```c |
| 204 |
void blake3_hasher_finalize_seek( |
| 205 |
const blake3_hasher *self, |
| 206 |
uint64_t seek, |
| 207 |
uint8_t *out, |
| 208 |
size_t out_len); |
| 209 |
``` |
| 210 |
|
| 211 |
The same as `blake3_hasher_finalize`, but with an additional `seek` |
| 212 |
parameter for the starting byte position in the output stream. To |
| 213 |
efficiently stream a large output without allocating memory, call this |
| 214 |
function in a loop, incrementing `seek` by the output length each time. |
| 215 |
|
| 216 |
--- |
| 217 |
|
| 218 |
```c |
| 219 |
void blake3_hasher_reset( |
| 220 |
blake3_hasher *self); |
| 221 |
``` |
| 222 |
|
| 223 |
Reset the hasher to its initial state, prior to any calls to |
| 224 |
`blake3_hasher_update`. Currently this is no different from calling |
| 225 |
`blake3_hasher_init` or similar again. |
| 226 |
|
| 227 |
# Security Notes |
| 228 |
|
| 229 |
Outputs shorter than the default length of 32 bytes (256 bits) provide less security. An N-bit |
| 230 |
BLAKE3 output is intended to provide N bits of first and second preimage resistance and N/2 |
| 231 |
bits of collision resistance, for any N up to 256. Longer outputs don't provide any additional |
| 232 |
security. |
| 233 |
|
| 234 |
Avoid relying on the secrecy of the output offset, that is, the `seek` argument of |
| 235 |
`blake3_hasher_finalize_seek`. [_Block-Cipher-Based Tree Hashing_ by Aldo |
| 236 |
Gunsing](https://eprint.iacr.org/2022/283) shows that an attacker who knows both the message |
| 237 |
and the key (if any) can easily determine the offset of an extended output. For comparison, |
| 238 |
AES-CTR has a similar property: if you know the key, you can decrypt a block from an unknown |
| 239 |
position in the output stream to recover its block index. Callers with strong secret keys |
| 240 |
aren't affected in practice, but secret offsets are a [design |
| 241 |
smell](https://en.wikipedia.org/wiki/Design_smell) in any case. |
| 242 |
|
| 243 |
# Building |
| 244 |
|
| 245 |
The easiest and most complete method of compiling this library is with CMake. |
| 246 |
This is the method described in the next section. Toward the end of the |
| 247 |
building section there are more in depth notes about compiling manually and |
| 248 |
things that are useful to understand if you need to integrate this library with |
| 249 |
another build system. |
| 250 |
|
| 251 |
## CMake |
| 252 |
|
| 253 |
The minimum version of CMake is 3.9. The following invocations will compile and |
| 254 |
install `libblake3`. With recent CMake: |
| 255 |
|
| 256 |
```bash |
| 257 |
cmake -S c -B c/build "-DCMAKE_INSTALL_PREFIX=/usr/local" |
| 258 |
cmake --build c/build --target install |
| 259 |
``` |
| 260 |
|
| 261 |
With an older CMake: |
| 262 |
|
| 263 |
```bash |
| 264 |
cd c |
| 265 |
mkdir build |
| 266 |
cd build |
| 267 |
cmake .. "-DCMAKE_INSTALL_PREFIX=/usr/local" |
| 268 |
cmake --build . --target install |
| 269 |
``` |
| 270 |
|
| 271 |
The following options are available when compiling with CMake: |
| 272 |
|
| 273 |
- `BLAKE3_USE_TBB`: Enable oneTBB parallelism (Requires a C++20 capable compiler) |
| 274 |
- `BLAKE3_FETCH_TBB`: Allow fetching oneTBB from GitHub (only if not found on system) |
| 275 |
- `BLAKE3_EXAMPLES`: Compile and install example programs |
| 276 |
|
| 277 |
Options can be enabled like this: |
| 278 |
|
| 279 |
```bash |
| 280 |
cmake -S c -B c/build "-DCMAKE_INSTALL_PREFIX=/usr/local" -DBLAKE3_USE_TBB=1 -DBLAKE3_FETCH_TBB=1 |
| 281 |
``` |
| 282 |
|
| 283 |
## Building manually |
| 284 |
|
| 285 |
We try to keep the build simple enough that you can compile this library "by |
| 286 |
hand", and it's expected that many callers will integrate it with their |
| 287 |
pre-existing build systems. See the `gcc` one-liner in the "Example" section |
| 288 |
above. |
| 289 |
|
| 290 |
### x86 |
| 291 |
|
| 292 |
Dynamic dispatch is enabled by default on x86. The implementation will |
| 293 |
query the CPU at runtime to detect SIMD support, and it will use the |
| 294 |
widest instruction set available. By default, `blake3_dispatch.c` |
| 295 |
expects to be linked with code for five different instruction sets: |
| 296 |
portable C, SSE2, SSE4.1, AVX2, and AVX-512. |
| 297 |
|
| 298 |
For each of the x86 SIMD instruction sets, four versions are available: |
| 299 |
three flavors of assembly (Unix, Windows MSVC, and Windows GNU) and one |
| 300 |
version using C intrinsics. The assembly versions are generally |
| 301 |
preferred. They perform better, they perform more consistently across |
| 302 |
different compilers, and they build more quickly. On the other hand, the |
| 303 |
assembly versions are x86\_64-only, and you need to select the right |
| 304 |
flavor for your target platform. |
| 305 |
|
| 306 |
Here's an example of building a shared library on x86\_64 Linux using |
| 307 |
the assembly implementations: |
| 308 |
|
| 309 |
```bash |
| 310 |
gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \ |
| 311 |
blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S blake3_avx2_x86-64_unix.S \ |
| 312 |
blake3_avx512_x86-64_unix.S |
| 313 |
``` |
| 314 |
|
| 315 |
When building the intrinsics-based implementations, you need to build |
| 316 |
each implementation separately, with the corresponding instruction set |
| 317 |
explicitly enabled in the compiler. Here's the same shared library using |
| 318 |
the intrinsics-based implementations: |
| 319 |
|
| 320 |
```bash |
| 321 |
gcc -c -fPIC -O3 -msse2 blake3_sse2.c -o blake3_sse2.o |
| 322 |
gcc -c -fPIC -O3 -msse4.1 blake3_sse41.c -o blake3_sse41.o |
| 323 |
gcc -c -fPIC -O3 -mavx2 blake3_avx2.c -o blake3_avx2.o |
| 324 |
gcc -c -fPIC -O3 -mavx512f -mavx512vl blake3_avx512.c -o blake3_avx512.o |
| 325 |
gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c \ |
| 326 |
blake3_avx2.o blake3_avx512.o blake3_sse41.o blake3_sse2.o |
| 327 |
``` |
| 328 |
|
| 329 |
Note above that building `blake3_avx512.c` requires both `-mavx512f` and |
| 330 |
`-mavx512vl` under GCC and Clang. Under MSVC, the single `/arch:AVX512` |
| 331 |
flag is sufficient. The MSVC equivalent of `-mavx2` is `/arch:AVX2`. |
| 332 |
MSVC enables SSE2 and SSE4.1 by default, and it doesn't have a |
| 333 |
corresponding flag. |
| 334 |
|
| 335 |
If you want to omit SIMD code entirely, you need to explicitly disable |
| 336 |
each instruction set. Here's an example of building a shared library on |
| 337 |
x86 with only portable code: |
| 338 |
|
| 339 |
```bash |
| 340 |
gcc -shared -O3 -o libblake3.so -DBLAKE3_NO_SSE2 -DBLAKE3_NO_SSE41 -DBLAKE3_NO_AVX2 \ |
| 341 |
-DBLAKE3_NO_AVX512 blake3.c blake3_dispatch.c blake3_portable.c |
| 342 |
``` |
| 343 |
|
| 344 |
### ARM NEON |
| 345 |
|
| 346 |
The NEON implementation is enabled by default on AArch64, but not on |
| 347 |
other ARM targets, since not all of them support it. To enable it, set |
| 348 |
`BLAKE3_USE_NEON=1`. Here's an example of building a shared library on |
| 349 |
ARM Linux with NEON support: |
| 350 |
|
| 351 |
```bash |
| 352 |
gcc -shared -O3 -o libblake3.so -DBLAKE3_USE_NEON=1 blake3.c blake3_dispatch.c \ |
| 353 |
blake3_portable.c blake3_neon.c |
| 354 |
``` |
| 355 |
|
| 356 |
To explicitiy disable using NEON instructions on AArch64, set |
| 357 |
`BLAKE3_USE_NEON=0`. |
| 358 |
|
| 359 |
```bash |
| 360 |
gcc -shared -O3 -o libblake3.so -DBLAKE3_USE_NEON=0 blake3.c blake3_dispatch.c \ |
| 361 |
blake3_portable.c |
| 362 |
``` |
| 363 |
|
| 364 |
Note that on some targets (ARMv7 in particular), extra flags may be |
| 365 |
required to activate NEON support in the compiler. If you see an error |
| 366 |
like... |
| 367 |
|
| 368 |
``` |
| 369 |
/usr/lib/gcc/armv7l-unknown-linux-gnueabihf/9.2.0/include/arm_neon.h:635:1: error: inlining failed |
| 370 |
in call to always_inline ‘vaddq_u32’: target specific option mismatch |
| 371 |
``` |
| 372 |
|
| 373 |
...then you may need to add something like `-mfpu=neon-vfpv4 |
| 374 |
-mfloat-abi=hard`. |
| 375 |
|
| 376 |
### Other Platforms |
| 377 |
|
| 378 |
The portable implementation should work on most other architectures. For |
| 379 |
example: |
| 380 |
|
| 381 |
```bash |
| 382 |
gcc -shared -O3 -o libblake3.so blake3.c blake3_dispatch.c blake3_portable.c |
| 383 |
``` |
| 384 |
|
| 385 |
### Multithreading |
| 386 |
|
| 387 |
Multithreading is available using [oneTBB], by compiling the optional C++ |
| 388 |
support file [`blake3_tbb.cpp`](./blake3_tbb.cpp). For an example of using |
| 389 |
`mmap` (non-Windows) and `blake3_hasher_update_tbb` to get large-file |
| 390 |
performance on par with [`b3sum`](../b3sum), see |
| 391 |
[`example_tbb.c`](./example_tbb.c). You can build it like this: |
| 392 |
|
| 393 |
```bash |
| 394 |
g++ -c -O3 -fno-exceptions -fno-rtti -DBLAKE3_USE_TBB -o blake3_tbb.o blake3_tbb.cpp |
| 395 |
gcc -O3 -o example_tbb -lstdc++ -ltbb -DBLAKE3_USE_TBB blake3_tbb.o example_tbb.c blake3.c \ |
| 396 |
blake3_dispatch.c blake3_portable.c blake3_sse2_x86-64_unix.S blake3_sse41_x86-64_unix.S \ |
| 397 |
blake3_avx2_x86-64_unix.S blake3_avx512_x86-64_unix.S |
| 398 |
``` |
| 399 |
|
| 400 |
NOTE: `-fno-exceptions` or equivalent is required to compile `blake3_tbb.cpp`, |
| 401 |
and public API methods with external C linkage are marked `noexcept`. Compiling |
| 402 |
that file with exceptions enabled will fail. Compiling with RTTI disabled isn't |
| 403 |
required but is recommended for code size. |