]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseRngLib/BaseRng.c
2 Random number generator services that uses CPU RNG instructions to
3 provide 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
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
15 #include "BaseRngLibInternals.h"
18 // Limited retry number when valid random data is returned.
19 // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32
20 // Architectures Software Developer's Manual".
22 #define GETRANDOM_RETRY_LIMIT 10
25 Generates a 16-bit random number.
27 if Rand is NULL, then ASSERT().
29 @param[out] Rand Buffer pointer to store the 16-bit random value.
31 @retval TRUE Random number generated successfully.
32 @retval FALSE Failed to generate the random number.
43 ASSERT (Rand
!= NULL
);
49 if (!ArchIsRngSupported ()) {
54 // A loop to fetch a 16 bit random value with a retry count limit.
56 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
57 if (ArchGetRandomNumber16 (Rand
)) {
66 Generates a 32-bit random number.
68 if Rand is NULL, then ASSERT().
70 @param[out] Rand Buffer pointer to store the 32-bit random value.
72 @retval TRUE Random number generated successfully.
73 @retval FALSE Failed to generate the random number.
84 ASSERT (Rand
!= NULL
);
90 if (!ArchIsRngSupported ()) {
95 // A loop to fetch a 32 bit random value with a retry count limit.
97 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
98 if (ArchGetRandomNumber32 (Rand
)) {
107 Generates a 64-bit random number.
109 if Rand is NULL, then ASSERT().
111 @param[out] Rand Buffer pointer to store the 64-bit random value.
113 @retval TRUE Random number generated successfully.
114 @retval FALSE Failed to generate the random number.
125 ASSERT (Rand
!= NULL
);
131 if (!ArchIsRngSupported ()) {
136 // A loop to fetch a 64 bit random value with a retry count limit.
138 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
139 if (ArchGetRandomNumber64 (Rand
)) {
148 Generates a 128-bit random number.
150 if Rand is NULL, then ASSERT().
152 @param[out] Rand Buffer pointer to store the 128-bit random value.
154 @retval TRUE Random number generated successfully.
155 @retval FALSE Failed to generate the random number.
164 ASSERT (Rand
!= NULL
);
170 if (!ArchIsRngSupported ()) {
175 // Read first 64 bits
177 if (!GetRandomNumber64 (Rand
)) {
182 // Read second 64 bits
184 return GetRandomNumber64 (++Rand
);