mirror of
https://github.com/darlinghq/darling-corecrypto.git
synced 2024-11-23 12:19:44 +00:00
Add basic implementation of ccrng_uniform
Needed for libc
This commit is contained in:
parent
a3333038d9
commit
7d7d27c087
@ -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_ */
|
||||
|
13
src/ccrng.c
13
src/ccrng.c
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user