]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseRngLib/AArch64/Rndr.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseRngLib / AArch64 / Rndr.c
CommitLineData
9301e564
RC
1/** @file\r
2 Random number generator service that uses the RNDR instruction\r
3 to provide pseudorandom numbers.\r
4\r
5 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 SPDX-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#include <Library/RngLib.h>\r
16\r
17#include "ArmRng.h"\r
18#include "BaseRngLibInternals.h"\r
19\r
20STATIC BOOLEAN mRndrSupported;\r
21\r
22//\r
23// Bit mask used to determine if RNDR instruction is supported.\r
24//\r
25#define RNDR_MASK ((UINT64)MAX_UINT16 << 60U)\r
26\r
27/**\r
28 The constructor function checks whether or not RNDR instruction is supported\r
29 by the host hardware.\r
30\r
31 The constructor function checks whether or not RNDR instruction is supported.\r
32 It will ASSERT() if RNDR instruction is not supported.\r
33 It will always return EFI_SUCCESS.\r
34\r
35 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
36\r
37**/\r
38EFI_STATUS\r
39EFIAPI\r
40BaseRngLibConstructor (\r
41 VOID\r
42 )\r
43{\r
44 UINT64 Isar0;\r
45 //\r
46 // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by\r
47 // MSR. A non-zero value indicates that the processor supports the RNDR instruction.\r
48 //\r
49 Isar0 = ArmReadIdIsar0 ();\r
50 ASSERT ((Isar0 & RNDR_MASK) != 0);\r
51\r
52 mRndrSupported = ((Isar0 & RNDR_MASK) != 0);\r
53\r
54 return EFI_SUCCESS;\r
55}\r
56\r
57/**\r
58 Generates a 16-bit random number.\r
59\r
60 @param[out] Rand Buffer pointer to store the 16-bit random value.\r
61\r
62 @retval TRUE Random number generated successfully.\r
63 @retval FALSE Failed to generate the random number.\r
64\r
65**/\r
66BOOLEAN\r
67EFIAPI\r
68ArchGetRandomNumber16 (\r
69 OUT UINT16 *Rand\r
70 )\r
71{\r
72 UINT64 Rand64;\r
73\r
74 if (ArchGetRandomNumber64 (&Rand64)) {\r
75 *Rand = Rand64 & MAX_UINT16;\r
76 return TRUE;\r
77 }\r
78\r
79 return FALSE;\r
80}\r
81\r
82/**\r
83 Generates a 32-bit random number.\r
84\r
85 @param[out] Rand Buffer pointer to store the 32-bit random value.\r
86\r
87 @retval TRUE Random number generated successfully.\r
88 @retval FALSE Failed to generate the random number.\r
89\r
90**/\r
91BOOLEAN\r
92EFIAPI\r
93ArchGetRandomNumber32 (\r
94 OUT UINT32 *Rand\r
95 )\r
96{\r
97 UINT64 Rand64;\r
98\r
99 if (ArchGetRandomNumber64 (&Rand64)) {\r
100 *Rand = Rand64 & MAX_UINT32;\r
101 return TRUE;\r
102 }\r
103\r
104 return FALSE;\r
105}\r
106\r
107/**\r
108 Generates a 64-bit random number.\r
109\r
110 @param[out] Rand Buffer pointer to store the 64-bit random value.\r
111\r
112 @retval TRUE Random number generated successfully.\r
113 @retval FALSE Failed to generate the random number.\r
114\r
115**/\r
116BOOLEAN\r
117EFIAPI\r
118ArchGetRandomNumber64 (\r
119 OUT UINT64 *Rand\r
120 )\r
121{\r
122 return ArmRndr (Rand);\r
123}\r
124\r
125/**\r
126 Checks whether RNDR is supported.\r
127\r
128 @retval TRUE RNDR is supported.\r
129 @retval FALSE RNDR is not supported.\r
130\r
131**/\r
132BOOLEAN\r
133EFIAPI\r
134ArchIsRngSupported (\r
135 VOID\r
136 )\r
137{\r
138 return mRndrSupported;\r
139}\r