int rand_bytes (uint8_t *output, int len) { static int gather_delay = 0; ctr_drbg_context *rng_ctx = rand_ctx_get(); while (len > 0) { const size_t blen = min_int (len, CTR_DRBG_MAX_REQUEST); if (0 != ctr_drbg_random(rng_ctx, output, blen)) return 0; output += blen; len -= blen; if (gather_delay++ >= FOX_ENTROPY_GATHER_FREQUENCY) { entropy_context *e_ctx = rng_ctx->p_entropy; ASSERT(e_ctx != NULL); if (0 != entropy_gather(e_ctx)) return 0; gather_delay = 0; } } return 1; }
void rand_ctx_init_platform_entropy(int min_size, bool use_urandom) { static bool platform_set = false; if (!platform_set) { ctr_drbg_context *cd_ctx = rand_ctx_get(); entropy_context *ec = cd_ctx->p_entropy; f_source_ptr f_source = platform_entropy_poll; ASSERT(NULL != ec); #ifndef WIN32 if (use_urandom) f_source = urandom_entropy_poll; #endif if (0 != entropy_add_source(ec, f_source, NULL, min_size)) msg (M_FATAL, "Failed to add platform source to entropy pool"); if (!rand_ctx_reseed()) msg (M_FATAL, "ERROR: Random number generator failed to obtain entropy to reseed"); platform_set = true; } }
void rand_ctx_enable_prediction_resistance() { mbedtls_ctr_drbg_context *cd_ctx = rand_ctx_get(); mbedtls_ctr_drbg_set_prediction_resistance(cd_ctx, 1); }
int rand_ctx_reseed() { ctr_drbg_context *cd_ctx = rand_ctx_get(); entropy_context *ec = cd_ctx->p_entropy; ASSERT(NULL != ec); if (!entropy_gather_blocking(ec)) return 0; if (0 != ctr_drbg_reseed(cd_ctx, NULL, 0)) return 0; return 1; }
int rand_bytes (uint8_t *output, int len) { ctr_drbg_context *rng_ctx = rand_ctx_get(); while (len > 0) { const size_t blen = min_int (len, CTR_DRBG_MAX_REQUEST); if (0 != ctr_drbg_random(rng_ctx, output, blen)) return 0; output += blen; len -= blen; } return 1; }