static void thread_local_test_thread(void) { void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST); if (ptr != NULL) { return; } if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_TEST, &g_destructor_called_count, thread_local_destructor)) { return; } if (CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST) != &g_destructor_called_count) { return; } g_test_thread_ok = 1; }
static struct rand_buffer *get_thread_local_buffer(void) { struct rand_buffer *buf = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF); if (buf != NULL) { return buf; } buf = OPENSSL_malloc(sizeof(struct rand_buffer)); if (buf == NULL) { return NULL; } buf->used = BUF_SIZE; /* To trigger a |fill_with_entropy| on first use. */ if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF, buf, OPENSSL_free)) { OPENSSL_free(buf); return NULL; } return buf; }
static int test_thread_local(void) { void *ptr = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_TEST); if (ptr != NULL) { fprintf(stderr, "Thread-local data was non-NULL at start.\n"); } thread_t thread; if (!run_thread(&thread, thread_local_test_thread) || !wait_for_thread(thread)) { fprintf(stderr, "thread failed.\n"); return 0; } if (!g_test_thread_ok) { fprintf(stderr, "Thread-local data didn't work in thread.\n"); return 0; } if (g_destructor_called_count != 1) { fprintf(stderr, "Destructor should have been called once, but actually called %u " "times.\n", g_destructor_called_count); return 0; } /* thread_local_test2_thread doesn't do anything, but it tests that the * thread destructor function works even if thread-local storage wasn't used * for a thread. */ if (!run_thread(&thread, thread_local_test2_thread) || !wait_for_thread(thread)) { fprintf(stderr, "thread failed.\n"); return 0; } return 1; }
int RAND_bytes(uint8_t *buf, size_t len) { if (len == 0) { return 1; } if (!CRYPTO_have_hwrand() || !CRYPTO_hwrand(buf, len)) { /* Without a hardware RNG to save us from address-space duplication, the OS * entropy is used directly. */ CRYPTO_sysrand(buf, len); return 1; } struct rand_thread_state *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND); if (state == NULL) { state = OPENSSL_malloc(sizeof(struct rand_thread_state)); if (state == NULL || !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state, rand_thread_state_free)) { CRYPTO_sysrand(buf, len); return 1; } memset(state->partial_block, 0, sizeof(state->partial_block)); state->calls_used = kMaxCallsPerRefresh; } if (state->calls_used >= kMaxCallsPerRefresh || state->bytes_used >= kMaxBytesPerRefresh) { CRYPTO_sysrand(state->key, sizeof(state->key)); state->calls_used = 0; state->bytes_used = 0; state->partial_block_used = sizeof(state->partial_block); } if (len >= sizeof(state->partial_block)) { size_t remaining = len; while (remaining > 0) { // kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this // is sufficient and easier on 32-bit. static const size_t kMaxBytesPerCall = 0x80000000; size_t todo = remaining; if (todo > kMaxBytesPerCall) { todo = kMaxBytesPerCall; } CRYPTO_chacha_20(buf, buf, todo, state->key, (uint8_t *)&state->calls_used, 0); buf += todo; remaining -= todo; state->calls_used++; } } else { if (sizeof(state->partial_block) - state->partial_block_used < len) { CRYPTO_chacha_20(state->partial_block, state->partial_block, sizeof(state->partial_block), state->key, (uint8_t *)&state->calls_used, 0); state->partial_block_used = 0; } unsigned i; for (i = 0; i < len; i++) { buf[i] ^= state->partial_block[state->partial_block_used++]; } state->calls_used++; } state->bytes_used += len; return 1; }