diff --git a/include/corecrypto/ccrng.h b/include/corecrypto/ccrng.h index f329222..ef58214 100644 --- a/include/corecrypto/ccrng.h +++ b/include/corecrypto/ccrng.h @@ -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_ */ diff --git a/src/ccrng.c b/src/ccrng.c index aeb714d..64311b6 100644 --- a/src/ccrng.c +++ b/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; +};