static void _rs_stir(void) { u_char rnd[KEYSZ + IVSZ]; if (getentropy(rnd, sizeof rnd) == -1) { #ifdef SIGKILL raise(SIGKILL); #else exit(9); /* windows */ #endif } if (!rs) _rs_init(rnd, sizeof(rnd)); else _rs_rekey(rnd, sizeof(rnd)); explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */ /* invalidate rs_buf */ rs->rs_have = 0; memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); rs->rs_count = 1600000; }
static void _rs_random_buf(void *_buf, size_t n) { unsigned char *buf = (unsigned char *)_buf; size_t m; _rs_stir_if_needed(n); while (n > 0) { if (rs_have > 0) { if (n < rs_have) { m = n; } else { m = rs_have; } memcpy(buf, rs_buf + RSBUFSZ - rs_have, m); pure_memzero(rs_buf + RSBUFSZ - rs_have, m); buf += m; n -= m; rs_have -= m; } if (rs_have == 0) { _rs_rekey(NULL, 0); } } }
static void _rs_stir(void) { unsigned char rnd[KEYSZ + IVSZ]; #ifdef WITH_OPENSSL if (RAND_bytes(rnd, sizeof(rnd)) <= 0) fatal("Couldn't obtain random bytes (error %ld)", ERR_get_error()); #else getrnd(rnd, sizeof(rnd)); #endif if (!rs_initialized) { rs_initialized = 1; _rs_init(rnd, sizeof(rnd)); } else _rs_rekey(rnd, sizeof(rnd)); explicit_bzero(rnd, sizeof(rnd)); /* invalidate rs_buf */ rs_have = 0; memset(rs_buf, 0, RSBUFSZ); rs_count = 1600000; }
void arc4random_addrandom(unsigned char *dat, int datlen) { _ARC4_LOCK(); _rs_stir_if_needed(datlen); _rs_rekey(dat, datlen); _ARC4_UNLOCK(); }
static inline void _rs_random_u32(crypto_uint4 *val) { _rs_stir_if_needed(sizeof(*val)); if (rs_have < sizeof(*val)) { _rs_rekey(NULL, 0); } memcpy(val, rs_buf + RSBUFSZ - rs_have, sizeof(*val)); pure_memzero(rs_buf + RSBUFSZ - rs_have, sizeof(*val)); rs_have -= sizeof(*val); }
static inline void _rs_random_u32(uint32_t *val) { _rs_stir_if_needed(sizeof(*val)); if (rs_have < sizeof(*val)) _rs_rekey(NULL, 0); memcpy(val, rs_buf + RSBUFSZ - rs_have, sizeof(*val)); memset(rs_buf + RSBUFSZ - rs_have, 0, sizeof(*val)); rs_have -= sizeof(*val); return; }
static inline void _rs_random_u32(uint32_t *val) { u_char *keystream; _rs_stir_if_needed(sizeof(*val)); if (rs->rs_have < sizeof(*val)) _rs_rekey(NULL, 0); keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have; memcpy(val, keystream, sizeof(*val)); memset(keystream, 0, sizeof(*val)); rs->rs_have -= sizeof(*val); }
static void _rs_stir(void) { unsigned char rnd[KEYSZ + IVSZ]; if (!rs_initialized) { random_data_source_fd = _rs_random_dev_open(); } if (random_data_source_fd != -1) { safe_read(random_data_source_fd, rnd, sizeof rnd); } else { #ifdef HAVE_RANDOM_DEV _exit(1); #else size_t i = (size_t) 0U; # ifdef HAVE_ARC4RANDOM crypto_uint4 r; do { r = arc4random(); memcpy(&rnd[i], &r, (size_t) 4U); i += (size_t) 4U; } while (i < sizeof(rnd)); # elif defined(HAVE_RANDOM) unsigned short r; do { r = (unsigned short) random(); rnd[i++] = r & 0xFF; rnd[i++] = (r << 8) & 0xFF; } while (i < sizeof(rnd)); # else unsigned char r; do { r = (unsigned char) rand(); rnd[i++] = r; } while (i < sizeof(rnd)); # endif #endif } if (!rs_initialized) { rs_initialized = 1; _rs_init(rnd, sizeof rnd); } else { _rs_rekey(rnd, sizeof rnd); } pure_memzero(rnd, sizeof rnd); /* invalidate rs_buf */ rs_have = 0; pure_memzero(rs_buf, RSBUFSZ); rs_count = 1600000; }
void arc4random_addrandom(unsigned char *dat, int datlen) { int m; _ARC4_LOCK(); if (!rs_initialized) _rs_stir(); while (datlen > 0) { m = MIN(datlen, KEYSZ + IVSZ); _rs_rekey(dat, m); dat += m; datlen -= m; } _ARC4_UNLOCK(); }
static void _rs_stir(void) { u_char rnd[KEYSZ + IVSZ]; if (getentropy(rnd, sizeof rnd) == -1) _getentropy_fail(); if (!rs) _rs_init(rnd, sizeof(rnd)); else _rs_rekey(rnd, sizeof(rnd)); explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */ /* invalidate rs_buf */ rs->rs_have = 0; memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); rs->rs_count = 1600000; }
static inline void _rs_random_buf(void *_buf, size_t n) { unsigned char *buf = (unsigned char *)_buf; size_t m; _rs_stir_if_needed(n); while (n > 0) { if (rs_have > 0) { m = MIN(n, rs_have); memcpy(buf, rs_buf + RSBUFSZ - rs_have, m); memset(rs_buf + RSBUFSZ - rs_have, 0, m); buf += m; n -= m; rs_have -= m; } if (rs_have == 0) _rs_rekey(NULL, 0); } }
static inline void _rs_random_buf(void *_buf, size_t n) { u_char *buf = (u_char *)_buf; u_char *keystream; size_t m; _rs_stir_if_needed(n); while (n > 0) { if (rs->rs_have > 0) { m = arc4_min(n, rs->rs_have); keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have; memcpy(buf, keystream, m); memset(keystream, 0, m); buf += m; n -= m; rs->rs_have -= m; } if (rs->rs_have == 0) _rs_rekey(NULL, 0); } }