

#include "internal/cryptlib.h"
#include <openssl/opensslconf.h>
#include "crypto/rand_pool.h"
#include "prov/seeding.h"

#ifdef OPENSSL_RAND_SEED_RDCPU
# if defined(OPENSSL_SYS_TANDEM) && defined(_TNS_X_TARGET)
#  include <builtin.h> 
#  include <string.h> 
# else
size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
# endif

static size_t get_hardware_random_value(unsigned char *buf, size_t len);


size_t ossl_prov_acquire_entropy_from_cpu(RAND_POOL *pool)
{
    size_t bytes_needed;
    unsigned char *buffer;

    bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 );
    if (bytes_needed > 0) {
        buffer = ossl_rand_pool_add_begin(pool, bytes_needed);

        if (buffer != NULL) {
            if (get_hardware_random_value(buffer, bytes_needed) == bytes_needed) {
                ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
            } else {
                ossl_rand_pool_add_end(pool, 0, 0);
            }
        }
    }

    return ossl_rand_pool_entropy_available(pool);
}

#if defined(OPENSSL_SYS_TANDEM) && defined(_TNS_X_TARGET)

static size_t get_hardware_random_value(unsigned char *buf, size_t len)
{
    size_t bytes_remaining = len;

    while (bytes_remaining > 0) {
        
        
        uint64_t random_value = 0;

        if (_rdrand64(&random_value) != 0) {
            unsigned char *random_buffer = (unsigned char *)&random_value;

            if (bytes_remaining >= sizeof(random_value)) {
                memcpy(buf, random_buffer, sizeof(random_value));
                bytes_remaining -= sizeof(random_value);
                buf += sizeof(random_value);
            } else {
                memcpy(buf,
                    random_buffer + (sizeof(random_value) - bytes_remaining),
                    bytes_remaining);
                bytes_remaining = 0; 
            }
        } else
            break;
    }
    if (bytes_remaining == 0)
        return len;
    return 0;
}
#else
static size_t get_hardware_random_value(unsigned char *buf, size_t len) {
    
    if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
	if (OPENSSL_ia32_rdseed_bytes(buf, len) != len)
	    return 0;
    } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
	if (OPENSSL_ia32_rdrand_bytes(buf, len) != len)
	    return 0;
    } else
	return 0;
    return len;
}
#endif

#else
NON_EMPTY_TRANSLATION_UNIT
#endif
