Blake2b#
BLAKE2b is optimized for 64-bit platforms and produces digests of any size between 1 and 64 bytes. It also has a build-in keying mechanism so that it can be used to replace HMAC-based constructions.
API Reference#
One-Shot#
Available Implementations
#include "Hacl_Hash_Blake2b.h"
#include "Hacl_Hash_Blake2b_Simd256.h"
Example (32)
// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX 64
void
print_hex_ln(size_t bytes_len, uint8_t* bytes)
{
for (int i = 0; i < bytes_len; ++i) {
printf("%02x", bytes[i]);
}
printf("\n");
}
// Reserve memory for a 64 byte digest, i.e.,
// for a BLAKE2b run with full 512-bit output.
uint8_t output[HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX];
// The message we want to hash.
const char* message = "Hello, HACL Packages!";
uint32_t message_len = strlen(message);
// BLAKE2b can be used as an HMAC, i.e., with a key.
// We don't want to use a key here and thus provide a zero-sized key.
uint32_t key_len = 0;
uint8_t* key = 0;
Hacl_Hash_Blake2b_hash_with_key(
output, HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX,
(uint8_t*)message, message_len,
key, key_len);
print_hex_ln(HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX, output);
-
void Hacl_Hash_Blake2b_hash_with_key(uint8_t *output, uint32_t output_len, uint8_t *input, uint32_t input_len, uint8_t *key, uint32_t key_len)#
Write the BLAKE2b digest of message
input
using keykey
intooutput
.- Parameters:
output – Pointer to
output_len
bytes of memory where the digest is written to.output_len – Length of the to-be-generated digest with 1 <=
output_len
<= 64.input – Pointer to
input_len
bytes of memory where the input message is read from.input_len – Length of the input message.
key – Pointer to
key_len
bytes of memory where the key is read from.key_len – Length of the key. Can be 0.
-
void Hacl_Hash_Blake2b_Simd256_hash_with_key(uint8_t *output, uint32_t output_len, uint8_t *input, uint32_t input_len, uint8_t *key, uint32_t key_len)#
Write the BLAKE2b digest of message
input
using keykey
intooutput
.- Parameters:
output – Pointer to
output_len
bytes of memory where the digest is written to.output_len – Length of the to-be-generated digest with 1 <=
output_len
<= 64.input – Pointer to
input_len
bytes of memory where the input message is read from.input_len – Length of the input message.
key – Pointer to
key_len
bytes of memory where the key is read from.key_len – Length of the key. Can be 0.
Streaming (without key)#
Available Implementations
#include "Hacl_Hash_Blake2b.h"
#include "Hacl_Hash_Blake2b_Simd256.h"
Example (32)
// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX 64
void
print_hex_ln(size_t bytes_len, uint8_t* bytes)
{
for (int i = 0; i < bytes_len; ++i) {
printf("%02x", bytes[i]);
}
printf("\n");
}
// This example shows how to hash the byte sequence "Hello, World!" in two
// chunks. As a bonus, it also shows how to obtain intermediate results by
// calling `finish` more than once.
const char* chunk_1 = "Hello, ";
const char* chunk_2 = "World!";
uint32_t chunk_1_size = strlen(chunk_1);
uint32_t chunk_2_size = strlen(chunk_2);
uint8_t digest_1[HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX];
uint8_t digest_2[HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX];
// Init
Hacl_Hash_Blake2b_state_t* state = Hacl_Hash_Blake2b_malloc();
Hacl_Hash_Blake2b_reset(state);
// 1/2 Include `Hello, ` into the hash calculation and
// obtain the intermediate hash of "Hello, ".
Hacl_Hash_Blake2b_update(state, (uint8_t*)chunk_1, chunk_1_size);
// This is optional when no intermediate results are required.
Hacl_Hash_Blake2b_digest(state, digest_1);
// 2/2 Include `World!` into the hash calculation and
// obtain the final hash of "Hello, World!".
Hacl_Hash_Blake2b_update(state, (uint8_t*)chunk_2, chunk_2_size);
Hacl_Hash_Blake2b_digest(state, digest_2);
// Cleanup
Hacl_Hash_Blake2b_free(state);
print_hex_ln(HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX, digest_1);
print_hex_ln(HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX, digest_2);
-
typedef struct Hacl_Hash_Blake2b_state_t_s Hacl_Hash_Blake2b_state_t#
-
Hacl_Hash_Blake2b_state_t *Hacl_Hash_Blake2b_malloc(void)#
Specialized allocation function that picks default values for all parameters, and has no key. Effectively, this is what you want if you intend to use Blake2 as a hash function. Further resettings of the state SHALL be done with
reset
.
-
Hacl_Streaming_Types_error_code Hacl_Hash_Blake2b_update(Hacl_Hash_Blake2b_state_t *state, uint8_t *chunk, uint32_t chunk_len)#
Update function; 0 = success, 1 = max length exceeded
-
void Hacl_Hash_Blake2b_digest(Hacl_Hash_Blake2b_state_t *state, uint8_t *output)#
Digest function. This function expects the
output
array to hold at leastdigest_length
bytes, wheredigest_length
was determined by your choice ofmalloc
function. Concretely, if you usedmalloc
ormalloc_with_key
, then the expected length is 32 for S, or 64 for B (default digest length). If you usedmalloc_with_params_and_key
, then the expected length is whatever you chose for thedigest_length
field of your parameters.
-
void Hacl_Hash_Blake2b_reset(Hacl_Hash_Blake2b_state_t *s)#
Specialized-purpose re-initialization function with no parameters and no key. This is what you want if you intend to use Blake2 as a hash function. The key length and digest length must have been set to their respective default values via your choice of malloc function (always true if you used
malloc
). All other parameters are reset to their default values. The behavior is unspecified if you violate this precondition.
-
void Hacl_Hash_Blake2b_free(Hacl_Hash_Blake2b_state_t *state)#
Free state function when there is no key
-
typedef struct Hacl_Hash_Blake2b_Simd256_state_t_s Hacl_Hash_Blake2b_Simd256_state_t#
-
Hacl_Hash_Blake2b_Simd256_state_t *Hacl_Hash_Blake2b_Simd256_malloc(void)#
State allocation function when there is no key
-
Hacl_Streaming_Types_error_code Hacl_Hash_Blake2b_Simd256_update(Hacl_Hash_Blake2b_Simd256_state_t *state, uint8_t *chunk, uint32_t chunk_len)#
Update function when there is no key; 0 = success, 1 = max length exceeded
-
void Hacl_Hash_Blake2b_Simd256_digest(Hacl_Hash_Blake2b_Simd256_state_t *state, uint8_t *output)#
Finish function when there is no key
-
void Hacl_Hash_Blake2b_Simd256_reset(Hacl_Hash_Blake2b_Simd256_state_t *s)#
Re-initialization function when there is no key
-
void Hacl_Hash_Blake2b_Simd256_free(Hacl_Hash_Blake2b_Simd256_state_t *state)#
Free state function when there is no key