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

[WIP/RFC] Remove prng registry #515

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
put PRNG descriptor into state
  • Loading branch information
sjaeckel committed Aug 21, 2024
commit 36e529420efa055ec57e6b07ebfc4f24f97fc00e
1 change: 0 additions & 1 deletion helper.pl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ sub check_descriptors {
my $fails = 0;
$fails = $fails + check_descriptor("ciphers", "cipher");
$fails = $fails + check_descriptor("hashes", "hash");
$fails = $fails + check_descriptor("prngs", "prng");
return $fails;
}

Expand Down
66 changes: 28 additions & 38 deletions src/headers/tomcrypt_prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,10 @@ struct sober128_prng {
};
#endif

typedef struct {
union {
char dummy[1];
#ifdef LTC_YARROW
struct yarrow_prng yarrow;
#endif
#ifdef LTC_RC4
struct rc4_prng rc4;
#endif
#ifdef LTC_CHACHA20_PRNG
struct chacha20_prng chacha;
#endif
#ifdef LTC_FORTUNA
struct fortuna_prng fortuna;
#endif
#ifdef LTC_SOBER128
struct sober128_prng sober128;
#endif
} u;
short ready; /* ready flag 0-1 */
LTC_MUTEX_TYPE(lock) /* lock */
} prng_state;
typedef struct ltc_prng_state prng_state;

/** PRNG descriptor */
extern struct ltc_prng_descriptor {
struct ltc_prng_descriptor {
/** Name of the PRNG */
const char *name;
/** size in bytes of exported state */
Expand Down Expand Up @@ -124,7 +103,31 @@ extern struct ltc_prng_descriptor {
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
*/
int (*test)(void);
} prng_descriptor[];
};

struct ltc_prng_state {
union {
char dummy[1];
#ifdef LTC_YARROW
struct yarrow_prng yarrow;
#endif
#ifdef LTC_RC4
struct rc4_prng rc4;
#endif
#ifdef LTC_CHACHA20_PRNG
struct chacha20_prng chacha;
#endif
#ifdef LTC_FORTUNA
struct fortuna_prng fortuna;
#endif
#ifdef LTC_SOBER128
struct sober128_prng sober128;
#endif
} u;
short ready; /* ready flag 0-1 */
struct ltc_prng_descriptor desc;
LTC_MUTEX_TYPE(lock) /* lock */
};

#ifdef LTC_YARROW
int yarrow_start(prng_state *prng);
Expand All @@ -135,7 +138,6 @@ int yarrow_done(prng_state *prng);
int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int yarrow_test(void);
extern const struct ltc_prng_descriptor yarrow_desc;
#endif

#ifdef LTC_FORTUNA
Expand All @@ -149,7 +151,6 @@ int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng);
int fortuna_test(void);
extern const struct ltc_prng_descriptor fortuna_desc;
#endif

#ifdef LTC_RC4
Expand All @@ -161,7 +162,6 @@ int rc4_done(prng_state *prng);
int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int rc4_test(void);
extern const struct ltc_prng_descriptor rc4_desc;
#endif

#ifdef LTC_CHACHA20_PRNG
Expand All @@ -173,7 +173,6 @@ int chacha20_prng_done(prng_state *prng);
int chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int chacha20_prng_test(void);
extern const struct ltc_prng_descriptor chacha20_prng_desc;
#endif

#ifdef LTC_SPRNG
Expand All @@ -185,7 +184,6 @@ int sprng_done(prng_state *prng);
int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int sprng_test(void);
extern const struct ltc_prng_descriptor sprng_desc;
#endif

#ifdef LTC_SOBER128
Expand All @@ -197,24 +195,16 @@ int sober128_done(prng_state *prng);
int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng);
int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng);
int sober128_test(void);
extern const struct ltc_prng_descriptor sober128_desc;
#endif

int find_prng(const char *name);
int register_prng(const struct ltc_prng_descriptor *prng);
int unregister_prng(const struct ltc_prng_descriptor *prng);
int register_all_prngs(void);
int prng_is_valid(int idx);
LTC_MUTEX_PROTO(ltc_prng_mutex)

/* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
* might not work on all platforms as planned
*/
unsigned long rng_get_bytes(unsigned char *out,
unsigned long outlen,
void (*callback)(void));

int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void));
int rng_make_prng(int bits, prng_state *prng, void (*callback)(void));

#ifdef LTC_PRNG_ENABLE_LTC_RNG
extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen,
Expand Down
29 changes: 0 additions & 29 deletions src/misc/crypt/crypt_find_prng.c

This file was deleted.

14 changes: 0 additions & 14 deletions src/misc/crypt/crypt_prng_descriptor.c

This file was deleted.

24 changes: 0 additions & 24 deletions src/misc/crypt/crypt_prng_is_valid.c

This file was deleted.

7 changes: 0 additions & 7 deletions src/misc/crypt/crypt_prng_rng_descriptor.c

This file was deleted.

38 changes: 0 additions & 38 deletions src/misc/crypt/crypt_register_all_prngs.c

This file was deleted.

42 changes: 0 additions & 42 deletions src/misc/crypt/crypt_register_prng.c

This file was deleted.

32 changes: 0 additions & 32 deletions src/misc/crypt/crypt_unregister_prng.c

This file was deleted.

3 changes: 2 additions & 1 deletion src/prngs/chacha20.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifdef LTC_CHACHA20_PRNG

const struct ltc_prng_descriptor chacha20_prng_desc =
static const struct ltc_prng_descriptor chacha20_prng_desc =
{
"chacha20",
40,
Expand All @@ -34,6 +34,7 @@ int chacha20_prng_start(prng_state *prng)
prng->ready = 0;
XMEMSET(&prng->u.chacha.ent, 0, sizeof(prng->u.chacha.ent));
prng->u.chacha.idx = 0;
prng->desc = chacha20_prng_desc;
LTC_MUTEX_INIT(&prng->lock)
return CRYPT_OK;
}
Expand Down
4 changes: 3 additions & 1 deletion src/prngs/fortuna.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to t
#define AES_TEST aes_test
#endif

const struct ltc_prng_descriptor fortuna_desc = {
static const struct ltc_prng_descriptor fortuna_desc = {
"fortuna",
64,
&fortuna_start,
Expand Down Expand Up @@ -256,6 +256,8 @@ int fortuna_start(prng_state *prng)
}
zeromem(prng->u.fortuna.IV, 16);

prng->desc = fortuna_desc;

LTC_MUTEX_INIT(&prng->lock)

return CRYPT_OK;
Expand Down
3 changes: 2 additions & 1 deletion src/prngs/rc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifdef LTC_RC4

const struct ltc_prng_descriptor rc4_desc =
static const struct ltc_prng_descriptor rc4_desc =
{
"rc4",
32,
Expand All @@ -36,6 +36,7 @@ int rc4_start(prng_state *prng)
prng->u.rc4.s.x = 0;
/* clear entropy (key) buffer */
XMEMSET(&prng->u.rc4.s.buf, 0, sizeof(prng->u.rc4.s.buf));
prng->desc = rc4_desc;
LTC_MUTEX_INIT(&prng->lock)
return CRYPT_OK;
}
Expand Down
Loading