Skip to content

Commit

Permalink
core: use static for internal fortuna CPRNG functions, add sr_ prefix…
Browse files Browse the repository at this point in the history
… to public functions
  • Loading branch information
henningw committed Oct 3, 2019
1 parent 7625993 commit 6f66bd2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
34 changes: 17 additions & 17 deletions src/core/rand/fortuna/fortuna.c
Expand Up @@ -138,31 +138,31 @@ typedef struct fortuna_state FState;
* - No memory allocations.
*/

void
static void
ciph_init(CIPH_CTX * ctx, const u_int8_t *key, int klen)
{
rijndael_set_key(ctx, (const u_int32_t *) key, klen, 1);
}

void
static void
ciph_encrypt(CIPH_CTX * ctx, const u_int8_t *in, u_int8_t *out)
{
rijndael_encrypt(ctx, (const u_int32_t *) in, (u_int32_t *) out);
}

void
static void
md_init(MD_CTX * ctx)
{
sr_SHA256_Init(ctx);
}

void
static void
md_update(MD_CTX * ctx, const u_int8_t *data, int len)
{
sr_SHA256_Update(ctx, data, len);
}

void
static void
md_result(MD_CTX * ctx, u_int8_t *dst)
{
SHA256_CTX tmp;
Expand All @@ -175,7 +175,7 @@ md_result(MD_CTX * ctx, u_int8_t *dst)
/*
* initialize state
*/
void
static void
init_state(FState *st)
{
int i;
Expand All @@ -189,7 +189,7 @@ init_state(FState *st)
* Endianess does not matter.
* It just needs to change without repeating.
*/
void
static void
inc_counter(FState *st)
{
u_int32_t *val = (u_int32_t *) st->counter;
Expand All @@ -206,7 +206,7 @@ inc_counter(FState *st)
/*
* This is called 'cipher in counter mode'.
*/
void
static void
encrypt_counter(FState *st, u_int8_t *dst)
{
ciph_encrypt(&st->ciph, st->counter, dst);
Expand All @@ -218,7 +218,7 @@ encrypt_counter(FState *st, u_int8_t *dst)
* The time between reseed must be at least RESEED_INTERVAL
* microseconds.
*/
int
static int
enough_time_passed(FState *st)
{
int ok;
Expand Down Expand Up @@ -251,7 +251,7 @@ enough_time_passed(FState *st)
/*
* generate new key from all the pools
*/
void
static void
reseed(FState *st)
{
unsigned k;
Expand Down Expand Up @@ -297,7 +297,7 @@ reseed(FState *st)
/*
* Pick a random pool. This uses key bytes as random source.
*/
unsigned
static unsigned
get_rand_pool(FState *st)
{
unsigned rnd;
Expand All @@ -317,8 +317,8 @@ get_rand_pool(FState *st)
/*
* update pools
*/
void
add_entropy_int(FState *st, const u_int8_t *data, unsigned len)
static void
add_entropy(FState *st, const u_int8_t *data, unsigned len)
{
unsigned pos;
u_int8_t hash[BLOCK];
Expand Down Expand Up @@ -348,7 +348,7 @@ add_entropy_int(FState *st, const u_int8_t *data, unsigned len)
/*
* Just take 2 next blocks as new key
*/
void
static void
rekey(FState *st)
{
encrypt_counter(st, st->key);
Expand All @@ -362,7 +362,7 @@ rekey(FState *st)
* This can also be viewed as spreading the startup
* entropy over all of the components.
*/
void
static void
startup_tricks(FState *st)
{
int i;
Expand All @@ -387,7 +387,7 @@ startup_tricks(FState *st)
st->tricks_done = 1;
}

void
static void
extract_data(FState *st, unsigned count, u_int8_t *dst)
{
unsigned n;
Expand Down Expand Up @@ -445,7 +445,7 @@ fortuna_add_entropy(const u_int8_t *data, unsigned len)
}
if (!data || !len)
return;
add_entropy_int(&main_state, data, len);
add_entropy(&main_state, data, len);
}

void
Expand Down
7 changes: 5 additions & 2 deletions src/core/rand/fortuna/random.c
Expand Up @@ -7,6 +7,9 @@
* Copyright (c) 2019 Henning Westerholt
* All rights reserved.
*
* Based on https://github.com/waitman/libfortuna, refactoring
* done in this version: https://github.com/henningw/libfortuna
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand Down Expand Up @@ -160,14 +163,14 @@ static int get_random_bytes_int(u_int8_t *dst, unsigned count)

/* public functions */

int get_pseudo_random_bytes(u_int8_t *dst, unsigned count)
int sr_get_pseudo_random_bytes(u_int8_t *dst, unsigned count)
{
return get_random_bytes_int(dst, count);
}



int add_entropy(const u_int8_t *data, unsigned count)
int sr_add_entropy(const u_int8_t *data, unsigned count)
{
system_reseed();
fortuna_add_entropy(data, count);
Expand Down
10 changes: 7 additions & 3 deletions src/core/rand/fortuna/random.h
@@ -1,11 +1,15 @@
/*
* random.h
* Acquire randomness from system. For seeding RNG.
* Get pseudo random numbers from RNG.
*
* Copyright (c) 2001 Marko Kreen
* Copyright (c) 2019 Henning Westerholt
* All rights reserved.
*
* Based on https://github.com/waitman/libfortuna, several cleanups
* done in this version: https://github.com/henningw/libfortuna
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
Expand Down Expand Up @@ -52,9 +56,9 @@
*/
#define SYSTEM_RESEED_MAX (12*60*60) /* 12h */

int get_pseudo_random_bytes(unsigned char *dst, unsigned count);
int sr_get_pseudo_random_bytes(unsigned char *dst, unsigned count);

int add_entropy(const unsigned char *data, unsigned count);
int sr_add_entropy(const unsigned char *data, unsigned count);


#endif
#endif

0 comments on commit 6f66bd2

Please sign in to comment.