2016-05-18 02:52:22 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 19:00:56 +08:00
|
|
|
*
|
2018-12-06 20:52:15 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* 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
|
1998-12-21 19:00:56 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2016-01-05 12:00:33 +08:00
|
|
|
#include <openssl/opensslconf.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/rand.h"
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/engine.h>
|
2017-04-06 17:30:03 +08:00
|
|
|
#include "internal/thread_once.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "rand_local.h"
|
2017-11-17 22:00:35 +08:00
|
|
|
#include "e_os.h"
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2020-05-08 08:25:19 +08:00
|
|
|
# include "prov/rand_pool.h"
|
|
|
|
# include "prov/seeding.h"
|
|
|
|
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
2001-09-26 04:23:40 +08:00
|
|
|
/* non-NULL if default_RAND_meth is ENGINE-provided */
|
2017-06-22 21:21:43 +08:00
|
|
|
static ENGINE *funct_ref;
|
|
|
|
static CRYPTO_RWLOCK *rand_engine_lock;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2017-06-22 21:21:43 +08:00
|
|
|
static CRYPTO_RWLOCK *rand_meth_lock;
|
|
|
|
static const RAND_METHOD *default_RAND_meth;
|
|
|
|
static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
|
2017-09-01 05:16:22 +08:00
|
|
|
|
2018-11-08 04:53:30 +08:00
|
|
|
static int rand_inited = 0;
|
2017-08-03 02:00:52 +08:00
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
DEFINE_RUN_ONCE_STATIC(do_rand_init)
|
2017-04-06 17:30:03 +08:00
|
|
|
{
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
Revert the crypto "global lock" implementation
Conceptually, this is a squashed version of:
Revert "Address feedback"
This reverts commit 75551e07bd2339dfea06ef1d31d69929e13a4495.
and
Revert "Add CRYPTO_thread_glock_new"
This reverts commit ed6b2c7938ec6f07b15745d4183afc276e74c6dd.
But there were some intervening commits that made neither revert apply
cleanly, so instead do it all as one shot.
The crypto global locks were an attempt to cope with the awkward
POSIX semantics for pthread_atfork(); its documentation (the "RATIONALE"
section) indicates that the expected usage is to have the prefork handler
lock all "global" locks, and the parent and child handlers release those
locks, to ensure that forking happens with a consistent (lock) state.
However, the set of functions available in the child process is limited
to async-signal-safe functions, and pthread_mutex_unlock() is not on
the list of async-signal-safe functions! The only synchronization
primitives that are async-signal-safe are the semaphore primitives,
which are not really appropriate for general-purpose usage.
However, the state consistency problem that the global locks were
attempting to solve is not actually a serious problem, particularly for
OpenSSL. That is, we can consider four cases of forking application
that might use OpenSSL:
(1) Single-threaded, does not call into OpenSSL in the child (e.g.,
the child calls exec() immediately)
For this class of process, no locking is needed at all, since there is
only ever a single thread of execution and the only reentrancy is due to
signal handlers (which are themselves limited to async-signal-safe
operation and should not be doing much work at all).
(2) Single-threaded, calls into OpenSSL after fork()
The application must ensure that it does not fork() with an unexpected
lock held (that is, one that would get unlocked in the parent but
accidentally remain locked in the child and cause deadlock). Since
OpenSSL does not expose any of its internal locks to the application
and the application is single-threaded, the OpenSSL internal locks
will be unlocked for the fork(), and the state will be consistent.
(OpenSSL will need to reseed its PRNG in the child, but that is
an orthogonal issue.) If the application makes use of locks from
libcrypto, proper handling for those locks is the responsibility of
the application, as for any other locking primitive that is available
for application programming.
(3) Multi-threaded, does not call into OpenSSL after fork()
As for (1), the OpenSSL state is only relevant in the parent, so
no particular fork()-related handling is needed. The internal locks
are relevant, but there is no interaction with the child to consider.
(4) Multi-threaded, calls into OpenSSL after fork()
This is the case where the pthread_atfork() hooks to ensure that all
global locks are in a known state across fork() would come into play,
per the above discussion. However, these "calls into OpenSSL after
fork()" are still subject to the restriction to async-signal-safe
functions. Since OpenSSL uses all sorts of locking and libc functions
that are not on the list of safe functions (e.g., malloc()), this
case is not currently usable and is unlikely to ever be usable,
independently of the locking situation. So, there is no need to
go through contortions to attempt to support this case in the one small
area of locking interaction with fork().
In light of the above analysis (thanks @davidben and @achernya), go
back to the simpler implementation that does not need to distinguish
"library-global" locks or to have complicated atfork handling for locks.
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/5089)
2018-01-16 23:49:54 +08:00
|
|
|
rand_engine_lock = CRYPTO_THREAD_lock_new();
|
2018-04-29 02:35:54 +08:00
|
|
|
if (rand_engine_lock == NULL)
|
|
|
|
return 0;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2018-04-29 02:35:54 +08:00
|
|
|
|
Revert the crypto "global lock" implementation
Conceptually, this is a squashed version of:
Revert "Address feedback"
This reverts commit 75551e07bd2339dfea06ef1d31d69929e13a4495.
and
Revert "Add CRYPTO_thread_glock_new"
This reverts commit ed6b2c7938ec6f07b15745d4183afc276e74c6dd.
But there were some intervening commits that made neither revert apply
cleanly, so instead do it all as one shot.
The crypto global locks were an attempt to cope with the awkward
POSIX semantics for pthread_atfork(); its documentation (the "RATIONALE"
section) indicates that the expected usage is to have the prefork handler
lock all "global" locks, and the parent and child handlers release those
locks, to ensure that forking happens with a consistent (lock) state.
However, the set of functions available in the child process is limited
to async-signal-safe functions, and pthread_mutex_unlock() is not on
the list of async-signal-safe functions! The only synchronization
primitives that are async-signal-safe are the semaphore primitives,
which are not really appropriate for general-purpose usage.
However, the state consistency problem that the global locks were
attempting to solve is not actually a serious problem, particularly for
OpenSSL. That is, we can consider four cases of forking application
that might use OpenSSL:
(1) Single-threaded, does not call into OpenSSL in the child (e.g.,
the child calls exec() immediately)
For this class of process, no locking is needed at all, since there is
only ever a single thread of execution and the only reentrancy is due to
signal handlers (which are themselves limited to async-signal-safe
operation and should not be doing much work at all).
(2) Single-threaded, calls into OpenSSL after fork()
The application must ensure that it does not fork() with an unexpected
lock held (that is, one that would get unlocked in the parent but
accidentally remain locked in the child and cause deadlock). Since
OpenSSL does not expose any of its internal locks to the application
and the application is single-threaded, the OpenSSL internal locks
will be unlocked for the fork(), and the state will be consistent.
(OpenSSL will need to reseed its PRNG in the child, but that is
an orthogonal issue.) If the application makes use of locks from
libcrypto, proper handling for those locks is the responsibility of
the application, as for any other locking primitive that is available
for application programming.
(3) Multi-threaded, does not call into OpenSSL after fork()
As for (1), the OpenSSL state is only relevant in the parent, so
no particular fork()-related handling is needed. The internal locks
are relevant, but there is no interaction with the child to consider.
(4) Multi-threaded, calls into OpenSSL after fork()
This is the case where the pthread_atfork() hooks to ensure that all
global locks are in a known state across fork() would come into play,
per the above discussion. However, these "calls into OpenSSL after
fork()" are still subject to the restriction to async-signal-safe
functions. Since OpenSSL uses all sorts of locking and libc functions
that are not on the list of safe functions (e.g., malloc()), this
case is not currently usable and is unlikely to ever be usable,
independently of the locking situation. So, there is no need to
go through contortions to attempt to support this case in the one small
area of locking interaction with fork().
In light of the above analysis (thanks @davidben and @achernya), go
back to the simpler implementation that does not need to distinguish
"library-global" locks or to have complicated atfork handling for locks.
Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com>
(Merged from https://github.com/openssl/openssl/pull/5089)
2018-01-16 23:49:54 +08:00
|
|
|
rand_meth_lock = CRYPTO_THREAD_lock_new();
|
2018-04-29 02:35:54 +08:00
|
|
|
if (rand_meth_lock == NULL)
|
2019-05-24 23:36:44 +08:00
|
|
|
goto err;
|
2018-04-10 16:22:52 +08:00
|
|
|
|
2018-11-08 04:53:30 +08:00
|
|
|
if (!rand_pool_init())
|
2019-05-24 23:36:44 +08:00
|
|
|
goto err;
|
2018-06-07 07:31:44 +08:00
|
|
|
|
2018-11-08 04:53:30 +08:00
|
|
|
rand_inited = 1;
|
2018-04-29 02:35:54 +08:00
|
|
|
return 1;
|
|
|
|
|
2019-05-24 23:36:44 +08:00
|
|
|
err:
|
2018-04-29 02:35:54 +08:00
|
|
|
CRYPTO_THREAD_lock_free(rand_meth_lock);
|
|
|
|
rand_meth_lock = NULL;
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
2018-04-29 02:35:54 +08:00
|
|
|
CRYPTO_THREAD_lock_free(rand_engine_lock);
|
|
|
|
rand_engine_lock = NULL;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2018-04-29 02:35:54 +08:00
|
|
|
return 0;
|
2017-04-06 17:30:03 +08:00
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
void rand_cleanup_int(void)
|
|
|
|
{
|
|
|
|
const RAND_METHOD *meth = default_RAND_meth;
|
|
|
|
|
2018-11-08 04:53:30 +08:00
|
|
|
if (!rand_inited)
|
|
|
|
return;
|
2018-08-22 04:51:28 +08:00
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
if (meth != NULL && meth->cleanup != NULL)
|
|
|
|
meth->cleanup();
|
|
|
|
RAND_set_rand_method(NULL);
|
2018-08-22 04:51:28 +08:00
|
|
|
rand_pool_cleanup();
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
2017-06-22 21:21:43 +08:00
|
|
|
CRYPTO_THREAD_lock_free(rand_engine_lock);
|
2018-04-29 02:35:54 +08:00
|
|
|
rand_engine_lock = NULL;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2017-06-22 21:21:43 +08:00
|
|
|
CRYPTO_THREAD_lock_free(rand_meth_lock);
|
2018-04-29 02:35:54 +08:00
|
|
|
rand_meth_lock = NULL;
|
2018-11-08 04:53:30 +08:00
|
|
|
rand_inited = 0;
|
2017-08-03 21:23:28 +08:00
|
|
|
}
|
|
|
|
|
2018-06-07 07:31:44 +08:00
|
|
|
/*
|
2019-07-02 16:04:04 +08:00
|
|
|
* RAND_close_seed_files() ensures that any seed file descriptors are
|
2020-05-08 08:25:19 +08:00
|
|
|
* closed after use. This only applies to libcrypto/default provider,
|
|
|
|
* it does not apply to other providers.
|
2018-06-07 07:31:44 +08:00
|
|
|
*/
|
|
|
|
void RAND_keep_random_devices_open(int keep)
|
|
|
|
{
|
2018-11-08 05:22:01 +08:00
|
|
|
if (RUN_ONCE(&rand_init, do_rand_init))
|
|
|
|
rand_pool_keep_random_devices_open(keep);
|
2018-06-07 07:31:44 +08:00
|
|
|
}
|
|
|
|
|
2017-08-03 21:23:28 +08:00
|
|
|
/*
|
2017-09-01 05:16:22 +08:00
|
|
|
* RAND_poll() reseeds the default RNG using random input
|
|
|
|
*
|
|
|
|
* The random input is obtained from polling various entropy
|
|
|
|
* sources which depend on the operating system and are
|
|
|
|
* configurable via the --with-rand-seed configure option.
|
|
|
|
*/
|
|
|
|
int RAND_poll(void)
|
|
|
|
{
|
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
2020-05-08 08:25:19 +08:00
|
|
|
int ret = meth == RAND_OpenSSL();
|
|
|
|
RAND_POOL *pool;
|
2017-09-01 05:16:22 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
if (meth == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
if (!ret) {
|
2017-09-01 05:16:22 +08:00
|
|
|
/* fill random pool and seed the current legacy RNG */
|
2019-07-20 17:22:46 +08:00
|
|
|
pool = rand_pool_new(RAND_DRBG_STRENGTH, 1,
|
2019-03-22 08:49:57 +08:00
|
|
|
(RAND_DRBG_STRENGTH + 7) / 8,
|
2018-10-10 07:53:29 +08:00
|
|
|
RAND_POOL_MAX_LENGTH);
|
2017-09-01 05:16:22 +08:00
|
|
|
if (pool == NULL)
|
|
|
|
return 0;
|
2020-05-08 08:25:19 +08:00
|
|
|
|
|
|
|
if (prov_pool_acquire_entropy(pool) == 0)
|
2017-09-01 05:16:22 +08:00
|
|
|
goto err;
|
2020-05-08 08:25:19 +08:00
|
|
|
|
2017-09-01 05:16:22 +08:00
|
|
|
if (meth->add == NULL
|
2018-03-06 06:45:44 +08:00
|
|
|
|| meth->add(rand_pool_buffer(pool),
|
|
|
|
rand_pool_length(pool),
|
|
|
|
(rand_pool_entropy(pool) / 8.0)) == 0)
|
2017-09-01 05:16:22 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = 1;
|
2019-05-24 23:36:44 +08:00
|
|
|
err:
|
|
|
|
rand_pool_free(pool);
|
2017-09-01 05:16:22 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-09-26 04:23:40 +08:00
|
|
|
int RAND_set_rand_method(const RAND_METHOD *meth)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-06-22 21:21:43 +08:00
|
|
|
if (!RUN_ONCE(&rand_init, do_rand_init))
|
2017-04-06 17:30:03 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
CRYPTO_THREAD_write_lock(rand_meth_lock);
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
2016-02-26 01:09:06 +08:00
|
|
|
ENGINE_finish(funct_ref);
|
|
|
|
funct_ref = NULL;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2015-01-22 11:40:55 +08:00
|
|
|
default_RAND_meth = meth;
|
2017-04-06 17:30:03 +08:00
|
|
|
CRYPTO_THREAD_unlock(rand_meth_lock);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2001-04-18 12:18:16 +08:00
|
|
|
const RAND_METHOD *RAND_get_rand_method(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-06 17:30:03 +08:00
|
|
|
const RAND_METHOD *tmp_meth = NULL;
|
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
if (!RUN_ONCE(&rand_init, do_rand_init))
|
2017-04-06 17:30:03 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
CRYPTO_THREAD_write_lock(rand_meth_lock);
|
2017-06-22 21:21:43 +08:00
|
|
|
if (default_RAND_meth == NULL) {
|
2019-05-24 23:36:44 +08:00
|
|
|
# ifndef OPENSSL_NO_ENGINE
|
2017-06-22 21:21:43 +08:00
|
|
|
ENGINE *e;
|
|
|
|
|
|
|
|
/* If we have an engine that can do RAND, use it. */
|
|
|
|
if ((e = ENGINE_get_default_RAND()) != NULL
|
|
|
|
&& (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
funct_ref = e;
|
2017-06-22 21:21:43 +08:00
|
|
|
default_RAND_meth = tmp_meth;
|
|
|
|
} else {
|
|
|
|
ENGINE_finish(e);
|
2017-08-03 21:23:28 +08:00
|
|
|
default_RAND_meth = &rand_meth;
|
2017-06-22 21:21:43 +08:00
|
|
|
}
|
2019-05-24 23:36:44 +08:00
|
|
|
# else
|
2017-08-03 21:23:28 +08:00
|
|
|
default_RAND_meth = &rand_meth;
|
2019-05-24 23:36:44 +08:00
|
|
|
# endif
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2017-04-06 17:30:03 +08:00
|
|
|
tmp_meth = default_RAND_meth;
|
|
|
|
CRYPTO_THREAD_unlock(rand_meth_lock);
|
|
|
|
return tmp_meth;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2001-09-26 04:23:40 +08:00
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
# if !defined(OPENSSL_NO_ENGINE)
|
2001-09-26 04:23:40 +08:00
|
|
|
int RAND_set_rand_engine(ENGINE *engine)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
const RAND_METHOD *tmp_meth = NULL;
|
2017-04-06 17:30:03 +08:00
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
if (!RUN_ONCE(&rand_init, do_rand_init))
|
2017-04-06 17:30:03 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
if (engine != NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!ENGINE_init(engine))
|
|
|
|
return 0;
|
|
|
|
tmp_meth = ENGINE_get_RAND(engine);
|
2016-02-26 01:09:06 +08:00
|
|
|
if (tmp_meth == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ENGINE_finish(engine);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 17:30:03 +08:00
|
|
|
CRYPTO_THREAD_write_lock(rand_engine_lock);
|
2015-01-22 11:40:55 +08:00
|
|
|
/* This function releases any prior ENGINE so call it first */
|
|
|
|
RAND_set_rand_method(tmp_meth);
|
|
|
|
funct_ref = engine;
|
2017-04-06 17:30:03 +08:00
|
|
|
CRYPTO_THREAD_unlock(rand_engine_lock);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2020-05-08 08:25:19 +08:00
|
|
|
# endif
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2008-11-12 11:58:08 +08:00
|
|
|
void RAND_seed(const void *buf, int num)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
2017-06-22 21:21:43 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
if (meth != NULL && meth->seed != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
meth->seed(buf, num);
|
|
|
|
}
|
1998-12-21 19:00:56 +08:00
|
|
|
|
2017-06-22 21:21:43 +08:00
|
|
|
void RAND_add(const void *buf, int num, double randomness)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
2017-06-22 21:21:43 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
if (meth != NULL && meth->add != NULL)
|
2017-06-22 21:21:43 +08:00
|
|
|
meth->add(buf, num, randomness);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-01-14 04:59:17 +08:00
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
# if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
|
|
|
|
int RAND_pseudo_bytes(unsigned char *buf, int num)
|
|
|
|
{
|
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
|
|
|
|
|
|
|
if (meth != NULL && meth->pseudorand != NULL)
|
|
|
|
return meth->pseudorand(buf, num);
|
|
|
|
RANDerr(RAND_F_RAND_PSEUDO_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
int RAND_status(void)
|
|
|
|
{
|
|
|
|
RAND_DRBG *drbg;
|
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
|
|
|
|
|
|
|
if (meth != NULL && meth != RAND_OpenSSL())
|
|
|
|
return meth->status != NULL ? meth->status() : 0;
|
|
|
|
|
|
|
|
if ((drbg = RAND_DRBG_get0_master()) == NULL || drbg->rand == NULL)
|
|
|
|
return EVP_RAND_STATE_UNINITIALISED;
|
|
|
|
return EVP_RAND_state(drbg->rand) == EVP_RAND_STATE_READY;
|
|
|
|
}
|
|
|
|
#else /* !FIPS_MODULE */
|
|
|
|
|
|
|
|
const RAND_METHOD *RAND_get_rand_method(void)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* !FIPS_MODULE */
|
|
|
|
|
2017-08-03 02:00:52 +08:00
|
|
|
/*
|
|
|
|
* This function is not part of RAND_METHOD, so if we're not using
|
|
|
|
* the default method, then just call RAND_bytes(). Otherwise make
|
|
|
|
* sure we're instantiated and use the private DRBG.
|
|
|
|
*/
|
2020-01-16 00:34:55 +08:00
|
|
|
int RAND_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
|
2017-08-03 02:00:52 +08:00
|
|
|
{
|
2017-08-27 23:46:33 +08:00
|
|
|
RAND_DRBG *drbg;
|
2019-05-24 23:36:44 +08:00
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
2017-08-03 02:00:52 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
if (meth != NULL && meth != RAND_OpenSSL()) {
|
|
|
|
if (meth->bytes != NULL)
|
|
|
|
return meth->bytes(buf, num);
|
|
|
|
RANDerr(RAND_F_RAND_PRIV_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-03 02:00:52 +08:00
|
|
|
|
2019-06-28 18:23:46 +08:00
|
|
|
drbg = OPENSSL_CTX_get0_private_drbg(ctx);
|
2019-12-12 05:41:00 +08:00
|
|
|
if (drbg != NULL)
|
|
|
|
return RAND_DRBG_bytes(drbg, buf, num);
|
2017-08-03 02:00:52 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
return 0;
|
2017-08-03 02:00:52 +08:00
|
|
|
}
|
|
|
|
|
2019-06-28 18:23:46 +08:00
|
|
|
int RAND_priv_bytes(unsigned char *buf, int num)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-01-16 00:34:55 +08:00
|
|
|
return RAND_priv_bytes_ex(NULL, buf, num);
|
2019-06-28 18:23:46 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 00:34:55 +08:00
|
|
|
int RAND_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
|
2019-06-28 18:23:46 +08:00
|
|
|
{
|
|
|
|
RAND_DRBG *drbg;
|
2015-01-22 11:40:55 +08:00
|
|
|
const RAND_METHOD *meth = RAND_get_rand_method();
|
2017-06-22 21:21:43 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
if (meth != NULL && meth != RAND_OpenSSL()) {
|
2019-06-28 18:23:46 +08:00
|
|
|
if (meth->bytes != NULL)
|
|
|
|
return meth->bytes(buf, num);
|
|
|
|
RANDerr(RAND_F_RAND_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
drbg = OPENSSL_CTX_get0_public_drbg(ctx);
|
2019-12-12 05:41:00 +08:00
|
|
|
if (drbg != NULL)
|
|
|
|
return RAND_DRBG_bytes(drbg, buf, num);
|
2019-06-28 18:23:46 +08:00
|
|
|
|
2019-12-12 05:41:00 +08:00
|
|
|
return 0;
|
2019-06-28 18:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int RAND_bytes(unsigned char *buf, int num)
|
|
|
|
{
|
2020-01-16 00:34:55 +08:00
|
|
|
return RAND_bytes_ex(NULL, buf, num);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|