Skip to content

Commit

Permalink
beautify code
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Mueller <[email protected]>
  • Loading branch information
smuellerDD committed Nov 27, 2023
1 parent 9f6886e commit bd7899e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
21 changes: 11 additions & 10 deletions drng/src/cshake_drng.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ static void cshake256_drng_selftest(int *tested, const char *impl)
#define LC_CSHAKE256_DRNG_ENCODE_LENGTH 84
#define LC_CSHAKE256_DRNG_HASH_TYPE lc_cshake256

static void lc_cshake256_encode(struct lc_hash_ctx *cshake_ctx, const uint8_t n,
const uint8_t *alpha, size_t alphalen)
static void lc_cshake256_drng_encode(struct lc_hash_ctx *cshake_ctx,
const uint8_t n, const uint8_t *alpha,
size_t alphalen)
{
static const uint8_t byte = 0xff;
uint8_t encode[LC_CSHAKE256_DRNG_KEYSIZE + 1];
Expand Down Expand Up @@ -421,10 +422,9 @@ static void lc_cshake256_encode(struct lc_hash_ctx *cshake_ctx, const uint8_t n,
* This generates T(0) and T(1) of size 1088 of the cSHAKE DRNG specification
* section 2.3.
*/
static void cshake256_drng_fke_init_ctx(struct lc_cshake256_drng_state *state,
struct lc_hash_ctx *cshake_ctx,
const uint8_t *addtl_input,
size_t addtl_input_len)
static void lc_cshake256_drng_fke_init_ctx(
struct lc_cshake256_drng_state *state, struct lc_hash_ctx *cshake_ctx,
const uint8_t *addtl_input, size_t addtl_input_len)
{
/* Initialize the cSHAKE with K(N) and the cust. string. */
lc_cshake_init(cshake_ctx,
Expand All @@ -433,7 +433,7 @@ static void cshake256_drng_fke_init_ctx(struct lc_cshake256_drng_state *state,
state->key, LC_CSHAKE256_DRNG_KEYSIZE);

/* Insert the additional data into the cSHAKE state. */
lc_cshake256_encode(cshake_ctx, 2, addtl_input, addtl_input_len);
lc_cshake256_drng_encode(cshake_ctx, 2, addtl_input, addtl_input_len);

/* Generate the K(N + 1) to store in the state and overwrite K(N). */
lc_cshake_final(cshake_ctx, state->key, LC_CSHAKE256_DRNG_KEYSIZE);
Expand Down Expand Up @@ -473,8 +473,8 @@ static int lc_cshake256_drng_generate(void *_state, const uint8_t *addtl_input,
LC_CSHAKE256_DRNG_KEYSIZE);

/* Instantiate cSHAKE with TMP_K(N), generate TMP_K(N + 1). */
cshake256_drng_fke_init_ctx(state, cshake_ctx, addtl_input,
addtl_input_len);
lc_cshake256_drng_fke_init_ctx(state, cshake_ctx, addtl_input,
addtl_input_len);

/* Generate the requested amount of output bits */
lc_cshake_final(cshake_ctx, out, todo);
Expand Down Expand Up @@ -537,7 +537,8 @@ static int lc_cshake256_drng_seed(void *_state, const uint8_t *seed,
lc_hash_update(cshake_ctx, seed, seedlen);

/* Insert the personalization string into the cSHAKE state. */
lc_cshake256_encode(cshake_ctx, initially_seeded, persbuf, perslen);
lc_cshake256_drng_encode(cshake_ctx, initially_seeded, persbuf,
perslen);

/* Generate the K(N + 1) to store in the state and overwrite K(N). */
lc_cshake_final(cshake_ctx, state->key, LC_CSHAKE256_DRNG_KEYSIZE);
Expand Down
21 changes: 11 additions & 10 deletions drng/src/kmac_drng.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ static void kmac256_drng_selftest(int *tested, const char *impl)
#define LC_KMAC256_DRNG_ENCODE_LENGTH 84
#define LC_KMAC256_DRNG_HASH_TYPE lc_cshake256

static void lc_kmac256_encode(struct lc_kmac_ctx *kmac_ctx, const uint8_t n,
const uint8_t *alpha, size_t alphalen)
static void lc_kmac256_drng_encode(struct lc_kmac_ctx *kmac_ctx,
const uint8_t n, const uint8_t *alpha,
size_t alphalen)
{
static const uint8_t byte = 0xff;
uint8_t encode[LC_KMAC256_DRNG_KEYSIZE + 1];
Expand Down Expand Up @@ -366,18 +367,18 @@ static void lc_kmac256_encode(struct lc_kmac_ctx *kmac_ctx, const uint8_t n,
* This generates T(0) and T(1) of size 1088 of the KMAC DRNG specification
* section 2.3.
*/
static void kmac256_drng_fke_init_ctx(struct lc_kmac256_drng_state *state,
struct lc_kmac_ctx *kmac_ctx,
const uint8_t *addtl_input,
size_t addtl_input_len)
static void lc_kmac256_drng_fke_init_ctx(struct lc_kmac256_drng_state *state,
struct lc_kmac_ctx *kmac_ctx,
const uint8_t *addtl_input,
size_t addtl_input_len)
{
/* Initialize the KMAC with K(N) and the cust. string. */
lc_kmac_init(kmac_ctx, state->key, LC_KMAC256_DRNG_KEYSIZE,
(uint8_t *)LC_KMAC_DRNG_CTX_CUSTOMIZATION_STRING,
sizeof(LC_KMAC_DRNG_CTX_CUSTOMIZATION_STRING) - 1);

/* Insert the additional data into the KMAC state. */
lc_kmac256_encode(kmac_ctx, 2, addtl_input, addtl_input_len);
lc_kmac256_drng_encode(kmac_ctx, 2, addtl_input, addtl_input_len);

/* Generate the K(N + 1) to store in the state and overwrite K(N). */
lc_kmac_final_xof(kmac_ctx, state->key, LC_KMAC256_DRNG_KEYSIZE);
Expand Down Expand Up @@ -416,8 +417,8 @@ static int lc_kmac256_drng_generate(void *_state, const uint8_t *addtl_input,
LC_KMAC256_DRNG_KEYSIZE);

/* Instantiate KMAC with TMP_K(N) and generate TMP_K(N + 1). */
kmac256_drng_fke_init_ctx(state, kmac_ctx, addtl_input,
addtl_input_len);
lc_kmac256_drng_fke_init_ctx(state, kmac_ctx, addtl_input,
addtl_input_len);

/* Generate the requested amount of output bits */
lc_kmac_final_xof(kmac_ctx, out, todo);
Expand Down Expand Up @@ -477,7 +478,7 @@ static int lc_kmac256_drng_seed(void *_state, const uint8_t *seed,
lc_kmac_update(kmac_ctx, seed, seedlen);

/* Insert the personalization string into the KMAC state. */
lc_kmac256_encode(kmac_ctx, initially_seeded, persbuf, perslen);
lc_kmac256_drng_encode(kmac_ctx, initially_seeded, persbuf, perslen);

/* Generate the K(N + 1) to store in the state and overwrite K(N). */
lc_kmac_final_xof(kmac_ctx, state->key, LC_KMAC256_DRNG_KEYSIZE);
Expand Down
12 changes: 6 additions & 6 deletions drng/src/xdrbg256.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ static inline void xdrbg256_shake_final(struct lc_hash_ctx *shake_ctx,
*
* * length of the hash is set to be equal to |V|
*/
static void lc_xdrbg256_encode(struct lc_hash_ctx *shake_ctx, const uint8_t n,
const uint8_t *alpha, size_t alphalen)
static void lc_xdrbg256_drng_encode(struct lc_hash_ctx *shake_ctx,
const uint8_t n, const uint8_t *alpha,
size_t alphalen)
{
static const uint8_t byte = 0xff;
uint8_t encode[LC_XDRBG256_DRNG_KEYSIZE + 1];
Expand Down Expand Up @@ -124,8 +125,7 @@ static void lc_xdrbg256_encode(struct lc_hash_ctx *shake_ctx, const uint8_t n,
lc_hash_init(enc_hash_ctx);
lc_hash_update(enc_hash_ctx, alpha, alphalen);
lc_hash_update(enc_hash_ctx, &byte, sizeof(byte));
lc_hash_set_digestsize(enc_hash_ctx, LC_XDRBG256_DRNG_KEYSIZE);
lc_hash_final(enc_hash_ctx, encode);
xdrbg256_shake_final(enc_hash_ctx, encode, LC_XDRBG256_DRNG_KEYSIZE);
lc_hash_zero(enc_hash_ctx);

/* Encode the length */
Expand Down Expand Up @@ -168,7 +168,7 @@ static void xdrbg256_drng_fke_init_ctx(struct lc_xdrbg256_drng_state *state,
lc_hash_update(shake_ctx, state->v, LC_XDRBG256_DRNG_KEYSIZE);

/* Insert alpha into the SHAKE state together with its encoding. */
lc_xdrbg256_encode(shake_ctx, 2, alpha, alphalen);
lc_xdrbg256_drng_encode(shake_ctx, 2, alpha, alphalen);

/* Generate the V to store in the state and overwrite V'. */
xdrbg256_shake_final(shake_ctx, state->v, LC_XDRBG256_DRNG_KEYSIZE);
Expand Down Expand Up @@ -268,7 +268,7 @@ static int lc_xdrbg256_drng_seed(void *_state, const uint8_t *seed,
lc_hash_update(shake_ctx, seed, seedlen);

/* Insert alpha into the SHAKE state together with its encoding. */
lc_xdrbg256_encode(shake_ctx, intially_seeded, alpha, alphalen);
lc_xdrbg256_drng_encode(shake_ctx, intially_seeded, alpha, alphalen);

/* Generate the V to store in the state and overwrite V'. */
xdrbg256_shake_final(shake_ctx, state->v, LC_XDRBG256_DRNG_KEYSIZE);
Expand Down

0 comments on commit bd7899e

Please sign in to comment.