Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-130213: POC let blake2module.c see far less internals #130483

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Modules/_hacl/Hacl_Hash_Blake2b_Simd256.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ The caller must satisfy the following requirements.
- The digest_length must not exceed 256 for S, 64 for B.

*/
Hacl_Hash_Blake2b_Simd256_state_t
void
*Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key(
Hacl_Hash_Blake2b_blake2_params *p,
bool last_node,
Expand Down Expand Up @@ -901,11 +901,12 @@ void Hacl_Hash_Blake2b_Simd256_reset(Hacl_Hash_Blake2b_Simd256_state_t *s)
*/
Hacl_Streaming_Types_error_code
Hacl_Hash_Blake2b_Simd256_update(
Hacl_Hash_Blake2b_Simd256_state_t *state,
void *state_in,
uint8_t *chunk,
uint32_t chunk_len
)
{
Hacl_Hash_Blake2b_Simd256_state_t* state = (Hacl_Hash_Blake2b_Simd256_state_t*)state_in;
Hacl_Hash_Blake2b_Simd256_state_t s = *state;
uint64_t total_len = s.total_len;
if ((uint64_t)chunk_len > 0xffffffffffffffffULL - total_len)
Expand Down Expand Up @@ -1122,8 +1123,9 @@ For convenience, this function returns `digest_length`. When in doubt, callers
can pass an array of size HACL_BLAKE2B_256_OUT_BYTES, then use the return value
to see how many bytes were actually written.
*/
uint8_t Hacl_Hash_Blake2b_Simd256_digest(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *dst)
uint8_t Hacl_Hash_Blake2b_Simd256_digest(void * state, uint8_t *dst)
{
Hacl_Hash_Blake2b_Simd256_state_t* s = (Hacl_Hash_Blake2b_Simd256_state_t*)state;
Hacl_Hash_Blake2b_Simd256_block_state_t block_state0 = (*s).block_state;
bool last_node0 = block_state0.thd;
uint8_t nn0 = block_state0.snd;
Expand Down Expand Up @@ -1203,8 +1205,9 @@ uint8_t Hacl_Hash_Blake2b_Simd256_digest(Hacl_Hash_Blake2b_Simd256_state_t *s, u
((Hacl_Hash_Blake2b_index){ .key_length = kk, .digest_length = nn, .last_node = last_node }).digest_length;
}

Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(Hacl_Hash_Blake2b_Simd256_state_t *s)
Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(void * state)
{
Hacl_Hash_Blake2b_Simd256_state_t* s = (Hacl_Hash_Blake2b_Simd256_state_t*)state;
Hacl_Hash_Blake2b_Simd256_block_state_t block_state = (*s).block_state;
bool last_node = block_state.thd;
uint8_t nn = block_state.snd;
Expand All @@ -1216,8 +1219,9 @@ Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(Hacl_Hash_Blake2b_Simd256
/**
Free state function when there is no key
*/
void Hacl_Hash_Blake2b_Simd256_free(Hacl_Hash_Blake2b_Simd256_state_t *state)
void Hacl_Hash_Blake2b_Simd256_free(void *state_in)
{
Hacl_Hash_Blake2b_Simd256_state_t* state = (Hacl_Hash_Blake2b_Simd256_state_t*)state_in;
Hacl_Hash_Blake2b_Simd256_state_t scrut = *state;
uint8_t *buf = scrut.buf;
Hacl_Hash_Blake2b_Simd256_block_state_t block_state = scrut.block_state;
Expand All @@ -1232,9 +1236,10 @@ void Hacl_Hash_Blake2b_Simd256_free(Hacl_Hash_Blake2b_Simd256_state_t *state)
/**
Copying. This preserves all parameters.
*/
Hacl_Hash_Blake2b_Simd256_state_t
*Hacl_Hash_Blake2b_Simd256_copy(Hacl_Hash_Blake2b_Simd256_state_t *state)
void
*Hacl_Hash_Blake2b_Simd256_copy(void * state_in)
{
Hacl_Hash_Blake2b_Simd256_state_t* state = (Hacl_Hash_Blake2b_Simd256_state_t*)state_in;
Hacl_Hash_Blake2b_Simd256_state_t scrut = *state;
Hacl_Hash_Blake2b_Simd256_block_state_t block_state0 = scrut.block_state;
uint8_t *buf0 = scrut.buf;
Expand Down
141 changes: 6 additions & 135 deletions Modules/_hacl/Hacl_Hash_Blake2b_Simd256.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,11 @@
extern "C" {
#endif

#include <string.h>
#include "python_hacl_namespaces.h"
#include "krml/types.h"
#include "krml/lowstar_endianness.h"
#include "krml/internal/target.h"

#include "Hacl_Streaming_Types.h"

#include "Hacl_Hash_Blake2b.h"
#include "libintvector.h"

#define HACL_HASH_BLAKE2B_SIMD256_BLOCK_BYTES (128U)

#define HACL_HASH_BLAKE2B_SIMD256_OUT_BYTES (64U)

#define HACL_HASH_BLAKE2B_SIMD256_KEY_BYTES (64U)

#define HACL_HASH_BLAKE2B_SIMD256_SALT_BYTES (16U)

#define HACL_HASH_BLAKE2B_SIMD256_PERSONAL_BYTES (16U)

typedef struct K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256__s
{
Lib_IntVector_Intrinsics_vec256 *fst;
Lib_IntVector_Intrinsics_vec256 *snd;
}
K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256_;

typedef struct Hacl_Hash_Blake2b_Simd256_block_state_t_s
{
uint8_t fst;
uint8_t snd;
bool thd;
K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256_ f3;
}
Hacl_Hash_Blake2b_Simd256_block_state_t;

typedef struct Hacl_Hash_Blake2b_Simd256_state_t_s
{
Hacl_Hash_Blake2b_Simd256_block_state_t block_state;
uint8_t *buf;
uint64_t total_len;
}
Hacl_Hash_Blake2b_Simd256_state_t;

/**
General-purpose allocation function that gives control over all
Expand All @@ -90,74 +51,19 @@ The caller must satisfy the following requirements.
- The digest_length must not exceed 256 for S, 64 for B.

*/
Hacl_Hash_Blake2b_Simd256_state_t
void
*Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key(
Hacl_Hash_Blake2b_blake2_params *p,
bool last_node,
uint8_t *k
);

/**
Specialized allocation function that picks default values for all
parameters, except for the key_length. Further resettings of the state SHALL be
done with `reset_with_key`, and SHALL feature the exact same key length `kk` as
passed here. In other words, once you commit to a key length, the only way to
change this parameter is to allocate a new object.

The caller must satisfy the following requirements.
- The key_length must not exceed 256 for S, 64 for B.

*/
Hacl_Hash_Blake2b_Simd256_state_t
*Hacl_Hash_Blake2b_Simd256_malloc_with_key0(uint8_t *k, uint8_t kk);

/**
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_Hash_Blake2b_Simd256_state_t *Hacl_Hash_Blake2b_Simd256_malloc(void);

/**
General-purpose re-initialization function with parameters and
key. You cannot change digest_length, key_length, or last_node, meaning those values in
the parameters object must be the same as originally decided via one of the
malloc functions. All other values of the parameter can be changed. The behavior
is unspecified if you violate this precondition.
*/
void
Hacl_Hash_Blake2b_Simd256_reset_with_key_and_params(
Hacl_Hash_Blake2b_Simd256_state_t *s,
Hacl_Hash_Blake2b_blake2_params *p,
uint8_t *k
);

/**
Specialized-purpose re-initialization function with no parameters,
and a key. The key length must be the same as originally decided via your choice
of malloc function. All other parameters are reset to their default values. The
original call to malloc MUST have set digest_length to the default value. The
behavior is unspecified if you violate this precondition.
*/
void
Hacl_Hash_Blake2b_Simd256_reset_with_key(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *k);

/**
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_Simd256_reset(Hacl_Hash_Blake2b_Simd256_state_t *s);

/**
Update function; 0 = success, 1 = max length exceeded
*/
Hacl_Streaming_Types_error_code
Hacl_Hash_Blake2b_Simd256_update(
Hacl_Hash_Blake2b_Simd256_state_t *state,
void *state,
uint8_t *chunk,
uint32_t chunk_len
);
Expand All @@ -173,55 +79,20 @@ For convenience, this function returns `digest_length`. When in doubt, callers
can pass an array of size HACL_BLAKE2B_256_OUT_BYTES, then use the return value
to see how many bytes were actually written.
*/
uint8_t Hacl_Hash_Blake2b_Simd256_digest(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *dst);
uint8_t Hacl_Hash_Blake2b_Simd256_digest(void *s, uint8_t *dst);

Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(Hacl_Hash_Blake2b_Simd256_state_t *s);
Hacl_Hash_Blake2b_index Hacl_Hash_Blake2b_Simd256_info(void *s);

/**
Free state function when there is no key
*/
void Hacl_Hash_Blake2b_Simd256_free(Hacl_Hash_Blake2b_Simd256_state_t *state);
void Hacl_Hash_Blake2b_Simd256_free(void *state);

/**
Copying. This preserves all parameters.
*/
Hacl_Hash_Blake2b_Simd256_state_t
*Hacl_Hash_Blake2b_Simd256_copy(Hacl_Hash_Blake2b_Simd256_state_t *state);

/**
Write the BLAKE2b digest of message `input` using key `key` into `output`.

@param output Pointer to `output_len` bytes of memory where the digest is written to.
@param output_len Length of the to-be-generated digest with 1 <= `output_len` <= 64.
@param input Pointer to `input_len` bytes of memory where the input message is read from.
@param input_len Length of the input message.
@param key Pointer to `key_len` bytes of memory where the key is read from.
@param 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 key `key` and
parameters `params` into `output`. The `key` array must be of length
`params.key_length`. The `output` array must be of length
`params.digest_length`.
*/
void
Hacl_Hash_Blake2b_Simd256_hash_with_key_and_params(
uint8_t *output,
uint8_t *input,
uint32_t input_len,
Hacl_Hash_Blake2b_blake2_params params,
uint8_t *key
);
*Hacl_Hash_Blake2b_Simd256_copy(void *state);

#if defined(__cplusplus)
}
Expand Down
124 changes: 124 additions & 0 deletions Modules/_hacl/internal/Hacl_Hash_Blake2b_Simd256.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,130 @@ extern "C" {
#include "../Hacl_Hash_Blake2b_Simd256.h"
#include "libintvector.h"

#define HACL_HASH_BLAKE2B_SIMD256_BLOCK_BYTES (128U)

#define HACL_HASH_BLAKE2B_SIMD256_OUT_BYTES (64U)

#define HACL_HASH_BLAKE2B_SIMD256_KEY_BYTES (64U)

#define HACL_HASH_BLAKE2B_SIMD256_SALT_BYTES (16U)

#define HACL_HASH_BLAKE2B_SIMD256_PERSONAL_BYTES (16U)

typedef struct K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256__s
{
Lib_IntVector_Intrinsics_vec256 *fst;
Lib_IntVector_Intrinsics_vec256 *snd;
}
K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256_;

typedef struct Hacl_Hash_Blake2b_Simd256_block_state_t_s
{
uint8_t fst;
uint8_t snd;
bool thd;
K____Lib_IntVector_Intrinsics_vec256___Lib_IntVector_Intrinsics_vec256_ f3;
}
Hacl_Hash_Blake2b_Simd256_block_state_t;

typedef struct Hacl_Hash_Blake2b_Simd256_state_t_s
{
Hacl_Hash_Blake2b_Simd256_block_state_t block_state;
uint8_t *buf;
uint64_t total_len;
}
Hacl_Hash_Blake2b_Simd256_state_t;

/**
Specialized allocation function that picks default values for all
parameters, except for the key_length. Further resettings of the state SHALL be
done with `reset_with_key`, and SHALL feature the exact same key length `kk` as
passed here. In other words, once you commit to a key length, the only way to
change this parameter is to allocate a new object.

The caller must satisfy the following requirements.
- The key_length must not exceed 256 for S, 64 for B.

*/
Hacl_Hash_Blake2b_Simd256_state_t
*Hacl_Hash_Blake2b_Simd256_malloc_with_key0(uint8_t *k, uint8_t kk);

/**
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_Hash_Blake2b_Simd256_state_t *Hacl_Hash_Blake2b_Simd256_malloc(void);

/**
General-purpose re-initialization function with parameters and
key. You cannot change digest_length, key_length, or last_node, meaning those values in
the parameters object must be the same as originally decided via one of the
malloc functions. All other values of the parameter can be changed. The behavior
is unspecified if you violate this precondition.
*/
void
Hacl_Hash_Blake2b_Simd256_reset_with_key_and_params(
Hacl_Hash_Blake2b_Simd256_state_t *s,
Hacl_Hash_Blake2b_blake2_params *p,
uint8_t *k
);

/**
Specialized-purpose re-initialization function with no parameters,
and a key. The key length must be the same as originally decided via your choice
of malloc function. All other parameters are reset to their default values. The
original call to malloc MUST have set digest_length to the default value. The
behavior is unspecified if you violate this precondition.
*/
void
Hacl_Hash_Blake2b_Simd256_reset_with_key(Hacl_Hash_Blake2b_Simd256_state_t *s, uint8_t *k);

/**
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_Simd256_reset(Hacl_Hash_Blake2b_Simd256_state_t *s);

/**
Write the BLAKE2b digest of message `input` using key `key` into `output`.

@param output Pointer to `output_len` bytes of memory where the digest is written to.
@param output_len Length of the to-be-generated digest with 1 <= `output_len` <= 64.
@param input Pointer to `input_len` bytes of memory where the input message is read from.
@param input_len Length of the input message.
@param key Pointer to `key_len` bytes of memory where the key is read from.
@param 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 key `key` and
parameters `params` into `output`. The `key` array must be of length
`params.key_length`. The `output` array must be of length
`params.digest_length`.
*/
void
Hacl_Hash_Blake2b_Simd256_hash_with_key_and_params(
uint8_t *output,
uint8_t *input,
uint32_t input_len,
Hacl_Hash_Blake2b_blake2_params params,
uint8_t *key
);

void
Hacl_Hash_Blake2b_Simd256_init(Lib_IntVector_Intrinsics_vec256 *hash, uint32_t kk, uint32_t nn);

Expand Down
Loading
Loading