Add basic implementation of ccrng_uniform

Needed for libc
This commit is contained in:
Ariel Abreu 2020-06-15 14:02:28 -04:00
parent a3333038d9
commit 7d7d27c087
No known key found for this signature in database
GPG Key ID: F4D43CC7053EA2B3
2 changed files with 16 additions and 0 deletions

View File

@ -40,4 +40,7 @@ struct ccrng_state {
#define ccrng_generate(ctx, outlen, out) ((ctx)->generate((ctx), (outlen), (out)))
/* Generate a random value in [0, bound) */
int ccrng_uniform(struct ccrng_state *rng, uint64_t bound, uint64_t *rand);
#endif /* _CORECRYPTO_CCRNG_H_ */

View File

@ -21,3 +21,16 @@ struct ccrng_state* ccrng(int* error) {
return (struct ccrng_state*)&ccrng_global_system_rng_instance;
};
int ccrng_uniform(struct ccrng_state *rng, uint64_t bound, uint64_t *rand) {
// TODO(@facekapow): make this a proper uniform RNG
// (actually, the whole RNG system needs to be fixed)
//
// the current implementation for this function does at least satisfy the requirement that the value be
// between 0 and the upper bound, but i wouldn't say the number it generates has been "uniformly generated"
uint64_t tmp = 0;
ccrng_generate(rng, sizeof(tmp), &tmp);
*rand = tmp % bound;
return 0;
};