]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseRngLib/BaseRng.c
2 Random number generator services that uses RdRand instruction access
3 to provide high-quality random numbers.
5 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
14 // Bit mask used to determine if RdRand instruction is supported.
16 #define RDRAND_MASK BIT30
19 // Limited retry number when valid random data is returned.
20 // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32
21 // Architectures Software Developer's Mannual".
23 #define RDRAND_RETRY_LIMIT 10
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 RETURN_SUCCESS.
33 @retval RETURN_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 return RETURN_SUCCESS
;
55 Generates a 16-bit random number.
57 if Rand is NULL, then ASSERT().
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.
73 ASSERT (Rand
!= NULL
);
76 // A loop to fetch a 16 bit random value with a retry count limit.
78 for (Index
= 0; Index
< RDRAND_RETRY_LIMIT
; Index
++) {
79 if (AsmRdRand16 (Rand
)) {
88 Generates a 32-bit random number.
90 if Rand is NULL, then ASSERT().
92 @param[out] Rand Buffer pointer to store the 32-bit random value.
94 @retval TRUE Random number generated successfully.
95 @retval FALSE Failed to generate the random number.
106 ASSERT (Rand
!= NULL
);
109 // A loop to fetch a 32 bit random value with a retry count limit.
111 for (Index
= 0; Index
< RDRAND_RETRY_LIMIT
; Index
++) {
112 if (AsmRdRand32 (Rand
)) {
121 Generates a 64-bit random number.
123 if Rand is NULL, then ASSERT().
125 @param[out] Rand Buffer pointer to store the 64-bit random value.
127 @retval TRUE Random number generated successfully.
128 @retval FALSE Failed to generate the random number.
139 ASSERT (Rand
!= NULL
);
142 // A loop to fetch a 64 bit random value with a retry count limit.
144 for (Index
= 0; Index
< RDRAND_RETRY_LIMIT
; Index
++) {
145 if (AsmRdRand64 (Rand
)) {
154 Generates a 128-bit random number.
156 if Rand is NULL, then ASSERT().
158 @param[out] Rand Buffer pointer to store the 128-bit random value.
160 @retval TRUE Random number generated successfully.
161 @retval FALSE Failed to generate the random number.
170 ASSERT (Rand
!= NULL
);
173 // Read first 64 bits
175 if (!GetRandomNumber64 (Rand
)) {
180 // Read second 64 bits
182 return GetRandomNumber64 (++Rand
);