예제 #1
0
파일: arc4random.c 프로젝트: 16rd/rt-n56u
uint32_t
arc4random(void)
{
	if (!rs_initialized)
		__arc4random_stir();
	return arc4_getword(&rs);
}
예제 #2
0
int
iterator_next(iterator *iter)
{
	iter->off += (arc4_getword(&iter->as) % iter->skipmod) + 1;

	return (iter->off);
}
예제 #3
0
u_int32_t
arc4random(void)
{
    arc4_count -= 4;
    if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
        __arc4random_stir();
    return arc4_getword(&rs);
}
예제 #4
0
파일: arc4random.c 프로젝트: dimkr/lobsder
uint32_t
arc4random_pushb(const void *src, size_t len)
{
	size_t rlen;
	union {
		uint8_t buf[256];
		struct {
			struct timeval tv;
			const void *sp, *dp;
			size_t sz;
			uint32_t vu;
		} s;
		uint32_t xbuf;
	} idat;
	uint32_t res = 1;

	if (!rs_initialized) {
		arc4_init();
		rs_initialized = 1;
	}

	idat.s.sp = &idat;
	idat.s.dp = src;
	idat.s.sz = len;
	idat.s.vu = arc4_getword();
	gettimeofday(&idat.s.tv, NULL);

	rlen = MAX(sizeof(idat.s), len);
	while (rlen--)
		idat.buf[rlen % sizeof(idat.buf)] ^=
		    ((const uint8_t *)src)[rlen % len];
	rlen = MIN(sizeof(idat), MAX(sizeof(idat.s), len));

	if (arc4_writeback((void *)&idat, rlen, 1))
		res = 0;
	arc4_addrandom((void *)&idat, rlen);
	rlen = arc4_getbyte() & 1;
	if (res)
		res = idat.xbuf;
	else
		/* we got entropy from the kernel, so consider us stirred */
		stir_finish(idat.buf[5]);
	if (rlen)
		(void)arc4_getbyte();
	return (res ^ arc4_getword());
}
예제 #5
0
uint32_t
arc4random()
{

	arc4_count -= 4;
	if (!rs_initialized || arc4_count <= 0)
		arc4random_stir();
	return arc4_getword(&rs);
}
예제 #6
0
파일: loose_ends.c 프로젝트: ericgorr/xnu
u_int32_t arc4random(void)
{
    u_int32_t val;
    arc4_count -= 4;
    if (arc4_count <= 0 || !rs_initialized)
        arc4_stir();
    val = arc4_getword();
    return val;
}
예제 #7
0
파일: arc4random.c 프로젝트: Bhudipta/minix
ARC4RANDOM_EXPORT ARC4RANDOM_UINT32
arc4random(void)
{
	ARC4RANDOM_UINT32 val;
	_ARC4_LOCK();
	arc4_count -= 4;
	arc4_stir_if_needed();
	val = arc4_getword();
	_ARC4_UNLOCK();
	return val;
}
예제 #8
0
u_int32_t
arc4random(void)
{
	u_int32_t val;
	_ARC4_LOCK();
	arc4_count -= 4;
	arc4_stir_if_needed();
	val = arc4_getword();
	_ARC4_UNLOCK();
	return val;
}
예제 #9
0
unsigned int
arc4random(void)
{
	unsigned int rnd;

	arc4_check_init();
	arc4_check_stir();
	rnd = arc4_getword(&rs);

	return (rnd);
}
예제 #10
0
u_int32_t
arc4random(void)
{
        u_int32_t val;
        _ARC4_LOCK();
        arc4_count -= 4;
        if (arc4_count <= 0 || !rs_initialized || arc4_stir_pid != getpid())
                arc4_stir();
        val = arc4_getword();
        _ARC4_UNLOCK();
        return val;
}
예제 #11
0
u_int32_t
arc4random(void)
{
	u_int32_t rnd;

	THREAD_LOCK();
	arc4_check_init();
	arc4_check_stir();
	rnd = arc4_getword();
	arc4_count -= 4;
	THREAD_UNLOCK();

	return (rnd);
}
예제 #12
0
HB_U32 hb_arc4random( void )
{
   HB_U32 val;

   ARC4_LOCK();

   arc4_count -= 4;
   arc4_stir_if_needed();
   val = arc4_getword();

   ARC4_UNLOCK();

   return val;
}
예제 #13
0
void
iterator_init(iterator *iter, struct arc4_stream *as)
{
	int i;
	char derive[16];

	iter->skipmod = INIT_SKIPMOD;
	iter->as = *as;

	/* Put the PRNG in different state, using key dependant data
	 * provided by the PRNG itself.
	 */
	for (i = 0; i < sizeof(derive); i++)
		derive[i] = arc4_getbyte(&iter->as);
	arc4_addrandom(&iter->as, derive, sizeof(derive));

	iter->off = arc4_getword(&iter->as) % iter->skipmod;
}
예제 #14
0
파일: arc4random.c 프로젝트: dimkr/lobsder
/*
 * Calculate a uniformly distributed random number less than
 * upper_bound avoiding "modulo bias".
 *
 * Uniformity is achieved by generating new random numbers
 * until the one returned is outside the range
 * [0, 2^32 % upper_bound[. This guarantees the selected
 * random number will be inside the range
 * [2^32 % upper_bound, 2^32[ which maps back to
 * [0, upper_bound[ after reduction modulo upper_bound.
 */
uint32_t
arc4random_uniform(uint32_t upper_bound)
{
	uint32_t r, min;

	if (upper_bound < 2)
		return (0);

#if defined(ULONG_MAX) && (ULONG_MAX > 0xFFFFFFFFUL)
	min = 0x100000000UL % upper_bound;
#else
	/* calculate (2^32 % upper_bound) avoiding 64-bit math */
	if (upper_bound > 0x80000000U)
		/* 2^32 - upper_bound (only one "value area") */
		min = 1 + ~upper_bound;
	else
		/* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
		min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
#endif

	/*
	 * This could theoretically loop forever but each retry has
	 * p > 0.5 (worst case, usually far better) of selecting a
	 * number inside the range we need, so it should rarely need
	 * to re-roll (at all).
	 */
	arc4_count -= 4;
	if (!rs_initialized || arc4_stir_pid != getpid() || arc4_count <= 0)
		arc4random_stir();
	if (arc4_getbyte() & 1)
		(void)arc4_getbyte();
	do {
		r = arc4_getword();
	} while (r < min);

	return (r % upper_bound);
}