2 Random number generator services that uses RdRand instruction access
3 to provide high-quality random 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>
16 #include "BaseRngLibInternals.h"
19 // Bit mask used to determine if RdRand instruction is supported.
21 #define RDRAND_MASK BIT30
24 STATIC BOOLEAN mRdRandSupported
;
27 The constructor function checks whether or not RDRAND instruction is supported
30 The constructor function checks whether or not RDRAND instruction is supported.
31 It will ASSERT() if RDRAND instruction is not supported.
32 It will always return EFI_SUCCESS.
34 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
39 BaseRngLibConstructor (
46 // Determine RDRAND support by examining bit 30 of the ECX register returned by
47 // CPUID. A value of 1 indicates that processor support RDRAND instruction.
49 AsmCpuid (1, 0, 0, &RegEcx
, 0);
50 ASSERT ((RegEcx
& RDRAND_MASK
) == RDRAND_MASK
);
52 mRdRandSupported
= ((RegEcx
& RDRAND_MASK
) == RDRAND_MASK
);
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 (
72 return AsmRdRand16 (Rand
);
76 Generates a 32-bit random number.
78 @param[out] Rand Buffer pointer to store the 32-bit random value.
80 @retval TRUE Random number generated successfully.
81 @retval FALSE Failed to generate the random number.
86 ArchGetRandomNumber32 (
90 return AsmRdRand32 (Rand
);
94 Generates a 64-bit random number.
96 @param[out] Rand Buffer pointer to store the 64-bit random value.
98 @retval TRUE Random number generated successfully.
99 @retval FALSE Failed to generate the random number.
104 ArchGetRandomNumber64 (
108 return AsmRdRand64 (Rand
);
112 Checks whether RDRAND is supported.
114 @retval TRUE RDRAND is supported.
115 @retval FALSE RDRAND is not supported.
125 Existing software depends on this always returning TRUE, so for
128 return mRdRandSupported;