]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseRngLib/Rand/RdRand.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseRngLib / Rand / RdRand.c
CommitLineData
9301e564
RC
1/** @file\r
2 Random number generator services that uses RdRand instruction access\r
3 to provide high-quality random numbers.\r
4\r
5Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>\r
6Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8SPDX-License-Identifier: BSD-2-Clause-Patent\r
9\r
10**/\r
11\r
12#include <Uefi.h>\r
13#include <Library/BaseLib.h>\r
14#include <Library/DebugLib.h>\r
15\r
16#include "BaseRngLibInternals.h"\r
17\r
18//\r
19// Bit mask used to determine if RdRand instruction is supported.\r
20//\r
2f88bd3a 21#define RDRAND_MASK BIT30\r
9301e564 22\r
2f88bd3a 23STATIC BOOLEAN mRdRandSupported;\r
9301e564
RC
24\r
25/**\r
26 The constructor function checks whether or not RDRAND instruction is supported\r
27 by the host hardware.\r
28\r
29 The constructor function checks whether or not RDRAND instruction is supported.\r
30 It will ASSERT() if RDRAND instruction is not supported.\r
31 It will always return EFI_SUCCESS.\r
32\r
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
34\r
35**/\r
36EFI_STATUS\r
37EFIAPI\r
38BaseRngLibConstructor (\r
39 VOID\r
40 )\r
41{\r
42 UINT32 RegEcx;\r
43\r
44 //\r
45 // Determine RDRAND support by examining bit 30 of the ECX register returned by\r
46 // CPUID. A value of 1 indicates that processor support RDRAND instruction.\r
47 //\r
48 AsmCpuid (1, 0, 0, &RegEcx, 0);\r
49 ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);\r
50\r
51 mRdRandSupported = ((RegEcx & RDRAND_MASK) == RDRAND_MASK);\r
52\r
53 return EFI_SUCCESS;\r
54}\r
55\r
56/**\r
57 Generates a 16-bit random number.\r
58\r
59 @param[out] Rand Buffer pointer to store the 16-bit random value.\r
60\r
61 @retval TRUE Random number generated successfully.\r
62 @retval FALSE Failed to generate the random number.\r
63\r
64**/\r
65BOOLEAN\r
66EFIAPI\r
67ArchGetRandomNumber16 (\r
2f88bd3a 68 OUT UINT16 *Rand\r
9301e564
RC
69 )\r
70{\r
71 return AsmRdRand16 (Rand);\r
72}\r
73\r
74/**\r
75 Generates a 32-bit random number.\r
76\r
77 @param[out] Rand Buffer pointer to store the 32-bit random value.\r
78\r
79 @retval TRUE Random number generated successfully.\r
80 @retval FALSE Failed to generate the random number.\r
81\r
82**/\r
83BOOLEAN\r
84EFIAPI\r
85ArchGetRandomNumber32 (\r
2f88bd3a 86 OUT UINT32 *Rand\r
9301e564
RC
87 )\r
88{\r
89 return AsmRdRand32 (Rand);\r
90}\r
91\r
92/**\r
93 Generates a 64-bit random number.\r
94\r
95 @param[out] Rand Buffer pointer to store the 64-bit random value.\r
96\r
97 @retval TRUE Random number generated successfully.\r
98 @retval FALSE Failed to generate the random number.\r
99\r
100**/\r
101BOOLEAN\r
102EFIAPI\r
103ArchGetRandomNumber64 (\r
2f88bd3a 104 OUT UINT64 *Rand\r
9301e564
RC
105 )\r
106{\r
107 return AsmRdRand64 (Rand);\r
108}\r
109\r
110/**\r
111 Checks whether RDRAND is supported.\r
112\r
113 @retval TRUE RDRAND is supported.\r
114 @retval FALSE RDRAND is not supported.\r
115\r
116**/\r
117BOOLEAN\r
118EFIAPI\r
119ArchIsRngSupported (\r
120 VOID\r
121 )\r
122{\r
123 /*\r
124 Existing software depends on this always returning TRUE, so for\r
125 now hard-code it.\r
126\r
127 return mRdRandSupported;\r
128 */\r
129 return TRUE;\r
130}\r