Path: blob/main/crypto/openssl/providers/implementations/rands/seeding/rand_cpu_arm64.c
107992 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"1314#ifdef OPENSSL_RAND_SEED_RDCPU15#include "crypto/arm_arch.h"1617size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len);1819static size_t get_hardware_random_value(unsigned char *buf, size_t len);2021/*22* Acquire entropy using Arm-specific cpu instructions23*24* Uses the RNDRRS instruction. RNDR is never needed since25* RNDRRS will always be available if RNDR is an available26* instruction.27*28* Returns the total entropy count, if it exceeds the requested29* entropy count. Otherwise, returns an entropy count of 0.30*/31size_t ossl_prov_acquire_entropy_from_cpu(RAND_POOL *pool)32{33size_t bytes_needed;34unsigned char *buffer;3536bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);37if (bytes_needed > 0) {38buffer = ossl_rand_pool_add_begin(pool, bytes_needed);3940if (buffer != NULL) {41if (get_hardware_random_value(buffer, bytes_needed) == bytes_needed)42ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);43else44ossl_rand_pool_add_end(pool, 0, 0);45}46}4748return ossl_rand_pool_entropy_available(pool);49}5051static size_t get_hardware_random_value(unsigned char *buf, size_t len)52{53/* Always use RNDRRS or nothing */54if (OPENSSL_armcap_P & ARMV8_RNG) {55if (OPENSSL_rndrrs_bytes(buf, len) != len)56return 0;57} else {58return 0;59}60return len;61}6263#else64NON_EMPTY_TRANSLATION_UNIT65#endif /* OPENSSL_RAND_SEED_RDCPU */666768