83 *state = noise_blake2s_new();
87 *state = noise_blake2b_new();
91 *state = noise_sha256_new();
95 *state = noise_sha512_new();
228 (*(state->
reset))(state);
252 (*(state->
update))(state, data, data_len);
310 uint8_t *hash,
size_t hash_len)
313 if (!state || !data || !hash)
319 (*(state->
reset))(state);
320 (*(state->
update))(state, data, data_len);
353 const uint8_t *data2,
size_t data2_len, uint8_t *hash,
size_t hash_len)
356 if (!state || !data1 || !data2 || !hash)
362 (*(state->
reset))(state);
363 (*(state->
update))(state, data1, data1_len);
364 (*(state->
update))(state, data2, data2_len);
370 #define HMAC_IPAD 0x36
371 #define HMAC_OPAD 0x5C
381 static void noise_hashstate_xor_key(uint8_t *key, size_t key_len, uint8_t value)
383 while (key_len > 0) {
406 static void noise_hashstate_hmac
408 const uint8_t *data1,
size_t data1_len,
409 const uint8_t *data2,
size_t data2_len, uint8_t *hash)
416 key_block = alloca(block_len);
419 if (key_len <= block_len) {
420 memcpy(key_block, key, key_len);
421 memset(key_block + key_len, 0, block_len - key_len);
423 (*(state->
reset))(state);
424 (*(state->
update))(state, key, key_len);
425 (*(state->
finalize))(state, key_block);
426 memset(key_block + hash_len, 0, block_len - hash_len);
428 noise_hashstate_xor_key(key_block, block_len, HMAC_IPAD);
431 (*(state->
reset))(state);
432 (*(state->
update))(state, key_block, block_len);
433 (*(state->
update))(state, data1, data1_len);
435 (*(state->
update))(state, data2, data2_len);
439 noise_hashstate_xor_key(key_block, block_len, HMAC_IPAD ^ HMAC_OPAD);
442 (*(state->
reset))(state);
443 (*(state->
update))(state, key_block, block_len);
444 (*(state->
update))(state, hash, hash_len);
478 const uint8_t *data,
size_t data_len,
479 uint8_t *output1,
size_t output1_len,
480 uint8_t *output2,
size_t output2_len)
487 if (!state || !key || !data || !output1 || !output2)
490 if (output1_len > hash_len || output2_len > hash_len)
494 temp_key = alloca(hash_len);
495 temp_hash = alloca(hash_len + 1);
498 noise_hashstate_hmac(state, key, key_len, data, data_len, 0, 0, temp_key);
503 (state, temp_key, hash_len, temp_hash, 1, 0, 0, temp_hash);
504 memcpy(output1, temp_hash, output1_len);
507 temp_hash[hash_len] = 0x02;
509 (state, temp_key, hash_len, temp_hash, hash_len + 1, 0, 0, temp_hash);
510 memcpy(output2, temp_hash, output2_len);
543 const uint8_t *salt,
size_t salt_len,
size_t iterations,
544 uint8_t *output,
size_t output_len)
551 size_t i, index, index2;
554 if (!state || !passphrase || !salt || !output)
557 max_size = ((uint64_t)0xFFFFFFFFU) * hash_len;
558 if (output_len > max_size)
563 while (output_len > 0) {
565 ibuf[0] = (uint8_t)(i >> 24);
566 ibuf[1] = (uint8_t)(i >> 16);
567 ibuf[2] = (uint8_t)(i >> 8);
568 ibuf[3] = (uint8_t)i;
571 (state, passphrase, passphrase_len, salt, salt_len,
572 ibuf,
sizeof(ibuf), T);
573 memcpy(U, T, hash_len);
574 for (index = 1; index < iterations; ++index) {
576 (state, passphrase, passphrase_len, U, hash_len, 0, 0, U);
577 for (index2 = 0; index2 < hash_len; ++index2)
578 T[index2] ^= U[index2];
582 if (output_len >= hash_len) {
583 memcpy(output, T, hash_len);
585 output_len -= hash_len;
587 memcpy(output, T, output_len);
#define NOISE_ERROR_UNKNOWN_ID
Algorithm identifier is unknown.
void(* reset)(NoiseHashState *state)
Resets the HashState for a new hashing session.
uint16_t hash_len
Length of the output from this hash algorithm.
uint16_t block_len
Length of the underlying block for this hash algorithm.
void noise_clean(void *data, size_t size)
Cleans a block of memory to destroy its contents.
int noise_hashstate_pbkdf2(NoiseHashState *state, const uint8_t *passphrase, size_t passphrase_len, const uint8_t *salt, size_t salt_len, size_t iterations, uint8_t *output, size_t output_len)
Hashes a passphrase and salt using the PBKDF2 key derivation function.
#define NOISE_ERROR_INVALID_PARAM
Invalid parameter to function; e.g. a NULL value.
Internal structure of the NoiseHashState type.
#define NOISE_ERROR_NONE
Success, no error.
int noise_hashstate_get_max_hash_length(void)
Gets the maximum hash length for the supported algorithms.
int noise_hashstate_reset(NoiseHashState *state)
Resets the hash state.
#define NOISE_HASH_BLAKE2b
Hash identifier for "BLAKE2b".
#define NOISE_HASH_CATEGORY
Category for hash algorithms.
int noise_hashstate_new_by_id(NoiseHashState **state, int id)
Creates a new HashState object by its algorithm identifier.
#define NOISE_MAX_HASHLEN
Maximum hash length over all supported hash algorithms.
int noise_hashstate_new_by_name(NoiseHashState **state, const char *name)
Creates a new HashState object by its algorithm name.
int noise_hashstate_hash_one(NoiseHashState *state, const uint8_t *data, size_t data_len, uint8_t *hash, size_t hash_len)
Hashes a single data buffer and returns the hash value.
void(* destroy)(NoiseHashState *state)
Destroys this HashState prior to the memory being freed.
size_t noise_hashstate_get_block_length(const NoiseHashState *state)
Gets the length of the block for a HashState object.
#define NOISE_ERROR_NO_MEMORY
Insufficient memory to complete the operation.
void(* finalize)(NoiseHashState *state, uint8_t *hash)
Finalizes the HashState and returns the hash value.
#define NOISE_ERROR_UNKNOWN_NAME
Algorithm name is unknown.
int noise_hashstate_get_max_block_length(void)
Gets the maximum block length for the supported algorithms.
#define NOISE_ERROR_INVALID_LENGTH
Invalid length specified for a key, packet, etc.
#define NOISE_HASH_SHA256
Hash identifier for "SHA256".
void(* update)(NoiseHashState *state, const uint8_t *data, size_t len)
Updates the HashState with more input data.
int noise_name_to_id(int category, const char *name, size_t name_len)
Maps an algorithm name to the corresponding identifier.
int noise_hashstate_hkdf(NoiseHashState *state, const uint8_t *key, size_t key_len, const uint8_t *data, size_t data_len, uint8_t *output1, size_t output1_len, uint8_t *output2, size_t output2_len)
Hashes input data with a key to generate two output values.
int noise_hashstate_finalize(NoiseHashState *state, uint8_t *hash, size_t hash_len)
Finalizes the hash state and returns the hash value.
int noise_hashstate_get_hash_id(const NoiseHashState *state)
Gets the algorithm identifier for a HashState object.
Internal definitions for the library.
void noise_free(void *ptr, size_t size)
Destroys the contents of a block of memory and free it.
int noise_hashstate_free(NoiseHashState *state)
Frees a HashState object after destroying all sensitive material.
int noise_hashstate_update(NoiseHashState *state, const uint8_t *data, size_t data_len)
Updates the hash state with more data.
#define NOISE_HASH_BLAKE2s
Hash identifier for "BLAKE2s".
#define NOISE_HASH_SHA512
Hash identifier for "SHA512".
size_t noise_hashstate_get_hash_length(const NoiseHashState *state)
Gets the length of the hash output for a HashState object.
int hash_id
Algorithm identifier for the hash.
int noise_hashstate_hash_two(NoiseHashState *state, const uint8_t *data1, size_t data1_len, const uint8_t *data2, size_t data2_len, uint8_t *hash, size_t hash_len)
Hashes the concatenation of two data buffers and returns the combined hash value. ...
#define NOISE_HASH_NONE
Hash identifier that indicates "no hash".
size_t size
Total size of the structure including subclass state.