]>
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 | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #include <Library/BaseLib.h>\r | |
17 | #include <Library/DebugLib.h>\r | |
18 | \r | |
19 | //\r | |
20 | // Bit mask used to determine if RdRand instruction is supported.\r | |
21 | //\r | |
22 | #define RDRAND_MASK BIT30\r | |
23 | \r | |
24 | //\r | |
25 | // Limited retry number when valid random data is returned.\r | |
26 | // Uses the recommended value defined in Section 7.3.17 of "Intel 64 and IA-32\r | |
27 | // Architectures Software Developer's Mannual".\r | |
28 | //\r | |
29 | #define RDRAND_RETRY_LIMIT 10\r | |
30 | \r | |
31 | /**\r | |
32 | The constructor function checks whether or not RDRAND instruction is supported\r | |
33 | by the host hardware.\r | |
34 | \r | |
35 | The constructor function checks whether or not RDRAND instruction is supported.\r | |
36 | It will ASSERT() if RDRAND instruction is not supported.\r | |
37 | It will always return RETURN_SUCCESS.\r | |
38 | \r | |
39 | @retval RETURN_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
40 | \r | |
41 | **/\r | |
42 | RETURN_STATUS\r | |
43 | EFIAPI\r | |
44 | BaseRngLibConstructor (\r | |
45 | VOID\r | |
46 | )\r | |
47 | {\r | |
48 | UINT32 RegEcx;\r | |
49 | \r | |
50 | //\r | |
51 | // Determine RDRAND support by examining bit 30 of the ECX register returned by\r | |
52 | // CPUID. A value of 1 indicates that processor support RDRAND instruction.\r | |
53 | //\r | |
54 | AsmCpuid (1, 0, 0, &RegEcx, 0);\r | |
55 | ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);\r | |
56 | \r | |
57 | return RETURN_SUCCESS;\r | |
58 | }\r | |
59 | \r | |
60 | /**\r | |
61 | Generates a 16-bit random number.\r | |
62 | \r | |
63 | if Rand is NULL, then ASSERT().\r | |
64 | \r | |
65 | @param[out] Rand Buffer pointer to store the 16-bit random value.\r | |
66 | \r | |
67 | @retval TRUE Random number generated successfully.\r | |
68 | @retval FALSE Failed to generate the random number.\r | |
69 | \r | |
70 | **/\r | |
71 | BOOLEAN\r | |
72 | EFIAPI\r | |
73 | GetRandomNumber16 (\r | |
74 | OUT UINT16 *Rand\r | |
75 | )\r | |
76 | {\r | |
77 | UINT32 Index;\r | |
78 | \r | |
79 | ASSERT (Rand != NULL);\r | |
80 | \r | |
81 | //\r | |
82 | // A loop to fetch a 16 bit random value with a retry count limit.\r | |
83 | //\r | |
84 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
85 | if (AsmRdRand16 (Rand)) {\r | |
86 | return TRUE;\r | |
87 | }\r | |
88 | }\r | |
89 | \r | |
90 | return FALSE;\r | |
91 | }\r | |
92 | \r | |
93 | /**\r | |
94 | Generates a 32-bit random number.\r | |
95 | \r | |
96 | if Rand is NULL, then ASSERT().\r | |
97 | \r | |
98 | @param[out] Rand Buffer pointer to store the 32-bit random value.\r | |
99 | \r | |
100 | @retval TRUE Random number generated successfully.\r | |
101 | @retval FALSE Failed to generate the random number.\r | |
102 | \r | |
103 | **/\r | |
104 | BOOLEAN\r | |
105 | EFIAPI\r | |
106 | GetRandomNumber32 (\r | |
107 | OUT UINT32 *Rand\r | |
108 | )\r | |
109 | {\r | |
110 | UINT32 Index;\r | |
111 | \r | |
112 | ASSERT (Rand != NULL);\r | |
113 | \r | |
114 | //\r | |
115 | // A loop to fetch a 32 bit random value with a retry count limit.\r | |
116 | //\r | |
117 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
118 | if (AsmRdRand32 (Rand)) {\r | |
119 | return TRUE;\r | |
120 | }\r | |
121 | }\r | |
122 | \r | |
123 | return FALSE;\r | |
124 | }\r | |
125 | \r | |
126 | /**\r | |
127 | Generates a 64-bit random number.\r | |
128 | \r | |
129 | if Rand is NULL, then ASSERT().\r | |
130 | \r | |
131 | @param[out] Rand Buffer pointer to store the 64-bit random value.\r | |
132 | \r | |
133 | @retval TRUE Random number generated successfully.\r | |
134 | @retval FALSE Failed to generate the random number.\r | |
135 | \r | |
136 | **/\r | |
137 | BOOLEAN\r | |
138 | EFIAPI\r | |
139 | GetRandomNumber64 (\r | |
140 | OUT UINT64 *Rand\r | |
141 | )\r | |
142 | {\r | |
143 | UINT32 Index;\r | |
144 | \r | |
145 | ASSERT (Rand != NULL);\r | |
146 | \r | |
147 | //\r | |
148 | // A loop to fetch a 64 bit random value with a retry count limit.\r | |
149 | //\r | |
150 | for (Index = 0; Index < RDRAND_RETRY_LIMIT; Index++) {\r | |
151 | if (AsmRdRand64 (Rand)) {\r | |
152 | return TRUE;\r | |
153 | }\r | |
154 | }\r | |
155 | \r | |
156 | return FALSE;\r | |
157 | }\r | |
c8b6f16d TP |
158 | \r |
159 | /**\r | |
160 | Generates a 128-bit random number.\r | |
161 | \r | |
162 | if Rand is NULL, then ASSERT().\r | |
163 | \r | |
164 | @param[out] Rand Buffer pointer to store the 128-bit random value.\r | |
165 | \r | |
166 | @retval TRUE Random number generated successfully.\r | |
167 | @retval FALSE Failed to generate the random number.\r | |
168 | \r | |
169 | **/\r | |
170 | BOOLEAN\r | |
171 | EFIAPI\r | |
172 | GetRandomNumber128 (\r | |
173 | OUT UINT64 *Rand\r | |
174 | )\r | |
175 | {\r | |
176 | ASSERT (Rand != NULL);\r | |
177 | \r | |
178 | //\r | |
179 | // Read first 64 bits\r | |
180 | //\r | |
181 | if (!GetRandomNumber64 (Rand)) {\r | |
182 | return FALSE;\r | |
183 | }\r | |
184 | \r | |
185 | //\r | |
186 | // Read second 64 bits\r | |
187 | //\r | |
188 | return GetRandomNumber64 (++Rand);\r | |
189 | }\r |