MdePkg/BaseRngLib: Add support for ARMv8.5 RNG instructions
Make BaseRngLib more generic by moving x86-specific functionality into 'Rand' and adding files under 'AArch64' to support the optional ARMv8.5 RNG instruction RNDR that is a part of FEAT_RNG. Signed-off-by: Rebecca Cran <rebecca@nuviainc.com> Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn> Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
This commit is contained in:
committed by
mergify[bot]
parent
ef3840c1ff
commit
9301e5644c
@@ -1,8 +1,10 @@
|
||||
/** @file
|
||||
Random number generator services that uses RdRand instruction access
|
||||
to provide high-quality random numbers.
|
||||
Random number generator services that uses CPU RNG instructions to
|
||||
provide random numbers.
|
||||
|
||||
Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||
Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
||||
|
||||
SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||
|
||||
**/
|
||||
@@ -10,46 +12,15 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||
#include <Library/BaseLib.h>
|
||||
#include <Library/DebugLib.h>
|
||||
|
||||
//
|
||||
// Bit mask used to determine if RdRand instruction is supported.
|
||||
//
|
||||
#define RDRAND_MASK BIT30
|
||||
#include "BaseRngLibInternals.h"
|
||||
|
||||
//
|
||||
// Limited retry number when valid random data is returned.
|
||||
// Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32
|
||||
// Architectures Software Developer's Mannual".
|
||||
// Architectures Software Developer's Manual".
|
||||
//
|
||||
#define RDRAND_RETRY_LIMIT 10
|
||||
#define GETRANDOM_RETRY_LIMIT 10
|
||||
|
||||
/**
|
||||
The constructor function checks whether or not RDRAND instruction is supported
|
||||
by the host hardware.
|
||||
|
||||
The constructor function checks whether or not RDRAND instruction is supported.
|
||||
It will ASSERT() if RDRAND instruction is not supported.
|
||||
It will always return RETURN_SUCCESS.
|
||||
|
||||
@retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS.
|
||||
|
||||
**/
|
||||
RETURN_STATUS
|
||||
EFIAPI
|
||||
BaseRngLibConstructor (
|
||||
VOID
|
||||
)
|
||||
{
|
||||
UINT32 RegEcx;
|
||||
|
||||
//
|
||||
// Determine RDRAND support by examining bit 30 of the ECX register returned by
|
||||
// CPUID. A value of 1 indicates that processor support RDRAND instruction.
|
||||
//
|
||||
AsmCpuid (1, 0, 0, &RegEcx, 0);
|
||||
ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
|
||||
|
||||
return RETURN_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
Generates a 16-bit random number.
|
||||
@@ -72,11 +43,19 @@ GetRandomNumber16 (
|
||||
|
||||
ASSERT (Rand != NULL);
|
||||
|
||||
if (Rand == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!ArchIsRngSupported ()) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//
|
||||
// A loop to fetch a 16 bit random value with a retry count limit.
|
||||
//
|
||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
||||
if (AsmRdRand16 (Rand)) {
|
||||
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||
if (ArchGetRandomNumber16 (Rand)) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
@@ -105,11 +84,19 @@ GetRandomNumber32 (
|
||||
|
||||
ASSERT (Rand != NULL);
|
||||
|
||||
if (Rand == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!ArchIsRngSupported ()) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//
|
||||
// A loop to fetch a 32 bit random value with a retry count limit.
|
||||
//
|
||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
||||
if (AsmRdRand32 (Rand)) {
|
||||
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||
if (ArchGetRandomNumber32 (Rand)) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
@@ -138,11 +125,19 @@ GetRandomNumber64 (
|
||||
|
||||
ASSERT (Rand != NULL);
|
||||
|
||||
if (Rand == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!ArchIsRngSupported ()) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//
|
||||
// A loop to fetch a 64 bit random value with a retry count limit.
|
||||
//
|
||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
||||
if (AsmRdRand64 (Rand)) {
|
||||
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||
if (ArchGetRandomNumber64 (Rand)) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
@@ -169,6 +164,14 @@ GetRandomNumber128 (
|
||||
{
|
||||
ASSERT (Rand != NULL);
|
||||
|
||||
if (Rand == NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if (!ArchIsRngSupported ()) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
//
|
||||
// Read first 64 bits
|
||||
//
|
||||
|
Reference in New Issue
Block a user