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