Path: blob/main/crypto/openssl/providers/implementations/rands/seeding/rand_cpu_arm64.c
48531 views
/*1* Copyright 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"131415#ifdef OPENSSL_RAND_SEED_RDCPU16#include "crypto/arm_arch.h"1718size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len);1920static size_t get_hardware_random_value(unsigned char *buf, size_t len);2122/*23* Acquire entropy using Arm-specific cpu instructions24*25* Uses the RNDRRS instruction. RNDR is never needed since26* RNDRRS will always be available if RNDR is an available27* instruction.28*29* Returns the total entropy count, if it exceeds the requested30* entropy count. Otherwise, returns an entropy count of 0.31*/32size_t ossl_prov_acquire_entropy_from_cpu(RAND_POOL *pool)33{34size_t bytes_needed;35unsigned char *buffer;3637bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);38if (bytes_needed > 0) {39buffer = ossl_rand_pool_add_begin(pool, bytes_needed);4041if (buffer != NULL) {42if (get_hardware_random_value(buffer, bytes_needed) == bytes_needed)43ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);44else45ossl_rand_pool_add_end(pool, 0, 0);46}47}4849return ossl_rand_pool_entropy_available(pool);50}5152static size_t get_hardware_random_value(unsigned char *buf, size_t len)53{54/* Always use RNDRRS or nothing */55if (OPENSSL_armcap_P & ARMV8_RNG) {56if (OPENSSL_rndrrs_bytes(buf, len) != len)57return 0;58} else {59return 0;60}61return len;62}6364#else65NON_EMPTY_TRANSLATION_UNIT66#endif /* OPENSSL_RAND_SEED_RDCPU */676869