Commit f61f62ea authored by Dr. Matthias St. Pierre's avatar Dr. Matthias St. Pierre
Browse files

Use RAND_DRBG_bytes() for RAND_bytes() and RAND_priv_bytes()



The functions RAND_bytes() and RAND_priv_bytes() are now both based
on a common implementation using RAND_DRBG_bytes() (if the default
OpenSSL rand method is active). This not only simplifies the code
but also has the advantage that additional input from a high precision
timer is added on every generate call if the timer is available.

Reviewed-by: default avatarKurt Roeckx <kurt@roeckx.be>
(Merged from https://github.com/openssl/openssl/pull/5251)
parent 1648338b
Loading
Loading
Loading
Loading
+3 −13
Original line number Diff line number Diff line
@@ -776,26 +776,16 @@ void rand_drbg_cleanup_int(void)
/* Implements the default OpenSSL RAND_bytes() method */
static int drbg_bytes(unsigned char *out, int count)
{
    int ret = 0;
    size_t chunk;
    int ret;
    RAND_DRBG *drbg = RAND_DRBG_get0_public();

    if (drbg == NULL)
        return 0;

    CRYPTO_THREAD_write_lock(drbg->lock);
    for ( ; count > 0; count -= chunk, out += chunk) {
        chunk = count;
        if (chunk > drbg->max_request)
            chunk = drbg->max_request;
        ret = RAND_DRBG_generate(drbg, out, chunk, 0, NULL, 0);
        if (!ret)
            goto err;
    }
    ret = 1;

err:
    ret = RAND_DRBG_bytes(drbg, out, count);
    CRYPTO_THREAD_unlock(drbg->lock);

    return ret;
}

+2 −2
Original line number Diff line number Diff line
/*
 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
 * Copyright 1995-2018 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
@@ -719,7 +719,7 @@ int RAND_priv_bytes(unsigned char *buf, int num)

    /* We have to lock the DRBG before generating bits from it. */
    CRYPTO_THREAD_write_lock(drbg->lock);
    ret = RAND_DRBG_generate(drbg, buf, num, 0, NULL, 0);
    ret = RAND_DRBG_bytes(drbg, buf, num);
    CRYPTO_THREAD_unlock(drbg->lock);
    return ret;
}