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
23 STATIC BOOLEAN mRdRandSupported
;
26 The constructor function checks whether or not RDRAND instruction is supported
29 The constructor function checks whether or not RDRAND instruction is supported.
30 It will ASSERT() if RDRAND instruction is not supported.
31 It will always return EFI_SUCCESS.
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
38 BaseRngLibConstructor (
45 // Determine RDRAND support by examining bit 30 of the ECX register returned by
46 // CPUID. A value of 1 indicates that processor support RDRAND instruction.
48 AsmCpuid (1, 0, 0, &RegEcx
, 0);
49 ASSERT ((RegEcx
& RDRAND_MASK
) == RDRAND_MASK
);
51 mRdRandSupported
= ((RegEcx
& RDRAND_MASK
) == RDRAND_MASK
);
57 Generates a 16-bit random number.
59 @param[out] Rand Buffer pointer to store the 16-bit random value.
61 @retval TRUE Random number generated successfully.
62 @retval FALSE Failed to generate the random number.
67 ArchGetRandomNumber16 (
71 return AsmRdRand16 (Rand
);
75 Generates a 32-bit random number.
77 @param[out] Rand Buffer pointer to store the 32-bit random value.
79 @retval TRUE Random number generated successfully.
80 @retval FALSE Failed to generate the random number.
85 ArchGetRandomNumber32 (
89 return AsmRdRand32 (Rand
);
93 Generates a 64-bit random number.
95 @param[out] Rand Buffer pointer to store the 64-bit random value.
97 @retval TRUE Random number generated successfully.
98 @retval FALSE Failed to generate the random number.
103 ArchGetRandomNumber64 (
107 return AsmRdRand64 (Rand
);
111 Checks whether RDRAND is supported.
113 @retval TRUE RDRAND is supported.
114 @retval FALSE RDRAND is not supported.
124 Existing software depends on this always returning TRUE, so for
127 return mRdRandSupported;