|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include <openssl/rand.h>
|
|
Packit Service |
084de1 |
#include "rand_local.h"
|
|
Packit Service |
084de1 |
#include "crypto/rand.h"
|
|
Packit Service |
084de1 |
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifndef OPENSSL_RAND_SEED_OS
|
|
Packit Service |
084de1 |
# error "Unsupported seeding method configured; must be os"
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# include <windows.h>
|
|
Packit Service |
084de1 |
/* On Windows Vista or higher use BCrypt instead of the legacy CryptoAPI */
|
|
Packit Service |
084de1 |
# if defined(_MSC_VER) && _MSC_VER > 1500 /* 1500 = Visual Studio 2008 */ \
|
|
Packit Service |
084de1 |
&& defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
|
|
Packit Service |
084de1 |
# define USE_BCRYPTGENRANDOM
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifdef USE_BCRYPTGENRANDOM
|
|
Packit Service |
084de1 |
# include <bcrypt.h>
|
|
Packit Service |
084de1 |
# pragma comment(lib, "bcrypt.lib")
|
|
Packit Service |
084de1 |
# ifndef STATUS_SUCCESS
|
|
Packit Service |
084de1 |
# define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
# else
|
|
Packit Service |
084de1 |
# include <wincrypt.h>
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Intel hardware RNG CSP -- available from
|
|
Packit Service |
084de1 |
* http://developer.intel.com/design/security/rng/redist_license.htm
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
# define PROV_INTEL_SEC 22
|
|
Packit Service |
084de1 |
# define INTEL_DEF_PROV L"Intel Hardware Cryptographic Service Provider"
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
size_t rand_pool_acquire_entropy(RAND_POOL *pool)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
# ifndef USE_BCRYPTGENRANDOM
|
|
Packit Service |
084de1 |
HCRYPTPROV hProvider;
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
unsigned char *buffer;
|
|
Packit Service |
084de1 |
size_t bytes_needed;
|
|
Packit Service |
084de1 |
size_t entropy_available = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifdef OPENSSL_RAND_SEED_RDTSC
|
|
Packit Service |
084de1 |
entropy_available = rand_acquire_entropy_from_tsc(pool);
|
|
Packit Service |
084de1 |
if (entropy_available > 0)
|
|
Packit Service |
084de1 |
return entropy_available;
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifdef OPENSSL_RAND_SEED_RDCPU
|
|
Packit Service |
084de1 |
entropy_available = rand_acquire_entropy_from_cpu(pool);
|
|
Packit Service |
084de1 |
if (entropy_available > 0)
|
|
Packit Service |
084de1 |
return entropy_available;
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# ifdef USE_BCRYPTGENRANDOM
|
|
Packit Service |
084de1 |
bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
|
|
Packit Service |
084de1 |
buffer = rand_pool_add_begin(pool, bytes_needed);
|
|
Packit Service |
084de1 |
if (buffer != NULL) {
|
|
Packit Service |
084de1 |
size_t bytes = 0;
|
|
Packit Service |
084de1 |
if (BCryptGenRandom(NULL, buffer, bytes_needed,
|
|
Packit Service |
084de1 |
BCRYPT_USE_SYSTEM_PREFERRED_RNG) == STATUS_SUCCESS)
|
|
Packit Service |
084de1 |
bytes = bytes_needed;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
rand_pool_add_end(pool, bytes, 8 * bytes);
|
|
Packit Service |
084de1 |
entropy_available = rand_pool_entropy_available(pool);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (entropy_available > 0)
|
|
Packit Service |
084de1 |
return entropy_available;
|
|
Packit Service |
084de1 |
# else
|
|
Packit Service |
084de1 |
bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
|
|
Packit Service |
084de1 |
buffer = rand_pool_add_begin(pool, bytes_needed);
|
|
Packit Service |
084de1 |
if (buffer != NULL) {
|
|
Packit Service |
084de1 |
size_t bytes = 0;
|
|
Packit Service |
084de1 |
/* poll the CryptoAPI PRNG */
|
|
Packit Service |
084de1 |
if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL,
|
|
Packit Service |
084de1 |
CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
|
|
Packit Service |
084de1 |
if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
|
|
Packit Service |
084de1 |
bytes = bytes_needed;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CryptReleaseContext(hProvider, 0);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
rand_pool_add_end(pool, bytes, 8 * bytes);
|
|
Packit Service |
084de1 |
entropy_available = rand_pool_entropy_available(pool);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (entropy_available > 0)
|
|
Packit Service |
084de1 |
return entropy_available;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
|
|
Packit Service |
084de1 |
buffer = rand_pool_add_begin(pool, bytes_needed);
|
|
Packit Service |
084de1 |
if (buffer != NULL) {
|
|
Packit Service |
084de1 |
size_t bytes = 0;
|
|
Packit Service |
084de1 |
/* poll the Pentium PRG with CryptoAPI */
|
|
Packit Service |
084de1 |
if (CryptAcquireContextW(&hProvider, NULL,
|
|
Packit Service |
084de1 |
INTEL_DEF_PROV, PROV_INTEL_SEC,
|
|
Packit Service |
084de1 |
CRYPT_VERIFYCONTEXT | CRYPT_SILENT) != 0) {
|
|
Packit Service |
084de1 |
if (CryptGenRandom(hProvider, bytes_needed, buffer) != 0)
|
|
Packit Service |
084de1 |
bytes = bytes_needed;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
CryptReleaseContext(hProvider, 0);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
rand_pool_add_end(pool, bytes, 8 * bytes);
|
|
Packit Service |
084de1 |
entropy_available = rand_pool_entropy_available(pool);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (entropy_available > 0)
|
|
Packit Service |
084de1 |
return entropy_available;
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return rand_pool_entropy_available(pool);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int rand_pool_add_nonce_data(RAND_POOL *pool)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
struct {
|
|
Packit Service |
084de1 |
DWORD pid;
|
|
Packit Service |
084de1 |
DWORD tid;
|
|
Packit Service |
084de1 |
FILETIME time;
|
|
Packit Service |
084de1 |
} data = { 0 };
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Add process id, thread id, and a high resolution timestamp to
|
|
Packit Service |
084de1 |
* ensure that the nonce is unique with high probability for
|
|
Packit Service |
084de1 |
* different process instances.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
data.pid = GetCurrentProcessId();
|
|
Packit Service |
084de1 |
data.tid = GetCurrentThreadId();
|
|
Packit Service |
084de1 |
GetSystemTimeAsFileTime(&data.time);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int rand_pool_add_additional_data(RAND_POOL *pool)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
struct {
|
|
Packit Service |
084de1 |
DWORD tid;
|
|
Packit Service |
084de1 |
LARGE_INTEGER time;
|
|
Packit Service |
084de1 |
} data = { 0 };
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Add some noise from the thread id and a high resolution timer.
|
|
Packit Service |
084de1 |
* The thread id adds a little randomness if the drbg is accessed
|
|
Packit Service |
084de1 |
* concurrently (which is the case for the <master> drbg).
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
data.tid = GetCurrentThreadId();
|
|
Packit Service |
084de1 |
QueryPerformanceCounter(&data.time);
|
|
Packit Service |
084de1 |
return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
# if OPENSSL_API_COMPAT < 0x10100000L
|
|
Packit Service |
084de1 |
int RAND_event(UINT iMsg, WPARAM wParam, LPARAM lParam)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
RAND_poll();
|
|
Packit Service |
084de1 |
return RAND_status();
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void RAND_screen(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
RAND_poll();
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
# endif
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int rand_pool_init(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void rand_pool_cleanup(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void rand_pool_keep_random_devices_open(int keep)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#endif
|