]>
Commit | Line | Data |
---|---|---|
3aa8dc6c LQ |
1 | /** @file\r |
2 | RDRAND Support Routines.\r | |
3 | \r | |
4 | Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r | |
5 | This program and the accompanying materials\r | |
6 | are licensed and made available under the terms and conditions of the BSD License\r | |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "RdRand.h"\r | |
16 | \r | |
17 | /**\r | |
18 | Generates a 64-bit random number through RDRAND instruction.\r | |
19 | \r | |
20 | @param[out] Rand Buffer pointer to store the random result.\r | |
21 | \r | |
22 | @retval TRUE RDRAND call was successful.\r | |
23 | @retval FALSE Failed attempts to call RDRAND.\r | |
24 | \r | |
25 | **/\r | |
26 | BOOLEAN\r | |
27 | EFIAPI\r | |
28 | RdRand64Step (\r | |
29 | OUT UINT64 *Rand\r | |
30 | )\r | |
31 | {\r | |
32 | UINT32 RandLow;\r | |
33 | UINT32 RandHigh;\r | |
34 | \r | |
35 | //\r | |
36 | // Generating a 64-bit rand on a 32-bit system by \r | |
37 | // mapping two 32-bit RDRAND instructions.\r | |
38 | //\r | |
39 | if (!RdRand32Step (&RandLow)) {\r | |
40 | return FALSE;\r | |
41 | }\r | |
42 | if (!RdRand32Step (&RandHigh)) {\r | |
43 | return FALSE;\r | |
44 | }\r | |
45 | \r | |
46 | *Rand = (UINT64) RandLow | LShiftU64 ((UINT64)RandHigh, 32);\r | |
47 | \r | |
48 | return TRUE;\r | |
49 | }\r | |
50 | \r | |
51 | /**\r | |
52 | Calls RDRAND to request a word-length random number.\r | |
53 | \r | |
54 | @param[out] Rand Buffer pointer to store the random number.\r | |
55 | @param[in] NeedRetry Determine whether or not to loop retry.\r | |
56 | \r | |
57 | @retval EFI_SUCCESS Random word generation succeeded.\r | |
58 | @retval EFI_NOT_READY Failed to request random word.\r | |
59 | \r | |
60 | **/\r | |
61 | EFI_STATUS\r | |
62 | EFIAPI\r | |
63 | RdRandWord (\r | |
64 | OUT UINTN *Rand,\r | |
65 | IN BOOLEAN NeedRetry\r | |
66 | )\r | |
67 | {\r | |
68 | return RdRand32 (Rand, NeedRetry);\r | |
69 | }\r | |
70 | \r | |
71 | /**\r | |
72 | Calls RDRAND to request multiple word-length random numbers.\r | |
73 | \r | |
74 | @param[in] Length Size of the buffer, in words, to fill with.\r | |
75 | @param[out] RandBuffer Pointer to the buffer to store the random result.\r | |
76 | \r | |
77 | @retval EFI_SUCCESS Random words generation succeeded.\r | |
78 | @retval EFI_NOT_READY Failed to request random words.\r | |
79 | \r | |
80 | **/\r | |
81 | EFI_STATUS\r | |
82 | EFIAPI\r | |
83 | RdRandGetWords (\r | |
84 | IN UINTN Length,\r | |
85 | OUT UINTN *RandBuffer\r | |
86 | )\r | |
87 | {\r | |
88 | EFI_STATUS Status;\r | |
89 | UINT32 Index;\r | |
90 | \r | |
91 | for (Index = 0; Index < Length; Index++) {\r | |
92 | //\r | |
93 | // Obtain one word-length (32-bit) Random Number with possible retry-loop.\r | |
94 | //\r | |
95 | Status = RdRand32 (RandBuffer, TRUE);\r | |
96 | if (EFI_ERROR (Status)) {\r | |
97 | return Status;\r | |
98 | }\r | |
99 | \r | |
100 | RandBuffer++;\r | |
101 | }\r | |
102 | \r | |
103 | return EFI_SUCCESS;\r | |
104 | }\r |