static int rand_status(void) { CRYPTO_THREAD_ID cur; int ret; int do_not_lock; if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init)) return 0; cur = CRYPTO_THREAD_get_current_id(); /* * check if we already have the lock (could happen if a RAND_poll() * implementation calls RAND_status()) */ if (crypto_lock_rand) { CRYPTO_THREAD_read_lock(rand_tmp_lock); do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur); CRYPTO_THREAD_unlock(rand_tmp_lock); } else do_not_lock = 0; if (!do_not_lock) { CRYPTO_THREAD_write_lock(rand_lock); /* * Prevent deadlocks in case we end up in an async engine */ ASYNC_block_pause(); /* * prevent rand_bytes() from trying to obtain the lock again */ CRYPTO_THREAD_write_lock(rand_tmp_lock); locking_threadid = cur; CRYPTO_THREAD_unlock(rand_tmp_lock); crypto_lock_rand = 1; } if (!initialized) { RAND_poll(); initialized = 1; } ret = entropy >= ENTROPY_NEEDED; if (!do_not_lock) { /* before unlocking, we must clear 'crypto_lock_rand' */ crypto_lock_rand = 0; ASYNC_unblock_pause(); CRYPTO_THREAD_unlock(rand_lock); } return ret; }
int BN_BLINDING_is_current_thread(BN_BLINDING *b) { return CRYPTO_THREAD_compare_id(CRYPTO_THREAD_get_current_id(), b->tid); }
static int rand_add(const void *buf, int num, double add) { int i, j, k, st_idx; long md_c[2]; unsigned char local_md[MD_DIGEST_LENGTH]; EVP_MD_CTX *m; int do_not_lock; int rv = 0; if (!num) return 1; /* * (Based on the rand(3) manpage) * * The input is chopped up into units of 20 bytes (or less for * the last block). Each of these blocks is run through the hash * function as follows: The data passed to the hash function * is the current 'md', the same number of bytes from the 'state' * (the location determined by in incremented looping index) as * the current 'block', the new key data 'block', and 'count' * (which is incremented after each use). * The result of this is kept in 'md' and also xored into the * 'state' at the same locations that were used as input into the * hash function. */ m = EVP_MD_CTX_new(); if (m == NULL) goto err; if (!RUN_ONCE(&rand_lock_init, do_rand_lock_init)) goto err; /* check if we already have the lock */ if (crypto_lock_rand) { CRYPTO_THREAD_ID cur = CRYPTO_THREAD_get_current_id(); CRYPTO_THREAD_read_lock(rand_tmp_lock); do_not_lock = CRYPTO_THREAD_compare_id(locking_threadid, cur); CRYPTO_THREAD_unlock(rand_tmp_lock); } else do_not_lock = 0; if (!do_not_lock) CRYPTO_THREAD_write_lock(rand_lock); st_idx = state_index; /* * use our own copies of the counters so that even if a concurrent thread * seeds with exactly the same data and uses the same subarray there's * _some_ difference */ md_c[0] = md_count[0]; md_c[1] = md_count[1]; memcpy(local_md, md, sizeof md); /* state_index <= state_num <= STATE_SIZE */ state_index += num; if (state_index >= STATE_SIZE) { state_index %= STATE_SIZE; state_num = STATE_SIZE; } else if (state_num < STATE_SIZE) { if (state_index > state_num) state_num = state_index; } /* state_index <= state_num <= STATE_SIZE */ /* * state[st_idx], ..., state[(st_idx + num - 1) % STATE_SIZE] are what we * will use now, but other threads may use them as well */ md_count[1] += (num / MD_DIGEST_LENGTH) + (num % MD_DIGEST_LENGTH > 0); if (!do_not_lock) CRYPTO_THREAD_unlock(rand_lock); for (i = 0; i < num; i += MD_DIGEST_LENGTH) { j = (num - i); j = (j > MD_DIGEST_LENGTH) ? MD_DIGEST_LENGTH : j; if (!MD_Init(m)) goto err; if (!MD_Update(m, local_md, MD_DIGEST_LENGTH)) goto err; k = (st_idx + j) - STATE_SIZE; if (k > 0) { if (!MD_Update(m, &(state[st_idx]), j - k)) goto err; if (!MD_Update(m, &(state[0]), k)) goto err; } else if (!MD_Update(m, &(state[st_idx]), j)) goto err; /* DO NOT REMOVE THE FOLLOWING CALL TO MD_Update()! */ if (!MD_Update(m, buf, j)) goto err; /* * We know that line may cause programs such as purify and valgrind * to complain about use of uninitialized data. The problem is not, * it's with the caller. Removing that line will make sure you get * really bad randomness and thereby other problems such as very * insecure keys. */ if (!MD_Update(m, (unsigned char *)&(md_c[0]), sizeof(md_c))) goto err; if (!MD_Final(m, local_md)) goto err; md_c[1]++; buf = (const char *)buf + j; for (k = 0; k < j; k++) { /* * Parallel threads may interfere with this, but always each byte * of the new state is the XOR of some previous value of its and * local_md (intermediate values may be lost). Alway using locking * could hurt performance more than necessary given that * conflicts occur only when the total seeding is longer than the * random state. */ state[st_idx++] ^= local_md[k]; if (st_idx >= STATE_SIZE) st_idx = 0; } } if (!do_not_lock) CRYPTO_THREAD_write_lock(rand_lock); /* * Don't just copy back local_md into md -- this could mean that other * thread's seeding remains without effect (except for the incremented * counter). By XORing it we keep at least as much entropy as fits into * md. */ for (k = 0; k < (int)sizeof(md); k++) { md[k] ^= local_md[k]; } if (entropy < ENTROPY_NEEDED) /* stop counting when we have enough */ entropy += add; if (!do_not_lock) CRYPTO_THREAD_unlock(rand_lock); rv = 1; err: EVP_MD_CTX_free(m); return rv; }