mirror of https://github.com/acidanthera/audk.git
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:
parent
ef3840c1ff
commit
9301e5644c
|
@ -0,0 +1,31 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# ArmReadIdIsar0() for AArch64
|
||||||
|
#
|
||||||
|
# Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
#
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
.text
|
||||||
|
.p2align 2
|
||||||
|
GCC_ASM_EXPORT(ArmReadIdIsar0)
|
||||||
|
|
||||||
|
#/**
|
||||||
|
# Reads the ID_AA64ISAR0 Register.
|
||||||
|
#
|
||||||
|
# @return The contents of the ID_AA64ISAR0 register.
|
||||||
|
#
|
||||||
|
#**/
|
||||||
|
#UINT64
|
||||||
|
#EFIAPI
|
||||||
|
#ArmReadIdIsar0 (
|
||||||
|
# VOID
|
||||||
|
# );
|
||||||
|
#
|
||||||
|
ASM_PFX(ArmReadIdIsar0):
|
||||||
|
mrs x0, id_aa64isar0_el1 // Read ID_AA64ISAR0 Register
|
||||||
|
ret
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
;------------------------------------------------------------------------------
|
||||||
|
;
|
||||||
|
; ArmReadIdIsar0() for AArch64
|
||||||
|
;
|
||||||
|
; Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
;
|
||||||
|
; SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
;
|
||||||
|
;------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
EXPORT ArmReadIdIsar0
|
||||||
|
AREA BaseLib_LowLevel, CODE, READONLY
|
||||||
|
|
||||||
|
;/**
|
||||||
|
; Reads the ID_AA64ISAR0 Register.
|
||||||
|
;
|
||||||
|
; @return The contents of the ID_AA64ISAR0 register.
|
||||||
|
;
|
||||||
|
;**/
|
||||||
|
;UINT64
|
||||||
|
;EFIAPI
|
||||||
|
;ArmReadIdIsar0 (
|
||||||
|
; VOID
|
||||||
|
; );
|
||||||
|
;
|
||||||
|
ArmReadIdIsar0
|
||||||
|
mrs x0, id_aa64isar0_el1 // Read ID_AA64ISAR0 Register
|
||||||
|
ret
|
||||||
|
|
||||||
|
END
|
|
@ -0,0 +1,37 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# ArmRndr() for AArch64
|
||||||
|
#
|
||||||
|
# Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
#
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "BaseRngLibInternals.h"
|
||||||
|
|
||||||
|
.text
|
||||||
|
.p2align 2
|
||||||
|
GCC_ASM_EXPORT(ArmRndr)
|
||||||
|
|
||||||
|
#/**
|
||||||
|
# Generates a random number using RNDR.
|
||||||
|
# Returns TRUE on success; FALSE on failure.
|
||||||
|
#
|
||||||
|
# @param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
#
|
||||||
|
# @retval TRUE Random number generated successfully.
|
||||||
|
# @retval FALSE Failed to generate the random number.
|
||||||
|
#
|
||||||
|
#**/
|
||||||
|
#BOOLEAN
|
||||||
|
#EFIAPI
|
||||||
|
#ArmRndr (
|
||||||
|
# OUT UINT64 *Rand
|
||||||
|
# );
|
||||||
|
#
|
||||||
|
ASM_PFX(ArmRndr):
|
||||||
|
mrs x1, RNDR
|
||||||
|
str x1, [x0]
|
||||||
|
cset x0, ne // RNDR sets NZCV to 0b0100 on failure
|
||||||
|
ret
|
|
@ -0,0 +1,39 @@
|
||||||
|
;------------------------------------------------------------------------------
|
||||||
|
;
|
||||||
|
; ArmRndr() for AArch64
|
||||||
|
;
|
||||||
|
; Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
;
|
||||||
|
; SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
;
|
||||||
|
;------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#include "BaseRngLibInternals.h"
|
||||||
|
|
||||||
|
EXPORT ArmRndr
|
||||||
|
AREA BaseLib_LowLevel, CODE, READONLY
|
||||||
|
|
||||||
|
|
||||||
|
;/**
|
||||||
|
; Generates a random number using RNDR.
|
||||||
|
; Returns TRUE on success; FALSE on failure.
|
||||||
|
;
|
||||||
|
; @param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
;
|
||||||
|
; @retval TRUE Random number generated successfully.
|
||||||
|
; @retval FALSE Failed to generate the random number.
|
||||||
|
;
|
||||||
|
;**/
|
||||||
|
;BOOLEAN
|
||||||
|
;EFIAPI
|
||||||
|
;ArmRndr (
|
||||||
|
; OUT UINT64 *Rand
|
||||||
|
; );
|
||||||
|
;
|
||||||
|
ArmRndr
|
||||||
|
mrs x1, RNDR
|
||||||
|
str x1, [x0]
|
||||||
|
cset x0, ne // RNDR sets NZCV to 0b0100 on failure
|
||||||
|
ret
|
||||||
|
|
||||||
|
END
|
|
@ -0,0 +1,42 @@
|
||||||
|
/** @file
|
||||||
|
Random number generator service that uses the RNDR instruction
|
||||||
|
to provide pseudorandom numbers.
|
||||||
|
|
||||||
|
Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
|
||||||
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef ARM_RNG_H_
|
||||||
|
#define ARM_RNG_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a random number using RNDR.
|
||||||
|
Returns TRUE on success; FALSE on failure.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArmRndr (
|
||||||
|
OUT UINT64 *Rand
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Reads the ID_AA64ISAR0 Register.
|
||||||
|
|
||||||
|
@return The contents of the ID_AA64ISAR0 register.
|
||||||
|
|
||||||
|
**/
|
||||||
|
UINT64
|
||||||
|
EFIAPI
|
||||||
|
ArmReadIdIsar0 (
|
||||||
|
VOID
|
||||||
|
);
|
||||||
|
|
||||||
|
#endif /* ARM_RNG_H_ */
|
|
@ -0,0 +1,139 @@
|
||||||
|
/** @file
|
||||||
|
Random number generator service that uses the RNDR instruction
|
||||||
|
to provide pseudorandom 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
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <Uefi.h>
|
||||||
|
#include <Library/BaseLib.h>
|
||||||
|
#include <Library/DebugLib.h>
|
||||||
|
#include <Library/RngLib.h>
|
||||||
|
|
||||||
|
#include "ArmRng.h"
|
||||||
|
#include "BaseRngLibInternals.h"
|
||||||
|
|
||||||
|
STATIC BOOLEAN mRndrSupported;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Bit mask used to determine if RNDR instruction is supported.
|
||||||
|
//
|
||||||
|
#define RNDR_MASK ((UINT64)MAX_UINT16 << 60U)
|
||||||
|
|
||||||
|
/**
|
||||||
|
The constructor function checks whether or not RNDR instruction is supported
|
||||||
|
by the host hardware.
|
||||||
|
|
||||||
|
The constructor function checks whether or not RNDR instruction is supported.
|
||||||
|
It will ASSERT() if RNDR instruction is not supported.
|
||||||
|
It will always return EFI_SUCCESS.
|
||||||
|
|
||||||
|
@retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
|
||||||
|
|
||||||
|
**/
|
||||||
|
EFI_STATUS
|
||||||
|
EFIAPI
|
||||||
|
BaseRngLibConstructor (
|
||||||
|
VOID
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT64 Isar0;
|
||||||
|
//
|
||||||
|
// Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by
|
||||||
|
// MSR. A non-zero value indicates that the processor supports the RNDR instruction.
|
||||||
|
//
|
||||||
|
Isar0 = ArmReadIdIsar0 ();
|
||||||
|
ASSERT ((Isar0 & RNDR_MASK) != 0);
|
||||||
|
|
||||||
|
mRndrSupported = ((Isar0 & RNDR_MASK) != 0);
|
||||||
|
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 16-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 16-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber16 (
|
||||||
|
OUT UINT16 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT64 Rand64;
|
||||||
|
|
||||||
|
if (ArchGetRandomNumber64 (&Rand64)) {
|
||||||
|
*Rand = Rand64 & MAX_UINT16;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 32-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 32-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber32 (
|
||||||
|
OUT UINT32 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
UINT64 Rand64;
|
||||||
|
|
||||||
|
if (ArchGetRandomNumber64 (&Rand64)) {
|
||||||
|
*Rand = Rand64 & MAX_UINT32;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 64-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber64 (
|
||||||
|
OUT UINT64 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return ArmRndr (Rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks whether RNDR is supported.
|
||||||
|
|
||||||
|
@retval TRUE RNDR is supported.
|
||||||
|
@retval FALSE RNDR is not supported.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchIsRngSupported (
|
||||||
|
VOID
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return mRndrSupported;
|
||||||
|
}
|
|
@ -1,8 +1,10 @@
|
||||||
/** @file
|
/** @file
|
||||||
Random number generator services that uses RdRand instruction access
|
Random number generator services that uses CPU RNG instructions to
|
||||||
to provide high-quality random numbers.
|
provide random numbers.
|
||||||
|
|
||||||
|
Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
||||||
|
|
||||||
SPDX-License-Identifier: BSD-2-Clause-Patent
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
|
||||||
**/
|
**/
|
||||||
|
@ -10,46 +12,15 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
#include <Library/BaseLib.h>
|
#include <Library/BaseLib.h>
|
||||||
#include <Library/DebugLib.h>
|
#include <Library/DebugLib.h>
|
||||||
|
|
||||||
//
|
#include "BaseRngLibInternals.h"
|
||||||
// Bit mask used to determine if RdRand instruction is supported.
|
|
||||||
//
|
|
||||||
#define RDRAND_MASK BIT30
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Limited retry number when valid random data is returned.
|
// 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
|
// 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.
|
Generates a 16-bit random number.
|
||||||
|
@ -72,11 +43,19 @@ GetRandomNumber16 (
|
||||||
|
|
||||||
ASSERT (Rand != NULL);
|
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.
|
// A loop to fetch a 16 bit random value with a retry count limit.
|
||||||
//
|
//
|
||||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||||
if (AsmRdRand16 (Rand)) {
|
if (ArchGetRandomNumber16 (Rand)) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,11 +84,19 @@ GetRandomNumber32 (
|
||||||
|
|
||||||
ASSERT (Rand != NULL);
|
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.
|
// A loop to fetch a 32 bit random value with a retry count limit.
|
||||||
//
|
//
|
||||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||||
if (AsmRdRand32 (Rand)) {
|
if (ArchGetRandomNumber32 (Rand)) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,11 +125,19 @@ GetRandomNumber64 (
|
||||||
|
|
||||||
ASSERT (Rand != NULL);
|
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.
|
// A loop to fetch a 64 bit random value with a retry count limit.
|
||||||
//
|
//
|
||||||
for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {
|
for (Index = 0; Index < GETRANDOM_RETRY_LIMIT; Index++) {
|
||||||
if (AsmRdRand64 (Rand)) {
|
if (ArchGetRandomNumber64 (Rand)) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,6 +164,14 @@ GetRandomNumber128 (
|
||||||
{
|
{
|
||||||
ASSERT (Rand != NULL);
|
ASSERT (Rand != NULL);
|
||||||
|
|
||||||
|
if (Rand == NULL) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ArchIsRngSupported ()) {
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Read first 64 bits
|
// Read first 64 bits
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
## @file
|
## @file
|
||||||
# Instance of RNG (Random Number Generator) Library.
|
# Instance of RNG (Random Number Generator) Library.
|
||||||
#
|
#
|
||||||
# BaseRng Library that uses CPU RdRand instruction access to provide
|
# BaseRng Library that uses CPU RNG instructions (e.g. RdRand) to
|
||||||
# high-quality random numbers.
|
# provide random numbers.
|
||||||
#
|
#
|
||||||
|
# Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
# SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
@ -22,11 +23,25 @@
|
||||||
CONSTRUCTOR = BaseRngLibConstructor
|
CONSTRUCTOR = BaseRngLibConstructor
|
||||||
|
|
||||||
#
|
#
|
||||||
# VALID_ARCHITECTURES = IA32 X64
|
# VALID_ARCHITECTURES = IA32 X64 AARCH64
|
||||||
#
|
#
|
||||||
|
|
||||||
[Sources.Ia32, Sources.X64]
|
[Sources]
|
||||||
BaseRng.c
|
BaseRng.c
|
||||||
|
BaseRngLibInternals.h
|
||||||
|
|
||||||
|
[Sources.Ia32, Sources.X64]
|
||||||
|
Rand/RdRand.c
|
||||||
|
|
||||||
|
[Sources.AARCH64]
|
||||||
|
AArch64/Rndr.c
|
||||||
|
AArch64/ArmRng.h
|
||||||
|
|
||||||
|
AArch64/ArmReadIdIsar0.S | GCC
|
||||||
|
AArch64/ArmRng.S | GCC
|
||||||
|
|
||||||
|
AArch64/ArmReadIdIsar0.asm | MSFT
|
||||||
|
AArch64/ArmRng.asm | MSFT
|
||||||
|
|
||||||
[Packages]
|
[Packages]
|
||||||
MdePkg/MdePkg.dec
|
MdePkg/MdePkg.dec
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
// /** @file
|
// /** @file
|
||||||
// Instance of RNG (Random Number Generator) Library.
|
// Instance of RNG (Random Number Generator) Library.
|
||||||
//
|
//
|
||||||
// BaseRng Library that uses CPU RdRand instruction access to provide
|
// BaseRng Library that uses CPU RNG instructions to provide
|
||||||
// high-quality random numbers.
|
// random numbers.
|
||||||
//
|
//
|
||||||
// Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
// Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
|
||||||
//
|
//
|
||||||
|
@ -13,5 +13,4 @@
|
||||||
|
|
||||||
#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG Library"
|
#string STR_MODULE_ABSTRACT #language en-US "Instance of RNG Library"
|
||||||
|
|
||||||
#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses CPU RdRand instruction access to provide high-quality random numbers"
|
#string STR_MODULE_DESCRIPTION #language en-US "BaseRng Library that uses CPU RNG instructions to provide random numbers"
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
/** @file
|
||||||
|
|
||||||
|
Architecture specific interface to RNG functionality.
|
||||||
|
|
||||||
|
Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
|
||||||
|
|
||||||
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#ifndef BASE_RNGLIB_INTERNALS_H_
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 16-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 16-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber16 (
|
||||||
|
OUT UINT16 *Rand
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 32-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 32-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber32 (
|
||||||
|
OUT UINT32 *Rand
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 64-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber64 (
|
||||||
|
OUT UINT64 *Rand
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks whether the RNG instruction is supported.
|
||||||
|
|
||||||
|
@retval TRUE RNG instruction is supported.
|
||||||
|
@retval FALSE RNG instruction is not supported.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchIsRngSupported (
|
||||||
|
VOID
|
||||||
|
);
|
||||||
|
|
||||||
|
#if defined (MDE_CPU_AARCH64)
|
||||||
|
|
||||||
|
// RNDR, Random Number
|
||||||
|
#define RNDR S3_3_C2_C4_0
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // BASE_RNGLIB_INTERNALS_H_
|
|
@ -0,0 +1,131 @@
|
||||||
|
/** @file
|
||||||
|
Random number generator services that uses RdRand instruction access
|
||||||
|
to provide high-quality 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
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
#include <Uefi.h>
|
||||||
|
#include <Library/BaseLib.h>
|
||||||
|
#include <Library/DebugLib.h>
|
||||||
|
|
||||||
|
#include "BaseRngLibInternals.h"
|
||||||
|
|
||||||
|
//
|
||||||
|
// Bit mask used to determine if RdRand instruction is supported.
|
||||||
|
//
|
||||||
|
#define RDRAND_MASK BIT30
|
||||||
|
|
||||||
|
|
||||||
|
STATIC BOOLEAN mRdRandSupported;
|
||||||
|
|
||||||
|
/**
|
||||||
|
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 EFI_SUCCESS.
|
||||||
|
|
||||||
|
@retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
|
||||||
|
|
||||||
|
**/
|
||||||
|
EFI_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);
|
||||||
|
|
||||||
|
mRdRandSupported = ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
|
||||||
|
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 16-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 16-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber16 (
|
||||||
|
OUT UINT16 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return AsmRdRand16 (Rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 32-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 32-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber32 (
|
||||||
|
OUT UINT32 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return AsmRdRand32 (Rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generates a 64-bit random number.
|
||||||
|
|
||||||
|
@param[out] Rand Buffer pointer to store the 64-bit random value.
|
||||||
|
|
||||||
|
@retval TRUE Random number generated successfully.
|
||||||
|
@retval FALSE Failed to generate the random number.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchGetRandomNumber64 (
|
||||||
|
OUT UINT64 *Rand
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return AsmRdRand64 (Rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Checks whether RDRAND is supported.
|
||||||
|
|
||||||
|
@retval TRUE RDRAND is supported.
|
||||||
|
@retval FALSE RDRAND is not supported.
|
||||||
|
|
||||||
|
**/
|
||||||
|
BOOLEAN
|
||||||
|
EFIAPI
|
||||||
|
ArchIsRngSupported (
|
||||||
|
VOID
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Existing software depends on this always returning TRUE, so for
|
||||||
|
now hard-code it.
|
||||||
|
|
||||||
|
return mRdRandSupported;
|
||||||
|
*/
|
||||||
|
return TRUE;
|
||||||
|
}
|
|
@ -267,6 +267,11 @@
|
||||||
#
|
#
|
||||||
RegisterFilterLib|Include/Library/RegisterFilterLib.h
|
RegisterFilterLib|Include/Library/RegisterFilterLib.h
|
||||||
|
|
||||||
|
[LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64]
|
||||||
|
## @libraryclass Provides services to generate random number.
|
||||||
|
#
|
||||||
|
RngLib|Include/Library/RngLib.h
|
||||||
|
|
||||||
[LibraryClasses.IA32, LibraryClasses.X64]
|
[LibraryClasses.IA32, LibraryClasses.X64]
|
||||||
## @libraryclass Abstracts both S/W SMI generation and detection.
|
## @libraryclass Abstracts both S/W SMI generation and detection.
|
||||||
##
|
##
|
||||||
|
@ -288,10 +293,6 @@
|
||||||
#
|
#
|
||||||
SmmPeriodicSmiLib|Include/Library/SmmPeriodicSmiLib.h
|
SmmPeriodicSmiLib|Include/Library/SmmPeriodicSmiLib.h
|
||||||
|
|
||||||
## @libraryclass Provides services to generate random number.
|
|
||||||
#
|
|
||||||
RngLib|Include/Library/RngLib.h
|
|
||||||
|
|
||||||
## @libraryclass Provides services to log the SMI handler registration.
|
## @libraryclass Provides services to log the SMI handler registration.
|
||||||
SmiHandlerProfileLib|Include/Library/SmiHandlerProfileLib.h
|
SmiHandlerProfileLib|Include/Library/SmiHandlerProfileLib.h
|
||||||
|
|
||||||
|
|
|
@ -145,6 +145,9 @@
|
||||||
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf
|
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibSmm.inf
|
||||||
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf
|
MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibUefiShell.inf
|
||||||
|
|
||||||
|
[Components.IA32, Components.X64, Components.AARCH64]
|
||||||
|
MdePkg/Library/BaseRngLib/BaseRngLib.inf
|
||||||
|
|
||||||
[Components.IA32, Components.X64]
|
[Components.IA32, Components.X64]
|
||||||
MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
|
MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsic.inf
|
||||||
MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
|
MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicSev.inf
|
||||||
|
@ -168,7 +171,6 @@
|
||||||
MdePkg/Library/BaseS3StallLib/BaseS3StallLib.inf
|
MdePkg/Library/BaseS3StallLib/BaseS3StallLib.inf
|
||||||
MdePkg/Library/SmmMemLib/SmmMemLib.inf
|
MdePkg/Library/SmmMemLib/SmmMemLib.inf
|
||||||
MdePkg/Library/SmmIoLib/SmmIoLib.inf
|
MdePkg/Library/SmmIoLib/SmmIoLib.inf
|
||||||
MdePkg/Library/BaseRngLib/BaseRngLib.inf
|
|
||||||
MdePkg/Library/SmmPciExpressLib/SmmPciExpressLib.inf
|
MdePkg/Library/SmmPciExpressLib/SmmPciExpressLib.inf
|
||||||
MdePkg/Library/SmiHandlerProfileLibNull/SmiHandlerProfileLibNull.inf
|
MdePkg/Library/SmiHandlerProfileLibNull/SmiHandlerProfileLibNull.inf
|
||||||
MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
|
MdePkg/Library/MmServicesTableLib/MmServicesTableLib.inf
|
||||||
|
|
Loading…
Reference in New Issue