Latest update.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=\
|
||||
randfile.c rand_lib.c rand_err.c rand_crng_test.c rand_egd.c \
|
||||
rand_win.c rand_unix.c rand_vms.c drbg_lib.c drbg_ctr.c \
|
||||
rand_win.c rand_unix.c rand_vms.c drbg_lib.c drbg_ctr.c rand_vxworks.c \
|
||||
drbg_hash.c drbg_hmac.c
|
||||
|
||||
+12
-2
@@ -1101,6 +1101,17 @@ static int drbg_add(const void *buf, int num, double randomness)
|
||||
|
||||
buflen = (size_t)num;
|
||||
|
||||
#ifdef FIPS_MODE
|
||||
/*
|
||||
* NIST SP-800-90A mandates that entropy *shall not* be provided
|
||||
* by the consuming application. By setting the randomness to zero,
|
||||
* we ensure that the buffer contents will be added to the internal
|
||||
* state of the DRBG only as additional data.
|
||||
*
|
||||
* (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
|
||||
*/
|
||||
randomness = 0.0;
|
||||
#endif
|
||||
if (buflen < seedlen || randomness < (double) seedlen) {
|
||||
#if defined(OPENSSL_RAND_SEED_NONE)
|
||||
/*
|
||||
@@ -1117,7 +1128,7 @@ static int drbg_add(const void *buf, int num, double randomness)
|
||||
return ret;
|
||||
#else
|
||||
/*
|
||||
* If an os entropy source is avaible then we declare the buffer content
|
||||
* If an os entropy source is available then we declare the buffer content
|
||||
* as additional data by setting randomness to zero and trigger a regular
|
||||
* reseeding.
|
||||
*/
|
||||
@@ -1125,7 +1136,6 @@ static int drbg_add(const void *buf, int num, double randomness)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
if (randomness > (double)seedlen) {
|
||||
/*
|
||||
* The purpose of this check is to bound |randomness| by a
|
||||
|
||||
@@ -13,45 +13,52 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include "internal/rand_int.h"
|
||||
#include "internal/thread_once.h"
|
||||
#include "rand_lcl.h"
|
||||
|
||||
static RAND_POOL *crngt_pool;
|
||||
static unsigned char *crngt_prev;
|
||||
static unsigned char crngt_prev[EVP_MAX_MD_SIZE];
|
||||
|
||||
int (*crngt_get_entropy)(unsigned char *) = &rand_crngt_get_entropy_cb;
|
||||
int (*crngt_get_entropy)(unsigned char *, unsigned char *, unsigned int *)
|
||||
= &rand_crngt_get_entropy_cb;
|
||||
|
||||
int rand_crngt_get_entropy_cb(unsigned char *buf)
|
||||
int rand_crngt_get_entropy_cb(unsigned char *buf, unsigned char *md,
|
||||
unsigned int *md_size)
|
||||
{
|
||||
int r;
|
||||
size_t n;
|
||||
unsigned char *p;
|
||||
|
||||
while ((n = rand_pool_acquire_entropy(crngt_pool)) != 0)
|
||||
if (n >= CRNGT_BUFSIZ) {
|
||||
p = rand_pool_detach(crngt_pool);
|
||||
n = rand_pool_acquire_entropy(crngt_pool);
|
||||
if (n >= CRNGT_BUFSIZ) {
|
||||
p = rand_pool_detach(crngt_pool);
|
||||
r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
|
||||
if (r != 0)
|
||||
memcpy(buf, p, CRNGT_BUFSIZ);
|
||||
rand_pool_reattach(crngt_pool, p);
|
||||
return 1;
|
||||
}
|
||||
rand_pool_reattach(crngt_pool, p);
|
||||
return r;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void rand_crngt_cleanup(void)
|
||||
{
|
||||
rand_pool_free(crngt_pool);
|
||||
OPENSSL_secure_free(crngt_prev);
|
||||
crngt_pool = NULL;
|
||||
crngt_prev = NULL;
|
||||
}
|
||||
|
||||
int rand_crngt_init(void)
|
||||
{
|
||||
unsigned char buf[CRNGT_BUFSIZ];
|
||||
|
||||
if ((crngt_pool = rand_pool_new(0, CRNGT_BUFSIZ, CRNGT_BUFSIZ)) == NULL)
|
||||
return 0;
|
||||
if ((crngt_prev = OPENSSL_secure_malloc(CRNGT_BUFSIZ)) != NULL
|
||||
&& crngt_get_entropy(crngt_prev))
|
||||
if (crngt_get_entropy(buf, crngt_prev, NULL)) {
|
||||
OPENSSL_cleanse(buf, sizeof(buf));
|
||||
return 1;
|
||||
}
|
||||
rand_crngt_cleanup();
|
||||
return 0;
|
||||
}
|
||||
@@ -74,7 +81,8 @@ size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
|
||||
int entropy, size_t min_len, size_t max_len,
|
||||
int prediction_resistance)
|
||||
{
|
||||
unsigned char buf[CRNGT_BUFSIZ];
|
||||
unsigned char buf[CRNGT_BUFSIZ], md[EVP_MAX_MD_SIZE];
|
||||
unsigned int sz;
|
||||
RAND_POOL *pool;
|
||||
size_t q, r = 0, s, t = 0;
|
||||
int attempts = 3;
|
||||
@@ -87,17 +95,18 @@ size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
|
||||
|
||||
while ((q = rand_pool_bytes_needed(pool, 1)) > 0 && attempts-- > 0) {
|
||||
s = q > sizeof(buf) ? sizeof(buf) : q;
|
||||
if (!crngt_get_entropy(buf)
|
||||
|| memcmp(crngt_prev, buf, CRNGT_BUFSIZ) == 0
|
||||
if (!crngt_get_entropy(buf, md, &sz)
|
||||
|| memcmp(crngt_prev, md, sz) == 0
|
||||
|| !rand_pool_add(pool, buf, s, s * 8))
|
||||
goto err;
|
||||
memcpy(crngt_prev, buf, CRNGT_BUFSIZ);
|
||||
memcpy(crngt_prev, md, sz);
|
||||
t += s;
|
||||
attempts++;
|
||||
}
|
||||
r = t;
|
||||
*pout = rand_pool_detach(pool);
|
||||
err:
|
||||
OPENSSL_cleanse(buf, sizeof(buf));
|
||||
rand_pool_free(pool);
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -334,8 +334,10 @@ int drbg_hmac_init(RAND_DRBG *drbg);
|
||||
* Entropy call back for the FIPS 140-2 section 4.9.2 Conditional Tests.
|
||||
* These need to be exposed for the unit tests.
|
||||
*/
|
||||
int rand_crngt_get_entropy_cb(unsigned char *buf);
|
||||
extern int (*crngt_get_entropy)(unsigned char *);
|
||||
int rand_crngt_get_entropy_cb(unsigned char *buf, unsigned char *md,
|
||||
unsigned int *md_size);
|
||||
extern int (*crngt_get_entropy)(unsigned char *buf, unsigned char *md,
|
||||
unsigned int *md_size);
|
||||
int rand_crngt_init(void);
|
||||
void rand_crngt_cleanup(void);
|
||||
|
||||
|
||||
+6
-26
@@ -30,7 +30,8 @@
|
||||
# include <sys/param.h>
|
||||
#endif
|
||||
|
||||
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
|
||||
#if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
|
||||
|| defined(__DJGPP__)
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
@@ -88,30 +89,8 @@ static uint64_t get_timer_bits(void);
|
||||
# undef OPENSSL_RAND_SEED_EGD
|
||||
#endif
|
||||
|
||||
#if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
|
||||
!defined(OPENSSL_RAND_SEED_NONE)
|
||||
# error "UEFI and VXWorks only support seeding NONE"
|
||||
#endif
|
||||
|
||||
#if defined(OPENSSL_SYS_VXWORKS)
|
||||
/* empty implementation */
|
||||
int rand_pool_init(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rand_pool_cleanup(void)
|
||||
{
|
||||
}
|
||||
|
||||
void rand_pool_keep_random_devices_open(int keep)
|
||||
{
|
||||
}
|
||||
|
||||
size_t rand_pool_acquire_entropy(RAND_POOL *pool)
|
||||
{
|
||||
return rand_pool_entropy_available(pool);
|
||||
}
|
||||
#if defined(OPENSSL_SYS_UEFI) && !defined(OPENSSL_RAND_SEED_NONE)
|
||||
# error "UEFI only supports seeding NONE"
|
||||
#endif
|
||||
|
||||
#if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) \
|
||||
@@ -608,7 +587,8 @@ size_t rand_pool_acquire_entropy(RAND_POOL *pool)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
|
||||
#if (defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_SYS_VXWORKS)) \
|
||||
|| defined(__DJGPP__)
|
||||
int rand_pool_add_nonce_data(RAND_POOL *pool)
|
||||
{
|
||||
struct {
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright 2019 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 <openssl/opensslconf.h>
|
||||
|
||||
#ifndef OPENSSL_SYS_VXWORKS
|
||||
NON_EMPTY_TRANSLATION_UNIT
|
||||
#else
|
||||
# include <openssl/rand.h>
|
||||
# include "rand_lcl.h"
|
||||
# include "internal/rand_int.h"
|
||||
# include "internal/cryptlib.h"
|
||||
# include <version.h>
|
||||
# include <taskLib.h>
|
||||
|
||||
# if defined(OPENSSL_RAND_SEED_NONE)
|
||||
/* none means none */
|
||||
# undef OPENSSL_RAND_SEED_OS
|
||||
# endif
|
||||
|
||||
# if defined(OPENSSL_RAND_SEED_OS)
|
||||
# if _WRS_VXWORKS_MAJOR >= 7
|
||||
# define RAND_SEED_VXRANDLIB
|
||||
# else
|
||||
# error "VxWorks <7 only support RAND_SEED_NONE"
|
||||
# endif
|
||||
# endif
|
||||
|
||||
# if defined(RAND_SEED_VXRANDLIB)
|
||||
# include <randomNumGen.h>
|
||||
# endif
|
||||
|
||||
/* Macro to convert two thirty two bit values into a sixty four bit one */
|
||||
# define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
|
||||
|
||||
static uint64_t get_time_stamp(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
|
||||
return TWO32TO64(ts.tv_sec, ts.tv_nsec);
|
||||
return time(NULL);
|
||||
}
|
||||
|
||||
static uint64_t get_timer_bits(void)
|
||||
{
|
||||
uint64_t res = OPENSSL_rdtsc();
|
||||
struct timespec ts;
|
||||
|
||||
if (res != 0)
|
||||
return res;
|
||||
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
|
||||
return TWO32TO64(ts.tv_sec, ts.tv_nsec);
|
||||
return time(NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* empty implementation
|
||||
* vxworks does not need to init/cleanup or keep open the random lib
|
||||
*/
|
||||
int rand_pool_init(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
void rand_pool_cleanup(void)
|
||||
{
|
||||
}
|
||||
|
||||
void rand_pool_keep_random_devices_open(int keep)
|
||||
{
|
||||
}
|
||||
|
||||
int rand_pool_add_additional_data(RAND_POOL *pool)
|
||||
{
|
||||
struct {
|
||||
CRYPTO_THREAD_ID tid;
|
||||
uint64_t time;
|
||||
} data;
|
||||
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
/*
|
||||
* Add some noise from the thread id and a high resolution timer.
|
||||
* The thread id adds a little randomness if the drbg is accessed
|
||||
* concurrently (which is the case for the <master> drbg).
|
||||
*/
|
||||
data.tid = CRYPTO_THREAD_get_current_id();
|
||||
data.time = get_timer_bits();
|
||||
|
||||
return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
|
||||
}
|
||||
|
||||
int rand_pool_add_nonce_data(RAND_POOL *pool)
|
||||
{
|
||||
struct {
|
||||
pid_t pid;
|
||||
CRYPTO_THREAD_ID tid;
|
||||
uint64_t time;
|
||||
} data;
|
||||
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
/*
|
||||
* Add process id, thread id, and a high resolution timestamp to
|
||||
* ensure that the nonce is unique with high probability for
|
||||
* different process instances.
|
||||
*/
|
||||
data.pid = getpid();
|
||||
data.tid = CRYPTO_THREAD_get_current_id();
|
||||
data.time = get_time_stamp();
|
||||
|
||||
return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
|
||||
}
|
||||
|
||||
size_t rand_pool_acquire_entropy(RAND_POOL *pool)
|
||||
{
|
||||
# if defined(RAND_SEED_VXRANDLIB)
|
||||
/* vxRandLib based entropy method */
|
||||
size_t bytes_needed;
|
||||
|
||||
bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
|
||||
if (bytes_needed > 0)
|
||||
{
|
||||
int retryCount = 0;
|
||||
STATUS result = ERROR;
|
||||
unsigned char *buffer;
|
||||
|
||||
buffer = rand_pool_add_begin(pool, bytes_needed);
|
||||
while ((result != OK) && (retryCount < 10)) {
|
||||
RANDOM_NUM_GEN_STATUS status = randStatus();
|
||||
|
||||
if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
|
||||
|| (status == RANDOM_NUM_GEN_MAX_ENTROPY) ) {
|
||||
result = randBytes(buffer, bytes_needed);
|
||||
if (result == OK)
|
||||
rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
|
||||
/*
|
||||
* no else here: randStatus said ok, if randBytes failed
|
||||
* it will result in another loop or no entropy
|
||||
*/
|
||||
} else {
|
||||
/*
|
||||
* give a minimum delay here to allow OS to collect more
|
||||
* entropy. taskDelay duration will depend on the system tick,
|
||||
* this is by design as the sw-random lib uses interrupts
|
||||
* which will at least happen during ticks
|
||||
*/
|
||||
taskDelay(5);
|
||||
}
|
||||
retryCount++;
|
||||
}
|
||||
}
|
||||
return rand_pool_entropy_available(pool);
|
||||
# else
|
||||
/*
|
||||
* SEED_NONE means none, without randlib we dont have entropy and
|
||||
* rely on it being added externally
|
||||
*/
|
||||
return rand_pool_entropy_available(pool);
|
||||
# endif /* defined(RAND_SEED_VXRANDLIB) */
|
||||
}
|
||||
|
||||
#endif /* OPENSSL_SYS_VXWORKS */
|
||||
Reference in New Issue
Block a user