/* Priority order of PRNG algorithms and then default GetEntropy if none. Does an initial entropy source and reseeding */ int32 psInitPrng(psRandom_t *ctx, void *userPtr) { #if defined(USE_FORTUNA) || defined(USE_YARROW) unsigned char entropyBytes[RANDOM_ENTROPY_BYTES]; int32 rc; #endif ctx->bytecount = 0; #if defined(USE_FORTUNA) || defined(USE_YARROW) if ((rc = psGetEntropy(entropyBytes, RANDOM_ENTROPY_BYTES, userPtr)) < 0) { return rc; } #endif #ifdef USE_YARROW if ((rc = psYarrowStart(&ctx->yarrow)) < 0) { return rc; } if ((rc = psYarrowAddEntropy(entropyBytes, RANDOM_ENTROPY_BYTES, &ctx->yarrow)) < 0) { return rc; } if ((rc = psYarrowReseed(&ctx->yarrow)) < 0) { return rc; } #endif return PS_SUCCESS; }
/** Import a PRNG state @param in The PRNG state @param inlen Size of the state @param prng The PRNG to import @return CRYPT_OK if successful */ int32 psYarrowImport(unsigned char *in, uint32 inlen, psYarrow_t *ctx) { int32 err; if (inlen != 64) { return PS_ARG_FAIL; } if ((err = psYarrowStart(ctx)) != PS_SUCCESS) { return err; } if ((err = psYarrowAddEntropy(in, 64, ctx)) != PS_SUCCESS) { return err; } if ((err = psYarrowReseed(ctx)) != PS_SUCCESS) { return err; } return err; }
/* Performs the read */ static int32 readRandomData(psRandom_t *ctx, unsigned char *bytes, uint32 size) { #if defined(USE_FORTUNA) || defined(USE_YARROW) unsigned char entropyBytes[RANDOM_ENTROPY_BYTES]; int32 rc; #endif /* Return random data. The defines above control how often to add entropy and reseed the key. */ ctx->callcount++; ctx->bytecount += size; #ifdef USE_YARROW if (ctx->bytecount >= RANDOM_BYTES_BEFORE_ENTROPY) { if ((rc = psGetEntropy(entropyBytes, RANDOM_ENTROPY_BYTES)) < 0) { return rc; } if ((rc = psYarrowAddEntropy(entropyBytes, RANDOM_ENTROPY_BYTES, &ctx->yarrow)) < 0) { return rc; } ctx->bytecount = 0; } if (ctx->callcount >= RANDOM_CALLS_BEFORE_RESEED) { if ((rc = psYarrowReseed(&ctx->yarrow)) < 0) { return rc; } ctx->callcount = 0; } return psYarrowRead(bytes, size, &ctx->yarrow); #endif /* If no PRNG algorithms defined, default to the low level GetEntropy function for all the randomness */ return psGetEntropy(bytes, size); }