Path: blob/main/crypto/openssl/providers/implementations/rands/seeding/rand_cpu_x86.c
48531 views
/*1* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include "internal/cryptlib.h"10#include <openssl/opensslconf.h>11#include "crypto/rand_pool.h"12#include "prov/seeding.h"1314#ifdef OPENSSL_RAND_SEED_RDCPU15# if defined(OPENSSL_SYS_TANDEM) && defined(_TNS_X_TARGET)16# include <builtin.h> /* _rdrand64 */17# include <string.h> /* memcpy */18# else19size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);20size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);21# endif2223static size_t get_hardware_random_value(unsigned char *buf, size_t len);2425/*26* Acquire entropy using Intel-specific cpu instructions27*28* Uses the RDSEED instruction if available, otherwise uses29* RDRAND if available.30*31* For the differences between RDSEED and RDRAND, and why RDSEED32* is the preferred choice, see https://goo.gl/oK3KcN33*34* Returns the total entropy count, if it exceeds the requested35* entropy count. Otherwise, returns an entropy count of 0.36*/37size_t ossl_prov_acquire_entropy_from_cpu(RAND_POOL *pool)38{39size_t bytes_needed;40unsigned char *buffer;4142bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);43if (bytes_needed > 0) {44buffer = ossl_rand_pool_add_begin(pool, bytes_needed);4546if (buffer != NULL) {47if (get_hardware_random_value(buffer, bytes_needed) == bytes_needed) {48ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);49} else {50ossl_rand_pool_add_end(pool, 0, 0);51}52}53}5455return ossl_rand_pool_entropy_available(pool);56}5758#if defined(OPENSSL_SYS_TANDEM) && defined(_TNS_X_TARGET)59/* Obtain random bytes from the x86 hardware random function in 64 bit chunks */60static size_t get_hardware_random_value(unsigned char *buf, size_t len)61{62size_t bytes_remaining = len;6364while (bytes_remaining > 0) {65/* Always use 64 bit fetch, then use the lower bytes as needed. */66/* The platform is big-endian. */67uint64_t random_value = 0;6869if (_rdrand64(&random_value) != 0) {70unsigned char *random_buffer = (unsigned char *)&random_value;7172if (bytes_remaining >= sizeof(random_value)) {73memcpy(buf, random_buffer, sizeof(random_value));74bytes_remaining -= sizeof(random_value);75buf += sizeof(random_value);76} else {77memcpy(buf,78random_buffer + (sizeof(random_value) - bytes_remaining),79bytes_remaining);80bytes_remaining = 0; /* This will terminate the loop */81}82} else83break;84}85if (bytes_remaining == 0)86return len;87return 0;88}89#else90static size_t get_hardware_random_value(unsigned char *buf, size_t len) {91/* Whichever comes first, use RDSEED, RDRAND or nothing */92if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {93if (OPENSSL_ia32_rdseed_bytes(buf, len) != len)94return 0;95} else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {96if (OPENSSL_ia32_rdrand_bytes(buf, len) != len)97return 0;98} else99return 0;100return len;101}102#endif103104#else105NON_EMPTY_TRANSLATION_UNIT106#endif107108109