Blake2s#

BLAKE2s is optimized for 8- to 32-bit platforms and produces digests of any size between 1 and 32 bytes.

API Reference#

One-Shot#

Available Implementations

#include "Hacl_Hash_Blake2s.h"

Example (32)

// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX 32
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 32 byte digest, i.e.,
// for a BLAKE2s run with full 256-bit output.
uint8_t output[HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX];

// The message we want to hash.
const char* message = "Hello, HACL Packages!";
uint32_t message_len = strlen(message);

// BLAKE2s 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_Blake2s_hash_with_key(
  output, HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX,
  (uint8_t*)message, message_len,
  key, key_len);

print_hex_ln(HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX, output);
void Hacl_Hash_Blake2s_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 BLAKE2s digest of message input using key key into output.

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 <= 32.

  • 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_Blake2.h"

Example (32)

// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX 32
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_BLAKE2S_DIGEST_LENGTH_MAX];
uint8_t digest_2[HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX];

// Init
Hacl_Hash_Blake2s_state_t* state = Hacl_Hash_Blake2s_malloc();
Hacl_Hash_Blake2s_reset(state);

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

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

// Cleanup
Hacl_Hash_Blake2s_free(state);

print_hex_ln(HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX, digest_1);
print_hex_ln(HACL_HASH_BLAKE2S_DIGEST_LENGTH_MAX, digest_2);
typedef struct Hacl_Hash_Blake2s_state_t_s Hacl_Hash_Blake2s_state_t#
Hacl_Hash_Blake2s_state_t *Hacl_Hash_Blake2s_malloc(void)#

State allocation function when there is no key

Hacl_Streaming_Types_error_code Hacl_Hash_Blake2s_update(Hacl_Hash_Blake2s_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_Blake2s_digest(Hacl_Hash_Blake2s_state_t *state, uint8_t *output)#

Finish function when there is no key

void Hacl_Hash_Blake2s_reset(Hacl_Hash_Blake2s_state_t *state)#

Re-initialization function when there is no key

void Hacl_Hash_Blake2s_free(Hacl_Hash_Blake2s_state_t *state)#

Free state function when there is no key