Changeset View
Changeset View
Standalone View
Standalone View
head/sys/libkern/arc4random.c
Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | |||||
void | void | ||||
arc4rand(void *ptr, u_int len, int reseed) | arc4rand(void *ptr, u_int len, int reseed) | ||||
{ | { | ||||
struct chacha20_s *chacha20; | struct chacha20_s *chacha20; | ||||
struct timeval tv; | struct timeval tv; | ||||
u_int length; | u_int length; | ||||
u_int8_t *p; | u_int8_t *p; | ||||
if (reseed || atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED)) | if (__predict_false(reseed || | ||||
(arc4rand_iniseed_state == ARC4_ENTR_HAVE && | |||||
atomic_cmpset_int(&arc4rand_iniseed_state, ARC4_ENTR_HAVE, ARC4_ENTR_SEED)))) | |||||
CHACHA20_FOREACH(chacha20) | CHACHA20_FOREACH(chacha20) | ||||
chacha20_randomstir(chacha20); | chacha20_randomstir(chacha20); | ||||
chacha20 = &chacha20inst[curcpu]; | |||||
getmicrouptime(&tv); | getmicrouptime(&tv); | ||||
chacha20 = &chacha20inst[curcpu]; | |||||
/* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */ | /* We may get unlucky and be migrated off this CPU, but that is expected to be infrequent */ | ||||
if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed)) | if ((chacha20->numbytes > CHACHA20_RESEED_BYTES) || (tv.tv_sec > chacha20->t_reseed)) | ||||
chacha20_randomstir(chacha20); | chacha20_randomstir(chacha20); | ||||
mtx_lock(&chacha20->mtx); | |||||
p = ptr; | p = ptr; | ||||
mtx_lock(&chacha20->mtx); | |||||
while (len) { | while (len) { | ||||
length = MIN(CHACHA20_BUFFER_SIZE, len); | length = MIN(CHACHA20_BUFFER_SIZE, len); | ||||
chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length); | chacha_encrypt_bytes(&chacha20->ctx, chacha20->m_buffer, p, length); | ||||
p += length; | p += length; | ||||
len -= length; | len -= length; | ||||
chacha20->numbytes += length; | chacha20->numbytes += length; | ||||
if (chacha20->numbytes > CHACHA20_RESEED_BYTES) { | if (chacha20->numbytes > CHACHA20_RESEED_BYTES) { | ||||
mtx_unlock(&chacha20->mtx); | mtx_unlock(&chacha20->mtx); | ||||
Show All 22 Lines |