/** Generate a pseudo-random number. * This uses the #localkey structure plus current time as input to * MD5, feeding most of the MD5 output back to #localkey and using one * output words as the pseudo-random output. * @return A 32-bit pseudo-random number. */ unsigned int ircrandom(void) { struct timeval tv; char usec[3]; /* Add some randomness to the pool. */ gettimeofday(&tv, 0); usec[0] = tv.tv_usec; usec[1] = tv.tv_usec >> 8; usec[2] = tv.tv_usec >> 16; random_add_entropy(usec, 3); /* Perform MD5 step. */ localkey.buf[0] = 0x67452301; localkey.buf[1] = 0xefcdab89; localkey.buf[2] = 0x98badcfe; localkey.buf[3] = 0x10325476; MD5Transform(localkey.buf, (uint32*)localkey.in); /* Feed back 12 bytes of hash value into randomness pool. */ random_add_entropy((char*)localkey.buf, 12); /* Return the final word of hash, which should not provide any * useful insight into current pool contents. */ return localkey.buf[3]; }
/*ARGSUSED*/ static int rnd_write(dev_t dev, struct uio *uiop, cred_t *credp) { int error; uint8_t buf[WRITEBUFSIZE]; size_t bytes; minor_t devno; devno = getminor(dev); while (uiop->uio_resid > 0) { bytes = min(sizeof (buf), uiop->uio_resid); /* See comments in rnd_read() */ uiop->uio_loffset = 0; if ((error = uiomove(buf, bytes, UIO_WRITE, uiop)) != 0) return (error); switch (devno) { case DEVRANDOM: if ((error = random_add_entropy(buf, bytes, 0)) != 0) return (error); break; case DEVURANDOM: if ((error = random_add_pseudo_entropy(buf, bytes, 0)) != 0) return (error); break; default: return (ENXIO); } } return (0); }
static void event_handler(switch_event_t *event) { char *buf; if (switch_event_serialize(event, &buf, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { random_add_entropy(rfd, buf, strlen(buf)); free(buf); } }
/** Seed the PRNG with a string. * @param[in] from Client setting the seed (may be NULL). * @param[in] fields Input arguments (fields[0] is used). * @param[in] count Number of input arguments. * @return Non-zero on success, zero on error. */ int random_seed_set(struct Client* from, const char* const* fields, int count) { if (count < 1) { if (from) /* send an error */ return need_more_params(from, "SET"); else { log_write(LS_CONFIG, L_ERROR, 0, "Not enough fields in F line"); return 0; } } random_add_entropy(fields[0], strlen(fields[0])); return 1; }