<commit_before>/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>

#include "e_os.h"

#if !(defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_DSPBIOS))
# include <sys/time.h>
#endif
#if defined(OPENSSL_SYS_VXWORKS)
# include <time.h>
#endif

#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/async.h>
#include "rand_lcl.h"

#include <openssl/err.h>

#include <internal/thread_once.h>

#ifdef OPENSSL_FIPS
# include <openssl/fips.h>
#endif

#if defined(BN_DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
# define PREDICT
#endif

/* #define PREDICT      1 */

#define STATE_SIZE      1023
static size_t state_num = 0, state_index = 0;
static unsigned char state[STATE_SIZE + MD_DIGEST_LENGTH];
static unsigned char md[MD_DIGEST_LENGTH];
static long md_count[2] = { 0, 0 };

static double entropy = 0;
static int initialized = 0;

static CRYPTO_RWLOCK *rand_lock = NULL;
static CRYPTO_RWLOCK *rand_tmp_lock = NULL;
static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;

/* May be set only when a thread holds rand_lock (to prevent double locking) */
static unsigned int crypto_lock_rand = 0;
/* access to locking_threadid is synchronized by rand_tmp_lock */
/* valid iff crypto_lock_rand is set */
static CRYPTO_THREAD_ID locking_threadid;

#ifdef PREDICT
int rand_predictable = 0;
#endif

static int rand_hw_seed(EVP_MD_CTX *ctx);

static void rand_cleanup(void);
static int rand_seed(const void *buf, int num);
static int rand_add(const void *buf, int num, double add_entropy);
static int rand_bytes(unsigned char *buf, int num, int pseudo);
static int rand_nopseudo_bytes(unsigned char *buf, int num);
#if OPENSSL_API_COMPAT < 0x10100000L
static int rand_pseudo_bytes(unsigned char *buf, int num);
#endif
static int rand_status(void);

static RAND_METHOD rand_meth = {
    rand_seed,
    rand_nopseudo_bytes,
    rand_cleanup,
    rand_add,
#if OPENSSL_API_COMPAT < 0x10100000L
    rand_pseudo_bytes,
#else
    NULL,
#endif
    rand_status
};

DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
{
    OPENSSL_init_crypto(0, NULL);
    rand_lock = CRYPTO_THREAD_lock_new();
    rand_tmp_lock = CRYPTO_THREAD_lock_new();
    return rand_lock != NULL && rand_tmp_lock != NULL;
}

RAND_METHOD *RAND_OpenSSL(void)
{
    return (&rand_meth);
}

static void rand_cleanup(void)
{
    OPENSSL_cleanse(state, sizeof(state));
    state_num = 0;
    state_index = 0;
    OPENSSL_cleanse(md, MD_DIGEST_LENGTH);
    md_count[0] = 0;
    md_count[1] = 0;
    entropy = 0;
    initialized = 0;
    CRYPTO_THREAD_lock_free(rand_lock);
    CRYPTO_THREAD_lock_free(rand_tmp_lock);
}

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);<commit_msg>Make rand_add predictable when fuzzing<commit_after>/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>

#include "e_os.h"

#if !(defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_DSPBIOS))
# include <sys/time.h>
#endif
#if defined(OPENSSL_SYS_VXWORKS)
# include <time.h>
#endif

#include <openssl/opensslconf.h>
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/async.h>
#include "rand_lcl.h"

#include <openssl/err.h>

#include <internal/thread_once.h>

#ifdef OPENSSL_FIPS
# include <openssl/fips.h>
#endif

#if defined(BN_DEBUG) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
# define PREDICT
#endif

/* #define PREDICT      1 */

#define STATE_SIZE      1023
static size_t state_num = 0, state_index = 0;
static unsigned char state[STATE_SIZE + MD_DIGEST_LENGTH];
static unsigned char md[MD_DIGEST_LENGTH];
static long md_count[2] = { 0, 0 };

static double entropy = 0;
static int initialized = 0;

static CRYPTO_RWLOCK *rand_lock = NULL;
static CRYPTO_RWLOCK *rand_tmp_lock = NULL;
static CRYPTO_ONCE rand_lock_init = CRYPTO_ONCE_STATIC_INIT;

/* May be set only when a thread holds rand_lock (to prevent double locking) */
static unsigned int crypto_lock_rand = 0;
/* access to locking_threadid is synchronized by rand_tmp_lock */
/* valid iff crypto_lock_rand is set */
static CRYPTO_THREAD_ID locking_threadid;

#ifdef PREDICT
int rand_predictable = 0;
#endif

static int rand_hw_seed(EVP_MD_CTX *ctx);

static void rand_cleanup(void);
static int rand_seed(const void *buf, int num);
static int rand_add(const void *buf, int num, double add_entropy);
static int rand_bytes(unsigned char *buf, int num, int pseudo);
static int rand_nopseudo_bytes(unsigned char *buf, int num);
#if OPENSSL_API_COMPAT < 0x10100000L
static int rand_pseudo_bytes(unsigned char *buf, int num);
#endif
static int rand_status(void);

static RAND_METHOD rand_meth = {
    rand_seed,
    rand_nopseudo_bytes,
    rand_cleanup,
    rand_add,
#if OPENSSL_API_COMPAT < 0x10100000L
    rand_pseudo_bytes,
#else
    NULL,
#endif
    rand_status
};

DEFINE_RUN_ONCE_STATIC(do_rand_lock_init)
{
    OPENSSL_init_crypto(0, NULL);
    rand_lock = CRYPTO_THREAD_lock_new();
    rand_tmp_lock = CRYPTO_THREAD_lock_new();
    return rand_lock != NULL && rand_tmp_lock != NULL;
}

RAND_METHOD *RAND_OpenSSL(void)
{
    return (&rand_meth);
}

static void rand_cleanup(void)
{
    OPENSSL_cleanse(state, sizeof(state));
    state_num = 0;
    state_index = 0;
    OPENSSL_cleanse(md, MD_DIGEST_LENGTH);
    md_count[0] = 0;
    md_count[1] = 0;
    entropy = 0;
    initialized = 0;
    CRYPTO_THREAD_lock_free(rand_lock);
    CRYPTO_THREAD_lock_free(rand_tmp_lock);
}

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;

#ifdef PREDICT
    if (rand_predictable)
        return 1;
#endif

    /*
     * (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);
{'fraction_non_alphanumeric': 0.09466417037210391, 'fraction_numerical': 0.01345658787736953, 'mean_word_length': 3.9148936170212765, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3670705', 'n_tokens_mistral': 3112, 'n_tokens_neox': 2944, 'n_words': 884}