]>
Commit | Line | Data |
---|---|---|
255c8e22 QL |
1 | /** @file\r |
2 | Random number generator services that uses RdRand instruction access\r | |
3 | to provide high-quality random numbers.\r | |
4 | \r | |
5 | Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r | |
9344f092 | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
255c8e22 QL |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include <Library/BaseLib.h>\r | |
11 | #include <Library/DebugLib.h>\r | |
12 | \r | |
13 | //\r | |
14 | // Bit mask used to determine if RdRand instruction is supported.\r | |
15 | //\r | |
16 | #define RDRAND_MASK BIT30\r | |
17 | \r | |
18 | //\r | |
19 | // Limited retry number when valid random data is returned.\r | |
20 | // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32\r | |
21 | // Architectures Software Developer's Mannual".\r | |
22 | //\r | |
23 | #define RDRAND_RETRY_LIMIT 10\r | |
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 RETURN_SUCCESS.\r | |
32 | \r | |
33 | @retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
34 | \r | |
35 | **/\r | |
36 | RETURN_STATUS\r | |
37 | EFIAPI\r | |
38 | BaseRngLibConstructor (\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 | return RETURN_SUCCESS;\r | |
52 | }\r | |
53 | \r | |
54 | /**\r | |
55 | Generates a 16-bit random number.\r | |
56 | \r | |
57 | if Rand is NULL, then ASSERT().\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 | |
65 | BOOLEAN\r | |
66 | EFIAPI\r | |
67 | GetRandomNumber16 (\r | |
68 | OUT UINT16 *Rand\r | |
69 | )\r | |
70 | {\r | |
71 | UINT32 Index;\r | |
72 | \r | |
73 | ASSERT (Rand != NULL);\r | |
74 | \r | |
75 | //\r | |
76 | // A loop to fetch a 16 bit random value with a retry count limit.\r | |
77 | //\r | |
78 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
79 | if (AsmRdRand16 (Rand)) {\r | |
80 | return TRUE;\r | |
81 | }\r | |
82 | }\r | |
83 | \r | |
84 | return FALSE;\r | |
85 | }\r | |
86 | \r | |
87 | /**\r | |
88 | Generates a 32-bit random number.\r | |
89 | \r | |
90 | if Rand is NULL, then ASSERT().\r | |
91 | \r | |
92 | @param[out] Rand Buffer pointer to store the 32-bit random value.\r | |
93 | \r | |
94 | @retval TRUE Random number generated successfully.\r | |
95 | @retval FALSE Failed to generate the random number.\r | |
96 | \r | |
97 | **/\r | |
98 | BOOLEAN\r | |
99 | EFIAPI\r | |
100 | GetRandomNumber32 (\r | |
101 | OUT UINT32 *Rand\r | |
102 | )\r | |
103 | {\r | |
104 | UINT32 Index;\r | |
105 | \r | |
106 | ASSERT (Rand != NULL);\r | |
107 | \r | |
108 | //\r | |
109 | // A loop to fetch a 32 bit random value with a retry count limit.\r | |
110 | //\r | |
111 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
112 | if (AsmRdRand32 (Rand)) {\r | |
113 | return TRUE;\r | |
114 | }\r | |
115 | }\r | |
116 | \r | |
117 | return FALSE;\r | |
118 | }\r | |
119 | \r | |
120 | /**\r | |
121 | Generates a 64-bit random number.\r | |
122 | \r | |
123 | if Rand is NULL, then ASSERT().\r | |
124 | \r | |
125 | @param[out] Rand Buffer pointer to store the 64-bit random value.\r | |
126 | \r | |
127 | @retval TRUE Random number generated successfully.\r | |
128 | @retval FALSE Failed to generate the random number.\r | |
129 | \r | |
130 | **/\r | |
131 | BOOLEAN\r | |
132 | EFIAPI\r | |
133 | GetRandomNumber64 (\r | |
134 | OUT UINT64 *Rand\r | |
135 | )\r | |
136 | {\r | |
137 | UINT32 Index;\r | |
138 | \r | |
139 | ASSERT (Rand != NULL);\r | |
140 | \r | |
141 | //\r | |
142 | // A loop to fetch a 64 bit random value with a retry count limit.\r | |
143 | //\r | |
144 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
145 | if (AsmRdRand64 (Rand)) {\r | |
146 | return TRUE;\r | |
147 | }\r | |
148 | }\r | |
149 | \r | |
150 | return FALSE;\r | |
151 | }\r | |
c8b6f16d TP |
152 | \r |
153 | /**\r | |
154 | Generates a 128-bit random number.\r | |
155 | \r | |
156 | if Rand is NULL, then ASSERT().\r | |
157 | \r | |
158 | @param[out] Rand Buffer pointer to store the 128-bit random value.\r | |
159 | \r | |
160 | @retval TRUE Random number generated successfully.\r | |
161 | @retval FALSE Failed to generate the random number.\r | |
162 | \r | |
163 | **/\r | |
164 | BOOLEAN\r | |
165 | EFIAPI\r | |
166 | GetRandomNumber128 (\r | |
167 | OUT UINT64 *Rand\r | |
168 | )\r | |
169 | {\r | |
170 | ASSERT (Rand != NULL);\r | |
171 | \r | |
172 | //\r | |
173 | // Read first 64 bits\r | |
174 | //\r | |
175 | if (!GetRandomNumber64 (Rand)) {\r | |
176 | return FALSE;\r | |
177 | }\r | |
178 | \r | |
179 | //\r | |
180 | // Read second 64 bits\r | |
181 | //\r | |
182 | return GetRandomNumber64 (++Rand);\r | |
183 | }\r |