]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseRngLib/Rand/RdRand.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseRngLib / Rand / RdRand.c
1 /** @file
2 Random number generator services that uses RdRand instruction access
3 to provide high-quality random 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
16 #include "BaseRngLibInternals.h"
17
18 //
19 // Bit mask used to determine if RdRand instruction is supported.
20 //
21 #define RDRAND_MASK BIT30
22
23 STATIC BOOLEAN mRdRandSupported;
24
25 /**
26 The constructor function checks whether or not RDRAND instruction is supported
27 by the host hardware.
28
29 The constructor function checks whether or not RDRAND instruction is supported.
30 It will ASSERT() if RDRAND instruction is not supported.
31 It will always return EFI_SUCCESS.
32
33 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 BaseRngLibConstructor (
39 VOID
40 )
41 {
42 UINT32 RegEcx;
43
44 //
45 // Determine RDRAND support by examining bit 30 of the ECX register returned by
46 // CPUID. A value of 1 indicates that processor support RDRAND instruction.
47 //
48 AsmCpuid (1, 0, 0, &RegEcx, 0);
49 ASSERT ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
50
51 mRdRandSupported = ((RegEcx & RDRAND_MASK) == RDRAND_MASK);
52
53 return EFI_SUCCESS;
54 }
55
56 /**
57 Generates a 16-bit random number.
58
59 @param[out] Rand Buffer pointer to store the 16-bit random value.
60
61 @retval TRUE Random number generated successfully.
62 @retval FALSE Failed to generate the random number.
63
64 **/
65 BOOLEAN
66 EFIAPI
67 ArchGetRandomNumber16 (
68 OUT UINT16 *Rand
69 )
70 {
71 return AsmRdRand16 (Rand);
72 }
73
74 /**
75 Generates a 32-bit random number.
76
77 @param[out] Rand Buffer pointer to store the 32-bit random value.
78
79 @retval TRUE Random number generated successfully.
80 @retval FALSE Failed to generate the random number.
81
82 **/
83 BOOLEAN
84 EFIAPI
85 ArchGetRandomNumber32 (
86 OUT UINT32 *Rand
87 )
88 {
89 return AsmRdRand32 (Rand);
90 }
91
92 /**
93 Generates a 64-bit random number.
94
95 @param[out] Rand Buffer pointer to store the 64-bit random value.
96
97 @retval TRUE Random number generated successfully.
98 @retval FALSE Failed to generate the random number.
99
100 **/
101 BOOLEAN
102 EFIAPI
103 ArchGetRandomNumber64 (
104 OUT UINT64 *Rand
105 )
106 {
107 return AsmRdRand64 (Rand);
108 }
109
110 /**
111 Checks whether RDRAND is supported.
112
113 @retval TRUE RDRAND is supported.
114 @retval FALSE RDRAND is not supported.
115
116 **/
117 BOOLEAN
118 EFIAPI
119 ArchIsRngSupported (
120 VOID
121 )
122 {
123 /*
124 Existing software depends on this always returning TRUE, so for
125 now hard-code it.
126
127 return mRdRandSupported;
128 */
129 return TRUE;
130 }