2 Random number generator service that uses the RNDR instruction
3 to provide pseudorandom numbers.
5 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/BaseLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/RngLib.h>
18 #include "BaseRngLibInternals.h"
20 STATIC BOOLEAN mRndrSupported
;
23 // Bit mask used to determine if RNDR instruction is supported.
25 #define RNDR_MASK ((UINT64)MAX_UINT16 << 60U)
28 The constructor function checks whether or not RNDR instruction is supported
31 The constructor function checks whether or not RNDR instruction is supported.
32 It will ASSERT() if RNDR instruction is not supported.
33 It will always return EFI_SUCCESS.
35 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
40 BaseRngLibConstructor (
46 // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by
47 // MSR. A non-zero value indicates that the processor supports the RNDR instruction.
49 Isar0
= ArmReadIdIsar0 ();
50 ASSERT ((Isar0
& RNDR_MASK
) != 0);
52 mRndrSupported
= ((Isar0
& RNDR_MASK
) != 0);
58 Generates a 16-bit random number.
60 @param[out] Rand Buffer pointer to store the 16-bit random value.
62 @retval TRUE Random number generated successfully.
63 @retval FALSE Failed to generate the random number.
68 ArchGetRandomNumber16 (
74 if (ArchGetRandomNumber64 (&Rand64
)) {
75 *Rand
= Rand64
& MAX_UINT16
;
83 Generates a 32-bit random number.
85 @param[out] Rand Buffer pointer to store the 32-bit random value.
87 @retval TRUE Random number generated successfully.
88 @retval FALSE Failed to generate the random number.
93 ArchGetRandomNumber32 (
99 if (ArchGetRandomNumber64 (&Rand64
)) {
100 *Rand
= Rand64
& MAX_UINT32
;
108 Generates a 64-bit random number.
110 @param[out] Rand Buffer pointer to store the 64-bit random value.
112 @retval TRUE Random number generated successfully.
113 @retval FALSE Failed to generate the random number.
118 ArchGetRandomNumber64 (
122 return ArmRndr (Rand
);
126 Checks whether RNDR is supported.
128 @retval TRUE RNDR is supported.
129 @retval FALSE RNDR is not supported.
138 return mRndrSupported
;