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_Blake2.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_Blake2b_32_blake2b(HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX,
                        output,
                        message_len,
                        (uint8_t*)message,
                        key_len,
                        key);

print_hex_ln(HACL_HASH_BLAKE2B_DIGEST_LENGTH_MAX, output);
void Hacl_Blake2b_32_blake2b(uint32_t nn, uint8_t *output, uint32_t ll, uint8_t *d, uint32_t kk, uint8_t *k)#

Write the BLAKE2b digest of message d using key k into output.

Parameters:
  • nn – Length of the to-be-generated digest with 1 <= nn <= 64.

  • output – Pointer to nn bytes of memory where the digest is written to.

  • ll – Length of the input message.

  • d – Pointer to ll bytes of memory where the input message is read from.

  • kk – Length of the key. Can be 0.

  • k – Pointer to kk bytes of memory where the key is read from.

Streaming (without key)#

Available Implementations

#include "Hacl_Streaming_Blake2.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_Streaming_Blake2_blake2b_32_state_s* state =
  Hacl_Streaming_Blake2_blake2b_32_no_key_create_in();
Hacl_Streaming_Blake2_blake2b_32_no_key_init(state);

// 1/2 Include `Hello, ` into the hash calculation and
// obtain the intermediate hash of "Hello, ".
Hacl_Streaming_Blake2_blake2b_32_no_key_update(
  state, (uint8_t*)chunk_1, chunk_1_size);
// This is optional when no intermediate results are required.
Hacl_Streaming_Blake2_blake2b_32_no_key_finish(state, digest_1);

// 2/2 Include `World!` into the hash calculation and
// obtain the final hash of "Hello, World!".
Hacl_Streaming_Blake2_blake2b_32_no_key_update(
  state, (uint8_t*)chunk_2, chunk_2_size);
Hacl_Streaming_Blake2_blake2b_32_no_key_finish(state, digest_2);

// Cleanup
Hacl_Streaming_Blake2_blake2b_32_no_key_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_Streaming_Blake2_blake2b_32_state_s Hacl_Streaming_Blake2_blake2b_32_state#
Hacl_Streaming_Blake2_blake2b_32_state *Hacl_Streaming_Blake2_blake2b_32_no_key_create_in(void)#

State allocation function when there is no key

void Hacl_Streaming_Blake2_blake2b_32_no_key_init(Hacl_Streaming_Blake2_blake2b_32_state *s1)#

(Re)-initialization function when there is no key

Hacl_Streaming_Types_error_code Hacl_Streaming_Blake2_blake2b_32_no_key_update(Hacl_Streaming_Blake2_blake2b_32_state *p, uint8_t *data, uint32_t len)#

Update function when there is no key; 0 = success, 1 = max length exceeded

void Hacl_Streaming_Blake2_blake2b_32_no_key_finish(Hacl_Streaming_Blake2_blake2b_32_state *p, uint8_t *dst)#

Finish function when there is no key

void Hacl_Streaming_Blake2_blake2b_32_no_key_free(Hacl_Streaming_Blake2_blake2b_32_state *s1)#

Free state function when there is no key