]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseRngLib/BaseRng.c
5b63d8f7146b2ba8d42b692688b6d567fc256d00
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
26 Generates a 16-bit random number.
28 if Rand is NULL, then ASSERT().
30 @param[out] Rand Buffer pointer to store the 16-bit random value.
32 @retval TRUE Random number generated successfully.
33 @retval FALSE Failed to generate the random number.
44 ASSERT (Rand
!= NULL
);
50 if (!ArchIsRngSupported ()) {
55 // A loop to fetch a 16 bit random value with a retry count limit.
57 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
58 if (ArchGetRandomNumber16 (Rand
)) {
67 Generates a 32-bit random number.
69 if Rand is NULL, then ASSERT().
71 @param[out] Rand Buffer pointer to store the 32-bit random value.
73 @retval TRUE Random number generated successfully.
74 @retval FALSE Failed to generate the random number.
85 ASSERT (Rand
!= NULL
);
91 if (!ArchIsRngSupported ()) {
96 // A loop to fetch a 32 bit random value with a retry count limit.
98 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
99 if (ArchGetRandomNumber32 (Rand
)) {
108 Generates a 64-bit random number.
110 if Rand is NULL, then ASSERT().
112 @param[out] Rand Buffer pointer to store the 64-bit random value.
114 @retval TRUE Random number generated successfully.
115 @retval FALSE Failed to generate the random number.
126 ASSERT (Rand
!= NULL
);
132 if (!ArchIsRngSupported ()) {
137 // A loop to fetch a 64 bit random value with a retry count limit.
139 for (Index
= 0; Index
< GETRANDOM_RETRY_LIMIT
; Index
++) {
140 if (ArchGetRandomNumber64 (Rand
)) {
149 Generates a 128-bit random number.
151 if Rand is NULL, then ASSERT().
153 @param[out] Rand Buffer pointer to store the 128-bit random value.
155 @retval TRUE Random number generated successfully.
156 @retval FALSE Failed to generate the random number.
165 ASSERT (Rand
!= NULL
);
171 if (!ArchIsRngSupported ()) {
176 // Read first 64 bits
178 if (!GetRandomNumber64 (Rand
)) {
183 // Read second 64 bits
185 return GetRandomNumber64 (++Rand
);